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.
Regards,
Tyler
No luck. I get an AttributeError when I attempt to read that file into a new DataFrame:
'DataFrame' object has no attribute '_data'
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.
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.
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)
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.