2

I'm looking for a way to be able to frequently update a bunch of shapefiles (different land use and other areas). I would like to erase from them the lakes and rivers which are in another shape (hydro.shp).

I could just do them on by one but it doesn't sound really time effective considering that the hydro.shp is often modified. To add to the problem, I have a basic license, so I can't use the erase tool...

So, how to batch erase without the erase tool?

1
  • To do this I think you will need to use ArcPy. Performing an erase with a Basic (or Standard) level license is easy - just use Union as a workaround. To do batch operations without iterating in ArcPy or ModelBuilder your only option is the batch grid i.e. right-click on a system or custom tool and choose Batch.
    – PolyGeo
    Apr 6, 2016 at 0:27

3 Answers 3

2

Here is an updated answer based on a better understanding of your request. This code performs a union between the eraser and the erasee shapefiles and then selects the overlapping features and deletes them .It then deletes the unnecessary fields to maintain just the original fields from the original shapefiles. I clip the hydro layer first just to remove any extraneous features to avoid errors. In the future you could convert this to an arcgis tool so you can feed in the shapefiles as parameters as opposed to hardcoding the paths to the shapefiles. This worked on the sample dataset I had. Let meknow if it works for you.

import arcpy
import os

#delete any data tha might be stored in memory
arcpy.Delete_management("in_memory")

#set workspace to where all your shapefiles are 
arcpy.env.workspace="c:/data"

#define where the hydro shapefile is located, ideally in a different folder than the others 
hydroshp="c:/mydata/hydro.shp"

fcs=arcpy.ListFeatureClasses()
for fc in fcs:
    #create an empty list to be populated with the original shapefile's field names
    fieldnames=[]

    #List all fields in the shapefile and populate the fieldnames list with their names
    fields=arcpy.ListFields(fc)
    for field in fields:
        fieldnames.append(field.name)

    #define layer name for each shapefile
    fclayer=fc.rstrip(".shp")

    #create output files for the clip and the union layers
    fcClip=os.path.join("in_memory",fclayer+"clip")
    fcUnion=fclayer+"union.shp"#os.path.join("in_memory",fclayer+"union")

    #clip the shapefile using the hydro layer
    arcpy.Clip_analysis(fc,hydroshp,fcClip)

    #create a union of the clipped hydro and the shapefile
    arcpy.Union_analysis([fc,fcClip],fcUnion,"ALL")

    #delete any unnecessary fields from your union shapefile
    unionfields=arcpy.ListFields(fcUnion)
    for ufield in unionfields:
        if ufield.name not in fieldnames:
            arcpy.DeleteField_management(fcUnion,ufield.name)

    #Create layer file so you can perform a selection on it
    arcpy.MakeFeatureLayer_management(fcUnion,fclayer)
    arcpy.SelectLayerByLocation_management(fclayer,"WITHIN",fcClip)

    #Check if there are any selected features so you don't accidentally delete all the features.
    desc=arcpy.Describe(fclayer)
    if desc.FIDSet != "":
        arcpy.DeleteFeatures_management(fclayer)    
print "done!!"
2
  • Changed it a litle and it's working great!! Thanks a lot!
    – Rashomon
    Apr 12, 2016 at 1:25
  • To the left of my answer you should see the upvote and downvote arrows. There's a grey check mark below the downvote arrow. Click on it to accept the answer.
    – ketar
    Apr 13, 2016 at 22:55
2

Short of a script, you could try this in the meantime.

1) http://www.wwu.edu/huxley/spatial/tut/erase.htm

This web page recommends you

1) Union both polygons

2) Select the Polygons from the Unioned Polygon via select by location where Unioned polygons intersect your erased polygon (Hydro.shp)

3) Begin Editing and Delete Selected polygons from your Unioned Polygon.

Note: this could cause issues if you have a layer that gets selected that you do not want erased.

You can put all of this inside a model in model builder to make it easier for you.

2
  • 1
    Hi abrantmier, while this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
    – bummi
    Mar 25, 2016 at 15:17
  • Thanks, but I saw that option in my previous search. Looks a little too sketchy. Correct me if I'm wrong, but union would unite attribute tables too. So I would have to modify them afterwards. If I dont fond a better way, I'll vive it a try.
    – Rashomon
    Mar 27, 2016 at 2:47
0
import arcpy
#set workspace to where all your shapefiles are
arcpy.env.workspace="c:/data"
#define where the hydro shapefile is located, ideally in a different folder than the others
hydroshp="c:/mydata/hydro.shp"
fcs=arcpy.ListFeatureClasses()
for fc in fcs:
    #define layer name for each shapefile
    fclayer=fc.rstrip(".shp")
    #Create layer file so you can perform a selection on it
    arcpy.MakeFeatureLayer_management(fc,fclayer)
    arcpy.SelectLayerByLocation_management(fclayer,"ARE_IDENTICAL_TO",hydroshp)
    #Check if there are any selected features so you don't accidentally delete all the features.
    desc=arcpy.Describe(fclayer)
    if desc.FIDSet != "":
        arcpy.DeleteFeatures_management(fclayer)       
print "done!!"
5
  • I have edited my answer to show you the solution. Let me know if you have any other questions.
    – ketar
    Mar 29, 2016 at 15:25
  • Thanks a lot for trying! The script run perfectly, but it dosen't do what I'm looking for. Actually, I didn't see any change in my workspace after I ran the script. What line / fonction is "erasing" the hydro area from the other layers?
    – Rashomon
    Mar 31, 2016 at 19:17
  • Huh! It worked perfectly for me. Did you refresh the workspace? The select selects the features in the 'land' layers that are exactly identical to features in the hydro layer. The delete then deletes those features from the land layers. Sorry I couldn't help.
    – ketar
    Mar 31, 2016 at 20:12
  • I think I can see the problem. There are no "land" features that are exactly the same shape as the "hydro' features. That's why I was aiming for an Erase-like way to do it so that the "land" features would be splitted using the hydro feature and then use the hydro features shape to select and delete them. Maybe that's what the script you kindly wrote is doing, but i'm not that experienced with python to see what part does the "splitting"...
    – Rashomon
    Apr 4, 2016 at 17:07
  • oooh, I had assumed that the features were exactly identical. I wish I had access to your data so I could design a python solution that would work.
    – ketar
    Apr 6, 2016 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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