2

In another thread, I was given this code to import large numbers of dxfs into QGIS. This code works in QGIS2.18, but they just released QGIS 3.0 and have changed the codes to use a different version of Python. This code doesn't work in 3.0. Running it generates a series of requests to set the CRS, but nothing then imports.

How can I edit this code (or other codes) to be compatible with QGIS 3.0? What was changed? I tried an online converter but it didn't alter anything.

import glob, os
path = "E:/aawork/aasites/aa-all towns and villages/Hemelhempstead/osmap 2012/"
for layer in glob.glob(path + "*.dxf"):
 vlayer = QgsVectorLayer(layer, 'name', 'ogr')
 subLayers = vlayer.dataProvider().subLayers()
 for subLayer in subLayers:
 geom_type = subLayer.split(':')[-1]
 if geom_type == 'LineString':
 uri = "%s|layername=entities|geometrytype=%s" % (layer, geom_type,)
 dfx_file_name = os.path.splitext(os.path.basename(layer))[0]
 layer_name = "%s - %s" % (dfx_file_name,geom_type,)
 sub_vlayer = QgsVectorLayer(uri, layer_name, 'ogr')
 QgsMapLayerRegistry.instance().addMapLayer(sub_vlayer)
asked Mar 5, 2018 at 10:37
1
  • Btw: Test the PlugIn Another DXF Importer / DXF2Shape Converter Multiple files can be imported at the same time (batch import). One click for a lot of files. Commented Mar 5, 2018 at 11:05

1 Answer 1

3

You could try the following:

import glob, os
path = "E:/aawork/aasites/aa-all towns and villages/Hemelhempstead/osmap 2012/"
for layer in glob.glob(path + "*.dxf"):
 vlayer = QgsVectorLayer(layer, 'name', 'ogr')
 subLayers = vlayer.dataProvider().subLayers()
 for subLayer in subLayers:
 if 'Point' in subLayer:
 uri = "%s|layername=entities|geometrytype=Point" % (layer)
 if 'LineString' in subLayer:
 uri = "%s|layername=entities|geometrytype=LineString" % (layer)
 dfx_file_name = os.path.splitext(os.path.basename(layer))[0]
 sub_vlayer = QgsVectorLayer(uri, dfx_file_name, 'ogr')
 QgsProject.instance().addMapLayer(sub_vlayer)

For the CRS, you could set a specific one as default when loading your dxf files which can be done from the the menubar:

Settings > Options > CRS > CRS for new layers
answered Mar 5, 2018 at 11:00
7
  • That's already done. It doesn't enable the code to work. Apparently Python has changed how the separators work, but I don't know how they changed them. Commented Mar 5, 2018 at 13:46
  • OK, I tried the amended version and checked that the CRS is set to default, but it's still asking me for a crs for each and every one of the dxfs in that folder. That's a lot of dxfs. The set to default doesn't seem to affect the dxf import. Commented Mar 5, 2018 at 13:50
  • 1
    @PJLightning - I can't replicate your issue. Are you using the official release version? The version I have is 3.0.0-Girona (you can check this from the menubar Help > About). Commented Mar 5, 2018 at 13:59
  • Another dxf converter does work for small numbers of dxfs, but it's about ten times slower than the python code. It's not a great option for large areas. It did import the dxfs eventually, though, which I guess is the main thing. Commented Mar 5, 2018 at 14:03
  • 1
    Oh wait! I thought I'd set a default crs, but I'd set it for the project, not for the layers. It's working. I'll select your code as the answer. Thank you. Commented Mar 5, 2018 at 14:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.