OGR & Python – Batch Processing

I have been looking at using OGR in/with Python. Initially I wanted to find an easy way to merge a number of shapefiles with OGR. But there appeared to be no straight forward way to do that using ogr2ogr from the commandline.

Python bindings for OGR are included in the installation for FWTools, and provided your PYTHONPATH in Windows is set up properly, all you need to get going is:

import ogr

For other installations, e.g. OSGEO4W, you might need from osgeo import ogr

There is a script here (page 22) for merging shapefiles. But it works by iterating through first files and then each feature to write each one into a new shapefile. This took forever ! I guess what I was looking for was a program or way to simply take one shapefile and tack it to the “end” of the next without crawling through one field at a time and copying it over to the next file.

Then I read about shapefile.py on Joel Lawhead’s geospatialpython.com. This is a really neat module that allows you to access features and features parts/points in shapefile directly.

import shapefile

r = shapefile.Reader(“yourfile.shp“)
features = r.shapes()
print Number of features: “,len(features)
cnt=1
for point in features[yourchoice].points:
   print Coordinates for Point “,cnt, point
   cnt=cnt+1

Works like a char. But again, to simply aggregate many shapefile in one SHP, this would take to long. So for now Darren’s little Windows script to batch process the SHP’s with ogr2ogr has been the best way. I haven’t really used Python for such purposes but I guess you could.

Advertisements

1 Comment

Filed under Uncategorized

One response to “OGR & Python – Batch Processing

  1. Arne,

    Thanks for the nice comments about the Python Shapefile Library! Great blog and I realize I’m posting on the old version.

    And I agree – iterating through each feature is waaaaay too slow.

    Here’s a very quick, pure python script to do it:

    # Merge a bunch of shapefiles quickly!
    import glob
    import shapefile
    files = glob.glob(“*.shp”)
    w = shapefile.Writer()
    for f in files:
    r = shapefile.Reader(f)
    w._shapes.extend(r.shapes())
    w.save(“merged”)

    All it takes is 8 lines of code and it is very fast. The above sample does not count for the attributes.

    I’ll do a post on geospatialpython.com for dealing with that.

    No matter how you merge a shapefile it’s a little more complex than just appending like you would a text file. You have 3 files all with headers which must be updated every time the file changes. The headers of the shp and shx file include the bounding box of ALL features so you have to iterate through every feature at some point but you do have a choice of when and how which can make a big difference.

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