Search code examples
arcgisarcpy

Arcpy - How to change spatial reference of geometry objects in a spatially enabled dataframe?


I'm using the following code to export data from an arcgis pro feature class into a spatially enabled dataframe. Exported data spatial reference has to be 4326.

out_coordinate_system = arcpy.SpatialReference(4326)
    arcpy.env.addOutputsToMap = False
    arcpy.Project_management(fc, fc.name + "_wgs84", out_coordinate_system)
    arcpy.env.addOutputsToMap = True

    #Create a spatially enabled dataframe from featureclass 
    sdf = pd.DataFrame.spatial.from_featureclass(fc.name + "_wgs84")

    #Cleanup temporary layer created for reprojection
    arcpy.Delete_management(fc.name + "_wgs84")


Is there a way to avoid the creating a temporary feature_class with the Project tool and reproject geometry objects to SR 4326 in the SHAPE column of the sdf object using arcpy.geometry ?


Solution

  • Managed to make it work with the project_as() function as objects stored in the SHAPE column of the sdf have arcgis.geometry objects data type.

    out_coordinate_system = arcpy.SpatialReference(4326)
    sdf = pd.DataFrame.spatial.from_featureclass(fc.name)
    sdf.dropna(inplace=True)
    sdf['SHAPE'] = sdf['SHAPE'].apply(lambda x: x.project_as(out_coordinate_system))
    
    sdf['SHAPE'].apply(lambda x: type(x))
    0    <class 'arcgis.geometry._types.Polygon'>
    1    <class 'arcgis.geometry._types.Polygon'>
    2    <class 'arcgis.geometry._types.Polygon'>
    Name: SHAPE, dtype: object
    
    dir(sdf['SHAPE'][0])
    ['EWKT', 'JSON', 'WKB', 'WKT', '_HASARCPY', '_HASSHAPELY', '__class__', '__contains__', '__delattr__', '__delitem__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__geo_interface__', '__getattr__', '__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setattribute__', '__setitem__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_ao', '_check_geometry_engine', '_class_attributes', '_from_geojson', '_repr_svg_', '_shoelace_area', '_type', '_wkt', 'angle_distance_to', 'area', 'as_arcpy', 'as_shapely', 'boundary', 'buffer', 'centroid', 'clear', 'clip', 'contains', 'convex_hull', 'coordinates', 'copy', 'crosses', 'cut', 'densify', 'difference', 'disjoint', 'distance_to', 'envelope', 'equals', 'extent', 'first_point', 'from_shapely', 'fromkeys', 'generalize', 'geoextent', 'geometry_type', 'get', 'get_area', 'get_length', 'get_part', 'has_m', 'has_z', 'hull_rectangle', 'intersect', 'is_empty', 'is_multipart', 'is_valid', 'items', 'keys', 'label_point', 'last_point', 'length', 'length3D', 'measure_on_line', 'overlaps', 'part_count', 'point_count', 'point_from_angle_and_distance', 'pop', 'popitem', 'position_along_line', 'project_as', 'query_point_and_distance', 'rotate', 'scale', 'segment_along_line', 'setdefault', 'skew', 'snap_to_line', 'spatial_reference', 'svg', 'symmetric_difference', 'touches', 'translate', 'true_centroid', 'type', 'union', 'update', 'values', 'within']