Iterate through polygons and perform Intersect

635
4
Jump to solution
06-25-2020 04:12 PM
KathyGillis
New Contributor II

Hi, I'm hopping for some help as I work through the process of writing a piece of code to produce an output of polygon intersects. My shape file contains many polygons which I would like to determine if any of them intersect and if they do add that intersect results to a new shape file. The code I used is below. I'm getting and error which is shown in a pic below.

Ultimately I'd like to have a single output shape file containing all intersect results. If you know a more efficient way or know how to make what I have work I am all ears!

I have attached a sample of the polygons. Many thanks for your attention!

I'm limited to using ArcMap 10.7 and python 2.7

```

import arcpy
from arcpy import env
import os

##Set working directory and workspace
wk_dir = os.getcwd()
env.workspace = wk_dir

##Assign feature class and working fields
fc = wk_dir + '\\CBT_Composite.gdb\\example_polys'
fields = ['RecordID', 'SHAPE@']

##Assign output file path and name
out = wk_dir + '\\output.gdb\\A'
##print fc_path + fc
with arcpy.da.SearchCursor(fc, fields) as cursor:
    for row in cursor:
        record = '{0}'.format(row[0]).split('.')[0]
        polygon = row[1]
        for row2 in cursor:
            if row2 != row:
                arcpy.Intersect_arc(row2[1], polygon, out +record) 

```

  picture of code

pic of error

0 Kudos
1 Solution

Accepted Solutions
MehdiPira1
Esri Contributor

Hi Kathy Gillis‌,

If you change the variables and run the following code you should get the intersected shapes, all in one feature class:

arcpy.env.workspace = r"workspace path"
arcpy.analysis.Intersect("example_polys #", "example_polys_Intersect", "ALL", None, "INPUT")

‍‍‍‍‍

View solution in original post

4 Replies
MehdiPira1
Esri Contributor

Hi Kathy Gillis‌,

If you change the variables and run the following code you should get the intersected shapes, all in one feature class:

arcpy.env.workspace = r"workspace path"
arcpy.analysis.Intersect("example_polys #", "example_polys_Intersect", "ALL", None, "INPUT")

‍‍‍‍‍
KathyGillis
New Contributor II

Thank you for your response Mehdi Pira!

Can you clarify what ' #' stands for in the Intersect statement?

Thank you Again - Kathy

0 Kudos
MehdiPira1
Esri Contributor

Not a problem, Kathy Gillis

This sign # is for Priority Ranks. Refer to the link below for more info:

Priority ranks and geoprocessing tools—Help | Documentation 

KathyGillis
New Contributor II

Thank you! @mehdi Pira It worked perfectly. Smiling really big!