Ejemplo n.º 1
0
 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()
Ejemplo n.º 2
0
class RegionStatsAPI(Resource):
    """ serves stats for regions

        basically is a proxy for google earth engine
    """

    def __init__(self):
        super(RegionStatsAPI, self).__init__()
        self.ee = Stats()

    def stats_for(self, report_id, assetid, table):
        return self.ee.get_stats(report_id, assetid,  table)

    # TODO: change for get
    def list(self, report_id):
        cache_key = 'stats_' + report_id
        data = memcache.get(cache_key)
        if not data:
            try:
                data = StatsStore.all().filter('report_id =', report_id).fetch(1)[0].json
            except IndexError:
                # launch caching!
                logging.info("launching stats calc")
                deferred.defer(update_report_stats, report_id)
                abort(404)
            memcache.set(cache_key, data)
        return Response(data, mimetype='application/json')

    def get(self, report_id, id):
        r = Report.get(Key(report_id))
        s = self.stats_for(str(r.key().id()), r.assetid, int(id))
        data = json.dumps(s)
        return Response(data, mimetype='application/json')

    def polygon(self):
        """ return stats for given polygon """
        data = json.loads(request.data)
        polygon = data['polygon']
        reports = data['reports']
        try:
            reports = [Report.get(Key(x)) for x in reports]
        except ValueError:
            logging.error("can't find some report")
            abort(404)
        #TODO: test if polygon is ccw
        # exchange lat, lon -> lon, lat
        normalized_poly = [(coord[1], coord[0]) for coord in polygon]
        stats = self.ee.get_stats_for_polygon([(str(r.key().id()), r.assetid) for r in reports], [normalized_poly])
        try:
            # aggregate
            data['def'] = sum(s['def'] for s in stats)
            data['deg'] = sum(s['deg'] for s in stats)
            data['total_area'] = stats[0]['total_area']
            return Response(json.dumps(data), mimetype='application/json')
        except (KeyError, ValueError, IndexError):
            abort(404)
 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()
Ejemplo n.º 4
0
class StatsTest(unittest.TestCase):
    """ test for reporting api 
    """
    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 test_stats(self):
        """
        """
        # freeze map with on area
        report_id = self.r.key().id()
        data = self.ndfi.freeze_map(self.r.base_map(),
                                    int(settings.FT_TABLE_ID),
                                    self.r.key().id())
        self.assertTrue('data' in data)
        self.assertTrue('id' in data['data'])
        self.assertTrue(len(data['data']['id']) > 0)

        asset_id = data['data']['id']
        print "report id: ", asset_id

        # get stats for this area
        st = self.stats.get_stats_for_polygon(report_id, asset_id,
                                              self.polygon)

        self.assertTrue(st is not None)
        polygon_stats = st[0]
        print polygon_stats
        self.assertTrue(float(polygon_stats['def']) > 0.0)
        self.assertAlmostEquals(0.0, float(polygon_stats['deg']))
        def_area = float(polygon_stats['def'])
        print "deforested area: ", def_area

        # get stats for this area
        # move the polygon a little bit
        p = [[[x, y + 1.0] for x, y in self.polygon[0]]]
        st = self.stats.get_stats_for_polygon(report_id, asset_id, p)
        self.assertTrue(st is not None)
        print st[0]
        polygon_stats = st['data']['properties']['classHistogram']['values'][
            'null']['values']
        self.assertTrue(float(polygon_stats['def']) > 0.0)
        self.assertTrue(float(polygon_stats['def']) < def_area)
        print "new deforested area: ", float(polygon_stats['def'])

        # search in area whiout deforesation
        p = [[[x + 4.0, y] for x, y in self.polygon[0]]]
        st = self.stats.get_stats_for_polygon(report_id, asset_id, p)
        self.assertTrue(st is not None)
        polygon_stats = st[0]
        self.assertAlmostEquals(0.0, float(polygon_stats['def']))
        self.assertAlmostEquals(0.0, float(polygon_stats['deg']))
Ejemplo n.º 5
0
 def __init__(self):
     super(RegionStatsAPI, self).__init__()
     self.ee = Stats()
Ejemplo n.º 6
0
def stats_for(report_id, assetid, table):
    ee = Stats()
    return ee.get_stats(report_id, assetid,  table)
Ejemplo n.º 7
0
 def __init__(self):
     super(RegionStatsAPI, self).__init__()
     self.ee = Stats()
class StatsTest(unittest.TestCase):
    """ test for reporting api 
    """

    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 test_stats(self):
        """
        """
        # freeze map with on area
        report_id = self.r.key().id()
        data = self.ndfi.freeze_map(self.r.base_map(), 
                             int(settings.FT_TABLE_ID),
                             self.r.key().id())
        self.assertTrue('data' in data)
        self.assertTrue('id' in data['data'])
        self.assertTrue(len(data['data']['id']) > 0)
    
        asset_id = data['data']['id']
        print "report id: ", asset_id

        # get stats for this area
        st = self.stats.get_stats_for_polygon(report_id, asset_id, self.polygon)
        
        self.assertTrue(st is not None)
        polygon_stats = st[0]
        print polygon_stats
        self.assertTrue(float(polygon_stats['def']) > 0.0)
        self.assertAlmostEquals(0.0, float(polygon_stats['deg']))
        def_area = float(polygon_stats['def'])
        print "deforested area: ", def_area

        # get stats for this area
        # move the polygon a little bit
        p = [[[x, y + 1.0] for x, y in self.polygon[0]]]
        st = self.stats.get_stats_for_polygon(report_id, asset_id, p)
        self.assertTrue(st is not None)
        print st[0]
        polygon_stats = st['data']['properties']['classHistogram']['values']['null']['values']
        self.assertTrue(float(polygon_stats['def']) > 0.0)
        self.assertTrue(float(polygon_stats['def']) < def_area)
        print "new deforested area: ", float(polygon_stats['def'])

        # search in area whiout deforesation
        p = [[[x+4.0, y] for x, y in self.polygon[0]]]
        st = self.stats.get_stats_for_polygon(report_id, asset_id, p)
        self.assertTrue(st is not None)
        polygon_stats = st[0]
        self.assertAlmostEquals(0.0, float(polygon_stats['def']))
        self.assertAlmostEquals(0.0, float(polygon_stats['deg']))
Ejemplo n.º 9
0
def stats_for(report_id, assetid, table):
    ee = Stats()
    return ee.get_stats(report_id, assetid, table)