3

I want to open hundreds of KML files in QGIS. And it keeps showing this window. It's troublesome to set importing choices for every layer for hundreds of times. Is there any other more convenient method to finish this?

Example

0

I offer you a solution in python, to load all the information of the kml file

To load a single kml:

fileName = "C:\Users\fran\Downloads\kml\KML_Samples.kml"
layer = QgsVectorLayer(fileName,"test","ogr")
subLayers =layer.dataProvider().subLayers()

for subLayer in subLayers:
    name = subLayer.split('!!::!!')[1]
    uri = "%s|layername=%s" % (fileName, name,)
    #Create layer
    sub_vlayer = QgsVectorLayer(uri, name, 'ogr')
    #Add layer to map
    QgsProject.instance().addMapLayer(sub_vlayer)

To load a folder with kml:

import os

path = r'C:\Users\fran\Downloads\kml'
for r, d, f in os.walk(path):
    for file in f:
        if '.kml' in file:
            fileName = os.path.join(r, file)
            layer = QgsVectorLayer(fileName,"test","ogr")
            subLayers =layer.dataProvider().subLayers()

            for subLayer in subLayers:
                name = subLayer.split('!!::!!')[1]
                uri = "%s|layername=%s" % (fileName, name,)
                #Create layer
                sub_vlayer = QgsVectorLayer(uri, name, 'ogr')
                #Add layer to map
                QgsProject.instance().addMapLayer(sub_vlayer)

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.