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)
  • 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. – Mike Mar 5 '18 at 11:05
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
  • 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. – PJ Lightning Mar 5 '18 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. – PJ Lightning Mar 5 '18 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). – Joseph Mar 5 '18 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. – PJ Lightning Mar 5 '18 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. – PJ Lightning Mar 5 '18 at 14:09

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.