1

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.

  • Your code snippet lacks the import arcpy needed to run outside of the Python window of ArcMap but, assuming that you have it in your code, I think you need to include some print statements to make sure iterations are happening when you think they are. – PolyGeo Jul 22 '15 at 12:40
  • You can try using the script provided in this thread and this thread to see if it works. Just replace the GetParameterAsTexts with your hard-coded paths if running outside an ArcGIS toolbox. – John Jul 22 '15 at 13:00
  • 1
    Also, is Feature To Polygon designed to work on a layer selection? Perhaps export the layer as a standalone FC with Copy Features (in memory perhaps) and try running the tool. – John Jul 22 '15 at 13:07
  • @PolyGeo: I took out all the things that are not directly linked to this script, but I included the import statement for completeness again ;-) – BritishSteel Jul 22 '15 at 13:39
  • @John: like any geoprocessing tool it should work with a selection, and it does since the script is running in ArcGIS (but not outside). But you are absolutely right: exporting the layer to a separate file solves the problem! I cannot believe how much time I lost on this. I'm doing the exact same thing with other tools in my script and it all works fine with selections... it just won't do it for FeatureToPolygon. You should add your suggestion as an answer, so I can mark it as checked. – BritishSteel Jul 22 '15 at 13:46
2

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."

  • Exporting the layers as standalone in_memory features is what did it! – BritishSteel Jul 27 '15 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.