在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:openwisp/django-rest-framework-gis开源软件地址:https://github.com/openwisp/django-rest-framework-gis开源编程语言:Python 99.6%开源软件介绍:django-rest-framework-gisGeographic add-ons for Django Rest Framework - Mailing List. Install last stable version from pypipip install djangorestframework-gis Install development versionpip install https://github.com/openwisp/django-rest-framework-gis/tarball/master SetupAdd INSTALLED_APPS = [
# ...
'rest_framework',
'rest_framework_gis',
# ...
] Compatibility with DRF, Django and Python
FieldsGeometryFieldProvides a This field takes three optional arguments:
Note: While New in 0.9.3: there is no need to define this field explicitly in your serializer,
it's mapped automatically during initialization in GeometrySerializerMethodFieldProvides a SerializersGeoModelSerializer (DEPRECATED)Deprecated, will be removed in 1.0: Using this serializer is not needed anymore since 0.9.3, if you add
Provides a For example, the following model: class Location(models.Model):
"""
A model which holds information about a particular location
"""
address = models.CharField(max_length=255)
city = models.CharField(max_length=100)
state = models.CharField(max_length=100)
point = models.PointField() By default, the DRF ModelSerializer ver < 0.9.3 will output: {
"id": 1,
"address": "742 Evergreen Terrace",
"city": "Springfield",
"state": "Oregon",
"point": "POINT(-123.0208 44.0464)"
} In contrast, the {
"id": 1,
"address": "742 Evergreen Terrace",
"city": "Springfield",
"state": "Oregon",
"point": {
"type": "Point",
"coordinates": [-123.0208, 44.0464],
}
} Note: For
GeoFeatureModelSerializer
{
"id": 1,
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-123.0208, 44.0464],
},
"properties": {
"address": "742 Evergreen Terrace",
"city": "Springfield",
"state": "Oregon"
}
} If you are serializing an object list, {
"type": "FeatureCollection",
"features": [
{
"id": 1
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-123.0208, 44.0464],
},
"properties": {
"address": "742 Evergreen Terrace",
"city": "Springfield",
"state": "Oregon",
}
}
{
"id": 2,
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-123.0208, 44.0489],
},
"properties": {
"address": "744 Evergreen Terrace",
"city": "Springfield",
"state": "Oregon"
}
}
} Specifying the geometry field: "geo_field"
from rest_framework_gis.serializers import GeoFeatureModelSerializer
class LocationSerializer(GeoFeatureModelSerializer):
""" A class to serialize locations as GeoJSON compatible data """
class Meta:
model = Location
geo_field = "point"
# you can also explicitly declare which fields you want to include
# as with a ModelSerializer.
fields = ('id', 'address', 'city', 'state') Using GeometrySerializerMethodField as "geo_field"
from django.contrib.gis.geos import Point
from rest_framework_gis.serializers import GeoFeatureModelSerializer, GeometrySerializerMethodField
class LocationSerializer(GeoFeatureModelSerializer):
""" A class to serialize locations as GeoJSON compatible data """
# a field which contains a geometry value and can be used as geo_field
other_point = GeometrySerializerMethodField()
def get_other_point(self, obj):
return Point(obj.point.lat / 2, obj.point.lon / 2)
class Meta:
model = Location
geo_field = 'other_point' Serializer for Specifying the ID: "id_field"The primary key of the model (usually the "id" attribute) is
automatically used as the The default behaviour follows the GeoJSON RFC,
but it can be disabled by setting from rest_framework_gis.serializers import GeoFeatureModelSerializer
class LocationSerializer(GeoFeatureModelSerializer):
class Meta:
model = Location
geo_field = "point"
id_field = False
fields = ('id', 'address', 'city', 'state') The from rest_framework_gis.serializers import GeoFeatureModelSerializer
class LocationSerializer(GeoFeatureModelSerializer):
class Meta:
model = Location
geo_field = 'point'
id_field = 'slug'
fields = ('slug', 'address', 'city', 'state') Bounding Box: "auto_bbox" and "bbox_geo_field"The GeoJSON specification allows a feature to contain a
boundingbox of a feature.
from rest_framework_gis.serializers import GeoFeatureModelSerializer
class LocationSerializer(GeoFeatureModelSerializer):
class Meta:
model = Location
geo_field = 'geometry'
auto_bbox = True The second approach uses the from rest_framework_gis.serializers import GeoFeatureModelSerializer
class LocationSerializer(GeoFeatureModelSerializer):
class Meta:
model = BoxedLocation
geo_field = 'geometry'
bbox_geo_field = 'bbox_geometry' Custom GeoJSON properties sourceIn GeoJSON each feature can have a The following example shows how to use a PostgreSQL HStore field as a source for
the # models.py
class Link(models.Model):
"""
Metadata is stored in a PostgreSQL HStore field, which allows us to
store arbitrary key-value pairs with a link record.
"""
metadata = HStoreField(blank=True, null=True, default=dict)
geo = models.LineStringField()
objects = models.GeoManager()
# serializers.py
class NetworkGeoSerializer(GeoFeatureModelSerializer):
class Meta:
model = models.Link
geo_field = 'geo'
auto_bbox = True
def get_properties(self, instance, fields):
# This is a PostgreSQL HStore field, which django maps to a dict
return instance.metadata
def unformat_geojson(self, feature):
attrs = {
self.Meta.geo_field: feature["geometry"],
"metadata": feature["properties"]
}
if self.Meta.bbox_geo_field and "bbox" in feature:
attrs[self.Meta.bbox_geo_field] = Polygon.from_bbox(feature["bbox"])
return attrs When the serializer renders GeoJSON, it calls the method
The reverse is also required: mapping a GeoJSON formatted structure to
attributes of your model. This task is done by PaginationWe provide a GeoJsonPaginationBased on Code example: from rest_framework_gis.pagination import GeoJsonPagination
# --- other omitted imports --- #
class GeojsonLocationList(generics.ListCreateAPIView):
# -- other omitted view attributes --- #
pagination_class = GeoJsonPagination Example result response (cut to one element only instead of 10): {
"type": "FeatureCollection",
"count": 25,
"next": "http://localhost:8000/geojson/?page=2",
"previous": null,
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
42.0,
50.0
]
},
"properties": {
"name": "test"
}
}
]
} Filtersnote: this feature has been tested up to django-filter 1.0. We provide a GeometryFilterfrom rest_framework_gis.filterset import GeoFilterSet
from rest_framework_gis.filters import GeometryFilter
from django_filters import filters
class RegionFilter(GeoFilterSet):
slug = filters.CharFilter(name='slug', lookup_expr='istartswith')
contains_geom = GeometryFilter(name='geom', lookup_expr='contains')
class Meta:
model = Region We can then filter in the URL, using GeoJSON, and we will perform a
GeoFilterSetThe InBBoxFilterProvides a
from rest_framework_gis.filters import InBBoxFilter
class LocationList(ListAPIView):
queryset = models.Location.objects.all()
serializer_class = serializers.LocationSerializer
bbox_filter_field = 'point'
filter_backends = (InBBoxFilter,)
bbox_filter_include_overlapping = True # Optional We can then filter in the URL, using Bounding Box format (min Lon, min
Lat, max Lon, max Lat), and we can search for instances within the
bounding box, e.g.:
By default, InBBoxFilter will only return those instances entirely
within the stated bounding box. To include those instances which overlap
the bounding box, include Note that if you are using other filters, you'll want to include your other filter backend in your view. For example:
全部评论
专题导读
上一篇:webasyst/webasyst-framework: Webasyst PHP Framework发布时间:2022-06-07下一篇:spring-projects/spring-security: Spring Security发布时间:2022-06-07热门推荐
热门话题
阅读排行榜
|
请发表评论