Example #1
0
    def setUp(self):
        self.loader = SIAStadsdeelLoader('sia-stadsdeel')
        # "sia-stadsdeel" type Areas are derived from "stadsdeel" type Areas,
        # here we create "stadsdeel" AreaType and provide some Area instances
        # using that type
        stadsdeel_area_type = AreaType.objects.create(
            name='Stadsdeel',
            code='stadsdeel',
            description='Stadsdeel voor tests.'
        )
        cbs_gemeente_type = AreaType.objects.create(
            name='Stadsdeel',
            code='cbs-gemeente-2019',
            description='CBS gemeentegrens voor tests.'
        )

        # We create three city distticts of 10x10 meters located at City Hall to
        # test with. (Code stolen from the Gebieden API tests.)
        x, y = RD_STADHUIS
        width, height = 10, 10
        self.bbox_1 = [x, y, x + width, y + height]
        self.bbox_2 = [x, y + height, x + width, y + 2 * height]
        self.bbox_3 = [x, y + 2 * height, x + width, y + 3 * height]

        zuid_geometry = MultiPolygon([Polygon.from_bbox(self.bbox_1)])
        zuid_geometry.srid = 28992
        zuid_geometry.transform(ct=4326)
        noord_geometry = MultiPolygon([Polygon.from_bbox(self.bbox_2)])
        noord_geometry.srid = 28992
        noord_geometry.transform(ct=4326)
        weesp_geometry = MultiPolygon([Polygon.from_bbox(self.bbox_3)])
        weesp_geometry.srid = 28992
        weesp_geometry.transform(ct=4326)

        Area.objects.create(
            name='Zuid',
            code='SOME_CBS_CODE_1',
            _type=stadsdeel_area_type,
            geometry=zuid_geometry,
        )
        Area.objects.create(
            name='Noord',
            code='SOME_CBS_CODE_2',
            _type=stadsdeel_area_type,
            geometry=noord_geometry,
        )
        Area.objects.create(
            name='Weesp',
            code='SOME_CBS_CODE_3',
            _type=cbs_gemeente_type,
            geometry=weesp_geometry,
        )

        self.assertEqual(Area.objects.count(), 3)
        self.assertEqual(Area.objects.filter(_type__code='stadsdeel').count(), 2)
Example #2
0
    def test_get_imixed3(self):
        """
        Test the logic for getting mixed geounits outside a boundary at the
        lowest geolevel.
        """
        level = self.geolevels[0]
        bigunits = self.geounits[level.id]
        boundary = MultiPolygon(
            Polygon(
                LinearRing(Point((0, 0)), Point((1, 0)), Point((1, 1)),
                           Point((0, 0)))))
        boundary.srid = 3785

        units = Geounit.get_mixed_geounits(
            [str(bigunits[3].id),
             str(bigunits[6].id),
             str(bigunits[7].id)], self.legbod, level.id, boundary, False)
        numunits = len(units)
        # this test should return 3, for the large geounits are completely
        # without yet intersect at the corner. the net geometry from this
        # set of mixed geounits is correct, though
        self.assertEqual(
            3, numunits,
            'Number of geounits outside boundary is incorrect. (%d)' %
            numunits)

        units = Geounit.get_mixed_geounits(
            [str(bigunits[0].id),
             str(bigunits[4].id),
             str(bigunits[8].id)], self.legbod, level.id, boundary, False)
        numunits = len(units)
        self.assertEqual(
            63, numunits,
            'Number of geounits outside boundary is incorrect. (%d)' %
            numunits)
Example #3
0
    def test_get_mixed3(self):
        """
        Test the logic for getting mixed geounits inside a boundary at the
        lowest geolevel.
        """
        level = self.geolevels[0]
        bigunits = self.geounits[level.id]
        boundary = MultiPolygon(
            Polygon(
                LinearRing(Point((0, 0)), Point((1, 0)), Point((1, 1)),
                           Point((0, 0)))))
        boundary.srid = 3785

        units = Geounit.get_mixed_geounits(
            [str(bigunits[1].id),
             str(bigunits[2].id),
             str(bigunits[5].id)], self.legbod, level.id, boundary, True)
        numunits = len(units)
        self.assertEqual(
            3, numunits,
            'Number of geounits inside boundary is incorrect. (%d)' % numunits)

        units = Geounit.get_mixed_geounits(
            [str(bigunits[0].id),
             str(bigunits[4].id),
             str(bigunits[8].id)], self.legbod, level.id, boundary, True)
        numunits = len(units)
        self.assertEqual(
            63, numunits,
            'Number of geounits inside boundary is incorrect. (%d)' % numunits)
Example #4
0
    def get_data_bounds_polygon(self):
        parsed_geojson = json.loads(self.data)
        def get_polygons(obj):
            polys = []
            if "type" in obj:
                if obj["type"] == "FeatureCollection":
                    for feature in obj["features"]:
                        extent_polys = get_polygons(feature)
                        polys.extend(extent_polys)
                elif obj["type"] == "Feature":
                    # process "geometry"
                    geom = GEOSGeometry(json.dumps(obj["geometry"]))
                    # get extent_poly of geom
                    extent_poly = Polygon.from_bbox(geom.extent)
                    polys.append(extent_poly)

                elif obj["type"] in ("Polygon", "LineString", "Point"):
                    # process "geometry"
                    geom = GEOSGeometry(json.dumps(obj))
                    # get extent_poly of geom
                    extent_poly = Polygon.from_bbox(geom.extent)
                    polys.append(extent_poly)
            return polys

        geojson_extent_polygons = []
        if isinstance(parsed_geojson, list):
            for obj in parsed_geojson:
                polygons = get_polygons(obj)
                geojson_extent_polygons.extend(polygons)
        elif "type" in parsed_geojson and parsed_geojson["type"] in ("FeatureCollection", "Feature", "Polygon", "LineString", "Point"):
            polygons = get_polygons(parsed_geojson)
            geojson_extent_polygons.extend(polygons)


        # process polygons into polyons extent
        mploy = MultiPolygon(geojson_extent_polygons)
        mploy.srid = WGS84_SRID  # Expected...
        mploy.transform(METERS_SRID)
        poly = Polygon.from_bbox(mploy.extent)
        poly.srid  = METERS_SRID
        return poly
Example #5
0
 def _elaborate_geo_filter(self, data):
     if issubclass(GEOSGeometry, django.contrib.gis.geos.GEOSGeometry) and \
         issubclass(MultiPolygon, django.contrib.gis.geos.GEOSGeometry):
         if 'geo_field_name' in data and 'polygons_srid' in data and 'polygons' in data:
             geo_field_name = data.get('geo_field_name')
             polygons = []
             for geo_text in data['polygons']:
                 polygons.append(GEOSGeometry(geo_text))
             geo = MultiPolygon(polygons)
             try:
                 geo.srid = int(data.get('polygons_srid'))
             except ValueError:
                 print(_('ERROR: geo srid is incorrect'))
             geo_field = self.model._meta.get_field(geo_field_name)
             _geo = geo.transform(geo_field.srid, clone=True).buffer(
                 0)  # se l'unione del multipolygon risultasse invalida
             qset = Q(**{geo_field_name + '__within': _geo})
         else:
             qset = Q()
         return qset
     return Q()
Example #6
0
 def footprint_geom(self):
     # TODO: Check if extent of footprint
     e = self.geo_reference
     mp = MultiPolygon(Polygon.from_bbox((e.minx, e.miny, e.maxx, e.maxy)))
     mp.srid = e.srid
     return mp