“Cropping” Shapefiles – Clipping Features

I have gotten a little carried away teaching myself Python, wrapping my brain around OOP and designing classes. But I’ve had relatively little opportunity using all that Python for what I intended it to help me with – Geoprocessing in ArcGIS. So I decided on a little review of what I learned a while back and apply that to a very common task in my “day job”.

What I spend about 80% of my GIS time doing is making pretty maps for engineering reports. Typically, these are based on SHP data for infrastructure (street, highways, interstates), hydrology (streams, rivers, lakes), administrative boundaries (city, county), geology, and project related information (e.g. boreholes, property boundaries, building foot print).

For North Texas projects, I was using GIS data from the North Central Texas Council of Governments (NCTCOG). But for projects in other regions of the state, I have been using ESRI data, USGS-NHD data, TNRIS data (especially for Texas geology), and data from other local sources, e.g. CAPCOG, for the Austin, Texas, region.

The “problem” with some of the ESRI data, apart from its datedness (When you’re working on construction related projects, it doesn’t help you that entire subdivisions or brandnew toll roads are missing from your data sets.), has been that it comes in SDC (Smart Data Compression) format. Which is great for cramming nationwide data onto DVDs but a pain when you end up adding Alaska roads to a Tarrant County, Texas, project MXD. Hey, wait, hear me out – of course, I know that this is not a “problem”. I’ve just been too lazy (busy, both) to fix it since half the time I can manage with only SHP files instead of SDC.

So how to “crop” those large files down to the region of interest ? Python ! While you can perform geoprocessing on feature classes in a geodatabase, you’re limited when it comes to geoprocessing with feature classes in SDC files. So one way out is to extract the feature class you need in ArcCatalog. I was amazed at the compression of SDC when I saw that extraction of one feature class from a 300MB SDC file created a 1.34 GB SHP file and took 34 minutes. That begs the question whether working with only the SHP you need is even more efficient (faster) than using the SDC file.

But I needed only a small portion of the SHP file – enough to cover a 15-county area. So I wrote this little micro Python script:

import arcgisscripting
gp = arcgisscripting.create()

try:
    # Set the workspace
    gp.Workspace = “c:/GeoProc

    # Process: Clip the det_riv shapefile to the extent of the metro_county shapefile and name it metro_riv
    gp.Clip_analysis(“det_riv.shp”, “metro_county.shp”, “metro_riv.shp”)

except:
    # In the event of an error, print messages
    print gp.GetMessages()

This created a 5MB shapefile with only rivers in the 15-county metro area I do most of my work in. Okay, this wasn’t magic, and a dozen lines of code are barely programming. But it worked, and I am glad I finally got around to using some Python on something I’ve been meaning to try in a long time.

Advertisements

1 Comment

Filed under Uncategorized

One response to ““Cropping” Shapefiles – Clipping Features

  1. Olivo

    Maybe I missed something, why did not you use the clip tool from ArcToolBox in Arc Catalog (thus with no need to load the layers)?. Or maybe you just wanted to experiment with scripting?.

    Just wondering whether there are advantages in using a script for this. Regards.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s