Thu 13 March 2014

Filed under Blog

Tags gdal ogr geoprocessing

Sometimes, one wants to do some really simple geoprocessing on a large numbers of shapefiles. While you could definitely use Spatialite for that, I would rather avoid this. ogr2ogr, on the other hand, provides handy access to Spatialite-like functionality. Let's say you wanted to produce a text file which contained the X and Y coordinates of your many shapefiles as well as any other attributes that might have been there first. We can invoke the spatialite SQL dialect to do this simple geoprocessing and to stuff the output in a CSV file:

 for location in loc1 loc2
 do
   for i in `find ${ROOT}/${location} -iname "*shp"`
   do
     fname=`basename $i .shp`
     touch /tmp/${location}.csv
     ogr2ogr -f "CSV"  -append -dialect "sqlite" \
         -sql "SELECT id, month, doy_min, doy_max, n_pixels, ST_X(ST_Centroid(GEOMETRY)), ST_Y(ST_Centroid(GEOMETRY)) from $fname" \
         /tmp/${location}.csv $i
   done
done

Note that you can do many things like this. In this particular case, you could subset a particular extent, transform coordinates etc etc using ogr2ogr command line parameters. The process is fast and painless, and provided that you can come up with a suitable find command, very flexible and easy!

Comment

EO & DA Ramblings © J Gomez-Dans Powered by Pelican and Twitter Bootstrap. Icons by Font Awesome and Font Awesome More