Following a comment on Error using SQL statement in ogr2ogr with Python string formatter, I am trying to use gdal.VectorTranslate()
instead of ogr2ogr
, but I keep receiving an error.
Using Windows, Python 3.7, and GDAL 3.3.1.
The following ogr2ogr
approach works fine.
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'))
But trying gdal.VectorTranslate()
based on the documentation for VectorTranslate and VectorTranslateOptions here throws the error as shown. I want to try this approach to see if it passes SQL statements easier than the -sql
option in ogr2ogr
.
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
args = gdal.VectorTranslateOptions(
format="KML",
SQLStatement=command.format(outfile=fout, infile='points.shp', my_id=ii, name='county')
)
gdal.VectorTranslate(
destNameOrDestDS=fout,
srcDS=shpecs,
options=args
)
Error:
C:\Users\name\anaconda3\envs\myenv\python.exe "D:/Documents/test.py"
Traceback (most recent call last):
D:\Documents\points
File "D:/Documents/test.py", line 51, in <module>
options=args
D:\Documents\working
File "C:\Users\name\anaconda3\envs\myenv\lib\site-packages\osgeo\gdal.py", line 857, in VectorTranslate
return wrapper_GDALVectorTranslateDestDS(destNameOrDestDS, srcDS, opts, callback, callback_data)
File "C:\Users\name\anaconda3\envs\myenv\lib\site-packages\osgeo\gdal.py", line 4625, in wrapper_GDALVectorTranslateDestDS
return _gdal.wrapper_GDALVectorTranslateDestDS(*args)
TypeError: in method 'wrapper_GDALVectorTranslateDestDS', argument 1 of type 'GDALDatasetShadow *'
I receive the same error if I pass the options directly into VectorTranslate.