5

I have a dxf file of 3d contours. after import it does have only layer,x,y attribute and doesn't have Z attribute.

How can I provide Z attribute to create a TIN file?

7

Miro's answer will work fine, but if you just want to add a Z value to your attributes table (perhaps for labeling in a map or some other reason), I would start as he suggests:

v.in.dxf - to import contours including z dimension (SQLite works well as an output format)

Now instead of rasterizing, use the field calculator to pull the Z value from the 3D geometry into the attribute table. Because we are dealing with contours all Z values should be the same for a given linestring, so you can simply use the following formula:

z( start_point( $geometry ))
  • 1
    I just recently figured out this is possible now also directly in QGIS in processing toolbox - v.in.dxf works there just fine (for contours only) even using shapefile as output and z( start_point( $geometry )) to get Z as attribute in field calculator. Unfortunately I am not able to run this tool in model builder where it expects a layers name in DXF. I get dxf contours from many sources and layer names are pretty random. I would like to set v.in.dxf to all/any layers in model builder but can't figure out how. – Miro Jan 26 '17 at 5:25
  • Version 3> on wards z( $geometry ) for point layers that are imported from the v.in.dxf works jsut nicely. – Kapanther Dec 20 '18 at 4:59
7

When talking about having only contours in 3D dxf, the proper way is definitely NOT extracting the vertices as XYZ and interpolating the surface from points. That way you lose the information about how are the points connected which at least leads to losing some detail or it can be even worse.

If you use Dxf2xyz and you don't want to lose information I strongly suggest to get the Z values from points back to imported lines as additional attribute Z (Vector / Data management tools / Join attributes by location). After that rasterize the contours (Raster / Conversion / Rasterize) and use Grass / Raster (r.*) / r.surf.contour from Processing / Toolbox.

Or even better, use open source GRASS (comes together with QGIS OSGeo4W installation) - all functions below are now available as tools in QGIS toolbox (In QGIS top menu click on Processing / Toolbox):

v.in.dxf - to import contours including z dimension

In QGIS use field calculator to get z field - z( start_point( $geometry ))

v.to.rast (use=z) - convert lines to raster

r.surf.contour - to linearly interpolate the rest of the surface (DEM) between contours

  • I have tried this method, but I can't seem to get it to work. I have exported my DWG file as a DXF file and then used the v.in.dxf to import to contours into qgis, but when I use the v.to.rast the whole area turns black, and the new layer has a range from NAN to 1. When opening the attribute table I can't see any height information. do you know where it went wrong? – KristianMortensen Aug 21 '17 at 13:19
  • @KristianMortensen please check if you have values in your imported contours. In more recent versions of QGIS you can use v.in.dxf to get temporary layer as lines and field calculator to get z field z( start_point( $geometry )) to check if z values are imported properly. After that you can use build in rasterize function to make the conversion. – Miro Aug 22 '17 at 10:21
  • 1
    I would also like to mention that in many cases extracting vertices and interpolation of that is pretty sufficient. In last two years I mostly did it in the way I have densified vertices on lines to desired resolution and use triangulation interpolation - I made my own robust python script for that as interpolations within QGIS toolbox failed with the number of vertices I usually have. – Miro Aug 22 '17 at 10:31
  • Thanks for the answer, one last question, is it possible to make the lowest elevation curve be zero?. So instead of it being 1332 meters above ocean level it starts from zero? – KristianMortensen Aug 30 '17 at 13:44
  • 1
    @KristianMortensen I am not sure what you mean by that. Once you use field calculator and populate column z you can do any calculation with it again with field calculator. You can update z column or create new (z_new) for example with calculation "z"-1332. – Miro Aug 30 '17 at 14:06
3

You can use a free utility (Dxf2xyz 2.0, downloadable from http://www.guthcad.com/freestuff.htm) to convert from both dwg and dxf files to an "xyz" file, i.e. a comma delimited text file containing xyz coordinates. Then you need to change the extension file from ".xyz" to ".txt", before you load the xyz file in QGIS by the button "Add delimited text layer". Then you save the text layer to shapefile to have the points you need to create the TIN file.

1

From QGIS 2.18.2, there is a tool called "DWG/DXF Import" in the Project Menu.

enter image description here

In this tool the creation of a GeoPackage is required. Then you can import your dxf file and the elevation and simbology are retained.

0

Another workaround. Use GRASS GIS command v.to.3d. Create a field in the vector layer, then run v.to.3d with -r option and define the field added before

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.