1

edit - This code no longer works in the new version of QGIS (QGIS 3.0) If I paste it into the python console it generates a series of requests for the CRS but then imports nothing. QGIS 3.0 has moved to a newer version of Python and the code I have is incompatible.

How do I edit it to make it work again?

Original question:

I just merged several hundred layers into QGIS which had been imported using this code:-

import glob, os
path = "H:/aOsmap/Miltkeyn/2012 ntfs/all/"
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]
        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)

Merging them was difficult because the merge window (vector-merge layers) mixes up the linestring layers and the point layers so selecting just the point layers or just the linestring layers, from about over 600 layers, is laborious and prone to clicking on the wrong layer.

It would be easier and quicker to import all the points from my dxfs in one go and merge those (saving the merged file) and then import all the linestrings from the dxfs and merge those.

However, I don't understand the code so I have no idea what to alter to just select points or just select lines.

Can anyone help me?

  • 1
    As per the Tour chit chat like "please", "thanks", etc should not be included in questions here. – PolyGeo Jan 31 '18 at 12:56
  • The tour makes no mention that those words are banned. In fact, one of the examples of correct usage in the tour INCLUDES the words 'Thank you' so your comment is incorrect in both cases. Saying please is not chit-chat; it is normal politeness. Excluding it turns a polite request for assistance into a rude demand for an answer. . – PJ Lightning Jan 31 '18 at 13:08
  • 2
    @PJLightning Thank you for your thoughts on this subject. Please review the "popular" meta post on this subject: meta.stackexchange.com/questions/2950/… – Aaron Jan 31 '18 at 13:18
  • Not to barge in @PolyGeo, but some other upvoted answers mention that please IS allowed and don't generate that much noise. If anything, the edit that bumped the question back up to the top was probably more disruptive than the please comment at the end. – FrankerZ Feb 1 '18 at 17:20
  • @FrankerZ my one word edit should be taken in context. I think helping users to understand that SE is not a discussion forum (where social niceties are fine) is important in order to keep questions and answers here clear and crisp. – PolyGeo Feb 1 '18 at 22:33
2

You can add an if statement to only allow certain a geom_type to be imported. Right after the line geom_type = subLayer.split(':')[-1] add:

  • For points:

    if geom_type == 'Point':
    
  • For lines:

    if geom_type == 'LineString':
    

Example:

import glob, os
path = "H:/aOsmap/Miltkeyn/2012 ntfs/all/"
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]
        # Here we can add the if statement
        if geom_type == 'Point':
            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)
  • That works. Would I add linestring instead of point for the lines? – PJ Lightning Jan 31 '18 at 12:52
  • 1
    @PJLightning - Yes and be careful because it is case-sensitive. LineString :) – Joseph Jan 31 '18 at 12:54
  • 1
    Information. The code given above is only applicable in versions of QGIS up to 2.18. As of version 3.0, the code no longer works because QGIS have moved to a different version of Python. – PJ Lightning Mar 2 '18 at 13:37

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.