AntholoGIS

January 7, 2008

Spliting shapefiles per column values

Filed under: GIS, Geoprocessing — Tags: — Eduardo Kanegae @ 10:57 pm

Take the following situation:

  • a large shapefile. E.g.: municipalities for a given country or, in my case, a collection of forestry stands grouped by projects - all stands for a given region
  • the need of spliting this shapefile per a given column ( e.g.: state, forest project ID)

What to do? ArcMap->Load large shapefile->Set Defintion Query->Export to new shapefile? In a pig´s eye!

Because I´m not an Arc power user, I always have some FOSSGIS with me:

1. The first step is to load the shapefile to a new PostGIS table. Using shp2pgsql you can quickly load any shapefile to a given PostGIS database.

2. Then, using PHP and ADOdb it´s possible to generate .bat instructions for extraction data from PostGIS to shapefiles with OGR commands:

<?
/*reference ADOdb classes*/
include('adodb/adodb.inc.php');

/*output folder for generating shapefiles*/
$folder4Extraction = "C:\\FreeGIS\\tasks\\output";

/*creates a new connection object*/
$dbPostGIS = ADONewConnection('postgres');
/*connects to PostgreSQL server*/
$dbPostGIS->Connect('my_postgresql_server','myuser','mypassword','db_postgis1');

if (!$dbPostGIS)exit("connection error");

/*This SQL could be something like:
 SELECT distinct city_name FROM municipalities*/
$strSQL = "SELECT distinct forest_farm_code FROM forestry_stands";

$rsExportSourceData = $dbPostGIS->Execute($strSQL);

if(!$rsExportSourceData) exit("retrieve error");

/*get the total of rows to process*/
$iTotalRecords = $rsExportSourceData->RecordCount();
for($i=0;$i<($iTotalRecords);$i++){

   $strExportCommand = "ogr2ogr -where \"forest_farm_code='".
            $rsExportSourceData->Fields("forest_farm_code").
            "'\" -t_srs EPSG:4326 -f \"ESRI Shapefile\" ".
            "$folder4Extraction".
            " PG:\"host=my_postgresql_server user=myuser password=mypassword dbname=db_postgis1\"".
            " -nln ".$rsExportSourceData->Fields("forest_farm_code") .
            " forestry_stands\n";
   echo $strExportCommand;
   $rsExportSourceData->MoveNext();
}
?>

Note: click here to get this snippet.

3. After running this PHP script some .bat commands will appear like:

ogr2ogr -where "forest_farm_code='F015'" -t_srs EPSG:4326 -f "ESRI Shapefile" \
  C:\FreeGIS\tasks\output \
  PG:"host=my_postgresql_server user=myuser password=mypassword dbname=db_postgis1" -nln F015 forestry_stands

In this case, ogr2ogr command will be used to extract data from PostGIS. Looking command parameters we have:

  • -where “forest_farm_code=’F015′” : filter condition. Only object in this condition will be exported
  • -t_srs EPSG:4326 : geodata will be outputed to WGS 84 ( EPGS:4326) spatial reference system
  • -f “ESRI Shapefile” : output file format
  • C:\FreeGIS\tasks\output : destiny dataset - the folder to store exported shapefile
  • PG:”host=my_postgresql_server user=myuser password=mypassword dbname=db_postgis1″ : source dataset.
  • -nln F015 : new layer name. The shapefile will be named as F015.shp
  • forestry_stands : the PostGIS source table

4. Finally, copy all generated command lines to a .bat file and run this .bat file inside a FWTools shell.
Now, just wait the exporting process and relax. :-)

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

You must be logged in to post a comment.

Blog at WordPress.com.