RWTH-EBC / FiLiP

FIWARE Library for Python (FiLiP) to work with FIWARE API
BSD 3-Clause "New" or "Revised" License
23 stars 13 forks source link

NGSI-LD Revise GeoProperty model #300

Open djs0109 opened 2 months ago

djs0109 commented 2 months ago

Is your feature request related to a problem? Please describe. According to https://datatracker.ietf.org/doc/html/rfc7946, Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, and GeometryCollection are allowed. However, GeometryCollection is explicitly excluded by the NGSI-LD specification. Right now, only "Point" is allowed

The implementation in V2 can be referred to https://github.com/RWTH-EBC/FiLiP/blob/49952a626e233459114e8d622570c7a5b3b749be/filip/models/ngsi_v2/base.py#L449

Suggestion

           import geojson_pydantic

            if type_ == "GeoProperty"":
                if isinstance(
                    value,
                    (
                        Point,
                        MultiPoint,
                        LineString,
                        MultiLineString,
                        Polygon,
                        MultiPolygon
                    ),
                ):
                    return value

                if isinstance(value, dict):
                    _geo_json_type = value.get("type", None)
                    if _geo_json_type == "Point":
                        return Point(**value)
                    elif _geo_json_type == "MultiPoint":
                        return MultiPoint(**value)
                    elif _geo_json_type == "LineString":
                        return LineString(**value)
                    elif _geo_json_type == "MultiLineString":
                        return MultiLineString(**value)
                    elif _geo_json_type == "Polygon":
                        return Polygon(**value)
                    elif _geo_json_type == "MultiPolygon":
                        return MultiPolygon(**value)
                raise TypeError(f"{type(value)} does not match "
                                f"{DataType.GEOJSON}")