| 1 |
from django.contrib.gis.geos import Point |
| 2 |
from rest_framework import pagination, serializers |
| 3 |
|
| 4 |
from rest_framework_gis import serializers as gis_serializers |
| 5 |
from rest_framework_gis.fields import GeometryField, GeometrySerializerMethodField |
| 6 |
|
| 7 |
from .models import ( |
| 8 |
BoxedLocation, |
| 9 |
GeometryModel, |
| 10 |
LineStringModel, |
| 11 |
Location, |
| 12 |
MultiLineStringModel, |
| 13 |
MultiPointModel, |
| 14 |
MultiPolygonModel, |
| 15 |
Nullable, |
| 16 |
OtherSridLocation, |
| 17 |
PointModel, |
| 18 |
PolygonModel, |
| 19 |
) |
| 20 |
|
| 21 |
__all__ = [ |
| 22 |
"LocationGeoSerializer", |
| 23 |
"PaginatedLocationGeoSerializer", |
| 24 |
"LocationGeoFeatureSerializer", |
| 25 |
"LocationGeoFeatureSlugSerializer", |
| 26 |
"LocationGeoFeatureFalseIdSerializer", |
| 27 |
"LocationGeoFeatureNoIdSerializer", |
| 28 |
"LocatedFileGeoFeatureSerializer", |
| 29 |
"BoxedLocationGeoFeatureSerializer", |
| 30 |
"LocationGeoFeatureBboxSerializer", |
| 31 |
"LocationGeoFeatureMethodSerializer", |
| 32 |
"NoneGeoFeatureMethodSerializer", |
| 33 |
"PointSerializer", |
| 34 |
"ChildPointSerializer", |
| 35 |
"ListChildPointSerializer", |
| 36 |
"LineStringSerializer", |
| 37 |
"PolygonSerializer", |
| 38 |
"MultiPolygonSerializer", |
| 39 |
"MultiLineStringSerializer", |
| 40 |
"MultiPointSerializer", |
| 41 |
"GeometrySerializerMethodFieldSerializer", |
| 42 |
"GeometrySerializer", |
| 43 |
"BoxedLocationGeoFeatureWithBBoxGeoFieldSerializer", |
| 44 |
"OtherSridLocationGeoSerializer", |
| 45 |
] |
| 46 |
|
| 47 |
|
| 48 |
class LocationGeoSerializer(serializers.ModelSerializer): |
| 49 |
"""location geo serializer""" |
| 50 |
|
| 51 |
details = serializers.HyperlinkedIdentityField(view_name="api_location_details") |
| 52 |
|
| 53 |
class Meta: |
| 54 |
model = Location |
| 55 |
fields = "__all__" |
| 56 |
|
| 57 |
|
| 58 |
class OtherSridLocationGeoSerializer(gis_serializers.GeoFeatureModelSerializer): |
| 59 |
"""Other SRID location geo serializer""" |
| 60 |
|
| 61 |
geometry = GeometryField(auto_bbox=True, transform=4326) |
| 62 |
|
| 63 |
class Meta: |
| 64 |
model = OtherSridLocation |
| 65 |
geo_field = "geometry" |
| 66 |
fields = "__all__" |
| 67 |
|
| 68 |
|
| 69 |
class PaginatedLocationGeoSerializer(pagination.PageNumberPagination): |
| 70 |
page_size_query_param = "limit" |
| 71 |
page_size = 40 |
| 72 |
max_page_size = 10000 |
| 73 |
|
| 74 |
|
| 75 |
class LocationGeoFeatureSerializer(gis_serializers.GeoFeatureModelSerializer): |
| 76 |
"""location geo serializer""" |
| 77 |
|
| 78 |
details = serializers.HyperlinkedIdentityField( |
| 79 |
view_name="api_geojson_location_details" |
| 80 |
) |
| 81 |
fancy_name = serializers.SerializerMethodField() |
| 82 |
|
| 83 |
def get_fancy_name(self, obj): |
| 84 |
return "Kool %s" % obj.name |
| 85 |
|
| 86 |
class Meta: |
| 87 |
model = Location |
| 88 |
geo_field = "geometry" |
| 89 |
fields = "__all__" |
| 90 |
|
| 91 |
|
| 92 |
class LocationGeoFeatureSlugSerializer(LocationGeoFeatureSerializer): |
| 93 |
"""use slug as id attribute""" |
| 94 |
|
| 95 |
class Meta: |
| 96 |
model = Location |
| 97 |
geo_field = "geometry" |
| 98 |
id_field = "slug" |
| 99 |
fields = ("name", "slug", "timestamp") |
| 100 |
|
| 101 |
|
| 102 |
class LocationGeoFeatureFalseIdSerializer(LocationGeoFeatureSerializer): |
| 103 |
"""id attribute set as False""" |
| 104 |
|
| 105 |
class Meta: |
| 106 |
model = Location |
| 107 |
geo_field = "geometry" |
| 108 |
id_field = False |
| 109 |
fields = "__all__" |
| 110 |
|
| 111 |
|
| 112 |
class LocationGeoFeatureNoIdSerializer(LocationGeoFeatureSerializer): |
| 113 |
""" |
| 114 |
id attribute left out, issue #82 |
| 115 |
see: https://github.com/openwisp/django-rest-framework-gis/issues/82 |
| 116 |
""" |
| 117 |
|
| 118 |
class Meta: |
| 119 |
model = Location |
| 120 |
geo_field = "geometry" |
| 121 |
fields = ("name",) |
| 122 |
|
| 123 |
|
| 124 |
class LocationGeoFeatureWritableIdSerializer(LocationGeoFeatureSerializer): |
| 125 |
"""default id attribute""" |
| 126 |
|
| 127 |
class Meta: |
| 128 |
model = Location |
| 129 |
geo_field = "geometry" |
| 130 |
fields = ("id", "name", "timestamp") |
| 131 |
|
| 132 |
id = serializers.CharField() |
| 133 |
|
| 134 |
|
| 135 |
class LocatedFileGeoFeatureSerializer(gis_serializers.GeoFeatureModelSerializer): |
| 136 |
"""located file geo serializer""" |
| 137 |
|
| 138 |
details = serializers.HyperlinkedIdentityField( |
| 139 |
view_name="api_geojson_located_file_details" |
| 140 |
) |
| 141 |
fancy_name = serializers.SerializerMethodField() |
| 142 |
file = serializers.FileField(allow_empty_file=True) |
| 143 |
|
| 144 |
def get_fancy_name(self, obj): |
| 145 |
return "Nice %s" % obj.name |
| 146 |
|
| 147 |
class Meta: |
| 148 |
model = Location |
| 149 |
geo_field = "geometry" |
| 150 |
exclude = [] |
| 151 |
|
| 152 |
|
| 153 |
class BoxedLocationGeoFeatureSerializer(gis_serializers.GeoFeatureModelSerializer): |
| 154 |
"""location geo serializer""" |
| 155 |
|
| 156 |
details = serializers.HyperlinkedIdentityField( |
| 157 |
view_name="api_geojson_boxedlocation_details" |
| 158 |
) |
| 159 |
|
| 160 |
class Meta: |
| 161 |
model = BoxedLocation |
| 162 |
geo_field = "geometry" |
| 163 |
bbox_geo_field = "bbox_geometry" |
| 164 |
fields = ["name", "details", "id", "timestamp"] |
| 165 |
|
| 166 |
|
| 167 |
class LocationGeoFeatureBboxSerializer(gis_serializers.GeoFeatureModelSerializer): |
| 168 |
class Meta: |
| 169 |
model = Location |
| 170 |
geo_field = "geometry" |
| 171 |
auto_bbox = True |
| 172 |
exclude = [] |
| 173 |
|
| 174 |
|
| 175 |
class LocationGeoFeatureMethodSerializer(gis_serializers.GeoFeatureModelSerializer): |
| 176 |
new_geometry = gis_serializers.GeometrySerializerMethodField() |
| 177 |
|
| 178 |
def get_new_geometry(self, obj): |
| 179 |
if obj.name.startswith("hidden"): |
| 180 |
return Point(0.0, 0.0) |
| 181 |
else: |
| 182 |
return obj.geometry |
| 183 |
|
| 184 |
class Meta: |
| 185 |
model = Location |
| 186 |
geo_field = "new_geometry" |
| 187 |
exclude = [] |
| 188 |
|
| 189 |
|
| 190 |
class NoneGeoFeatureMethodSerializer(gis_serializers.GeoFeatureModelSerializer): |
| 191 |
new_geometry = gis_serializers.GeometrySerializerMethodField() |
| 192 |
|
| 193 |
def get_new_geometry(self, obj): |
| 194 |
return None |
| 195 |
|
| 196 |
class Meta: |
| 197 |
model = Location |
| 198 |
geo_field = "new_geometry" |
| 199 |
fields = ["name", "slug", "id"] |
| 200 |
|
| 201 |
|
| 202 |
class NoGeoFeatureMethodSerializer(gis_serializers.GeoFeatureModelSerializer): |
| 203 |
class Meta: |
| 204 |
model = Nullable |
| 205 |
geo_field = None |
| 206 |
fields = ["name", "slug", "id"] |
| 207 |
|
| 208 |
|
| 209 |
class PointSerializer(gis_serializers.GeoFeatureModelSerializer): |
| 210 |
class Meta: |
| 211 |
model = PointModel |
| 212 |
geo_field = "location" |
| 213 |
fields = "__all__" |
| 214 |
|
| 215 |
|
| 216 |
class ChildPointSerializer(serializers.Serializer): |
| 217 |
point = PointSerializer() |
| 218 |
|
| 219 |
|
| 220 |
class ListChildPointSerializer(serializers.Serializer): |
| 221 |
points = PointSerializer(many=True) |
| 222 |
|
| 223 |
|
| 224 |
class LineStringSerializer(gis_serializers.GeoFeatureModelSerializer): |
| 225 |
class Meta: |
| 226 |
model = LineStringModel |
| 227 |
geo_field = "points" |
| 228 |
fields = "__all__" |
| 229 |
|
| 230 |
|
| 231 |
class PolygonSerializer(gis_serializers.GeoFeatureModelSerializer): |
| 232 |
class Meta: |
| 233 |
model = PolygonModel |
| 234 |
geo_field = "polygon" |
| 235 |
fields = "__all__" |
| 236 |
|
| 237 |
|
| 238 |
class PolygonModelSerializer(serializers.ModelSerializer): |
| 239 |
class Meta: |
| 240 |
model = PolygonModel |
| 241 |
fields = "__all__" |
| 242 |
|
| 243 |
|
| 244 |
class MultiPolygonSerializer(gis_serializers.GeoFeatureModelSerializer): |
| 245 |
class Meta: |
| 246 |
model = MultiPolygonModel |
| 247 |
geo_field = "polygon" |
| 248 |
fields = "__all__" |
| 249 |
|
| 250 |
|
| 251 |
class MultiLineStringSerializer(gis_serializers.GeoFeatureModelSerializer): |
| 252 |
class Meta: |
| 253 |
model = MultiLineStringModel |
| 254 |
geo_field = "points" |
| 255 |
fields = "__all__" |
| 256 |
|
| 257 |
|
| 258 |
class MultiPointSerializer(gis_serializers.GeoFeatureModelSerializer): |
| 259 |
class Meta: |
| 260 |
model = MultiPointModel |
| 261 |
geo_field = "points" |
| 262 |
fields = "__all__" |
| 263 |
auto_bbox = True |
| 264 |
|
| 265 |
|
| 266 |
class GeometrySerializer(gis_serializers.GeoFeatureModelSerializer): |
| 267 |
class Meta: |
| 268 |
model = GeometryModel |
| 269 |
geo_field = "points" |
| 270 |
fields = "__all__" |
| 271 |
auto_bbox = True |
| 272 |
|
| 273 |
|
| 274 |
class GeometrySerializerMethodFieldSerializer(PointSerializer): |
| 275 |
other_point = GeometrySerializerMethodField() |
| 276 |
|
| 277 |
@staticmethod |
| 278 |
def get_other_point(obj): |
| 279 |
return Point(obj.location.lat / 2, obj.location.lon / 2) |
| 280 |
|
| 281 |
class Meta: |
| 282 |
model = Location |
| 283 |
geo_field = "other_point" |
| 284 |
fields = "__all__" |
| 285 |
|
| 286 |
|
| 287 |
class BoxedLocationGeoFeatureWithBBoxGeoFieldSerializer( |
| 288 |
BoxedLocationGeoFeatureSerializer |
| 289 |
): |
| 290 |
class Meta(BoxedLocationGeoFeatureSerializer.Meta): |
| 291 |
fields = BoxedLocationGeoFeatureSerializer.Meta.fields + [ |
| 292 |
BoxedLocationGeoFeatureSerializer.Meta.bbox_geo_field |
| 293 |
] |