Pythonic ArcPy Geometries

1182
9
07-09-2015 10:40 AM
Status: Open
Labels (2)
JoshuaBixby
MVP Esteemed Contributor

Given that ArcPy is a Python site package, the ArcPy geometries should actually be Pythonic.  What do I mean by Pythonic?  ArcPy geometries and components should be accessible and modifiable using standard Python conventions like iterating, indexing, and slicing among others.  The idea isn't original; in fact, Pythonic geometries have already been implemented in GeoDjango (Geometries are Pythonic).

9 Comments
JoshuaBixby

Constructing empty geometries isn't possible using the ArcPy Geometry classes. The only way to currently create an empty geometry in ArcPy is to use the arcpy.FromWKT as a geometry constructor. I propose the ArcPy Geometry classes support the construction of empty geometries.

Currently, calling any of the ArcPy Geometry constructors with no arguments, a 'None' argument, or an empty arcpy.Array throws an exception.

>>> arcpy.Polygon()
Runtime error 
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\arcobjects\mixins.py", line 222, in __init__
    *gp_fixargs(args, True))
  File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\geoprocessing\_base.py", line 504, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))
RuntimeError: Object: CreateObject cannot create geometry from inputs


Instead, the constructors should generate an empty geometry of the appropriate type similar to geometry constructors from other Python (Shapely) and DBMS (SQL Server) packages.

>>> pg = arcpy.Polygon()
>>> pg.type
u'polygon'
>>> pg.WKT
u'MULTIPOLYGON EMPTY'
 


 

DuncanHornby
When I saw your idea I thought surely that's not right but having looked at the help file I realised there is no mechanism or method that allows you to add points to a polygon. In ArcObjects there is an interface called IPointsCollection and you would use that to insert points into a geometry. So as there is no way of adding points to an existing polygon its not suprising that the constructor won't allow the creation of a Polygon without an array of points. So I'm guessing until ESRI expose some method to add points to a polygon this idea will never get of the ground...

Still voted for it!
DavidWynne
Can create an empty polygon this way:
p = arcpy.Polygon(arcpy.Array(None))
 
DuncanHornby
Ha! I had tried the following and had come to the conclusion it was not possible as this throws an error:

p = arcpy.Polygon(arcpy.Array([]))

Nice to see it's possible.
 
JoshuaBixby
Funny, I tried None, arcpy.Array(), and arcpy.Array([]); but I never thought to try arcpy.Array(None).  That said, arcpy.Polygon(arcpy.Array(None)) is a bit too baroque for my tastes.  In the context of Zen, there is nothing simple or beautiful about the current implementation.  What is problematic with arcpy.Polygon()?

I will leave this idea open to push for a more elegant and straightfoward way to construct empty geometries.
JoshuaBixby

This is looking good for implementation, although the feedback isn't clear whether that will be in an upcoming minor or major release.

GreggRoemhildt1

Another great python Geometry implementation that I've worked with is Geoalchemy2.

GeoAlchemy 2 Documentation — GeoAlchemy2 0.3.0 documentation

ORM Tutorial — GeoAlchemy2 0.3.0 documentation

JoshuaBixby

Given empty geometries are actually/already supported by the geometry constructors, I think this idea should be merged with https://community.esri.com/ideas/10963‌ since what I am after is having the syntax for creating empty geometries be more intuitive and idiomatic.

JoshuaBixby

An example of making ArcPy Geometry constructors more Pythonic:  /blogs/tilting/2017/06/12/a-case-for-no-argument-arcpy-geometry-constructors?sr=search&searchId=c932...‌.