# How to efficiently convert a shapely linestring back into an arcpy polyline?

1316
2
Jump to solution
03-28-2023 02:45 AM
MVP Notable Contributor

All,

I'm hoping to tap into the python community for advice on how to efficiently convert a shapely linestring back into an arcpy polyline.  I am developing a suite of python tools in the ArcPro 3.1.0 toolbox (atbx).

So I read from a file geodatabase polyline geometries and convert them to a shapely linestring. I do this as shapely exposes a useful method on a polyline that does not exist in the arcpy.  A simplified snippet of my reading code (which works fine) is shown below:

``````from shapely.wkb import loads
with arcpy.da.SearchCursor(mylayer, ["SHAPE@WKB"]) as cursor:
for row in cursor:
geom = row[0]
line = loads(bytes(geom))  # Convert arcpy geometry into shapely geometry
# Do something with line using shapely
``````

Later on in my code I want to write the processed shapely linestring back into a new Feature Class using an insert cursor.

What I cannot work out, hence me coming to you, is how do I efficiently turn a linestring back into an arcpy polyline geometry so I can write it back to a new file geodatabase feature class?  In the help file the arcpy polylines take arrays of points when constructing a polyline. I was expecting to find the reverse of the WKB property on a polyline?  It seems to me that arcpy allows you to convert from a polyline into alternative ways of recording geometry (json, wkb, wkt) but does not seem to consume them?

Tags (4)
1 Solution

Accepted Solutions
MVP Frequent Contributor

arcpy has FromWKB() and FromWKT().

``````with arcpy.da.InsertCursor(new_fc, ["SHAPE@"]) as cursor:
new_geom = arcpy.FromWKB(line.wkb)
cursor.insertRow([new_geom])``````

Have a great day!
Johannes
2 Replies
MVP Frequent Contributor

arcpy has FromWKB() and FromWKT().

``````with arcpy.da.InsertCursor(new_fc, ["SHAPE@"]) as cursor:
new_geom = arcpy.FromWKB(line.wkb)
cursor.insertRow([new_geom])``````

Have a great day!
Johannes
MVP Notable Contributor

Johannes,

Awesome! It never occurred to me that it would be a function of arcpy, I was looking for some sort of read/write property of a geometry.  Well you have definitely earned yourself a 🍺

Thank you for your swift reply.

Duncan