-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathESRI2TableauFormat.py
95 lines (73 loc) · 2.85 KB
/
ESRI2TableauFormat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import csv
import re
import sys
#def convertESRI2TableauFormat( p_inputfile, p_outputfile):
if len(sys.argv) != 3:
print "usage: ESRI2TableauFormat INPUTCSV OUTPUTCVC"
sys.exit(1)
l_ESRIFileName = sys.argv[1]
if l_ESRIFileName.find(".csv") == -1:
print "INPUTFile must be a CSV"
sys.exit(1)
l_outfileName = sys.argv[2]
if l_outfileName.find(".csv") == -1:
print "OUTPUTFile must be a CSV"
sys.exit(1)
rdx=0
csv.field_size_limit(sys.maxsize)
outfile = open( l_outfileName, 'wb')
csvWriter = csv.writer( outfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
with open( l_ESRIFileName, 'rb') as csvfile:
csvR = csv.reader( csvfile, delimiter=',', quotechar='"')
for row in csvR:
rdx += 1
ci=0
rowValues = []
rowValues.append(row[1]);# ssc_code
rowValues.append(row[2]);# ssc_name
rowValues.append(row[4]);# state_code
rowValues.append(row[5]);# confidence
rowValues.append(row[6].strip());# AREA_SQKM
rowValues.append('');# place holder for lat
rowValues.append('');# place holder for long
rowValues.append('');# place holder for polygon
rowValues.append('');# place holder for point
if ( rdx==1): # first row add header col names
rowValues[5]=('Latitude');#
rowValues[6]=('Longitude');#
rowValues[7]=('Polygon_ID');#
rowValues[8]=('Point_ID');#
csvWriter.writerow( rowValues);
else:
for col in row:
ci += 1
LatLonStrArr=[]
if (ci==1): # first col has the polygon values.
polygonArr = []
polygonArr = re.findall('(\((?:-?\d+\.\d+\s-?\d+\.\d+,?)+\))', col)
polyx =0
for px in polygonArr:
polyx += 1
LatLonStrArr = []
LatLonStrArr = re.findall('(-?\d+\.\d+\s-?\d+\.\d+)', px)
if( len( LatLonStrArr) != 0 ):
pointx = 0
pointxx = 0
for lx in LatLonStrArr:
pointx += 1
resArr = re.findall( '(-?\d+\.\d+)', lx)
#print('>>>> lx='+lx + "/"+ str(len(resArr)))
rowValues[5] = resArr[1] # lat
rowValues[6] = resArr[0] #long
rowValues[7] = polyx
if( (pointx ==1) | (pointx == len(LatLonStrArr))| (len(LatLonStrArr)<=10)| (pointx % 10 == 0) ):
pointxx += 1
rowValues[8] = pointxx
csvWriter.writerow( rowValues);
#print('ROW========================='+ str(rdx))
#for rx in rowValues:
# print('val=' + str(rx))
#if (rdx >=100):
#break
csvfile.close()
outfile.close()