Select to view content in your preferred language

Geoprocessing Script to Clip based on Two Attributes

4711
14
08-04-2015 06:38 AM
JasonFultz
Regular Contributor

Hi All,

I am looking for a method to do a clip based on two attributes.  In the attached shapefile, I have a "Year" field with years ranging from 0 to 27 and a "COLOR" field populated with either Red or Green.  I am looking for the best way to cycle through each year and clip the Red polygons from the Green polygons (discarding the area that intersects).  Is a python script or iterative model the best approach for this?  I am still a novice python user but very interested in learning more about it.  Any help with this would be greatly appreciated.

Thanks,

Jason

Attribute_Screenshot.JPG

0 Kudos
14 Replies
JayantaPoddar
MVP Alum

Hello Jason,

You could also try this. I could call it a LAYMAN method.

1) Use Geoprocessing > Union tool to delineate the boundaries.

2) From the ouput layer from UNION, convert feature to point.

3) Start editing. From the points layer, select (by attribute) the points where [COLOR]= "Red" and delete them. Save edits.

4) Then go for a Select by Location where

     Target Layer: Union_output (polygon)

     Source Layer: Point_layer (point)

     Spatial Selection Method: Contain the source layer feature.

Delete the selected features (These are the overlapping features). Save edits.

5) In the Union_Output layer, Select (by attribute) the polygons where [COLOR]= "Red" and delete them. Save and stop editing.

The Union_Output will now contain the features where [COLOR]="Red".

6) Dissolve the above layer based on [YEAR] and [COLOR] (Choosing the [COLOR] field is optional, since all the features in this layer are red).



Think Location
JasonFultz
Regular Contributor

Great, Thank you Jayanta.  I will give this a try. 

Thanks for the reply!

Jason

0 Kudos
DarrenWiens2
MVP Alum

Here is an alternative way to get to your answer, using arcpy geometry objects, directly:

>>> dict = {}
... with arcpy.da.SearchCursor("Topsoil",["SHAPE@","COLOR","Year"]) as cursor: # loop through the topsoil feature class
...    for row in cursor:
...        if row[2] in dict:
...            if row[1] in dict[row[2]]:       
...                dict[row[2]][row[1]] = row[0].union(dict[row[2]][row[1]]) # combine all the reds and greens for that year
...            else:
...                dict[row[2]][row[1]] = row[0]
...        else:
...            dict[row[2]] = {row[1]:row[0]}
... arcpy.CopyFeatures_management("Topsoil",r'in_memory\newPolys') # copy schema + features
... arcpy.DeleteFeatures_management(r'in_memory\newPolys') # delete features
... insCursor = arcpy.da.InsertCursor(r'in_memory\newPolys',["SHAPE@","Year"]) # get ready to write
... for year in dict: # loop through dictionary
...    if len(dict[year]) == 2: # does it have red AND green?
...        newPoly = dict[year]['Green'].difference(dict[year]['Red']) # subtract red from green
...    else:
...        newPoly = dict[year]['Green'] # write the green poly if there is no red
...    insCursor.insertRow([newPoly,year]) # insert the polygon
JasonFultz
Regular Contributor

Thanks for providing this, Darren.  I will give it a try. As I mentioned above, I seem to be having issues saving a script from GeoNet and running it in IDLE or the ArcGIS Python window.   Any idea how to best save this without the row numbers?

Thanks again,

Jason

0 Kudos
JasonFultz
Regular Contributor

I gave this script a try but received an error stating "Empty Output Generated".   Would it have stopped because Year 00 only has a green polygon, and not a red polygon, so no clipping would be necessary for Y00?

Thanks again,

Jason

error.png

0 Kudos