[GIS] Split SHP file using ogr2ogr

ogr2ogrshapefilesplitting

I am trying to split a SHP file using all the attributes in the column header SUBURB. Below is an example of the attribute table sitting behind the SHP file:

Name          Suburb
ABC           BAY
BCV           BAY
ABS           ISLE
CSD           DUNE           
QWE           ISLE
DSA           BAY
AG            DUNE
UY            WELL
UYA           ISLE

I want to be able to specify in the ogr2ogr script that it should split the SHP file based on 'BAY', 'ISLE', 'DUNE'….. without having to specify each one of them manually.

I have tested the -where tag but unable to get it to split on all the items contained in the header SUBURB. I am new to ogr2ogr so still learning.

Also if this is possible how do I ensure the output SHP files contain the respective SUBURB name?

I am using a windows 8 machine.

The code that I'm using is

ogr2ogr -f "ESRI Shapefile" -where "SUBURB = 'WELL'" D:\Mapdata\Spatial_Data\SHP_Files\Output D:\Mapdata\Spatial_Data\SHP_Files\Input.shp

This code creates the shapefile with features where the SUBURB is 'WELL'. I don't know how to modify this script to get it to create separate shapefiles with respective names for all the SUBURBS.

Best Answer

First generate a list of SUBURB (I use SOVEREIGNT because playing with Natural Earth Data) using OGR SQL function and output to CSV

ogr2ogr -f CSV ne_10m_admin_0_countries.csv ne_10m_admin_0_countries.shp -sql 
"SELECT DISTINCT SOVEREIGNT FROM ne_10m_admin_0_countries"

Avoid the first line (header with skip=1) and loop on the list of suburbs (SOVEREIGNT in my case)

for /f "skip=1 usebackq tokens=1 delims=," %%a in ("ne_10m_admin_0_countries.csv") do (
      ogr2ogr -f "ESRI Shapefile" -where "SOVEREIGNT = '%%a'" "out/ne_10m_admin_0_countries_%%a.shp" ne_10m_admin_0_countries.shp )

For second part, tested on a Linux box through Wine so syntax error may arise.