Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

arcgis.geometry.Geometry.as_shapely does not return expected shapely geometry for multipolygon #2211

Open
pchengcarb opened this issue Jan 28, 2025 · 0 comments
Labels

Comments

@pchengcarb
Copy link

Describe the bug
arcgis.geometry.Geometry.as_shapely does not return expected shapely geometry for multipolygon.

To Reproduce
Steps to reproduce the behavior:

import shapely
from arcgis.features FeatureSet

# Multipolygon with hole GeoJSON
# Example from https://en.wikipedia.org/wiki/GeoJSON#Geometries
multipoly = {
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "MultiPolygon",
                "coordinates": [
                    [[[40.0, 40.0], [20.0, 45.0], [45.0, 30.0], [40.0, 40.0]]],
                    [
                        [
                            [20.0, 35.0],
                            [10.0, 30.0],
                            [10.0, 10.0],
                            [30.0, 5.0],
                            [45.0, 20.0],
                            [20.0, 35.0],
                        ],
                        [[30.0, 20.0], [20.0, 15.0], [20.0, 25.0], [30.0, 20.0]],
                    ],
                ],
            },
            "properties": {"prop0": "value0"},
        }
    ],
}

# Create FeatureSet from GeoJSON
fs = FeatureSet.from_geojson(multipoly)

# Get arcgis.geometry.Geometry
geom = fs.features[0].geometry

# Check how arcgis.geometry.Geometry looks
geom

Image

# Check arcgis.geometry.Geometry.__geo_interface__
print(geom.__geo_interface__) # as expected

Coordinates are as expected when calling Geometry.__geo_interface__.

{'coordinates': [[[(40.0, 40.0), (20.0, 45.0), (45.0, 30.0), (40.0, 40.0)], [(20.0, 35.0), (10.0, 30.0), (10.0, 10.0), (30.0, 5.0), (45.0, 20.0), (20.0, 35.0)], [(30.0, 20.0), (20.0, 15.0), (20.0, 25.0), (30.0, 20.0)]]], 'type': 'MultiPolygon'}
# Check how shapely geometry looks when using arcgis.geometry.Geometry.as_shapely
geom.as_shapely # not expected; should be a multipolygon with hole

Not expected geometry when calling arcgis.geometry.Geometry.as_shapely. It should be a multipolygon with hole.
Image

# Check the __geo_interface__ from the shapely geometry created by arcgis.geometry.Geometry.as_shapely
print(geom.as_shapely.__geo_interface__) # not expected; should be a multipolygon with hole but instead it is a polygon

Not expected shapely geometry when it is created by arcgis.geometry.Geometry.as_shapely. It should be a multipolygon with hole.

Geometry failed validation: Hole lies outside shell[20 35]. Repairing with `buffer(0)`.
{'type': 'Polygon', 'coordinates': (((40.0, 40.0), (45.0, 30.0), (20.0, 45.0), (40.0, 40.0)),)}

Use arcgis.geometry.Geometry.__geo_interface__ and shapely to create shapely geometry instead of using arcgis.geometry.Geometry.as_shapely.

# Use shapely directly to create shapely geometry using arcgis.geometry.Geometry.__geo_interface__, and repair any issues with it
# Convert geo interface to shapely geometry doc: https://shapely.readthedocs.io/en/2.0.1/manual.html#python-geo-interface
# Repair geometry doc: https://shapely.readthedocs.io/en/2.0.1/reference/shapely.make_valid.html
shapely.make_valid(shapely.geometry.shape(geom.__geo_interface__)) # as expected

This is the expected shapely geometry.

Image

This matches the multipolygon with hole example geometry used.

Image

error:

>>> geom.as_shapely == shapely.make_valid(shapely.geometry.shape(geom.__geo_interface__))
Geometry failed validation: Hole lies outside shell[20 35]. Repairing with `buffer(0)`.
False

There is no explicit error raised. The issue is unexpected behavior with arcgis.geometry.Geometry.as_shapely creating a shapely geometry that differs from creating a shapely geometry using shapely.geometry.shape.

Screenshots
See above.

Expected behavior
I expect arcgis.geometry.Geometry.as_shapely to create a shapely geometry that is equal to calling shapely.geometry.shape.

>>> geom.as_shapely == shapely.make_valid(shapely.geometry.shape(geom.__geo_interface__))
True

For reference, here are docs for how I create shapely geometry from arcgis.geometry.Geometry.__geo_interface__.
Convert geo interface to shapely geometry doc: https://shapely.readthedocs.io/en/2.0.1/manual.html#python-geo-interface
Repair geometry doc: https://shapely.readthedocs.io/en/2.0.1/reference/shapely.make_valid.html

Platform (please complete the following information):

  • OS: [e.g. iOS]: Windows
  • Browser [e.g. chrome, safari]: MS Edge
  • Python API Version [e.g. 1.6.2]: 2.4.0

Additional context
Add any other context about the problem here, attachments etc.

  • I am using ArcGIS Notebook Python 3 Standard - 11.0 on ArcGIS Online.
  • shapely version is what is provided in ArcGIS Notebook Python 3 Standard - 11.0: 2.0.1
  • No arcpy because it is unavailable in ArcGIS Notebook Python 3 Standard - 11.0
@pchengcarb pchengcarb added the bug label Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant