我正在编写一个 API,它应该返回其几何与给定几何相交的所有对象(EWKT 或 WKT 格式,待定义)。我正在尝试使用django-rest-framework-gis.filters
但到目前为止还没有工作。
我尝试过的:
模型.py:
class GeoData(models.Model):
geometry = models.GeometryField(srid=3857)
slug_name = models.CharField(max_length=255)
序列化程序.py:
from .models import GeoData
class GeoDataSerializer(serializers.ModelSerializer):
class Meta:
model = GeoData
视图.py:
from rest_framework import filters
from .serializers import GeoDataSerializer
from .filters import IntersectionFilter
from .models import GeoData
class GeoDataView(generics.ListAPIView):
serializer_class = GeoDataSerializer
filter_backends = (filters.DjangoFilterBackend, )
filter_class = IntersectionFilter
def get_queryset(self):
queryset = GeoData.objects.all()
return queryset
过滤器.py:
import django_filters
from rest_framework_gis import filters
from rest_framework_gis.filterset import GeoFilterSet
from .models import GeoData
class IntersectionFilter(GeoFilterSet):
slug_name = django_filters.CharFilter(name='slug_name', lookup_type='exact')
geometry = filters.GeometryFilter(name='geometry', lookup_type='intersects')
filter_overrides = {}
class Meta:
model = GeoData
现在,当调用我的网址时:(
http://localhost:8000/geodata/?geometry='LINESTRING (...)'
在 srid 3857 中也使用几何图形),我总是得到空结果(即使对于几何图形,我也确信它们确实与GeoData
对象有交集)。
我误解了什么?
配置:
Django==1.9.10
django-filter==0.15.2
djangorestframework==3.4.7
djangorestframework-gis==0.10.1