question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Geometry saved as MultiPolygon is loaded as Polygon and order changes

See original GitHub issue

Expected behavior and actual behavior.

I save a 3D multipolygon shapefile with row geometry type “MultiPolygon”. When I read this file again I expect that the row geometry type is still “MultiPolygon”. But actually it is downgraded to “Polygon” and also the polygons order in this geometry is changed.

This only happens for certain coordinates (see Run 2 below – only the coordinates changed!).

Steps to reproduce the problem.

import fiona
from pprint import pprint

fn = '/tmp/test_mp.shp'
opts = {
    'driver': 'ESRI Shapefile',
    'schema': {'geometry': '3D MultiPolygon', 'properties': {}}
}

# Run 1 - OK
polygons = [
    [[[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, 0.0, 0.0]]],
    [[[0.0, 0.0, 0.0], [2.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, 0.0, 0.0]]]
]

with fiona.open(fn, mode='w', **opts) as c:
    c.write({'geometry': {'type': 'MultiPolygon', 'coordinates': polygons}, 'properties': {}})

with fiona.open(fn, mode='r', **opts) as c:
    feature = next(iter(c))
    pprint(feature['geometry'])
    assert feature['geometry']['type'] == 'MultiPolygon' # ok!

# Run 2 - Failing
polygons = [
        [[[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, 0.0, 0.0]]],
        [[[0.0, 0.0, 0.0], [0.0, 1.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 0.0]]],
    ]

with fiona.open(fn, mode='w', **opts) as c:
    c.write({'geometry': {'type': 'MultiPolygon', 'coordinates': polygons}, 'properties': {}})

with fiona.open(fn, mode='r', **opts) as c:
    feature = next(iter(c))
    pprint(feature['geometry'])
    assert feature['geometry']['type'] == 'MultiPolygon'
    # type is downgraded to 'Polygon' here and polygon order is changed

Operating system

Arch Linux

Fiona and GDAL version and provenance

  • Python 3.8
  • Fiona==1.8.13.post1 installed from PyPI using pip 20.0.2
  • GDAL 3.0.4, released 2020/01/28

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
rbuffatcommented, Mar 24, 2020

I’m not familiar with the Shapefile format, respectively the implementation of the gdal Shapefile driver. But after reading https://github.com/OSGeo/gdal/issues/1787#issuecomment-522250212 I suspect it is best to check the validity of the geometry before writing them E.g. by using the shapely library. GeoJSON and GPKG seem to be more tolerant with writing invalid geometries.

It’s not ideal that the Shapefile driver does not raise an error when it can’t handle invalid geometries and does something unexpected. But this is probably something that should be handled within gdal and not fiona.

0reactions
sgilliescommented, Mar 25, 2020

I will close this one. In #870 I propose a documentation fix.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Understanding difference between Polygon and Multipolygon ...
A MultiPolygon is a MultiSurface whose elements are Polygons. ... they partition "The order of rings in the points array is not significant....
Read more >
9. Geometries — Introduction to PostGIS
ST_GeometryN: Returns the 1-based Nth geometry if the geometry is a GEOMETRYCOLLECTION, MULTIPOINT, MULTILINESTRING, MULTICURVE or MULTIPOLYGON. Otherwise, ...
Read more >
Saving centroid of a (multi)polygon as point geometry in a model
I takes the geometry from the polygon, calculates the centroid of it and inserts it's geometry as well-known text into the form, then...
Read more >
Add a point, line, and polygon | Overview - ArcGIS Developers
Learn how to display point , line , and polygon graphics in a map . ... Graphics are composed of a geometry ,...
Read more >
KML Reference | Keyhole Markup Language
When you are editing KML text files, you can load this Schema into any XML ... any changes will be reverted when the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found