This repository has been archived by the owner on Jul 20, 2020. It is now read-only.
/
parseRow.py
109 lines (102 loc) · 3.7 KB
/
parseRow.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from utilities import listFields, getShp, getOID, statusMessage, parseProp, makeInter
from arcpy import SpatialReference, SearchCursor
from parseGeometry import getParseFunc
from json import dump
#really the only global
wgs84="GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]];-400 -400 1000000000;-100000 10000;-100000 10000;8.98315284119522E-09;0.001;0.001;IsHighPrecision"
class parse:
def __init__(self,outFile,featureClass,fileType,includeGeometry, first=True):
self.outFile = outFile
self.fileType = fileType
#first we set put the local variables we'll need
[self.shp,self.shpType]=getShp(featureClass)
self.fields=listFields(featureClass)
self.oid=getOID(self.fields)
sr=SpatialReference()
sr.loadFromString(wgs84)
#the search cursor
self.rows=SearchCursor(featureClass,"",sr)
#don't want the shape field showing up as a property
del self.fields[self.shp]
self.first=first
self.status = statusMessage(featureClass)
#define the correct geometry function if we're exporting geometry
self.parseGeo = getParseFunc(self.shpType,includeGeometry)
self.i=0
if fileType=="geojson":
self.parse = self.parseGeoJSON
elif fileType=="csv":
self.parse = self.parseCSV
elif fileType=="json":
self.parse = self.parseJSON
elif fileType=="sqlite":
self.parse = self.parseSqlite
def cleanUp(self,row):
del row
del self.rows
return True
def parseCSV(self,row):
#more messages
self.status.update()
fc=parseProp(row,self.fields, self.shp)
if self.parseGeo:
try:
fc["geometry"]=self.parseGeo(row.getValue(self.shp))
except:
return
self.outFile[0].writerow(fc)
def parseGeoJSON(self,row):
#more messages
self.status.update()
fc={"type": "Feature"}
if self.parseGeo:
try:
fc["geometry"]=self.parseGeo(row.getValue(self.shp))
except:
return
else:
raise NameError("we need geometry for geojson")
fc["id"]=row.getValue(self.oid)
fc["properties"]=parseProp(row,self.fields, self.shp)
if fc["geometry"]=={}:
return
if self.first:
self.first=False
dump(fc,self.outFile)
else:
#if it isn't the first feature, add a comma
self.outFile.write(",")
dump(fc,self.outFile)
def parseJSON(self,row):
#more messages
self.status.update()
fc=parseProp(row,self.fields, self.shp)
if self.parseGeo:
try:
fc["geometry"]=self.parseGeo(row.getValue(self.shp))
except:
return
if self.first:
self.first=False
dump(fc,self.outFile)
else:
self.outFile.write(",")
dump(fc,self.outFile)
def parseSqlite(self,row):
#more messages
self.status.update()
fc=parseProp(row,self.fields, self.shp)
self.i=self.i+1
fc["OGC_FID"]=self.i
if self.parseGeo:
try:
fc["GEOMETRY"]=self.parseGeo(row.getValue(self.shp))
except:
return
keys = fc.keys()
values = fc.values()
[name,c,conn]=self.outFile
c.execute("""insert into {0}({1})
values({2})
""".format(name,", ".join(keys),makeInter(len(values))),values)
conn.commit()