I am iterating through a list of shapefiles and extracting subsets of each shapefile to save as a KML using ogr2ogr. How can I extract the subset based on multiple conditions while using string formatter to define the in/out files and conditions? I am following the answer from Selecting features by attributes using ogr2ogr? in my attempt.
Using Python 3.7
This example with only one condition works without issue:
import os
import numpy as np
import pathlib
example dummy ids
id1 = np.linspace(start=1, stop=5, num=5, endpoint=True, dtype=int)
id2 = np.linspace(start=301, stop=302, num=2, endpoint=True, dtype=int)
outpath = pathlib.Path("working").resolve()
for ii in id1:
command = (
' ogr2ogr -f "KML" "{outfile}" "{infile}" '
' -where "ID_1 = {my_id}" '
' -dsco NameField = "{name}" '
)
fout = 'test' + str(ii) + '.kml'
fout = outpath / fout
os.system(command.format(outfile=fout, infile='points.shp', my_id=ii, name='county'))
This example with multiple conditions using a SQL query returns an error:
for ii in id1:
for jj in id2:
command = (
' ogr2ogr -f "KML" "{outfile}" "{infile}" '
' -sql "SELECT * from {infile2} WHERE ID_1 = {my_id} AND ID_2 = {other_id}" '
' -dsco NameField = "{name}" '
)
print(command)
fout = 'test' + str(ii) + '_' + str(jj) + '.kml'
fout = outpath / fout
os.system(command.format(
outfile=fout,
infile='points.shp',
infile2=pathlib.Path('points.shp').stem,
my_id=ii,
other_id=jj,
name='county')
)
print statement returns:
ogr2ogr -f "KML" "{outfile}" "{infile}" -sql "SELECT * from {infile2} WHERE ID_1 = {my_id} AND ID_2 = {other_id}" -dsco NameField = "{name}"
And the error:
Warning 1: layer names ignored in combination with -sql.
ERROR 1: SQL Expression Parsing Error: syntax error, unexpected $undefined, expecting string or identifier. Occurred around :
SELECT * from points WHERE ID_1
^
gdal.VectorTranslate(pretty much identical toogr2ogr) rather than an external process, but if you really want to use an external process, don't useos.system, usesubprocess.*e.gsubprocess.run()which handles all the commandline argument formatting for you, you just pass in a list of arguments. – user2856 Jul 20 '21 at 00:39I also tried
– a11 Jul 20 '21 at 01:16subprocess.run(command.format(outfile=fout, infile='points.shp', my_id=ii, name='county'), shell=True)for my first example (that worked fine with os) and receivedWarning 6: creation option 'NameField' is not formatted with the key=value format ERROR 1: Couldn't fetch requested layer '='!and it just creates an empty KMLgdal.VectorTranslatebut ran into some errors; I thought it was worthwhile as its own question – a11 Jul 20 '21 at 01:49