arcgis-python-api
arcgis-python-api copied to clipboard
`arcgis.geometry.Geometry.as_shapely` does not return expected `shapely` geometry for multipolygon
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
# 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.
# 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.
This matches the multipolygon with hole example geometry used.
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