Select to view content in your preferred language

How to get SHAPE@WKT into spatial dataframe

338
1
01-29-2024 05:21 PM
VersaceGrace
New Contributor II

Hi,

I have some polygon data in a feature class that I want to get as a spatial dataframe. In the attribute table the shape type is listed as Polygon.

 

in a notebook inside arc I  used:

import pandas as pd

input_poly="mypolys" #featurelayer currently on map

sdf=pd.DataFrame.spatial.from_featureclass(input_poly)

 

when I print sdf I get an OBJECTID column and a SHAPE column, but the shape column comes back containing a dictionary of "rings", e.g. {"rings":[[[111.69, 16.45],... which is disgusting and not usable. Rings is a geometry/shape type I've never seen in arc- I was expecting POLYGON or at worst MULTIPOLYGON.

So  how to I get the WKT representation of my geometries as a column in my spatial dataframe (e.g. proper POLYGON type instead of "rings"). I know that under the hood there is a column called SHAPE@WKT, so how can I expose that data and get it into the spatial dataframe. I don't want to have to get it with a searchcursor- it feels very inefficient when that information already exists.

0 Kudos
1 Reply
KimOllivier
Regular Contributor II

Well I think that a list comprehension wrapped around a da.SearchCursor is a very efficient and fast way to extract the features into a more open format.

But why do you think that Pandas (a non spatial module) would handle spatial data? I am impressed that it worked at all! Have you considered GeoPandas? That does handle shape columns and provides you with spatial functions.

Or maybe use the arcgis module? That has spatial extensions for pandas where geojson data returned by the Python Rest API is handled smoothly.

There is another way to extract the features - use Numpy. There is a function in the da module FeatureClassToNumPyArray that translates directly from a featureclass to a numpy array. Maybe you can convert from the numpy array to a dataframe?

Finally there are Geometry Objects. These are in-memory arrays of the geometry from a featureclass that have a complete set of spatial operators built in. Every spatial tool in the toolbox has an equivalent function for pairs of geometry objects that do not need an advanced licence. You can write a complete ArcGIS clone using a Basic licence. I do this for selected operations if needed to be restricted to a Basic licence or if I find a faster way by short-cutting the search.

0 Kudos