PYTHON GIS - Geometrically clip all features contained in a database

0

 


In this article you will learn how to clip all the features (or shapefiles, as you prefer to call them) contained in a database using a shapefile.



The following procedure will help you when you have to trim spatial information considering the geometric shape of a specific shapefile; I advise you to carry out this procedure once you finish your project. To achieve this we will use a Python code developed by me, but first let's see what your conventional options would be:  


Option 1: how to geometrically crop a feature using the ArcMap (ArcGIS) layer editor.


0. Always make a backup of your data before making a modification.


1. Load the layer to be clipped, which we will call layer X, and also load the layer you want to use as the geometric clipping shape, which we will call layer Y.


2. Go to the “Standard” toolbar and press the “Search” button (a), in the search bar (b) type the word “clip”, and select the option “Clip (Data Analysis)” (c).







3. In item “Input feature” drag the X layer (a), and in item “Clip feature” drag the Y layer (b), run the tool, the result will be known as Z layer from now on (c).






4. In the Editor toolbar, press the Editor drop-down list and press the “Start Editing” button (a).



5. Turn off layers Y and Z (a), then select all polygons of layer X using the “Edit tool” of the “Editor” toolbar (b) and press on your keyboard the “Delete” key (c). The result should be a white background in the software display area (d).






6. Turn on layer Z (a) and select the polygons of this layer using the “Edit tool” of the “Editor” toolbar (b), press Crtl+C and then Ctrl+V (c) and choose as target layer layer X (d). The clipped polygons will now also be in layer X (e).










7. Go back to the “Editor” drop-down list in the corresponding toolbar and press “Stop Editing” (a), making sure to save the edit (b).



Option 2: how to geometrically crop a feature using geoprocessing tools


0. Always make a backup of your data before making a modification.

1. Load the layer to be trimmed which we will call layer X, also load the layer you want to use as the geometric trimming shape, which we will call layer Y.

2. Go to the “Standart” toolbar and press the “Search” button, which will display the “Search” window that will function in this case as a tool finder.  


3. Type the keywords “clip”, “delete” and “append” in order to select the tools “Clip (Data Analysis)”, “Delete (Data Management)” and “Append (Data Management)”, as required in the execution of the following flowchart.





4. Execute the flowchart. In this flowchart the layers used in each process are in brackets after the name of the item to which they correspond in each tool.


Applying option 1 or option 2 is easy when the process must be applied to only one or two layers within the database, but when the number increases to tens or hundreds of layers, it is better to have an automated method, so see below how to be much more efficient using Python code without programming.

How to geometrically trim features within a database with Python (ArcPy)


Step 0: Always make a backup of your data before making a modification.


Step 1: Open a blank MXD file, and press the “Pause drawing” button (a) or press F9 on your keyboard (This is to avoid the MXD file to collapse because of so much information).




Step 2: Load the layers to be clipped into the MXD, including the layer that will serve as the geometric clipping shape.


Step 3: Open the Python window in the Standard toolbar.


Muestra la ubicación de la ventana Python dentro de la barra de herramientas Standard (estándar)


Step 4: Copy the following code into a notepad and replace the text Clipper in line #04 with the name of the layer that will serve as the geometric clipping shape.



import arcpy                                                                     #01
mxd = arcpy.mapping.MapDocument("CURRENT")                                       #02
layers = arcpy.mapping.ListLayers(mxd)                                           #03
Clip_Features = "Clipper"                                                        #04
for layer in layers:                                                             #05
  if layer.name != Clip_Features:                                                #06
    clipped_layer = arcpy.Clip_analysis(layer.dataSource, Clip_Features, None)   #07
    arcpy.DeleteFeatures_management(layer)                                       #08
    arcpy.Append_management(clipped_layer, layer, "NO_TEST")                     #09
    arcpy.Delete_management(clipped_layer)                                       #10
    



Step 5: Copy the modified code into the Python window and press the Enter key twice.


After a few seconds you will see the “Clip (Data Analysis)”, “Delete (Data Management)” and “Append (Data Manangement)” tools start to run in the lower right corner, and after some time all the layers contained in the MXD will have been clipped.


Notes:


(a) If you want to be able to verify the result of the clipping before it is final, you can start an edit session before running the code.    


I hope you enjoyed this article, soon I will upload a video explaining the procedure that you will be able to consult in this blog. Save my blog among your favorite links, I will be uploading many more tricks of this style, remember that you can send me your concerns in the contact page or leave your comment, I will be attentive to respond.

You may like these posts

No comments