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 -EZUSoft-– Mike -EZUSoft-2018年03月05日 11:05:34 +00:00Commented Mar 5, 2018 at 11:05
1 Answer 1
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– PJ Lightning2018年03月05日 13:46:31 +00:00Commented 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.PJ Lightning– PJ Lightning2018年03月05日 13:50:19 +00:00Commented 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
).Joseph– Joseph2018年03月05日 13:59:38 +00:00Commented 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.PJ Lightning– PJ Lightning2018年03月05日 14:03:47 +00:00Commented Mar 5, 2018 at 14:03
-
1Oh 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– PJ Lightning2018年03月05日 14:09:33 +00:00Commented Mar 5, 2018 at 14:09