Monthly Archives: December 2010

Maneuvering Around Directories w/Python in Windows

Working a lot with SHP/TXT/KML files in Python on Windows, I have found it helpful to refresh my how-to memory :

To find out where you are:

import os
WorkingDirectory = os.getcwd()

To change directories:

import os

Really useful is the glob module.

import glob
for file in glob.glob(“*.*”):
    print file # print all file names in dir


import glob
for shp in glob.glob(“*.shp”):
    print shp # print names of all shapefiles in dir

Obviously, there is much more, e.g. os.path. See here ( and here (


Leave a comment

Filed under Uncategorized

Merging SHP-files with Geoscript

I hadn’t heard of Geoscript until I googled my way to this page. Geoscript is Java but there is a Python implementation for Jython. So if you’re familiar with either one, check it out.

The script certainly is shorter and looks more straight forward than other Python examples I had looked at. But as Tim Schaub observes, Darren’s Windows script (which I adapted for my purposes) is much more light weight.

Leave a comment

Filed under Uncategorized

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 on Joel Lawhead’s 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)
for point in features[yourchoice].points:
   print Coordinates for Point “,cnt, point

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.

1 Comment

Filed under Uncategorized