From 836616cd8890af31dd190aedf27253a9664ac698 Mon Sep 17 00:00:00 2001 From: Pat Prodanovic Date: Thu, 7 Feb 2019 11:28:18 -0500 Subject: [PATCH] updates --- shp2csv_all.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 shp2csv_all.py diff --git a/shp2csv_all.py b/shp2csv_all.py new file mode 100644 index 0000000..cb13f7c --- /dev/null +++ b/shp2csv_all.py @@ -0,0 +1,57 @@ +# +#+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+! +# # +# shp2csv_all.py # +# # +#+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+!+! +# +# Author: Pat Prodanovic, Ph.D., P.Eng. +# +# Date: Feb 7, 2019 +# +# Purpose: This script takes all *.shp files, and converts each to *.csv. +# I found that I was often running shp2csv.py too many times; this script +# simply automates this task. +# +# Uses: Python 2 or 3 +# +# Example: +# +# python shp2csv_all.py +# +# there are no input arguments to this script; it looks for all *.shp +# files, and converts each to *.csv using shp2csv.py script +# +#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# Global Imports +#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +import os,sys,glob,subprocess +# +# determine which version of python the user is running +if (sys.version_info > (3, 0)): + version = 3 + pystr = 'python3' +elif (sys.version_info > (2, 7)): + version = 2 + pystr = 'python' +# +# I/O +# gets a listing of all *.shp in the folder, and stores it in a list +shp_list = list() +shp_list = glob.glob('*.shp') + +# sort the list +shp_list.sort() + +# construct the file names for the output (replace *.shp with *.csv) +csv_list = list() + +for i in range(len(shp_list)): + csv_list.append(shp_list[i].rsplit('.',1)[0] + '.csv') + +# now call shp2csv.py for each *.shp file in the list +for i in range(len(shp_list)): + print('converting ' + shp_list[i]) + subprocess.call([pystr, 'shp2csv.py', '-i', shp_list[i], '-o', csv_list[i]]) + +print('All done!')