Question

I am newbie to Django and developing a REST API using Django Rest Framework (DRF) and GIS. Previously, I was using an SQL query for transforming a geometry:

select
    id, 
    name, 
    round(value::numeric, 2) as value,
    st_transform(geometry, 3857) as geometry
from
    my_model

...

class MyModel(models.Model):
    name = models.CharField(max_length=50, blank=True)
    value = models.FloatField()
    geometry = models.GeometryField(null=True, blank=True)


    class Meta:
        db_table = u'my_model'

    def __unicode__(self):
        return '%s' % self.name

Here serializer class:

class MyModelSerializer(serializers.GeoModelSerializer):
    class Meta:
        model = MyModel
        fields = ('id', 'name', 'value', 'geometry')

I have also tried to do this way but cannot transform output

class MyModelViewSet(viewsets.ModelViewSet):

    queryset = MyModel.objects.all().transform(3857)
    serializer_class = MyModelSerializer

How can I do this in Django Rest Framework and GIS? Where can I get query sample or a comprehensive tutorial for DRF?

Was it helpful?

Solution

Solved:

Add the srid argument to transform function. I was considering as

transform(srid)

first argument as srid but should be like this

transform(srid=3857)

class MyModelViewSet(viewsets.ModelViewSet):

    queryset = MyModel.objects.all().transform(srid=3857)
    serializer_class = MyModelSerializer

OTHER TIPS

You can provide a default SRID to your GeometryField -

geometry = models.GeometryField(srid=3857, null=True, blank=True)

And Django will automatically handle the conversion. More details on the doc and tutorial.

Where can i get queries sample or comprehensive tutorial for rest framework gis?

You can find all about geodjango on Django documentation. Django rest framework is noting but Django.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top