Geography Reference
In-Depth Information
course, if you are more comfortable with one of those over shell script-
ing, it makes sense to use what you know. If you want to tap into the
power of the GDAL/OGR bindings, you'll need to use Python, Ruby,
Java, or one of the other supported languages.
For Windows users it will likely be easier to use Ruby or Python, unless
you have access to a Unix-like shell through Cygwin or MSYS.
In this section, we'll take a look at a couple of examples, one using a
shell script and the other using Python with the GDAL/OGR bindings.
Converting Data with a Shell Script
In the simplest case, we have a directory full of files, and we want to
perform the same operation on each one. The basic application flow is
as follows:
1. Get a list of the files.
2. Loop over the list.
3. Perform some operation on each file.
4. Repeat until all files are processed.
It can't get too much simpler than that. Let's take an example using
bash and convert a batch of shapefiles from an Albers projection to
geographic coordinates in WGS 84. First let's figure out what command
and options we need to get the job done. We'll use ogr2ogr to convert the
files. Fortunately, all the shapefiles have an associated projection file,
so we don't have to worry about specifying that during the conversion.
Here is the bash script to do the conversion:
Download convert_shapefiles.sh
#!/bin/bash
Line 1
# Convert all shapefiles in the current directory to
-
# WGS84 projection. The converted shapefiles are placed
-
# in the geo subdirectory.
-
for shp in * .shp
5
do
-
echo "Processing $shp"
-
ogr2ogr -f "ESRI Shapefile" -t_srs EPSG:4326
geo/$shp $shp
-
done
-
Notice that on line 7, we are going to print a little message for each
shapefile that is processed. On line 8, we have the ogr2ogr command
that does the actual work. As it's processed, each converted shapefile
 
 
Search WWH ::




Custom Search