3

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.

dxfdat="*.dxf"
shpLinestring="*.shp"

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

2

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
dxfdat="d:/tar/3d.dxf"
shpLinestring="d:/tar/1.shp"

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)
7
  • 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 ? Commented Jul 25, 2018 at 9:33
  • I added my answer Commented 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 Commented Jul 25, 2018 at 10:34
  • I added my answer Commented Jul 26, 2018 at 6:08
  • it doesn't work for me, may be because i'm using postgresql ? Commented Jul 26, 2018 at 10:24

Your Answer

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

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