Friday, 1 September 2017

gdal - How to create dataset in python with creation options like in ogr2ogr with -dsco options



In ogr2ogr there is dsco option (data source creation option). e.g. ogr2ogr -f DXF -dsco trailer=trailer_buildings.dxf -dsco header=header_buildings.dxf out.dxf test_buldings_shp.shp. It works well, but how can I use the same dsco options in Python?


from osgeo import ogr,gdal
import os,sys

def create_dxf(dxf_out_path):
header_path = u'c:\header.dxf'
trailer_path = u'c:\trailer.dxf'
dxf_driver = ogr.GetDriverByName('DXF')
dsco = {'HEADER':header_path,'TRAILER':trailer_path}
dxf_ds = dxf_driver.CreateDataSource(dxf_out_path,**dsco)

gdal.SetConfigOption('DXF_INLINE_BLOCKS', 'TRUE')

This code throws an error: TypeError: Driver_CreateDataSource() takes at most 3 arguments (4 given)


If I keep only one argument in dsco dictionary I get the following error: TypeError: 'header' is an invalid keyword argument for this function. I've tried UPPER and lower cases but nothing helps. Documentation tells to watch supported options for each format here. And I clearly see that in DXF format there are HEADER and TRAILER creation option. But Python errors tells me vise versa things. I think option name in ogr2ogr utility doesn't correspond to option name in python gdal library, and if it so, where i can find documentation with full list of options for each vector format and for each language and utility? (e.g. ogr2ogr: -dsco HEADER='dxf_path.dxf'; python: kwargs = {'dxf_header':'dxf_path.dxf'}, etc...)



Answer



The options value is a list, not a dict, and the elements are key value pairs. Try:


co = ["HEADER=" + header_path, "TRAILER=" + trailer_path]
dxf_ds = dxf_driver.CreateDataSource(dxf_out_path, options=co)

I can't say dictionaries don't work, buy I don't recall seeing them in my experience. The python bindings closely follow the standard GDAL API.



No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...