I want to create a QGIS plugin that convert dxf files to shp. I didn't use DXF2SHP plugin in QGIS because I have some specific needs. I'm using gdal_ogr library.


opt1="-skipfailure  --config DXF_MERGE_BLOCK_GEOMETRIES FALSE --config DXF_INLINE_BLOCKS TRUE -gcp  z  -nlt LINESTRING "
sql1= "-sql \"SELECT *,ogr_style from entities where OGR_GEOMETRY LIKE '%LINESTRING%'\" "
processing.runalg('gdalogr:convertformat',dxfdat , 0,opt1 + sql1, shpLinestring)

I can make the convertion but is it possible to keep the elevation attribute from dxf file ?

1 Answer 1


The DXF is real 3D (Z-value not attribute)? Then test the following:

-dim val:(starting with GDAL 1.10) Force the coordinate dimension to val (valid values are XY, XYZ, XYM, and XYZM - for backwards compatibility 2 is an alias for XY and 3 is an alias for XYZ). This affects both the layer geometry type, and feature geometries. Starting with GDAL 1.11, the value can be set to "layer_dim" to instruct feature geometries to be promoted to the coordinate dimension declared by the layer. Support for M was added in GDAL 2.1

processing.runalg('gdalogr:convertformat',dxfdat , 0,sql1 + opt1 + ' -dim XYZ', shpLinestring)

A polyline usually has different z-values. As an attribute, you can only put one Z value for the entire line. If that's enough for you, create a new field in the field calculator:enter image description here

Another way with SQLite-Synax: Write the z-Value from the first point of LineString to Attribut

import processing

opt1="-skipfailure  --config DXF_MERGE_BLOCK_GEOMETRIES FALSE --config DXF_INLINE_BLOCKS TRUE -dim XYZ  -nlt LINESTRING "
sql1= "-dialect sqlite -sql \"SELECT *,ST_Z(ST_StartPoint (geometry)) as elevation, ogr_style from entities where GeometryType(geometry) LIKE '%LINESTRING%'\" "
processing.runalg('gdalogr:convertformat',dxfdat , 0,opt1 + sql1, shpLinestring)
  • thank u for the replay ! I found this answer gis.stackexchange.com/questions/199479/… , and thanks to ur answer I can keep elevation (step 2) but how to add it as a field in the output layer ? Jul 25, 2018 at 9:33
  • I added my answer Jul 25, 2018 at 10:25
  • I need to code that, I've tried to use field calculator using : processing.runalg('qgis:advancedpythonfieldcalculator', shpLinestring, 'elevation', QVariant.Double, 10, 2, '',' z(point_n($geometry, 1))', shpLinestring) but it's not working Jul 25, 2018 at 10:34
  • I added my answer Jul 26, 2018 at 6:08
  • it doesn't work for me, may be because i'm using postgresql ? Jul 26, 2018 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.