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
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,
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?
Great, Thank you Jayanta. I will give this a try.
Thanks for the reply!
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?
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
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
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?
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).
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))
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.