Using OGR in Python, I'd like to create some geometries within a DXF file, but not in the "0" layer, but in new ones.

I tried with:

import ogr, os, osr, sys
ds = ogr.GetDriverByName('DXF').CreateDataSource('output.dxf' )
myLayer = ds.CreateLayer('entities')
newFeature = ogr.Feature(feature_def = myLayer.GetLayerDefn())
newFeature.SetGeometryDirectly(ogr.CreateGeometryFromWkt( 'LINESTRING(0 0, 100 100)'))
myLayer.CreateFeature( newFeature )
ds = None

But I don't have any idea about how to continue and create new layers.

I tried also with

layers = ds.ExecuteSQL("INSERT INTO entities (Layer) VALUES ('Layer1')")

but it does not work.

  • Any suggestion?
  • Is it worth using another DXF+Python library?

Thank you very much.


'layer' is an attribute field. Add:

newFeature.SetField( myLayer.GetLayerDefn().GetFieldIndex("layer"), 'newlayer')

And the feature will appear on layer newlayer.

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.