Smathermather's Weblog

Remote Sensing, GIS, Ecology, and Oddball Techniques

Reprocessing imagery with GDAL and BASH — and then cleaning up afterward

Posted by smathermather on August 13, 2012

I show a simple example today of using gdal tools to change from raster to polygons with Bash.  That’s right.  Still no pythonista here.  I have poked ever so gently at the surface of ruby and rails recently, while watching another coder re-tune his brain to a new set of principles of least surprise.

By the way, for my regular readers, please be patient with me over the next few quarters.  I am endeavoring to undertake a much larger writing project for the next few months, which will leave little time for this blog.  But, hopefully at the end, it will be worth everyone’s while.


#!/bin/bash

for name in $( ls ????_???_gt.tif);
do

basename1=`echo $name | awk -F'.' '{print $1}'`              # Split off non-extension portion of name

if [ -f $basename1".shp" ]
then
echo $basename1".shp exists"
else
echo "Converting " $basename1".shp"

gdal_polygonize.py $name -f "ESRI Shapefile" $basename1".shp"

if [ $? -lt 1 ]
then
echo "Conversion a success."
echo "Removing original file."
rm $name
else
echo "mehmeh.  try again."
fi

fi

done

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

 
%d bloggers like this: