Geoaccessor.from_df

2690
3
01-11-2022 02:43 PM
PenelopeMitchell2
New Contributor II

Hello,

I am trying to convert a pandas dataframe that contains geometry (started from a feature class) to a spatially enabled dataframe using the GeoAccessor.from_df() function. See code snippet below:

 

sdf = GeoAccessor.from_df(CompDF, sr = 'OBJECTID', geometry_column='SHAPE')
sdf.spatial.plot()

 

referencing data as shown below for example:

OBJECTIDSHAPE
0{'x': 1587057.9938000003, 'y': 808409.8682000004, 'spatialReference': {'wkid': 32616, 'latestWkid': 32616}}

But I get a ValueError: Expected object or value 

PenelopeMitchell2_0-1641939553108.png

The data types are: OBJECTID is int64, SHAPE is object. I imagine the SHAPE dtype is throwing the error- if so, how do I get the SHAPE dtype to 'Geometry'? 

Or what is a good strategy to converting a dataframe with geometry information to a feature class or shapefile? I used to use GeoPandas but since I've upgraded my Pro version (2.8.2) the GeoPandas patch doesn't work in Pro due to compatibility issues.

Thanks for any insights.

0 Kudos
3 Replies
DanPatterson
MVP Esteemed Contributor

Just a wild guess, and not wanting to look up the help just yet, and based on esri's use of "sr", I suspect that that is the reference to the spatial reference and not OBJECTID

arcgis.features module — arcgis 1.9.1 documentation

sr

Optional integer. The WKID of the spatial reference.

(I had to check... just in case)


... sort of retired...
0 Kudos
jcarlson
MVP Esteemed Contributor

Is there some reason that you can't just use GeoAccessor.from_featureclass and leapfrog the non-spatial dataframe?

Using from_df is specifically for dataframes with an address string in them, and the resulting spatial dataframe will have the geocoded points.

Since your data looks like it's points, there are a couple ways you can do it. The first, and easiest, is to use set_geometry. You could even set the inplace parameter to "True" and turn your non-spatial dataframe into a spatial dataframe if you wanted.

sdf = CompDF.spatial.set_geometry('SHAPE', 32616)

 

The second method is more hacky, but I recall at least one or two situations where I had to go about it this way for some reason. Essentially, you take the 'SHAPE' column and split it up into multiple columns, then use from_xy to create the spatial dataframe.

xy_df = pd.concat([CompDF, CompDF['SHAPE'].apply(pd.Series)], axis=1)

sdf = GeoAccessor.from_xy(xy_df, 'x', 'y', 32616)

jcarlson_0-1641992285054.png

 

 

- Josh Carlson
Kendall County GIS
0 Kudos
PenelopeMitchell
New Contributor III

Thanks for your response! 

I tried the first approach but still got the same error.

PenelopeMitchell_0-1642111956366.png

I'll try the second method, but have to do some string manipulation on the x and y coord columns and just haven't had a chance to get to it yet. I'll update my post after i give it a go.

Thanks again, 

Penelope

 

0 Kudos