Bash shell scripts primarily for batch geoprocessing spatial data using the OGR2OGR utility, a part of the Geospatial Data Abstract Library: GDAL
There are also a few scripts in here for doing other useful things such as a batch find and replace.
####Mac OS X:
Install the GDAL Complete Framework package via King Chaos.
Be sure to follow the instructions for appending GDAL utilities' absolute path to the PATH
variable in your .bash_profile
or .bashrc
in your home directory.
####Linux:
Install FWTools (the Linux 64-bit version is here).
####Test:
Make sure ogr2ogr
is working in Bash with basic command: ogrinfo --version
You should get back something like:
GDAL 1.10.0, released 2013/04/24
You can either add the shell_scripts directory to your PATH
or run the commands with the full file path.
These resources have helped me a lot in building the ogr2ogr
batch commands:
-
FYI, GitHub currently likes (i.e., requires) data in the CRS
urn:ogc:def:crs:OGC:1.3:CRS84
/EPSG:4326
for displaying geoJSON. -
List of OGR-supported Vector Formats...so many options!
##FAQ
The scripts I authored in this repository were written while working with Cartographic Design software that primarily uses the ESRI Shapefile format. Thus most of these scripts assume that is your data format unless otherwise specified. Use the shp-to-geojson
and geojson-to-shp
scripts to convert data between the ESRI Shapefile and GeoJSON data formats as needed.
Many of these scripts contain variables that must be edited to in order to change their paramaters. I plan on updating this in the future as I originally wrote these when first learning bash scripting and did not fully understand how to implement $ARGV when running a script. For now open the scripts with a text editor and change variables for things like output projection (eg: $T_SRS
).
You will have to set the executible permission for these scripts before you can run them. First cd
to the folder in which they live and do chmod +ux *
to make them executible.
- clip-extent-project.sh
- clip-raster-to-shp.sh
- clip-to-polygon.sh
- geojson-to-shp.sh
- get-extent.sh
- gpx-to-shp.sh
- grep-find-replace.sh
- make-hillshades.sh
- merge.sh
- project-google.sh
- project-mercator.sh
- project-wgs84.sh
- rename-files-gis-friendly.sh
- separate-roads-by-type.sh
- separate-roads-by-type-skeletron.sh
Clips data given bounding box coordinates and projects to an output CRS. Change variable assignments within the script for these parameters.
ESRI Shapefile
cd
to the directory of your .shp
data
./clip-extent-project.sh
test
Clips a raster dataset to a shapefile polygon. Both datasets must be in the same projection.
.shp
and GDAL supported raster formats.
./clip-raster-to-shp.sh polygon-to-clip-to.shp raster-data.tiff
test
clips all data to a shapefile polygon
.shp
Edit the CLIP
variable in the script to specify the clipping polygon.
cd
to directory containing .shp
data
./clip-to-polygon.sh
test
This script will export all .geojson files in a directory to .shp files in a specified subdirectory
.geojson
cd
to the directory of .geojson
files you want to convert
./$SCRIPTHOME/geojson-to-shp.sh
Writes new files to a data
subdirectory.
returns bounding box coordinates for a dataset
.shp
./get-extent.sh $DIR/$FILE
-84.391994 33.758135 -84.376599 33.754353
converts .gpx
files to .shp
format
.gpx
./xxx_yyy.sh $ARGS
test
search through files in a directory and perform a find and replace.
.txt, .html, .css, etc.
./grep-find-replace.sh
test
Takes a Digital Elevation Model (DEM) raster and generates hillshades from 4 different light angles and a slope shade.These 5 files may then be composited in QGIS, TileMill, Photoshop, etc.
Note: Requires a color-slope.txt
file containing the following:
0 255 255 255
90 0 0 0
Suggestion: Process DEM prior to running this script (mosaic, clip, resample, reproject, etc)
all GDAL supported Raster files
cd
to the folder containing the DEM
then do: ./make-hillshades.sh some-input-dem.flt
merges a directory of shapefiles.
.shp
cd
to the folder containing files to merge
copy a shapefile to merge other shapefiles to by doing
ogr2ogr merge.shp shapefile-to-merge-everything-to.shp
then do
./merge.sh
test
projects data to google web-mercator projection EPSG:900913
.shp
./project-google.sh
test
projects data to WGS 94 / World Mercator EPSG:3395
.shp
change the -s_srs
input to your data's source CRS
then cd
to the folder containing your data and do
./project-mercator.sh
test
Projects data to WGS 84 EPSG:4326
.shp
./project-wgs84.sh
test
changes filenames in directory to be GIS friendly: removes all white spaces and makes name lowercase
.abc
./xxx_yyy.sh $ARGS
test
Takes an OpenStreetMap roads.shp
file extracted from bbbike.org and parses into multiple shapefiles based on the type
field.
.shp
./separate-roads-by-type.sh
test
Same as above but uses input generated from Michael Migurski's Skeletron.py script
.abc
./xxx_yyy.sh $ARGS
test
This converts all shapefiles in a directory $DIR1
into geoJSON files in $DIR2
. If $DIR1
is not specified, the script looks in the present working directory. If $DIR2
is not specificied, the script will place the new geoJSON files in a new subdirectory called geojson
.
.shp
./shp-to-geojson.sh $DIR1 $DIR2
converting file: /data/bike_lanes.shp...