3

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?

4

2 回答 2

3

解决了:

srid参数添加到转换函数。我正在考虑作为

变换(srid)

第一个参数为 srid 但应该是这样的

变换(srid=3857)

class MyModelViewSet(viewsets.ModelViewSet):

    queryset = MyModel.objects.all().transform(srid=3857)
    serializer_class = MyModelSerializer
于 2014-03-20T15:11:24.303 回答
2

您可以为您的GeometryField-

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

Django 会自动处理转换。有关文档教程的更多详细信息。

我在哪里可以获得关于休息框架 gis 的查询示例或综合教程?

您可以在Django 文档中找到有关 geodjango 的所有信息。Django rest 框架值得注意,但 Django。

于 2014-03-19T06:41:23.443 回答