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...)


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.

  • thank you for reply. It works. I was confused when read in documentation that options should to be PAIRS of key-value, that's why i decided to use dictionary instead of list, where one string is pair splited by = – Dmitriy Litvinov May 5 '16 at 6:42
  • Agreed, it can be confusing, especially for experienced python folks – user10353 May 5 '16 at 15:01

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.