Created
January 27, 2017 13:53
-
-
Save joeyklee/f7fee3a446f90fb34e48223e14e81685 to your computer and use it in GitHub Desktop.
python script to batch convert xml to shp and then combine all the shps to one
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import os | |
import subprocess | |
import shutil | |
def main(): | |
xmls = [ f for f in os.listdir(ifolder) if f[-4:] == ".xml"] | |
# print xmls[1][:-4] | |
# create a shapefile from each file | |
for i in xmls: | |
ofile = os.path.join(tempFolder, i[:-4] + ".shp") | |
ifile = os.path.join(ifolder, i) | |
cmd = ' '.join(['ogr2ogr', '-f', "'ESRI Shapefile'", ofile, ifile]) | |
subprocess.call([cmd], shell=True) | |
# # create a merge file | |
shps = [ f for f in os.listdir(tempFolder) if f[-4:] == ".shp"] | |
mergedFile = ofolder + "merged.shp" | |
mergecmd = ' '.join(['ogr2ogr', '-f', "'ESRI Shapefile'", mergedFile, os.path.join(tempFolder, shps[0])]) | |
subprocess.call([mergecmd], shell=True) | |
# then iterate and update the shapefile | |
for i in range(1,len(shps)): | |
updatecmd = ' '.join(['ogr2ogr', '-f', "'ESRI Shapefile'", '-update', '-append', mergedFile, os.path.join(tempFolder, shps[i]), '-nln', 'merged']) | |
subprocess.call([updatecmd], shell=True) | |
# remove the temp folder | |
shutil.rmtree(tempFolder, ignore_errors=False, onerror=None) | |
if __name__ == '__main__': | |
# change the working directory | |
datadir = sys.argv[1] | |
os.chdir(datadir) # '/Users/leejoey/Downloads/xml_v15_02_5/' | |
# check if the folder exists, if not then make it | |
if(os.path.isdir("_shp")): | |
print "already a folder" | |
else: | |
os.mkdir("_shp") | |
if(os.path.isdir("_temp")): | |
print "already a folder" | |
else: | |
os.mkdir("_temp") | |
# set the output folder | |
tempFolder = os.getcwd() + "/_temp/" | |
ifolder = os.getcwd() + "/" | |
ofolder = os.getcwd()+"/_shp/" | |
print ofolder | |
# call the function | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment