__geo_interface__ Generates Different and Incorrect Output When ArcPy Not Installed

974
4
Jump to solution
03-07-2018 09:02 AM
JoshuaBixby
MVP Esteemed Contributor

Hey ArcGIS API for Python staff on GeoNet, I found a bug for you.

It turns out the __geo_interface__ methods generate different, and incorrect, output when ArcPy is not installed along with ArcGIS API for Python.

>>> from arcgis.geometry import Geometry
>>> from pprint import pprint
>>>
>>> # simple square with a hole in the middle
... json = {
...     "rings": [
...         [
...             [0, 0],
...             [0, 10],
...             [10, 10],
...             [10, 0],
...             [0, 0]
...         ],
...         [
...             [3, 3],
...             [7, 3],
...             [7, 7],
...             [3, 7],
...             [3, 3]
...         ]
...     ],
...     "spatialReference": {
...         "wkid": 3857
...     }
... }
>>>
>>> # __geo_interface__ output when ArcPy is installed
>>> pprint(Geometry(json).__geo_interface__)
{'coordinates': [[[(0.0, 0.0),
                   (0.0, 10.0),
                   (10.0, 10.0),
                   (10.0, 0.0),
                   (0.0, 0.0)],
                  [(3.0, 3.0),
                   (7.0, 3.0),
                   (7.0, 7.0),
                   (3.0, 7.0),
                   (3.0, 3.0)]]],
 'type': 'MultiPolygon'}
>>>
>>> # __geo_interface__ output when ArcPy is not installed
>>> pprint(Geometry(json).__geo_interface__)
{'coordinates': [[[(0, 0),
                   (0, 10),
                   (10, 10),
                   (10, 0),
                   (0, 0)]],
                 [[(3, 3),
                 (7, 3),
                 (7, 7),
                 (3, 7),
                 (3, 3)]]],
 'type': 'MultiPolygon'}
>>>

Notice when ArcPy is not installed, the __geo_interface__ output contains an extra closing bracket on the outer ring of the polygon, thus turning a single-part polygon with a hole into a overlapping multi-part polygon.

0 Kudos
1 Solution

Accepted Solutions
RohitSingh2
Esri Contributor

Thanks for letting us know. We've created a bug report and will look into fixing these issues.

View solution in original post

0 Kudos
4 Replies
DanPatterson_Retired
MVP Emeritus

And float vs integer as well.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Some additional information for the ArcGIS API for Python team.  This issue affects any geometry properties or methods that rely on Shapely when ArcPy is not installed:

>>> from arcgis.geometry import Geometry
>>>
>>> # simple square with a hole in the middle
>>> json = {
...     "rings": [
...         [
...             [0, 0],
...             [0, 10],
...             [10, 10],
...             [10, 0],
...             [0, 0]
...         ],
...         [
...             [3, 3],
...             [7, 3],
...             [7, 7],
...             [3, 7],
...             [3, 3]
...         ]
...     ],
...     "spatialReference": {
...         "wkid": 3857
...     }
... }
>>>
>>> geom = Geometry(json)
>>> 
>>> geom
{'spatialReference': {'wkid': 3857}, 'rings': [[[0, 0], [0, 10], [10, 10], [10, 0], [0, 0]], [[3, 3], [7, 3], [7, 7], [3, 7], [3, 3]]]}
>>> type(geom)
<class 'arcgis.geometry._types.Polygon'>
>>> 
>>> # geometry properties that don't rely on Shapely when ArcPy is not installed
>>> geom.point_count
10
>>> geom.part_count
2
>>> geom.is_multipart
False
>>> 
>>> # geometry properties that do rely on Shapely when ArcPy is not installed
>>> geom.area
ParseException: Expected number but encountered ','
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\...\Continuum\miniconda3\envs\arcgis\lib\site-packages\arcgis\geometry\_types.py", line 585, in area
    return self.as_shapely.area
  File "C:\...\Continuum\miniconda3\envs\arcgis\lib\site-packages\arcgis\geometry\_types.py", line 517, in as_shapely
    return wkt.loads(self._wkt())
  File "C:\...\Continuum\miniconda3\envs\arcgis\lib\site-packages\shapely\wkt.py", line 10, in loads
    return geos.WKTReader(geos.lgeos).read(data)
  File "C:\...\Continuum\miniconda3\envs\arcgis\lib\site-packages\shapely\geos.py", line 254, in read
    "Could not create geometry because of errors "
shapely.errors.WKTReadingError: Could not create geometry because of errors while reading input.
>>> 

The bad code appears to be in __geo_interface__ property of arcgis.geometry._types .

0 Kudos
RohitSingh2
Esri Contributor

Thanks for letting us know. We've created a bug report and will look into fixing these issues.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I have confirmed this is fixed in version 1.5

0 Kudos