-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
417 lines (345 loc) · 14.3 KB
/
server.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import json
import logging
import os
import random
import string
import time
import config
# import drive
import ee
import jinja2
# import oauth2client.contrib.appengine
import webapp2
# from google.appengine.api import channel
# from google.appengine.api import taskqueue
from google.appengine.api import urlfetch
# from google.appengine.api import users
from datetime import date, datetime, timedelta
urlfetch.set_default_fetch_deadline(120000)
ee.data.setDeadline(60000)
###############################################################################
# Initialization. #
###############################################################################
# Use our App Engine service account's credentials.
EE_CREDENTIALS = ee.ServiceAccountCredentials(
config.EE_ACCOUNT, config.EE_PRIVATE_KEY_FILE)
# Create the Jinja templating system we use to dynamically generate HTML. See:
# http://jinja.pocoo.org/docs/dev/
JINJA2_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
autoescape=True,
extensions=['jinja2.ext.autoescape'])
# Initialize the EE API.
ee.Initialize(EE_CREDENTIALS)
###############################################################################
# Web request handlers. #
###############################################################################
class DataHandler(webapp2.RequestHandler):
"""A servlet base class for responding to data queries.
We use this base class to wrap our web request handlers with try/except
blocks and set per-thread values (e.g. URL_FETCH_TIMEOUT).
"""
def get(self):
self.Handle(self.DoGet)
def post(self):
self.Handle(self.DoPost)
def DoGet(self):
"""Processes a GET request and returns a JSON-encodable result."""
raise NotImplementedError()
def DoPost(self):
"""Processes a POST request and returns a JSON-encodable result."""
raise NotImplementedError()
# @OAUTH_DECORATOR.oauth_required
def Handle(self, handle_function):
"""Responds with the result of the handle_function or errors, if any."""
# Note: The fetch timeout is thread-local so must be set separately
# for each incoming request.
urlfetch.set_default_fetch_deadline(120000)
try:
response = handle_function()
except Exception as e: # pylint: disable=broad-except
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(e).__name__, e.args)
logging.info(type(e).__name__)
logging.info(e.args)
response = {'error': message}
if response:
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(json.dumps(response))
class MainHandler(webapp2.RequestHandler):
"""A servlet to handle requests to load the main web page."""
# @OAUTH_DECORATOR.oauth_required
def get(self):
print('MainHandler')
template = JINJA2_ENVIRONMENT.get_template('index.html')
self.response.out.write(template.render())
class RainfallHandler(DataHandler):
def post(self):
data = json.loads(self.request.body)
startDate = data.get('from')
endDate = data.get('to')
region = data.get('region')
"""Returns the main web page, populated with Rainfall map"""
try:
rainfallObj = GetRainfallMapId(startDate, endDate, region)
response = {
'mapid': rainfallObj.get('mapId').get('mapid'),
'token': rainfallObj.get('mapId').get('token'),
'colors': rainfallObj.get('colors'),
'values': rainfallObj.get('values')
}
except Exception as e: # pylint: disable=broad-except
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(e).__name__, e.args)
logging.info(type(e).__name__)
logging.info(e.args)
response = {
'error': type(e).__name__,
'message': e.args
}
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(json.dumps(response))
class CropHandler(DataHandler):
def post(self):
data = json.loads(self.request.body)
startDate = data.get('from')
endDate = data.get('to')
region = data.get('region')
"""Returns the main web page, populated with Rainfall map"""
mapid = GetCropMapId(startDate, endDate, region)
content = {
'mapid': mapid['mapid'],
'token': mapid['token']
}
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(json.dumps(content))
class ExportHandler(DataHandler):
"""A servlet to handle requests for image exports."""
logging.info('-----------ExportHandler------------')
def post(self):
"""Kicks off export of an image for the specified year and region.
HTTP Parameters:
startDate: start date
endDate: end date
region: river basin geometry
client_id: The ID of the client (for the Channel API).
"""
data = json.loads(self.request.body)
startDate = data.get('from')
endDate = data.get('to')
region = data.get('region')
response = GetExportUrl(startDate, endDate, region)
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(json.dumps(response))
# Define webapp2 routing from URL paths to web request handlers. See:
# http://webapp-improved.appspot.com/tutorials/quickstart.html
app = webapp2.WSGIApplication([
('/exportRainfall', ExportHandler),
('/exportCrop', ExportHandler),
('/rainfall', RainfallHandler),
('/crop', CropHandler),
('/', MainHandler)
])
###############################################################################
# Helpers. #
###############################################################################
def _get_coords(geojson):
return geojson.get('geometry').get('coordinates')
def _get_region(geom):
"""Return ee.Geometry from supplied GeoJSON object."""
poly = _get_coords(geom)
ptype = geom.get('geometry').get('type')
if ptype.lower() == 'multipolygon':
region = ee.Geometry.MultiPolygon(poly)
else:
region = ee.Geometry.Polygon(poly)
return region
def GetCropMapId(startDate, endDate, region):
# ***** Declare vector boundary(here example India Boundary *****
boundary = ee.FeatureCollection('ft:17JOXbbYVVanIDQtR689Ia1j_blb85l7lwkmwG_KH')
if region:
boundary = _get_region(region)
# ***** Filter precipitation data by boundary and date*****
L8 = (ee.ImageCollection("LANDSAT/LC8_L1T_TOA")
.filterBounds(boundary)
.filterDate(startDate, endDate))
# Visualization Parameters
vizParams = {
'bands': "ndvi",
'min':0,
'max':1,
'palette': "0000FF,D2691E,FFFF00,009500,FF0000,FFFFFF"
}
# Compute the mean brightness in the region in each image.
def NormalizedDifference(image):
result = image.normalizedDifference(['B5', 'B4']).rename(['ndvi'])
return image.addBands(result)
ndvi = L8.map(NormalizedDifference)
medianNDVI = ndvi.median().clip(boundary)
return medianNDVI.getMapId(vizParams)
def getLegendColors(image, boundary, vizParams):
buckets = 5;
scale = 10000;
histogram = image.reduceRegion(
reducer=ee.Reducer.histogram(buckets),
geometry=boundary,
scale=image.projection().nominalScale()
).get('precipitation').getInfo()
values = histogram.get('bucketMeans');
# Compute the mean brightness in the region in each image.
def getRGBColors(v):
color = ee.Image.constant(v).visualize(
min=50,
max=1000,
palette="#ffffff,#b8e4ff,#73aeff,#307be1,#001245"
).reduceRegion(ee.Reducer.first(), ee.Algorithms.GeometryConstructors.Point([0,0]), 1);
return color.getInfo()
colors = map(getRGBColors, values)
def getHexColors(color):
r = color['vis-red'];
g = color['vis-green'];
b = color['vis-blue'];
return ('#%02x%02x%02x' % (r, g, b))
hexColors = map(getHexColors, colors)
print(hexColors)
print(values)
return {
'colors': hexColors,
'values': values
}
def GetRainfallMapId(startDate, endDate, region):
rainfallObj = GetRainfallMap(startDate, endDate, region)
rainfall_masked = rainfallObj.get('rainfall_masked')
boundary = rainfallObj.get('boundary')
# ***** Set Visualization Parameters *****
vizParams = {
'bands': 'precipitation',
'min': 50,
'max': 1000,
'palette':"#ffffff,#b8e4ff,#73aeff,#307be1,#001245"
}
legendConfig = getLegendColors(rainfall_masked, boundary, vizParams)
response = {
'mapId': rainfall_masked.getMapId(vizParams),
'colors': legendConfig.get('colors'),
'values': legendConfig.get('values')
}
return response
def GetExportUrl(startDate, endDate, region):
# boundary = ee.FeatureCollection('ft:17JOXbbYVVanIDQtR689Ia1j_blb85l7lwkmwG_KH')
rainfallObj = GetRainfallMap(startDate, endDate, region)
boundary = rainfallObj.get('boundary')
rainfallColl = rainfallObj.get('rainfall')
rainfallTotal = rainfallObj.get('rainfallTotal')
rainfallCollwDt = rainfallObj.get('rainfallCollwDt')
prj = ee.Image(rainfallColl.first()).projection()
pixelArea = ee.Image.pixelArea().reproject(prj).clip(boundary)
# # //****** 6. Calculate: Area of River Basin *****
# # // Function declaration
# # // This creates a new column, named 'area' which contains the calculated area (in m^2) for each polygon of the river basin
boundary = ee.FeatureCollection(boundary)
basinPolyArea = boundary.map(lambda f: f.set({'area': f.area()}))
boundaryTotalArea = basinPolyArea.reduceColumns(
reducer= ee.Reducer.sum(),
selectors= ['area']
)
print(ee.Number(boundaryTotalArea.get('sum')).getInfo())
boundaryTotalArea_sqkm = ee.Number(boundaryTotalArea.get('sum')).divide(10 ** 6);
print(boundaryTotalArea_sqkm.getInfo())
# // ***** 7. Calculate: Median of rainfall (in mm) for all pixels within the basin for one image *******
# // ****** Note: The rainfall data comes in half-hourly (hh) timesteps, i.e. one new raster image for every half hour ******
def mmMedian(image):
median = image.reduceRegion(
reducer= ee.Reducer.median(),
geometry= boundary,
scale= 10000,
bestEffort= False
).get('precipitation');
return image.set({'medianRain': median});
def mcmVolume(image):
vol_image = (image
.divide(1e3) #// convert mm to metres
.multiply(pixelArea) #// multiply metres by pixelArea (mts) to get volume in metre^3
.divide(1e6) #// convert m^3 to Million Cubic Metres (MCM)
)
vol = vol_image.reduceRegion(
reducer= ee.Reducer.sum(),
geometry= boundary,
scale= 10000,
bestEffort= False
).get('precipitation')
return image.set({'volRain': vol})
# // Function call - #7.1 and #8.1
rainfall_mm = rainfallCollwDt.map(mmMedian);
# print(rainfall_mm);
rainfall_vol = rainfallCollwDt.map(mcmVolume);
# print(rainfall_vol);
# // Calculate a list of daily values - #7.2 and #8.2
daily_mm = rainfall_mm.reduceColumns(
selectors= ['medianRain','date'], # // select these two properties of each image we created before
reducer= ee.Reducer.sum().group(groupField= 1, groupName= 'Date')
).get('groups');
daily_mcm = rainfall_vol.reduceColumns(
selectors= ['volRain','date'],
reducer= ee.Reducer.sum().group(groupField= 1, groupName= 'Date')
).get('groups');
mmList = ee.List(daily_mm);
mcmList = ee.List(daily_mcm);
ll = mmList.length().getInfo(); #//List length
li = ll-1; #//List index (Length -1)
csvList = []
attribution = ee.String("CHIRPS daily: Climate Hazards Group InfraRed Precipitation with Station data (version 2.0 final) Link: https://code.earthengine.google.com/dataset/UCSB-CHG/CHIRPS/DAILY")
for x in range(0, ll):
date = ee.Date(ee.Dictionary(mmList.get(x)).get('Date'));
mm = ee.Number(ee.Dictionary(mmList.get(x)).get('sum'));
mcm = ee.Number(ee.Dictionary(mcmList.get(x)).get('sum'));
eeFeatureObj = ee.Feature(None, {
"Date": date,
"Rain (in mm)": mm,
"Rain (in MCM)": mcm,
"attribution": attribution
})
csvList.append(eeFeatureObj)
csv = ee.FeatureCollection(csvList)
try:
downloadUrl = ee.FeatureCollection(csv).getDownloadUrl(
filename='OWD_Rainfall'
)
response = {
'status': 'success',
'downloadUrl': downloadUrl
}
logging.info('Download URL: %s', downloadUrl)
except Exception as e: # pylint: disable=broad-except
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(e).__name__, e.args)
logging.info(type(e).__name__)
logging.info(e.args)
response = {'message': e.args, 'error': type(e).__name__}
return response
def GetRainfallMap(startDate, endDate, region):
# ***** Declare vector boundary *****
India_boundary = ee.FeatureCollection('ft:17JOXbbYVVanIDQtR689Ia1j_blb85l7lwkmwG_KH');
boundary = ee.FeatureCollection('ft:17JOXbbYVVanIDQtR689Ia1j_blb85l7lwkmwG_KH');
if region:
boundary = _get_region(region)
rainfallColl = (
ee.ImageCollection('UCSB-CHG/CHIRPS/DAILY')
.filterBounds(boundary)
.filterDate(startDate, endDate)
.select('precipitation')
)
def setRainfallDate(img):
dt = ee.Date(img.get('system:time_start')).format('YYYY-MM-dd')
return img.set({'date': dt})
rainfallCollwDt = rainfallColl.map(setRainfallDate)
# ***** Make rainfall image *****
rainfallTotal = rainfallCollwDt.sum().clip(boundary);
rainfall_masked = rainfallTotal.updateMask(rainfallTotal.gt(0));
return {
'rainfall': rainfallColl,
'rainfallTotal': rainfallTotal,
'rainfallCollwDt': rainfallCollwDt,
'rainfall_masked': rainfall_masked,
'boundary': boundary
}