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.

  • What operating system are you trying this on? It's a trivial script but knowing what system you're trying it on would help you get a targeted answer. – SaultDon Jan 13 '16 at 3:51
  • @SaultDon- Windows 8 machine. The code 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' – VGu Jan 13 '16 at 3:56
  • 3
    Please edit the question in response to requests for clarification. Those who would help you should not need to scan comments for critical details. – Vince Jan 13 '16 at 5:04

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.

| improve this answer | |
  • Thank you for your help. Just tested the first bit on creating the list of suburbs. Why does it give me an error message: "Warning 4: Failed to open \.csv, permission denied"? The reason why I ask this is because it creates the csv file okay yet gives me the error message. I will test the second part tomorrow. Thank you once again! – VGu Jan 13 '16 at 6:10
  • Hi @Thomas, I am unable to get the second part of the code to work on a windows machine. Can you point me in the right direction with respect to setting up the input/output file path in ogr2ogr on a windows machine? – VGu Jan 14 '16 at 4:54
  • It worked okay in the end. I figured out where the syntax issues were occurring. Thank you once again. The exact syntax: for /f "skip=1 usebackq tokens=1 delims=," %%a in ("Suburbs.csv") do ogr2ogr -f "ESRI Shapefile" -where "SUBURB = '%%a'" "Parks_%%a.shp" Parks.shp – VGu Jan 14 '16 at 5:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.