-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql.py
More file actions
145 lines (123 loc) · 4.21 KB
/
sql.py
File metadata and controls
145 lines (123 loc) · 4.21 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""
This module is used to construct sql statements,
sometimes using templates that are written out
in text files in a local folder.
Usage Example:
>>> import sql, db
>>> layers = sql.physLayers()
>>> print layers
['doitt_building_01_28jul2009', 'doitt_hydrography_01_282009', 'doitt_median_01_28jul2009', 'doitt_hydrography_structure_01_28jul2009', 'doitt_sidewalk_01_28jul2009', 'doitt_transportation_structure_01_28jul2009']
>>> siteID = 59
>>> sqlRequest = sql.getLayers(siteID, layers)
>>> data = db.run(sqlRequest)
>>> len(data)
5
"""
import layers
sqlRootPath = 'C:\\Users\\gallery\\LocalCodeNY\\PythonScripts'
def physLayers():
layDict = {}
for key in layers.physical:
layDict[layers.physical[key][0]] = layers.physical[key][1:]
return layDict
def amenityLayers():
layDict = {}
for key in layers.amenities:
layDict[layers.amenities[key][0]] = layers.amenities[key][1:]
return layDict
def siteLayers():
layDict = {}
for key in layers.sites:
layDict[layers.sites[key][0]] = layers.sites[key][1:]
return layDict
def healthLayers():
layDict = {}
for key in layers.health:
layDict[layers.health[key][0]] = layers.health[key][1:]
return layDict
def render(filePath, variableDict):
"""
Returns a string based on reading some template,
as designated by the file path, and then replacing
each key in the variableDict with the value in
variableDict associated with that key.
"""
sql = open(filePath, 'r').read()
for key in variableDict:
sql = sql.replace(key, variableDict[key])
return sql
def oneLayer( site_id, layer, columns=[]):
"""returns the sql statement to get the geometry and
other optional columns (as a list of strings) of
information based on which features touch the bounding
box of the parcel with the given id.
Usage Example:
>>> sId = 72
>>> layer = 'roads'
>>> cols = ['length', 'azimuth']
>>> sqlStatement = oneLayer(sId, layer, cols)
"""
template = '%s\\one_layer.sql' % sqlRootPath
colString = ''
for col in columns:
colString += ', %s.%s' % (layer, col)
varD = {
'$table':layer,
'$columns':colString,
'$site_id':str(site_id)
}
return render(template, varD)
def getParcel( site_id, columns = []):
"""Returns an SQL statement to retrieve a specific
parcel from the newyork_parcels layer, along with
any columns desired."""
template = '%s\\parcel.sql' % sqlRootPath
colString = ''
for col in columns:
colString += ', %s.%s' % ('newyork_parcels', col)
varD = {
'$site_id':str(site_id),
'$columns':colString,
}
return render(template, varD)
def getOtherParcels( site_id, columns = []):
template = '%s\\otherParcels.sql' % sqlRootPath
colString = ''
for col in columns:
colString += ', %s.%s' % ('newyork_parcels', col)
varD = {
'$site_id':str(site_id),
'$columns':colString,
}
return render(template, varD)
def getLayers(site_id, layerList, columnsDict={}):
"""
returns an sql statement to get the geometry and
any optional columns for a set of layers, by finding
which features in each layer overlap the bounding box
of the parcel with the input site id. Each key in the
columnsDict should precisely match one of the layer
names, and the value that corresponds to each key should
be a list of column name strings.
Usage Example:
>>> sId = 34
>>> layers = ['roads', 'parking_lots', 'trees']
>>> colsDict = {'roads':['length'], 'parking_lots':['area', 'rate']}
>>> sqlStatement = getLayers( sId, layers, colsDict )
"""
union = '\nUNION ALL\n\n'
parcels = 'newyork_parcels'
if parcels in columnsDict:
cols = columnsDict[parcels]
sqlString = getParcel( site_id, cols )
else:
sqlString = getParcel( site_id )
for layer in layerList:
sqlString += union
if layer in columnsDict:
cols = columnsDict[layer]
sqlString += oneLayer(site_id, layer, cols)
else:
sqlString += oneLayer(site_id, layer)
sqlString += ';'
return sqlString