Пример #1
0
 def test_remove_dupl_coords(self):
     a = {
         "crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:EPSG::31370"}},
         "type": "MultiPolygon",
         "coordinates": [
             [
                 [
                     [94826.49908124168, 193977.986615351],
                     [94826.49908124168, 193976.986615351],
                     [94826.49908124168, 193976.986615351],
                     [94820.99908124168, 193974.486615351],
                     [94824.99908124168, 193967.986615351],
                     [94830.99908124168, 193972.986615351],
                     [94826.49908124168, 193977.986615351],
                 ]
             ]
         ],
     }
     remove_dupl_coords(a["coordinates"])
     self.assertEqual(
         [
             [
                 [
                     [94826.49908124168, 193977.986615351],
                     [94826.49908124168, 193976.986615351],
                     [94820.99908124168, 193974.486615351],
                     [94824.99908124168, 193967.986615351],
                     [94830.99908124168, 193972.986615351],
                     [94826.49908124168, 193977.986615351],
                 ]
             ]
         ],
         a["coordinates"],
     )
def geometrie_validator(node, gj, max_area):
    """
    Validate a geojson if it is a valid geometry.
    Function checks if a geojson is a valid geometry.
    Current checks are:
    *Type of geometry must be geojson
    *geometry must be valid according to OGC-specifications
    *geometry must be inside bounding box of Flanders
    *geometry must be smaller than the given max_area
    :param node: A colander SchemaNode.
    :param gj: geojson to be validated
    :raise colander.Invalid: when geojson does not satisfy the upper conditions
    """
    if gj is None or not gj:
        raise colander.Invalid(node, "geometrie is verplicht en mag niet leeg zijn")
    try:
        geom = shape(gj)
        if geom.type != "MultiPolygon":
            raise colander.Invalid(node, 'geometrie is niet van het type MultiPolygon')
        srid = get_srid_from_geojson(gj)
        if srid is None or srid != 31370:
            raise colander.Invalid(node,
                                   'Geen geldige srid gevonden. '
                                   'Enkel geometrien met srid 31370 (Lambert 72) worden ondersteund')
        if geom.is_empty or not geom.is_valid or not geom.is_simple:
            geom = geom.buffer(0)
            if geom.is_empty or not geom.is_valid or not geom.is_simple:
                raise colander.Invalid(node,
                                       'Geometrie is niet geldig '
                                       '(bv: leeg, voldoet niet aan OGC, zelf-intersecterend,...)')
            gj.update(json.loads(json.dumps(mapping(geom))))
            if gj['type'] == 'Polygon':
                gj.update({'type': 'MultiPolygon', 'coordinates': [gj['coordinates']]})
        if geom.area > max_area:
            raise colander.Invalid(node, 'Geometrie is {} ha, groter dan {} ha, '
                                         'gelieve de polygoon te verkleinen.'.format(geom.area / 10000,
                                                                                     max_area / 10000))

        b = box(19680.692555495727, 146642.51885241456, 274994.53266103653,
                245606.4642544454)  # bounding box vlaanderen
        if not b.contains(geom):
            raise colander.Invalid(node, 'Geometrie ligt niet binnen de bounding box van Vlaanderen')
        #  todo remove duplicate consecutive coordinates (OGC SFA en ISO 19107:2003 standard)
        remove_dupl_coords(gj["coordinates"])
        return gj
    except ValueError:
        raise colander.Invalid(node, 'geometrie is geen geldig GeoJson')