Select to view content in your preferred language

arcgis.geometry Polygon not converting polyline to polygon

2686
15
03-11-2022 07:39 AM
TylerT
by
Frequent Contributor

Community,

I have a spatially enabled dataframe (SEDF) with polyline geometry.  Ultimately, I need polygon geometry. 

My attempt to use arcgis.geometry is shown below.  The Polygon() method doesn’t throw any errors but it also doesn’t seem to do anything.  The geometry remains a polygon.

Any suggestions would be appreciated.  Also, I don’t necessarily need to employ the method I’m trying, but it is desirable to stay within the SEDF.

polyline.JPG

Regards,

Tyler

0 Kudos
15 Replies
TylerT
by
Frequent Contributor

@jcarlson, any findings with the pickle file?

0 Kudos
jcarlson
MVP Esteemed Contributor

No luck. I get an AttributeError when I attempt to read that file into a new DataFrame:

'DataFrame' object has no attribute '_data' 

 

- Josh Carlson
Kendall County GIS
0 Kudos
by Anonymous User
Not applicable

Worth noting for posterity that this method is dangerous. This is why I couldn't test this data sample.

From the Python docs:

Warning

The pickle module is not secure. Only unpickle data you trust.

It is possible to construct malicious pickle data which will execute arbitrary code during unpickling. Never unpickle data that could have come from an untrusted source, or that could have been tampered with.

Consider signing data with hmac if you need to ensure that it has not been tampered with.

Safer serialization formats such as json may be more appropriate if you are processing untrusted data. See Comparison with json.

 

DavidAnderson_1701
Frequent Contributor

DavidAnderson_1701_0-1647271695974.png

 The above code shows how I was able to get this to work.  The SEDF Polygon operator does not quite work as I thought it should.  This statement did not wori:

temp_df['new_geom'] = temp_df['coor'].apply(lambda x : arcgis.geometry.Polygon(x))

I dropped back to using arcpy geometry constructors, which seems to work.

Note this is hard coded to just work for a single part polygon with no holes.   A more general formalation would be extract the arcpy code into a separate function.

0 Kudos
DavidAnderson_1701
Frequent Contributor

I read in the pickle file, then successfully applied the method I desribed above:

test_df = pd.read_pickle(r"polyline_sample.pkl")

test_df['coor'] = test_df['SHAPE'].apply(lambda x : arcgis.geometry.Polyline.coordinates(x))

test_df['poly_geom'] = test_df['coor'].apply(lambda x : arcpy.Polygon(arcpy.Array([arcpy.Point(*coords) for coords in x[0]])))

test_df.spatial.set_geometry('poly_geom',inplace=True)

DavidAnderson_1701_0-1647466742835.png

 

 

 

0 Kudos
jcarlson
MVP Esteemed Contributor

Excellent! Thanks for doing that! I have no idea why pandas didn't want to read the file in my env, but I had too much going on to really dig into it further.

- Josh Carlson
Kendall County GIS
0 Kudos