Geoprocessing Script to Clip based on Two Attributes

3668
14
08-04-2015 06:38 AM
JasonFultz
New Contributor III

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
WesMiller
Regular Contributor III

The script below should get you started. Open a new arcmap and add your Topsoil layer open your arcmap python window right click behind the ">>>" and select load and load this script hit enter and watch it run.

import arcpy,os


arcpy.env.overwriteOutput = True
fc ="Topsoil"
y = 28
out_path = "in_memory"
"""
#Create results featureclass

results = "results"
arcpy.CreateFeatureclass_management(out_path,results,"POLYGON",fc)
"""


for i in range(y):
    greenquery = '"COLOR" =' + "'Green'"
    redquery = '"COLOR" =' + "'Red'"
    value = str(i)
    if len(value) == 1:
        value = "0" + value
    value = "Y"+value
    yearquery = '"Year" ='+ "'%s'"%(value)
    Rquery = yearquery + "AND" + redquery
    Gquery = yearquery + "AND" + greenquery
    redLayer = "Red" + value
    greenLayer = "Green" + value
    #Create red layer
    arcpy.MakeFeatureLayer_management(fc,redLayer,Rquery)
    #Create green layer
    arcpy.MakeFeatureLayer_management(fc,greenLayer,Gquery)
    #print int(arcpy.GetCount_management(redLayer).getOutput(0))
    if int(arcpy.GetCount_management(redLayer).getOutput(0)) > 0:
        #do the clip
        #print value
        clip = "clip"+value
        arcpy.Clip_analysis(greenLayer,redLayer,os.path.join(out_path,clip))
0 Kudos
JasonFultz
New Contributor III

Wes, Thank you very much for the reply and for providing the script.  I'm afraid I'm going to show how much of a true Python novice I am by asking if there is a way to save this script that you supplied? I am unable to import it into Python window without it being saved as a .py file.  Is there a way to save if from GeoNet as a .py file?

Thanks again,

Jason

0 Kudos
WesMiller
Regular Contributor III

Highlight the code press "ctrl" key and the "c" together or with the code highlighted right click and select copy. Then open the python IDLE and under "File" select "New Window" and click in the new window and press "ctrl" and "v" key together or right click and select paste then save the code to a location and make sure to give a meaningful name ( a name that will tell you what the code is) and end it with ".py" you'll then be able to add to your arcmap python window

0 Kudos
JasonFultz
New Contributor III

When I tried this method, I received the following error in the screenshot.  I copied/pasted it into the python IDLE and saved it as a .py.  I then loaded it into the python window in ArcGIS and received the following syntax error.  I feel its because the method of copy/paste.  I would guess that the syntax error is because of the numbers getting copied over as well.  Any thoughts?  

Python.JPG

0 Kudos
WesMiller
Regular Contributor III

The code is attached

0 Kudos
JasonFultz
New Contributor III

Thanks, Wes.  I get the following error when I run the script. Any ideas?

error.JPG

0 Kudos
WesMiller
Regular Contributor III

Sorry, try this one.

0 Kudos
JasonFultz
New Contributor III

Thanks, Wes.  This script ran fine, but it made me realize I perhaps want to do an Erase rather than a Clip.  I believe I described this task incorrectly when I first posted, my apologies.  I am looking to eliminate the overlap between the red and green polygons for each year. Although I would like to still keep both the red and green polygons for each year, just eliminate any overlap between them. I can try to modify the script you provided to run an Erase instead of Clip. 

I do have one more question.  I noticed that when I ran the latest script you provided, it produced a new shapefile for each color for each year (screenshot below).  Ideally, I would like to end up with one shapefile that contains all polys, which still maintains year and color, only with the red/green overlap erased by year.

Thanks again for the help you have given me with this task.

Jason

Capture1.JPG

0 Kudos
WesMiller
Regular Contributor III

Jason the script creates 3 files for every year that has features with red. If I remember correctly Y00 and Y01 didn't have any red features to use. I think if you check just those years that begin with clip they may be what you are describing. If that is the case you just need to generate a list and feed it into the merge tool ArcGIS Help (10.2, 10.2.1, and 10.2.2)

0 Kudos