Esempio n. 1
0
    def get(self, request, admin_level_id, version=None):
        if not AdminLevel.objects.filter(id=admin_level_id).exists():
            raise exceptions.NotFound()

        admin_level = AdminLevel.objects.get(id=admin_level_id)
        if not admin_level.can_get(request.user):
            raise exceptions.PermissionDenied()

        areas = admin_level.geoarea_set.filter(polygons__isnull=False)
        if areas.count() > 0:
            try:
                envelope = Envelope(*areas[0].polygons.extent)
                for area in areas[1:]:
                    envelope.expand_to_include(*area.polygons.extent)

                return response.Response({
                    'bounds': {
                        'min_x': envelope.min_x,
                        'min_y': envelope.min_y,
                        'max_x': envelope.max_x,
                        'max_y': envelope.max_y,
                    }
                })
            except ValueError as e:
                return response.Response({
                    'bounds': None,
                })

        return response.Response({
            'bounds': None,
        })
Esempio n. 2
0
    def calc_cache(self, save=True):
        # GeoJSON
        geojson = json.loads(serialize(
            'geojson',
            self.geoarea_set.all(),
            geometry_field='polygons',
            fields=('pk', 'title', 'code', 'parent'),
        ))

        # Titles
        titles = {}
        for geo_area in self.geoarea_set.all():
            titles[str(geo_area.id)] = {
                'title': geo_area.title,
                'parent_id': str(geo_area.parent.pk) if geo_area.parent else None,
                'code': geo_area.code,
            }
        self.geo_area_titles = titles

        # Bounds
        bounds = {}
        areas = self.geoarea_set.filter(polygons__isnull=False)
        if areas.count() > 0:
            try:
                envelope = Envelope(*areas[0].polygons.extent)
                for area in areas[1:]:
                    envelope.expand_to_include(*area.polygons.extent)
                bounds = {
                    'minX': envelope.min_x,
                    'minY': envelope.min_y,
                    'maxX': envelope.max_x,
                    'maxY': envelope.max_y,
                }
            except ValueError:
                pass

        self.geojson_file.save(
            f'admin-level-{self.pk}.json',
            ContentFile(
                json.dumps(geojson, cls=DjangoJSONEncoder).encode('utf-8'),
            ),
        )
        self.bounds_file.save(
            f'admin-level-{self.pk}.json',
            ContentFile(
                json.dumps({'bounds': bounds}, cls=DjangoJSONEncoder).encode('utf-8'),
            ),
        )
        if save:
            self.save()
Esempio n. 3
0
def index_map(index, obj):
    time_start = None
    time_end = None
    extent = Envelope(0, 0, 0, 0)
    for l in obj.local_layers:
        start = end = None
        try:
            start, end = l.get_time_extent()
        except:
            _logger.warn('could not get time info for %s', l.typename)

        if start:
            start = util.iso_str_to_jdate(start)
            if time_start is None:
                time_start = start
            else:
                time_start = min(time_start, start)
        if end:
            end = util.iso_str_to_jdate(end)
            if time_end is None:
                time_end = start
            else:
                time_end = max(time_end, end)

        try:
            wms_metadata = l.metadata()
            extent.expand_to_include(wms_metadata.boundingBoxWGS84)
        except:
            _logger.warn('could not get WMS info for %s', l.typename)

    if time_start:
        index.time_start = time_start
    if time_end:
        index.time_end = time_end
    index.extent = extent.wkt
    index.save()
Esempio n. 4
0
def index_map(index, obj):
    time_start = None
    time_end = None
    extent = Envelope(0,0,0,0)
    for l in obj.local_layers:
        start = end = None
        try:
            start, end = l.get_time_extent()
        except:
            _logger.warn('could not get time info for %s', l.typename)

        if start:
            start = util.iso_str_to_jdate(start)
            if time_start is None:
                time_start = start
            else:
                time_start = min(time_start, start)
        if end:
            end = util.iso_str_to_jdate(end)
            if time_end is None:
                time_end = start
            else:
                time_end = max(time_end, end)

        try:
            wms_metadata = l.metadata()
            extent.expand_to_include(wms_metadata.boundingBoxWGS84)
        except:
            _logger.warn('could not get WMS info for %s', l.typename )

    if time_start:
        index.time_start = time_start
    if time_end:
        index.time_end = time_end
    index.extent = extent.wkt
    index.save()
Esempio n. 5
0
class EnvelopeTest(unittest.TestCase):

    def setUp(self):
        self.e = Envelope(0, 0, 5, 5)

    def test01_init(self):
        "Testing Envelope initialization."
        e1 = Envelope((0, 0, 5, 5))
        Envelope(0, 0, 5, 5)
        Envelope(0, '0', '5', 5)  # Thanks to ww for this
        Envelope(e1._envelope)
        self.assertRaises(GDALException, Envelope, (5, 5, 0, 0))
        self.assertRaises(GDALException, Envelope, 5, 5, 0, 0)
        self.assertRaises(GDALException, Envelope, (0, 0, 5, 5, 3))
        self.assertRaises(GDALException, Envelope, ())
        self.assertRaises(ValueError, Envelope, 0, 'a', 5, 5)
        self.assertRaises(TypeError, Envelope, 'foo')
        self.assertRaises(GDALException, Envelope, (1, 1, 0, 0))
        try:
            Envelope(0, 0, 0, 0)
        except GDALException:
            self.fail("shouldn't raise an exception for min_x == max_x or min_y == max_y")

    def test02_properties(self):
        "Testing Envelope properties."
        e = Envelope(0, 0, 2, 3)
        self.assertEqual(0, e.min_x)
        self.assertEqual(0, e.min_y)
        self.assertEqual(2, e.max_x)
        self.assertEqual(3, e.max_y)
        self.assertEqual((0, 0), e.ll)
        self.assertEqual((2, 3), e.ur)
        self.assertEqual((0, 0, 2, 3), e.tuple)
        self.assertEqual('POLYGON((0.0 0.0,0.0 3.0,2.0 3.0,2.0 0.0,0.0 0.0))', e.wkt)
        self.assertEqual('(0.0, 0.0, 2.0, 3.0)', str(e))

    def test03_equivalence(self):
        "Testing Envelope equivalence."
        e1 = Envelope(0.523, 0.217, 253.23, 523.69)
        e2 = Envelope((0.523, 0.217, 253.23, 523.69))
        self.assertEqual(e1, e2)
        self.assertEqual((0.523, 0.217, 253.23, 523.69), e1)

    def test04_expand_to_include_pt_2_params(self):
        "Testing Envelope expand_to_include -- point as two parameters."
        self.e.expand_to_include(2, 6)
        self.assertEqual((0, 0, 5, 6), self.e)
        self.e.expand_to_include(-1, -1)
        self.assertEqual((-1, -1, 5, 6), self.e)

    def test05_expand_to_include_pt_2_tuple(self):
        "Testing Envelope expand_to_include -- point as a single 2-tuple parameter."
        self.e.expand_to_include((10, 10))
        self.assertEqual((0, 0, 10, 10), self.e)
        self.e.expand_to_include((-10, -10))
        self.assertEqual((-10, -10, 10, 10), self.e)

    def test06_expand_to_include_extent_4_params(self):
        "Testing Envelope expand_to_include -- extent as 4 parameters."
        self.e.expand_to_include(-1, 1, 3, 7)
        self.assertEqual((-1, 0, 5, 7), self.e)

    def test06_expand_to_include_extent_4_tuple(self):
        "Testing Envelope expand_to_include -- extent as a single 4-tuple parameter."
        self.e.expand_to_include((-1, 1, 3, 7))
        self.assertEqual((-1, 0, 5, 7), self.e)

    def test07_expand_to_include_envelope(self):
        "Testing Envelope expand_to_include with Envelope as parameter."
        self.e.expand_to_include(Envelope(-1, 1, 3, 7))
        self.assertEqual((-1, 0, 5, 7), self.e)

    def test08_expand_to_include_point(self):
        "Testing Envelope expand_to_include with Point as parameter."
        self.e.expand_to_include(TestPoint(-1, 1))
        self.assertEqual((-1, 0, 5, 5), self.e)
        self.e.expand_to_include(TestPoint(10, 10))
        self.assertEqual((-1, 0, 10, 10), self.e)
Esempio n. 6
0
class EnvelopeTest(unittest.TestCase):

    def setUp(self):
        self.e = Envelope(0, 0, 5, 5)

    def test01_init(self):
        "Testing Envelope initilization."
        e1 = Envelope((0, 0, 5, 5))
        Envelope(0, 0, 5, 5)
        Envelope(0, '0', '5', 5)  # Thanks to ww for this
        Envelope(e1._envelope)
        self.assertRaises(OGRException, Envelope, (5, 5, 0, 0))
        self.assertRaises(OGRException, Envelope, 5, 5, 0, 0)
        self.assertRaises(OGRException, Envelope, (0, 0, 5, 5, 3))
        self.assertRaises(OGRException, Envelope, ())
        self.assertRaises(ValueError, Envelope, 0, 'a', 5, 5)
        self.assertRaises(TypeError, Envelope, 'foo')
        self.assertRaises(OGRException, Envelope, (1, 1, 0, 0))
        try:
            Envelope(0, 0, 0, 0)
        except OGRException:
            self.fail("shouldn't raise an exception for min_x == max_x or min_y == max_y")

    def test02_properties(self):
        "Testing Envelope properties."
        e = Envelope(0, 0, 2, 3)
        self.assertEqual(0, e.min_x)
        self.assertEqual(0, e.min_y)
        self.assertEqual(2, e.max_x)
        self.assertEqual(3, e.max_y)
        self.assertEqual((0, 0), e.ll)
        self.assertEqual((2, 3), e.ur)
        self.assertEqual((0, 0, 2, 3), e.tuple)
        self.assertEqual('POLYGON((0.0 0.0,0.0 3.0,2.0 3.0,2.0 0.0,0.0 0.0))', e.wkt)
        self.assertEqual('(0.0, 0.0, 2.0, 3.0)', str(e))

    def test03_equivalence(self):
        "Testing Envelope equivalence."
        e1 = Envelope(0.523, 0.217, 253.23, 523.69)
        e2 = Envelope((0.523, 0.217, 253.23, 523.69))
        self.assertEqual(e1, e2)
        self.assertEqual((0.523, 0.217, 253.23, 523.69), e1)

    def test04_expand_to_include_pt_2_params(self):
        "Testing Envelope expand_to_include -- point as two parameters."
        self.e.expand_to_include(2, 6)
        self.assertEqual((0, 0, 5, 6), self.e)
        self.e.expand_to_include(-1, -1)
        self.assertEqual((-1, -1, 5, 6), self.e)

    def test05_expand_to_include_pt_2_tuple(self):
        "Testing Envelope expand_to_include -- point as a single 2-tuple parameter."
        self.e.expand_to_include((10, 10))
        self.assertEqual((0, 0, 10, 10), self.e)
        self.e.expand_to_include((-10, -10))
        self.assertEqual((-10, -10, 10, 10), self.e)

    def test06_expand_to_include_extent_4_params(self):
        "Testing Envelope expand_to_include -- extent as 4 parameters."
        self.e.expand_to_include(-1, 1, 3, 7)
        self.assertEqual((-1, 0, 5, 7), self.e)

    def test06_expand_to_include_extent_4_tuple(self):
        "Testing Envelope expand_to_include -- extent as a single 4-tuple parameter."
        self.e.expand_to_include((-1, 1, 3, 7))
        self.assertEqual((-1, 0, 5, 7), self.e)

    def test07_expand_to_include_envelope(self):
        "Testing Envelope expand_to_include with Envelope as parameter."
        self.e.expand_to_include(Envelope(-1, 1, 3, 7))
        self.assertEqual((-1, 0, 5, 7), self.e)

    def test08_expand_to_include_point(self):
        "Testing Envelope expand_to_include with Point as parameter."
        self.e.expand_to_include(TestPoint(-1, 1))
        self.assertEqual((-1, 0, 5, 5), self.e)
        self.e.expand_to_include(TestPoint(10, 10))
        self.assertEqual((-1, 0, 10, 10), self.e)