0

I am trying to use arcpy and pandas to build a line geometry from 2 point geometry columns in a pandas dataframe. I create the dataframe by loading in the featureclass using the GeoAccessor

from arcgis import GeoAccessor
from pandas import DataFrame as pdDataFrame
from arcpy import Polyline as apPolyline, Array as apArray, PointGeometry as apPointGeo, Point as apPoint
sdf1 = pdDataFrame.spatial.from_featureclass(r'MyGDB\MyPointFC_spoints')
sdf2 = pdDataFrame.spatial.from_featureclass(r'MyGDB\MyPointFC_epoints')
sdf2 = sdf2.add_prefix('end_')
sdf1 = sdf1.merge(sdf2, how='Left', left_on='sjid', right_on='ejid')

SDF1 now has 2 SHAPE columns that are point columns named 'SHAPE' and 'end_SHAPE' I have a custom function that takes the 2 shape columns and makes a polyline

def buildSegments(x):
    point = apPoint()
    array = apArray()
    point.X = x['SHAPE'].x
    point.Y = x['SHAPE'].y
    array.add(point)
    point.X = x['end_SHAPE'].x
    point.Y = x['end_SHAPE'].y
    array.add(point)
    return apPolyline(array, spatial_reference=x['SHAPE'].spatial_reference)

Then I use this to update the SHAPE column in my datframe and drop all the columns that start with "end_"

sdf1['SHAPE'] = sdf1.apply(buildSegments, axis=1)
cols = [col for col in sdf1 if col.startswith('end_')]
sdf1 = sdf1.drop(columns=cols)
sdf1.spatial.to_featureclass(location=r'MyGDB\MyLineSegments', overwrite=True)

What I get is an empty geometry when I add a print statement to the custom function to print what is being returned to the SHAPE column it shows it is returning a geoprocessing object <geoprocessing describe geometry object object at 0x000001A9F576D120> how do I get the geometry from this object to update my geometry column with?

2
  • I'd use the .WKT property, but only after being sure that GeoPandas wasn't required. Commented Jan 23, 2024 at 0:13
  • Geopandas is not possible; WKT doesn't seem to work either. It is not outputting a featureclass in my file geodatabase Commented Jan 24, 2024 at 15:15

1 Answer 1

1

This solved my issue

from arcgis import GeoAccessor
from pandas import DataFrame as pdDataFrame
from arcpy import Polyline as apPolyline, Array as apArray, PointGeometry as apPointGeo, Point as apPoint, AsShape as apAsShape, SpatialReference as apSpatialReference

def buildSegments(x):
    sr = apSpatialReference(x['SHAPE'].spatial_reference.wkid)
    point = apPoint()
    array = apArray()
    point.X = x['SHAPE'].x
    point.Y = x['SHAPE'].y
    array.add(point)
    point.X = x['dest_SHAPE'].x
    point.Y = x['dest_SHAPE'].y
    array.add(point)
    return apAsShape(apPolyline(array, spatial_reference=sr).JSON, True)

sdf1 = pdDataFrame.spatial.from_featureclass(r'MyGDB\MyPointFC_spoints')
sdf2 = pdDataFrame.spatial.from_featureclass(r'MyGDB\MyPointFC_epoints')
sdf2 = sdf2.add_prefix('end_')
sdf1 = sdf1.merge(sdf2, how='Left', left_on='sjid', right_on='ejid')
sdf1['SHAPE'] = sdf1.apply(buildSegments, axis=1)
cols = [col for col in sdf1 if col.startswith('end_')]
sdf1 = sdf1.drop(columns=cols)
sdf1.spatial.to_featureclass(location=r'C:\MyDir\MyGDB\MyLineSegments', overwrite=True)

Using arcpy.AsShape converts the object to a SHAPE column object

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.