-
Notifications
You must be signed in to change notification settings - Fork 0
/
kriging_data.py
336 lines (255 loc) · 11.9 KB
/
kriging_data.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import pykrige.kriging_tools as kt
from pykrige.ok import OrdinaryKriging
import time
import datetime
import shapely
import json
import numpy as np
from shapely.geometry import MultiPoint, Point, box
from shapely.strtree import STRtree
import matplotlib.pyplot as plt
from shapely.wkt import loads
from tqdm import tqdm, trange
import shapefile
import geopandas as gpd
import data_manipulator as dj
class KrigingTool:
def __init__(self, **kwargs):
with open(kwargs['filename'], 'r') as json_file:
json_string = json_file.read()
self.json_reader = json.loads(json_string)
try:
with open(kwargs['filename'], 'r') as json_file:
json_string = json_file.read()
self.json_reader = json.loads(json_string)
except:
print("Error: Input a filename")
exit()
try:
self.binned_out = kwargs['bo']
self.binned_out_part = self.binned_out[:-8] + "_part.geojson"
self.csv_outfile_name = self.binned_out[:-8] + ".csv"
except:
print("Error: specify binned out data file with .geojson end")
try:
self.chunks = kwargs['chunks']
except:
self.chunks = 1
self.f=kwargs['fast']
self.st = kwargs['settimer']
self.num_vars = 0
self.county_boundary = shapefile.Reader('data/LA_County_City_Boundaries.shp')
self.ct = gpd.read_file("data/census_tracts.geojson")
self.main()
#Returns a dictionary of { lats, lons, variables }. Each contain an array in with points in order sharing the same index.
# The variables array at point i contains a dictionary of all the different variables and their values
def construct_dataset_from_geojson(self):
lats = []
lons = []
variables = []
#point_checker = pc.PointChecker()
for i in self.json_reader['features']:
lon = i['geometry']['coordinates'][0]
lat = i['geometry']['coordinates'][1]
if(self.check_if_point_within_bounds(lon, lat)):
lons.append(lon)
lats.append(lat)
varDict = {}
for j in i['properties']:
varDict[j] = i['properties'][j]
variables.append(varDict)
if(self.num_vars == 0):
self.num_vars = len(varDict)
geo_data={
'lats' : lats,
'lons' : lons,
'variables' : variables
}
return geo_data
def check_if_point_within_bounds(self, x, y):
p = Point(x, y)
#print(self.county_boundary.bbox)
b = box(
self.county_boundary.bbox[0],
self.county_boundary.bbox[1],
self.county_boundary.bbox[2],
self.county_boundary.bbox[3])
return b.contains(p)
def perform_kriging(self, data, grid_x, grid_y):
startTime = time.time()
OK = OrdinaryKriging(data[:,0], data[:,1], data[:,2], variogram_model='linear', verbose=False, enable_plotting=False)
z, ss = OK.execute('grid', grid_x, grid_y)
endTime = time.time()
if(self.st):
print("performKriging Method took {:.2f} seconds".format(endTime-startTime))
return z
#Expects return data from construct_dataset_from_geojson
def create_point_hashtable(self, geo_data):
lats = geo_data['lats']
lons = geo_data['lons']
variables = geo_data['variables']
hash_table = {}
for i in range(len(lats)):
index = str(lats[i]) + str(lons[i])
hash_table[index] = variables[i]
return hash_table
#Expecting return data from construct_dataset_from_geojson
def create_MultiPoint(self, geo_data):
points = []
lats = geo_data['lats']
lons = geo_data['lons']
for i in range(len(lats)):
p = Point(lats[i], lons[i])
points.append(p)
MP = MultiPoint(points)
return MP
def create_chunk_boxes(self, MP):
xmin = MP.bounds[0]
ymin = MP.bounds[1]
xmax = MP.bounds[2]
ymax = MP.bounds[3]
xsplit = np.linspace(xmin, xmax, self.chunks)
ysplit = np.linspace(ymin, ymax, self.chunks)
boxes = []
#Create a set of boxes to represent chunks within the space.
for i in range(0, self.chunks-1):
for j in range(0, self.chunks-1):
b = box(xsplit[i], ysplit[j], xsplit[i+1], ysplit[j+1])
boxes.append(b)
return boxes
#Takes a multipoint set and a list of chunk 'boxes' and creates an array pt_chunks
# of size len(boxes), where each pt_chunks[i] is an array of all points that fall within the box
# at boxes[i]
def split_points_into_chunks(self, MP, boxes):
tree = STRtree(MP)
pt_chunks = []
for b in boxes:
query_geom = b
pts = [o.wkt for o in tree.query(query_geom)]
pt_chunks.append(pts)
return pt_chunks
def fast_kriging(self):
geo_data = self.construct_dataset_from_geojson()
variables = geo_data['variables'][0]
krigged_variable_dict = {} #Dictionary to hold points and variables of data that is krigged
nonkrigged_variable_dict = {} #Dictionary to hold points and variables of data that is not krigged
#Create an array of centroid points called pts of census tracts from the census tract .shp fle
geometries = [i for i in self.ct['geometry']]
pts = [None] * len(geometries)
for i in trange(len(geometries)):
pts[i] = geometries[i].centroid
# Transform pts array to execute kriging
x_array = [i.x for i in pts]
y_array = [i.y for i in pts]
#For each variable
for variableName in tqdm(variables):
krigVar = input("Perform kriging on " + variableName + "? (y/n)")
print(krigVar)
while(krigVar.lower() != 'y' and krigVar.lower() != 'n'):
krigVar = input("Please insert 'y' or 'n' without quotes.")
if(krigVar.lower() == 'y'):
#Create an empty numpy array with a space for data to be used in kriging process
data = np.empty((len(geo_data['lats']), 3))
#Transfrom data into array
for i in trange(len(data)):
data[i][0] = geo_data['lats'][i]
data[i][1] = geo_data['lons'][i]
data[i][2] = geo_data['variables'][i][variableName]
#Run kriging algorithm on sample data
OK = OrdinaryKriging(data[:,0], data[:,1], data[:,2], variogram_model='linear', verbose=False, enable_plotting=False)
#Krig on selected centroid points
z, ss = OK.execute('points', y_array, x_array)
for i in trange(0, len(x_array)):
try:
krigged_variable_dict[ str((x_array[i], y_array[i])) ][variableName] = z[i]
except KeyError:
krigged_variable_dict[ str((x_array[i], y_array[i])) ] = {}
krigged_variable_dict[ str((x_array[i], y_array[i])) ][variableName] = z[i]
elif(krigVar.lower() == 'n'):
#TODO: Create case for choosing not to krig a variable. Create another dict with points and values from non-krigged data, store in seperate file
print("Not kriging " + variableName)
for i in trange(len(geo_data['lats'])):
try:
nonkrigged_variable_dict[ str(( geo_data['lons'][i], geo_data['lats'][i] ))][variableName] = geo_data['variables'][i][variableName]
except:
nonkrigged_variable_dict[ str(( geo_data['lons'][i], geo_data['lats'][i] ))] = {}
nonkrigged_variable_dict[ str(( geo_data['lons'][i], geo_data['lats'][i] ))][variableName] = geo_data['variables'][i][variableName]
return krigged_variable_dict, nonkrigged_variable_dict
def writeData(self, krigged_variable_dict, nonkrigged_variable_dict):
binnedData = self.binPoints(krigged_variable_dict)
d = dj.DataManipulator(binnedData)
binned_json_data = d.data_into_json('full')
part_binned_json_data = d.data_into_json('part')
csv_data = d.data_into_csv()
with open(self.binned_out, 'w+') as file:
file.write(binned_json_data)
with open(self.binned_out_part, 'w+') as file:
file.write(part_binned_json_data)
with open(self.csv_outfile_name, 'w+') as file:
file.write(csv_data)
def createPtArray(self, points):
ptArray = []
for pt in points:
#print(pt)
newPoint = Point(round(pt[1], 5), round(pt[0], 5))
ptArray.append(newPoint)
return ptArray
#Takes array of points in form [ [lat, lon, pm25], [...] ]
def createPointHashTable(self, points):
hashTable = {}
for pt in points:
newPoint = Point(round(pt[1], 5), round(pt[0], 5))
hashVal = self.hashFunction(newPoint.x, newPoint.y)
hashTable[str(newPoint.x) + str(newPoint.y)] = pt[2]
return hashTable
def hashFunction(self, x, y):
return (hash(abs(x) * abs(y)))
#Points should be a 2D array [ [lat, lon, pm25], [...] ]
def createGeoHash(self, geo):
hashVal = hash(str(geo))
return hashVal
#Creates a hashtable to store the census tract names
#based on a hash of the actual geometry
def createGeoHashTable(self):
hashTable = {}
geometries = [i for i in self.ct['geometry']]
#print(self.ct[0])
names = [i for i in self.ct['name']]
for i in range(0, len(geometries)):
hashTable[self.createGeoHash(str(geometries[i]))] = names[i]
return hashTable
def binPoints(self, variable_dict):
finalData = []
transformed_data = []
for key in variable_dict.keys():
new_data_array = []
lon, lat = key[1:-1].split(", ")
variables = variable_dict[key]
new_data_array = [float(lat), float(lon), variables]
transformed_data.append(new_data_array)
geometries = [i for i in self.ct['geometry']]
ptArray = self.createPtArray(transformed_data)
pointHashTable = self.createPointHashTable(transformed_data)
geometryHashTable = self.createGeoHashTable()
tree = STRtree(ptArray)
#Get all the points that intersect with a certain geometry
for geometry in tqdm(geometries):
pts = [o.wkt for o in tree.query(geometry)]
if(len(pts) > 0):
newData = [str(geometry), [], geometryHashTable[self.createGeoHash(str(geometry))]]
for point in pts:
point = shapely.wkt.loads(point)
lat = point.y
lon = point.x
try:
pm25 = pointHashTable[(str(point.y) + str(point.x))]
except KeyError:
pm25 = pointHashTable[(str(point.x) + str(point.y))]
newPoint = [lat, lon, pm25]
newData[1] = newPoint
finalData.append(newData)
#print(finalData)
return finalData
def main(self):
krigged_variable_dict, nonkrigged_variable_dict = self.fast_kriging()
self.writeData(krigged_variable_dict, nonkrigged_variable_dict)