ArcGIS Python Reproject Geometry Object List

ArcGIS Python Reproject Geometry Object List

Frequently I have a large list of geometry objects I need to get reprojected for analysis. A while back I discovered the reprojectAs method of the arcpy.Geometry object. While useful, I still needed a way to handle a few edge cases to make a more universal way to reproject. These edge cases include varying spatial references across the list of geometry objects, and applying a transformation when needed - when the geographic coordinate systems for the input and output are different. The Gist below is what I came up with to accomplish this task.

Most commonly I use this when trying to quickly work with a large volume of points collected from a variety of sources online, and get them in the same spatial reference for loading into a feature class, or more commonly perform a few analysis steps to validate, and clean up the data. Simply to play with this and test it is relatively easy to just load geometries from a feature class using a data analysis search cursor, and applying this function such as the example below, going from Web Mercator to just WGS84.

fc_path = r'.\test01.gdb\test_points'
sr_out = arcpy.SpatialReference(3857)
geom_list = [_[0] for _ in arcpy.da.SearchCursor(fc_path, 'SHAPE@')]
print(set(geom.spatialReference.name for geom in geom_list))
geom_list = [reproject_geometry_object(geom, sr_out) for geom in geom_list]
print(set(geom.spatialReference.name for geom in geom_list))

The output simply prints out the starting spatial references, and the ending spatial references. In my test case, there is only one for both. Using this function, the output will always be a single spatial reference anyway. Here is the example output for my test.

{'GCS_WGS_1984'}
{'WGS_1984_Web_Mercator_Auxiliary_Sphere'}

Incidentally, if you are wondering why I am using these lists of geometry objects, it is a lot faster than performing analysis with entire feature class datasets in geoprocessing tools. A good place to start understanding how to do this is available in the ArcGIS Documentation, Using Geometry Objects with Geoprocessing Tools.

While a bit of an esoteric topic, if this is something you find yourself in need of, typically you really need it. Hence, hopefully my struggles make your life easier!