def children(self, report_id, id): r = Report.get(Key(report_id)) z, x, y = Cell.cell_id(id) cell = Cell.get_or_default(r, x, y, z) cells = cell.children() return self._as_json( [x.as_dict() for x in cells if not self.is_in_backlist(x)])
def landsat(self, report_id, id): r = Report.get(Key(report_id)) z, x, y = Cell.cell_id(id) cell = Cell.get_or_default(r, x, y, z) bounds = cell.bounds(amazon_bounds) bounds = "%f,%f,%f,%f" % (bounds[1][1], bounds[1][0], bounds[0][1], bounds[0][0]) ee = EELandsat(LANDSAT7) d = ee.list(bounds=bounds) data = {} if len(d) >= 1: x = d[-1] img_info = x.split('/')[2][3:] path = img_info[:3] row = img_info[3:6] year = int(img_info[6: 10]) julian_date = img_info[10: 13] date = date_from_julian(int(julian_date), year) data = { 'info': img_info, 'path': path, 'row': row, 'year': year, 'timestamp': timestamp(date), 'date': date.isoformat() } return Response(json.dumps(data), mimetype='application/json')
def landsat(self, report_id, id): r = Report.get(Key(report_id)) z, x, y = Cell.cell_id(id) cell = Cell.get_or_default(r, x, y, z) bounds = cell.bounds(amazon_bounds) bounds = "%f,%f,%f,%f" % (bounds[1][1], bounds[1][0], bounds[0][1], bounds[0][0]) ee = EELandsat(LANDSAT7) d = ee.list(bounds=bounds) data = {} if len(d) >= 1: x = d[-1] img_info = x.split("/")[2][3:] path = img_info[:3] row = img_info[3:6] year = int(img_info[6:10]) julian_date = img_info[10:13] date = date_from_julian(int(julian_date), year) data = { "info": img_info, "path": path, "row": row, "year": year, "timestamp": timestamp(date), "date": date.isoformat(), } return Response(json.dumps(data), mimetype="application/json")
def setUp(self): # create resources self.polygon = [[[-63.154907226557498, -4.8118385341739005], [-64.143676757807498, -4.8118385341739005], [-64.132690429682498, -6.2879986723276584], [-62.814331054682498, -6.3535159310087908]]] self.inv_polygon = [[[y, x] for x, y in self.polygon[0]]] self.r = Report(start=date(year=2011, month=2, day=1), end=date(year=2011, month=3, day=1), finished=True) self.r.put() self.cell = Cell(x=0, y=0, z=2, report=self.r, ndfi_high=1.0, ndfi_low=0.0) self.cell.put() self.area = Area(geo=json.dumps(self.inv_polygon), added_by=None, type=1, cell=self.cell) self.area.put() self.area.create_fusion_tables() self.ndfi = NDFI(self.r.comparation_range(), self.r.range()) self.stats = Stats()
def ndfi_change(self, report_id, id): r = Report.get(Key(report_id)) z, x, y = Cell.cell_id(id) cell = Cell.get_or_default(r, x, y, z) ee = ndfi = NDFI('MOD09GA', r.comparation_range(), r.range()) bounds = cell.bounds(amazon_bounds) ne = bounds[0] sw = bounds[1] # spcify lon, lat F**K, MONKEY BALLS polygons = [[(sw[1], sw[0]), (sw[1], ne[0]), (ne[1], ne[0]), (ne[1], sw[0])]] cols = 1 rows = 1 if z < 2: cols = rows = 10 data = ndfi.ndfi_change_value(r.base_map(), [polygons], rows, cols) logging.info(data) ndfi = data['data'] #data['data']['properties']['ndfiSum']['values'] if request.args.get('_debug', False): ndfi['debug'] = { 'request': ee.ee.last_request, 'response': ee.ee.last_response } return Response(json.dumps(ndfi), mimetype='application/json')
class PolygonApi(unittest.TestCase, GoogleAuthMixin): def setUp(self): app.config['TESTING'] = True self.app = app.test_client() self.login('*****@*****.**', 'testuser') for x in Area.all(): x.delete() for x in Cell.all(): x.delete() r = Report(start=date.today(), finished=False) r.put() self.r = r self.cell = Cell(x=0, y=0, z=2, report=self.r, ndfi_high=1.0, ndfi_low=0.0) self.cell.put() self.area = Area(geo='[]', added_by=users.get_current_user(), type=1, cell=self.cell) self.area.put() def test_list(self): rv = self.app.get('/api/v0/report/' + str(self.r.key()) + '/cell/2_0_0/polygon') js = json.loads(rv.data) self.assertEquals(1, len(js)) def test_create_non_existing_cell(self): rv = self.app.post('/api/v0/report/' + str(self.r.key()) + '/cell/2_1_0/polygon', data='{"paths": "test", "type": 1}' ) self.assertEquals(2, Area.all().count()) self.assertEquals(4, Cell.all().count()) # check parents exists cell = Cell.all().filter('report =', self.r).filter('x =', 1).filter('y =', 0).filter('z =', 2).fetch(1)[0] self.assertEquals('test', cell.get_parent().last_change_by.nickname()) self.assertEquals('test', cell.get_parent().get_parent().last_change_by.nickname()) self.assertNotEquals(0, cell.get_parent().last_change_on) def test_create(self): rv = self.app.post('/api/v0/report/' + str(self.r.key()) + '/cell/2_0_0/polygon', data='{"paths": "[]", "type": 1}' ) self.assertEquals(2, Area.all().count()) rv = self.app.get('/api/v0/report/' + str(self.r.key()) + '/cell/2_0_0/polygon') js = json.loads(rv.data) self.assertEquals(2, len(js)) def test_update(self): rv = self.app.put('/api/v0/report/' + str(self.r.key()) + '/cell/2_0_0/polygon/' + str(self.area.key()), data='{"paths": "[[1, 2, 3]]", "type": 100}' ) self.assertEquals(1, Area.all().count()) a = Area.get(self.area.key()) self.assertEquals(100, a.type) self.assertEquals("\"[[1, 2, 3]]\"", a.geo) js = json.loads(rv.data) self.assertEquals(100, js['type']) self.assertEquals("[[1, 2, 3]]", js['paths']) def test_delete(self): rv = self.app.delete('/api/v0/report/' + str(self.r.key()) + '/cell/2_0_0/polygon/' + str(self.area.key())) self.assertEquals(0, Area.all().count())
def landsat(self, report_id, id): r = Report.get(Key(report_id)) z, x, y = Cell.cell_id(id) cell = Cell.get_or_default(r, x, y, z) bounds = cell.bounds(amazon_bounds) bounds = "%f,%f,%f,%f" % (bounds[1][1], bounds[1][0], bounds[0][1], bounds[0][0]) ee = EELandsat(LANDSAT7) d = ee.list(bounds=bounds) data = {} if len(d) >= 1: x = d[-1] img_info = x.split('/')[2][3:] path = img_info[:3] row = img_info[3:6] year = int(img_info[6:10]) julian_date = img_info[10:13] date = date_from_julian(int(julian_date), year) data = { 'info': img_info, 'path': path, 'row': row, 'year': year, 'timestamp': timestamp(date), 'date': date.isoformat() } return Response(json.dumps(data), mimetype='application/json')
class FTTest(unittest.TestCase): def setUp(self): app.config['TESTING'] = True self.app = app.test_client() r = Report(start=date.today(), finished=False) r.put() self.r = r self.cell = Cell(x=0, y=0, z=2, report=self.r, ndfi_high=1.0, ndfi_low=0.0) self.cell.put() self.area = Area( geo='[[[-61.5,-12],[-61.5,-11],[-60.5,-11],[-60.5,-12]]]', added_by=users.get_current_user(), type=1, cell=self.cell) #self.area.put() def test_save_on_ft(self): self.area.put() self.area.create_fusion_tables() self.assertNotEquals(None, self.area.fusion_tables_id) self.area.type = 2 self.area.save() self.area.update_fusion_tables() self.area.delete() self.area.delete_fusion_tables()
def ndfi_change(self, report_id, id): r = Report.get(Key(report_id)) z, x, y = Cell.cell_id(id) cell = Cell.get_or_default(r, x, y, z) ee = ndfi = NDFI('MOD09GA', r.comparation_range(), r.range()) bounds = cell.bounds(amazon_bounds) ne = bounds[0] sw = bounds[1] # spcify lon, lat polygons = [ (sw[1], sw[0]), (sw[1], ne[0]), (ne[1], ne[0]), (ne[1], sw[0]) ] cols = 1 rows = 1 if z < 2: cols = rows = 5 data = ndfi.ndfi_change_value(r.base_map(), {"type":"Polygon","coordinates":[polygons]}, rows, cols) logging.info(data) ndfi = data['data'] #data['data']['properties']['ndfiSum']['values'] if request.args.get('_debug', False): ndfi['debug'] = { 'request': ee.ee.last_request, 'response': ee.ee.last_response } return Response(json.dumps(ndfi), mimetype='application/json')
class NotesApiTest(unittest.TestCase, GoogleAuthMixin): def setUp(self): app.config['TESTING'] = True self.app = app.test_client() self.login('*****@*****.**', 'testuser') r = Report(start=date.today(), finished=False) r.put() self.r = r self.cell = Cell(x=0, y=0, z=2, report=self.r, ndfi_high=1.0, ndfi_low=0.0) self.cell.put() for x in Note.all(): x.delete() self.when = datetime.now() self.note = Note(msg='test msg', added_by=users.get_current_user(), cell=self.cell, added_on=self.when) self.note.put() def test_note_list(self): rv = self.app.get('/api/v0/report/' + str(self.r.key()) + '/cell/2_0_0/note') self.assertEquals(200, rv.status_code) js = json.loads(rv.data) self.assertEquals(1, len(js)) n = js[0] self.assertEquals('test msg', n['msg']) self.assertEquals('test', n['author']) self.assertEquals(timestamp(self.when), n['date']) self.assertEquals(1, Note.all().count()) def test_notes_create(self): rv = self.app.post('/api/v0/report/' + str(self.r.key()) + '/cell/2_0_0/note', data='{"msg": "test"}') self.assertEquals(200, rv.status_code) self.assertEquals(2, Note.all().count()) self.assertEquals(2, self.cell.note_set.count())
def list(self, report_id, cell_pos): r = Report.get(Key(report_id)) z, x, y = Cell.cell_id(cell_pos) cell = Cell.get_cell(r, x, y, z) if not cell: return self._as_json([]) else: return self._as_json([x.as_dict() for x in cell.area_set])
def list(self, report_id, cell_pos): r = Report.get(Key(report_id)) z, x, y = Cell.cell_id(cell_pos) cell = Cell.get_cell(r, x, y, z) notes = [] if cell: return self._as_json([x.as_dict() for x in cell.note_set]) return self._as_json([])
def rgb_mapid(self, report_id, id, r, g, b): report = Report.get(Key(report_id)) z, x, y = Cell.cell_id(id) cell = Cell.get_or_default(report, x, y, z) ndfi = NDFI('MOD09GA', report.comparation_range(), report.range()) poly = cell.bbox_polygon(amazon_bounds) mapid = ndfi.rgb_strech(poly, tuple(map(int, (r, g, b)))) if 'data' not in mapid: abort(404) return Response(json.dumps(mapid['data']), mimetype='application/json')
def create(self, report_id, cell_pos): r = Report.get(Key(report_id)) z, x, y = Cell.cell_id(cell_pos) cell = Cell.get_or_create(r, x, y, z) data = json.loads(request.data) if 'msg' not in data: abort(400) a = Note(msg=data['msg'], added_by=users.get_current_user(), cell=cell) a.save() return Response(a.as_json(), mimetype='application/json')
def create(self, report_id, cell_pos): r = Report.get(Key(report_id)) z, x, y = Cell.cell_id(cell_pos) cell = Cell.get_or_create(r, x, y, z) data = json.loads(request.data) if "msg" not in data: abort(400) a = Note(msg=data["msg"], added_by=users.get_current_user(), cell=cell) a.save() return Response(a.as_json(), mimetype="application/json")
def create(self, report_id, cell_pos): r = Report.get(Key(report_id)) z, x, y = Cell.cell_id(cell_pos) cell = Cell.get_or_create(r, x, y, z) data = json.loads(request.data) a = Area(geo=json.dumps(data["paths"]), type=data["type"], added_by=users.get_current_user(), cell=cell) a.save() cell.last_change_by = users.get_current_user() cell.put() return Response(a.as_json(), mimetype="application/json")
def rgb_mapid(self, report_id, id, r, g, b): report = Report.get(Key(report_id)) z, x, y = Cell.cell_id(id) cell = Cell.get_or_default(report, x, y, z) ndfi = NDFI("MOD09GA", report.comparation_range(), report.range()) poly = cell.bbox_polygon(amazon_bounds) mapid = ndfi.rgb_strech(poly, tuple(map(int, (r, g, b)))) if "data" not in mapid: abort(404) return Response(json.dumps(mapid["data"]), mimetype="application/json")
def rgb_mapid(self, report_id, operation, id, r, g, b, sensor): report = Report.get(Key(report_id)) z, x, y = Cell.cell_id(id) cell = Cell.get_or_default(report, operation, x, y, z) ndfi = NDFI(report.comparation_range(), report.range()) poly = cell.bbox_polygon(amazon_bounds) mapid = ndfi.rgb_stretch(poly, sensor, tuple(map(int, (r, g, b)))) if not mapid: abort(404) return Response(json.dumps(mapid), mimetype='application/json')
def setUp(self): r = Report(start=date.today(), finished=False) r.put() self.r = r self.cell = Cell(x=11, y=11, z=2, report=self.r, ndfi_high=1.0, ndfi_low=0.0) self.cell.put()
class CellTest(unittest.TestCase): def setUp(self): r = Report(start=date.today(), finished=False) r.put() self.r = r self.cell = Cell(x=11, y=11, z=2, report=self.r, ndfi_high=1.0, ndfi_low=0.0) self.cell.put() def test_parent_id(self): self.assertEquals('1_1_1', self.cell.parent_id)
def test_create_non_existing_cell(self): rv = self.app.post('/api/v0/report/' + str(self.r.key()) + '/cell/2_1_0/polygon', data='{"paths": "test", "type": 1}' ) self.assertEquals(2, Area.all().count()) self.assertEquals(4, Cell.all().count()) # check parents exists cell = Cell.all().filter('report =', self.r).filter('x =', 1).filter('y =', 0).filter('z =', 2).fetch(1)[0] self.assertEquals('test', cell.get_parent().last_change_by.nickname()) self.assertEquals('test', cell.get_parent().get_parent().last_change_by.nickname()) self.assertNotEquals(0, cell.get_parent().last_change_on)
def create(self, report_id, cell_pos): r = Report.get(Key(report_id)) z, x, y = Cell.cell_id(cell_pos) cell = Cell.get_or_create(r, x, y, z) data = json.loads(request.data) a = Area(geo=json.dumps(data['paths']), type=data['type'], added_by=users.get_current_user(), cell=cell) a.save() cell.last_change_by = users.get_current_user() cell.put() return Response(a.as_json(), mimetype='application/json')
def update(self, report_id, id): r = Report.get(Key(report_id)) z, x, y = Cell.cell_id(id) cell = Cell.get_or_default(r, x, y, z) cell.report = r data = json.loads(request.data) cell.ndfi_low = float(data['ndfi_low']) cell.ndfi_high = float(data['ndfi_high']) cell.done = data['done'] cell.last_change_by = users.get_current_user() cell.put() return Response(cell.as_json(), mimetype='application/json')
def test_create_non_existing_cell(self): rv = self.app.post('/api/v0/report/' + str(self.r.key()) + '/cell/2_1_0/polygon', data='{"paths": "test", "type": 1}') self.assertEquals(2, Area.all().count()) self.assertEquals(4, Cell.all().count()) # check parents exists cell = Cell.all().filter('report =', self.r).filter('x =', 1).filter( 'y =', 0).filter('z =', 2).fetch(1)[0] self.assertEquals('test', cell.get_parent().last_change_by.nickname()) self.assertEquals( 'test', cell.get_parent().get_parent().last_change_by.nickname()) self.assertNotEquals(0, cell.get_parent().last_change_on)
class CellTest(unittest.TestCase): def setUp(self): r = Report(start=date.today(), finished=False) r.put() self.r = r self.cell = Cell(x=11, y=11, z=2, report=self.r, ndfi_high=1.0, ndfi_low=0.0) self.cell.put() def test_parent_id(self): self.assertEquals('1_2_2', self.cell.parent_id)
def setUp(self): app.config['TESTING'] = True self.app = app.test_client() self.login('*****@*****.**', 'testuser') for x in Area.all(): x.delete() for x in Cell.all(): x.delete() r = Report(start=date.today(), finished=False) r.put() self.r = r self.cell = Cell(x=0, y=0, z=2, report=self.r, ndfi_high=1.0, ndfi_low=0.0) self.cell.put() self.area = Area(geo='[]', added_by=users.get_current_user(), type=1, cell=self.cell) self.area.put()
def ndfi_value_for_cells(cell_key): cell = Cell.get(Key(cell_key)) ndfi = NDFI('MOD09GA', cell.report.comparation_range(), cell.report.range()) bounds = cell.bounds(amazon_bounds) logging.info(bounds) ne = bounds[0] sw = bounds[1] polygons = [[(sw[1], sw[0]), (sw[1], ne[0]), (ne[1], ne[0]), (ne[1], sw[0])]] data = ndfi.ndfi_change_value(cell.report.base_map(), [polygons]) logging.info(data) if 'data' not in data: logging.error("can't get ndfi change value") return ndfi = data['data']['properties']['ndfiSum']['values'] for row in xrange(10): for col in xrange(10): idx = row * 10 + col count = float(ndfi['count'][idx]) s = float(ndfi['sum'][idx]) if count > 0.0: ratio = s / count else: ratio = 0.0 ratio = ratio / 10.0 #10 value is experimental # asign to cell logging.info('cell ndfi (%d, %d): %f' % (row, col, ratio)) c = cell.child(row, col) c.ndfi_change_value = ratio c.put()
def test_update_cell_2_0_1(self): rv = self.app.put('/api/v0/report/' + str(self.r.key()) + '/cell/2_1_3', data='''{ "ndfi_low": 0.0, "ndfi_high": 1.0, "done": false, "compare_view": "", "map_one_layer_status": "", "map_two_layer_status": "", "map_three_layer_status": "", "map_four_layer_status": "" }''') self.assertEquals(200, rv.status_code) js = json.loads(rv.data) q = Cell.all() q.filter("z =", 2) q.filter("x =", 1) q.filter("y =", 3) q.filter("report =", self.r) cell = q.fetch(1)[0] self.assertAlmostEquals(0, cell.ndfi_low) self.assertAlmostEquals(1.0, cell.ndfi_high) self.assertEquals('test', cell.get_parent().last_change_by.nickname()) self.assertEquals( 'test', cell.get_parent().get_parent().last_change_by.nickname()) self.assertNotEquals(0, cell.get_parent().last_change_on)
def update_main_cell_ndfi(z, x, y): r = Report.current() cell = Cell.get_or_create(r, x, y, z) deferred.defer(ndfi_value_for_cells, str(cell.key()), _queue="ndfichangevalue") return 'working'
def update_cells_ndfi(): r = Report.current() cell = Cell.get_or_default(r, 0, 0, 0) for c in iter(cell.children()): c.put() deferred.defer(ndfi_value_for_cells, str(c.key()), _queue="ndfichangevalue") return 'working'
def list(self, report_id): r = Report.get(Key(report_id)) cell = Cell.get_or_default(r, 0, 0, 0) return self._as_json([ x.as_dict() for x in iter(cell.children()) if not self.is_in_backlist(x) ])
def ndfi_value_for_cells(cell_key): cell = Cell.get(Key(cell_key)) ndfi = NDFI('MOD09GA', cell.report.comparation_range(), cell.report.range()) bounds = cell.bounds(amazon_bounds) logging.info(bounds) ne = bounds[0] sw = bounds[1] polygons = [[ (sw[1], sw[0]), (sw[1], ne[0]), (ne[1], ne[0]), (ne[1], sw[0]) ]] data = ndfi.ndfi_change_value(cell.report.base_map(), [polygons]) logging.info(data) if 'data' not in data: logging.error("can't get ndfi change value") return ndfi = data['data']['properties']['ndfiSum']['values'] for row in xrange(10): for col in xrange(10): idx = row*10 + col count = float(ndfi['count'][idx]) s = float(ndfi['sum'][idx]) if count > 0.0: ratio = s/count else: ratio = 0.0 ratio = ratio/10.0 #10 value is experimental # asign to cell logging.info('cell ndfi (%d, %d): %f' % (row, col, ratio)) c = cell.child(row, col) c.ndfi_change_value = ratio c.put()
def update_cells_ndfi_dummy(): r = Report.current() if not r: return 'create a report first' cell = Cell.get_or_default(r, 0, 0, 0) for c in iter(cell.children()): c.put() deferred.defer(ndfi_value_for_cells_dummy, str(c.key()), _queue="ndfichangevalue") return 'working DUMMY'
def setUp(self): app.config['TESTING'] = True self.app = app.test_client() r = Report(start=date.today(), finished=False) r.put() self.r = r self.cell = Cell(x=0, y=0, z=2, report=self.r, ndfi_high=1.0, ndfi_low=0.0) self.cell.put() self.area = Area( geo='[[[-61.5,-12],[-61.5,-11],[-60.5,-11],[-60.5,-12]]]', added_by=users.get_current_user(), type=1, cell=self.cell)
def setUp(self): app.config['TESTING'] = True self.app = app.test_client() r = Report(start=date.today(), finished=False) r.put() self.r = r self.cell = Cell(x=0, y=0, z=2, report=self.r, ndfi_high=1.0, ndfi_low=0.0) self.cell.put() self.area = Area(geo='[[[-61.5,-12],[-61.5,-11],[-60.5,-11],[-60.5,-12]]]', added_by=users.get_current_user(), type=1, cell=self.cell)
def update(self, report_id, operation, id): r = Report.get(Key(report_id)) z, x, y = Cell.cell_id(id) cell = Cell.get_or_default(r, operation, x, y, z) cell.report = r data = json.loads(request.data) cell.ndfi_low = float(data['ndfi_low']) cell.ndfi_high = float(data['ndfi_high']) cell.compare_view = str(data['compare_view']) cell.map_one_layer_status = str(data['map_one_layer_status']) cell.map_two_layer_status = str(data['map_two_layer_status']) cell.map_three_layer_status = str(data['map_three_layer_status']) cell.map_four_layer_status = str(data['map_four_layer_status']) cell.done = data['done'] cell.last_change_by = users.get_current_user() cell.put() return Response(cell.as_json(), mimetype='application/json')
def setUp(self): for x in models.CELL_BLACK_LIST[:]: models.CELL_BLACK_LIST.pop() app.config['TESTING'] = True self.login('*****@*****.**', 'testuser') self.app = app.test_client() for x in Cell.all(): x.delete() r = Report(start=date.today(), finished=False) r.put() self.r = r
def ndfi_change(self, report_id, id): r = Report.get(Key(report_id)) z, x, y = Cell.cell_id(id) cell = Cell.get_or_default(r, x, y, z) ndfi = NDFI('MOD09GA', r.comparation_range(), r.range()) bounds = cell.bounds(amazon_bounds) ne = bounds[0] sw = bounds[1] # spcify lon, lat F**K, MONKEY BALLS polygons = [[ (sw[1], sw[0]), (sw[1], ne[0]), (ne[1], ne[0]), (ne[1], sw[0]) ]] cols = 1 rows = 1 if z < 2: cols = rows = 10 data = ndfi.ndfi_change_value(r.base_map(), [polygons], rows, cols) logging.info(data) ndfi = data['data'] #data['data']['properties']['ndfiSum']['values'] return Response(json.dumps(ndfi), mimetype='application/json')
def ndfi_change(self, report_id, id): r = Report.get(Key(report_id)) z, x, y = Cell.cell_id(id) cell = Cell.get_or_default(r, x, y, z) ee = ndfi = NDFI("MOD09GA", r.comparation_range(), r.range()) bounds = cell.bounds(amazon_bounds) ne = bounds[0] sw = bounds[1] # spcify lon, lat F**K, MONKEY BALLS polygons = [[(sw[1], sw[0]), (sw[1], ne[0]), (ne[1], ne[0]), (ne[1], sw[0])]] cols = 1 rows = 1 if z < 2: cols = rows = 10 data = ndfi.ndfi_change_value(r.base_map(), [polygons], rows, cols) logging.info(data) ndfi = data["data"] # data['data']['properties']['ndfiSum']['values'] if request.args.get("_debug", False): ndfi["debug"] = {"request": ee.ee.last_request, "response": ee.ee.last_response} return Response(json.dumps(ndfi), mimetype="application/json")
def create_tables_cell(): """ creates a report for specified month """ z = request.args.get('z','') x = request.args.get('x','') y = request.args.get('y','') parent_id = request.args.get('parent_id','') assetid = request.args.get('assetid','') report = Report.find_by_assetid(assetid) if not z or not x or not y: abort(400) r = Cell(z=z, x=x, y=y, parent_id=parent_id, assetid=assetid, report=report) r.put() return r.as_json()
def setUp(self): app.config['TESTING'] = True self.app = app.test_client() self.login('*****@*****.**', 'testuser') r = Report(start=date.today(), finished=False) r.put() self.r = r self.cell = Cell(x=0, y=0, z=2, report=self.r, ndfi_high=1.0, ndfi_low=0.0) self.cell.put() for x in Note.all(): x.delete() self.when = datetime.now() self.note = Note(msg='test msg', added_by=users.get_current_user(), cell=self.cell, added_on=self.when) self.note.put()
class FTTest(unittest.TestCase): def setUp(self): app.config['TESTING'] = True self.app = app.test_client() r = Report(start=date.today(), finished=False) r.put() self.r = r self.cell = Cell(x=0, y=0, z=2, report=self.r, ndfi_high=1.0, ndfi_low=0.0) self.cell.put() self.area = Area(geo='[[[-61.5,-12],[-61.5,-11],[-60.5,-11],[-60.5,-12]]]', added_by=users.get_current_user(), type=1, cell=self.cell) #self.area.put() def test_save_on_ft(self): self.area.put() self.area.create_fusion_tables() self.assertNotEquals(None, self.area.fusion_tables_id) self.area.type = 2 self.area.save() self.area.update_fusion_tables() self.area.delete() self.area.delete_fusion_tables()
def test_cell_1_0_0(self): Cell(x=0, y=0, z=2, report=self.r, ndfi_high=1.0, ndfi_low=0.0).put() rv = self.app.get('/api/v0/report/' + str(self.r.key()) + '/cell/1_0_0/children') self.assertEquals(200, rv.status_code) js = json.loads(rv.data) num_cells = 5 * 5 # a 5 x 5 grid self.assertEquals(num_cells, len(js)) self.assertEquals(2, js[0]['z']) cell = [x for x in js if x['z'] == 2 and x['x'] == 0 and x['y'] == 0][0] self.assertAlmostEquals(0, cell['ndfi_low']) self.assertAlmostEquals(1.0, cell['ndfi_high'])
def ndfi_change(self, report_id, id): r = Report.get(Key(report_id)) z, x, y = Cell.cell_id(id) cell = Cell.get_or_default(r, x, y, z) ee = ndfi = NDFI(r.comparation_range(), r.range()) bounds = cell.bounds(amazon_bounds) ne = bounds[0] sw = bounds[1] # spcify lon, lat polygons = [(sw[1], sw[0]), (sw[1], ne[0]), (ne[1], ne[0]), (ne[1], sw[0])] cols = 1 rows = 1 if z < 2: cols = rows = 5 data = ndfi.ndfi_change_value(r.base_map(), { "type": "Polygon", "coordinates": [polygons] }, rows, cols) logging.info(data) ndfi = data #data['properties']['ndfiSum']['values'] return Response(json.dumps(ndfi), mimetype='application/json')
def ndfi_value_for_cells_dummy(cell_key): cell = Cell.get(Key(cell_key)) bounds = cell.bounds(amazon_bounds) logging.info(bounds) ne = bounds[0] sw = bounds[1] polygons = [[sw, (sw[0], ne[1]), ne, (ne[0], sw[1])]] for row in xrange(10): for col in xrange(10): c = cell.child(row, col) c.ndfi_change_value = random.random() c.put() cell.calculate_ndfi_change_from_childs()
def ndfi_value_for_cells_dummy(cell_key): cell = Cell.get(Key(cell_key)) bounds = cell.bounds(amazon_bounds) logging.info(bounds) ne = bounds[0] sw = bounds[1] polygons = [[ sw, (sw[0], ne[1]), ne, (ne[0], sw[1]) ]] for row in xrange(10): for col in xrange(10): c = cell.child(row, col) c.ndfi_change_value = random.random() c.put() cell.calculate_ndfi_change_from_childs()
def export_areas(): compounddate = request.args.get('compounddate','') if not compounddate: abort(400) month = compounddate[4:6] year = compounddate[0:4] start = datetime.date(int(year), int(month), 1) start = datetime.datetime.combine(start, datetime.time()) end = datetime.date(int(year), int(month), calendar.monthrange(int(year), int(month))[1]) end = datetime.datetime.combine(end, datetime.time()) report = Report.find_by_period(start, end) polygons = Cell.polygon_by_report(report) return json.dumps(polygons)
def test_update_cell_2_0_1(self): rv = self.app.put('/api/v0/report/' + str(self.r.key())+'/cell/2_1_3', data='{"ndfi_low": 0.0, "ndfi_high": 1.0, "done": false}' ) self.assertEquals(200, rv.status_code) js = json.loads(rv.data) q = Cell.all() q.filter("z =", 2) q.filter("x =", 1) q.filter("y =", 3) q.filter("report =", self.r) cell = q.fetch(1)[0] self.assertAlmostEquals(0, cell.ndfi_low) self.assertAlmostEquals(1.0, cell.ndfi_high) self.assertEquals('test', cell.get_parent().last_change_by.nickname()) self.assertEquals('test', cell.get_parent().get_parent().last_change_by.nickname()) self.assertNotEquals(0, cell.get_parent().last_change_on)
def bounds(self, report_id, id): r = Report.get(Key(report_id)) z, x, y = Cell.cell_id(id) cell = Cell.get_or_default(r, x, y, z) return Response(json.dumps(cell.bounds(amazon_bounds)), mimetype='application/json')
def get(self, report_id, id): r = Report.get(Key(report_id)) z, x, y = Cell.cell_id(id) cell = Cell.get_or_default(r, x, y, z) return Response(cell.as_json(), mimetype='application/json')