[OSGeo Africa] Batch conversion from shp to kml
Zibusiso Ncube
ncubezedm at gmail.com
Wed Aug 17 01:43:32 PDT 2016
Apologies for the late delivery🙈
here is something you can use for that conversion..whilst the full reply
with all the bells and whistles is still being moderated through the osgeo
40kb limit ....
i figured you might be in urgent need for this. its a solutiion that uses
two files the main.py which is attached and ogr2ogr.py ,download it from...
https://svn.osgeo.org/gdal/trunk/gdal/swig/python/samples/ogr2ogr.py
main.py is what you run ( edit it and run it using IDLE which comes with
your python installation ... but whatever ide can do the job.I chose IDLE
for the simplicitity in this case.)
1- edit the file location (see filePathNamesList in script) for where you
shapefiles lie. I would just copy all of them into one folder. and point
the script to that folder
2- just make sure the ogr2ogr.py file is in the same folder as the main.py.
This is a python port of the GDAL cpp ogr2ogr.
3 - if you are using IDLE ...run the main.py
lastly if you do not have GDAL installed go to...
https://sandbox.idre.ucla.edu/sandbox/tutorials/installing-gdal-for-windows
read STEP 2 ....this guy explains so nicely...its a breeze
i have attached the main.py
Give me a shout through a personal email, if your are having issues.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/africa/attachments/20160817/6d641a02/attachment.html>
-------------- next part --------------
#*****************************************************************************
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#
#**************************************************************************
import sys
import os
from os.path import basename
import ogr2ogr
import os
import sys
from osgeo import ogr
#import KmlConvertor
def filePathNamesList(directoryPath, extension):
""" List of all files of a specific extension within a specific directory
path - directory in which the files are contained e.g C:\\dir\\subDir\\subSubDir\\subSubSubDir
extension - file extension e.g. .txt
"""
fileList=[]
#===========================================================================
# for f in os.listdir(directoryPath):
# if f.endswith(extension):
# fileList.append(directoryPath +"\\" +f)
#
# return fileList
#===========================================================================
fileList = [os.path.join(root, name)
for root, dirs, files in os.walk(directoryPath)
for name in files
if name.endswith((extension))]
return fileList
def convertSHPToKML(inputPath,outputPath):
""" OGR2OGR
I Did Not write This just adapted it. Make sure the ogr2ogr.py file
is included with these python files
"""
ogr2ogr.main(['','-f','KML',outputPath,inputPath])
def progression(percent, bar = 20):
sys.stdout.write("\r")
progress = ""
## for i in range(bar):
## if i < int(bar * percent):
## progress += "#"
## else:
## progress += " "
sys.stdout.write("[ %s ] %.2f%%" % (progress, percent * 100))
sys.stdout.write("\r")
sys.stdout.flush()
def main():
#pathList=None
extension=".shp"
#===========================================================================
# Give it some time to do its work (I did not test it just wrote and assumed
# it works ...just watch the memory and alert me if you think there are
# memory leaks.
# ....
#Zibusiso
#===========================================================================
pathList= filePathNamesList(r"C:\Users\NcubeZ\Desktop\TestShapefiles", extension)
#print(pathList)
count=0
for path in pathList:
#check if shpToKml folder exists and create one if it does not exists
shpToKMLfolder = os.path.dirname(path)+"\\shpToKML"
if not os.path.exists(shpToKMLfolder):
os.makedirs(shpToKMLfolder)
#create new filepath
outputPath=shpToKMLfolder+"\\"+ os.path.splitext(basename(path))[0]+".kml"
convertSHPToKML(path,outputPath)
outputPath=None
count+=1
percentagefilescompleted = (float(count)/len(pathList))
progression(percentagefilescompleted)
if __name__ == "__main__":
main()
More information about the Africa
mailing list