Wednesday, 10 August 2016

python - FeatureToPolygon (to cut polygons by line) not working correctly outside of ArcGIS


I asked a question yesterday, about splitting polygons using a line, and I ended up using the approach that is described in the comments here.



Although this worked with my test data, it causes a lot of issues when running it with more complex datasets.


Therefore I am working on a new approach, which works fine in ArcGIS, but not outside of ArcGIS.


In the following screenshot you can see my polygons and my line. I would like to have each polygon, crossed by a line, to be split by that line (in this case, each polygon would turn into two polygons).


enter image description here


Here is the workflow:


Step 1) MakeFeatureLayer_management: Create feature layer (because we are selecting the polygons)
Step 2) SelectLayerByLocation_management: Select all the buffers that are crossed by the line, because only those buffers should be considered
Step 3) buffersToBeSplit: iterate over the selection to extract all names, and push them into a list (each buffer has a unique name, which is found in the column Name)
Step 4) SelectLayerByAttribute_management: iterate over the list, and use each name to select the corresponding buffer
Step 5) FeatureToPolygon_management: for each buffer: use the buffer to cut it with the line



Note: I am splitting each feature separately, because splitting all of the buffers at once creates a lot of extra polygons (see comments mentioned above)


And here is the code:


import arcpy

polygon = r"D:\Data\buffers.shp"
line = r"D:\Data\line.shp"


# Step 1
polygon_fl = arcpy.MakeFeatureLayer_management(polygon,'polygon_fl')


# Step 2
arcpy.SelectLayerByLocation_management(polygon_fl,'INTERSECT',line)

# Step 3
buffersToBeSplit = []
for row in arcpy.SearchCursor('polygon_fl'):
buffersToBeSplit.append(str(row.Name))

# Step 4, 5

i = 0
for bufferName in buffersToBeSplit:
i += 1
query = '"Name" = \'%s\'' % bufferName
arcpy.SelectLayerByAttribute_management('polygon_fl','NEW_SELECTION',query)
arcpy.FeatureToPolygon_management(['polygon_fl',line],r"D:\Data\Output\cutPoly%s.shp" % str(i))

Proof that the Select by Location works: enter image description here


And here is the result when I am running this code from the Python console in ArcGIS:


enter image description here



Exactly what I need! Each buffer has now been split into two seperate parts.


Now the problem arises when I am running this code from outside of ArcGIS (PyScripter). The result looks as follows:


enter image description here


You can see that only the very first polygon is split (top left) and all the others are ignored. The fact that each polygon is returned means that the the selection is working fine, but what is not working is the part that uses the line to cut that selection.


I've been trying for quite a while now, and I absolutely cannot figure out why this code would work inside of ArcGIS but not outside.


PS: what I am trying to implement is what can be easily done in SAGA GIS using the Polygon-line-intersection tool . If you are a QGIS-user, you can find the tool in the Processing toolbox.



Answer



As requested, comment is appended below as an answer:


"Also, is Feature To Polygon designed to work on a layer selection? Perhaps export the layer as a standalone feature class with Copy Features (in memory perhaps) and try running the tool that way.


If that doesn't solve it, there are some threads here and here that use arcpy.Geometry.cut() for those who do not have an Advanced license to run Feature To Polygon."



No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...