2

I have a folder full of KML files and I want to create a .shp, .shx, .prj and .dbf files from these KMLs.

How can I accomplish this?

Below is the code that I am working on:

enter code here
import os , glob , qgis

path = r"\KML DUMP"

Location = r"\shapefile"


files = glob.iglob(os.path.join(path,"*.KML"))
num=1

for file in files:
    if os.path.isfile(file):
        layer = QgsVectorLayer(path + file, file, "ogr")

        writer = QgsVectorFileWriter.writeAsVectorFormat(layer, os.path.join(Location,'shape'+str(num)+'.shp'), "utf-8", layer.crs(), "ESRI Shapefile")
        writer = QgsVectorFileWriter.writeAsVectorFormat(layer, os.path.join(Location,'shape'+str(num)+'.shp'), "utf-8", layer.crs(), "ESRI Shx file")
        writer = QgsVectorFileWriter.writeAsVectorFormat(layer, os.path.join(Location,'shape'+str(num)+'.shp'), "utf-8", layer.crs(), "ESRI dbf file")
        writer = QgsVectorFileWriter.writeAsVectorFormat(layer, os.path.join(Location,'shape'+str(num)+'.shp'), "utf-8", layer.crs(), "ESRI prj file")
        num= num+1
2
  • I'm not for sure, but it seems that using the merger vector layers tool would be the easiest way. It can create a temporary layer that can be saved in any format QGIS supports. It is under the Vector>Data Management Tools menu.
    – Pooneil
    Commented Jan 28, 2019 at 23:43
  • I am unable to do that because not all kmls have the same geometry
    – Apmartin
    Commented Jan 29, 2019 at 0:20

2 Answers 2

1

You don't need to create all the extra files of the shapefile. You can just use the writer once for creating the shapefile and it will creates the extra files needed. I have successfully testing the following code with the Qgis 3.2 version...

import os,qgis
path = r"\KML DUMP"
Location = r"\shapefile"
## to parse the elements of the kml dump folder
for file in os.listdir(path):
    ## process only kml files by checking the extension
    if file.endswith(".kml"):
        ##to set up the input layer
        layer=  QgsVectorLayer(os.path.join(path,file), file, "ogr")
        ##to display the processed kml
        print(file)
        ##to set up the output shapefile, create a path with the name and #the shp extension
        output_shp=os.path.join(Location,file.split(".kml")[0]+".shp")
        ##proceed to the export
        data_writer = QgsVectorFileWriter.writeAsVectorFormat(layer,output_shp,"utf-8",layer.crs(),"ESRI Shapefile")
0

Best and simplest approach is to use OGR2OGR and put it in a Batch file with a For /r so it will iterate over each file found. This script will prompt for folder. Or modify it to be hard coded.

This script uses OSGEO4W installation of GDAL so modify accordingly. @echo off REM install GDAL from here https://trac.osgeo.org/osgeo4w/ REM https://www.gdal.org/ogr2ogr.html echo converts all KML to Shapefile set /p pathfoldertokml=path to shapefiles: cd /d %pathfoldertokml% for /r %%f in (*.kml) do ogr2ogr -f "ESRI Shapefile" -skipfailures -progress --config GDAL_DATA "C:\OSGeo4W64\share\gdal" -t_srs EPSG:4326 %%~dpnf.shp %%f echo. echo complete pause

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.