django-rest-framework-gis icon indicating copy to clipboard operation
django-rest-framework-gis copied to clipboard

How Deserialize data from geojson format

Open irfanpule opened this issue 4 years ago • 2 comments

example I have serializer

class PointSerializer(GeoFeatureModelSerializer):
    class Meta:
        model = PointModel
        fields = '__all__'
        geo_field = 'geom'

and I get serialize data or response

{
    "type": "FeatureCollection",
    "features": [
        {
            "id": 395,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    106.92374152131379,
                    -6.938941860571504
                ]
            },
            "properties": {
                "date": "2020-02-10",
                "time": "17:15:04",
                "video_sec": 7,
                "speed": 3.406,
                "road_number": 1
            }
        },
        {
            "id": 432,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    106.92334589548409,
                    -6.940676830708981
                ]
            },
            "properties": {
                "date": "2020-02-10",
                "time": "17:15:41",
                "video_sec": 44,
                "speed": 6.093,
                "photo_file": "",
                "road_number": 1
            }
        }
    ]
}

How I deserializer this response ?

irfanpule avatar Dec 28 '20 08:12 irfanpule

@irfanpule may be you want to convert string you get back to dictionary If that's the case, you can use json.loads(response.data) to deserialize it

devkapilbansal avatar Jan 13 '21 22:01 devkapilbansal

it will deserialize a feature, not a FeatureCollection

{
            "id": 395,
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    106.92374152131379,
                    -6.938941860571504
                ]
            },
            "properties": {
                "date": "2020-02-10",
                "time": "17:15:04",
                "video_sec": 7,
                "speed": 3.406,
                "road_number": 1
            }
        }

so you can pass the features array from your request body to your PointSerializer model like so:

PointSerializer(data=request.data["features"], many=True)`

lukemckinstry avatar Jan 20 '23 21:01 lukemckinstry