Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions rest_framework_gis/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,16 @@ def to_representation(self, instance):
"""
# prepare OrderedDict geojson structure
feature = OrderedDict()
# the list of fields that will be processed by get_properties
# we will remove fields that have been already processed
# to increase performance on large numbers
fields = list(self.fields.values())

# keep track of the fields being processed
processed_fields = set()

# optional id attribute
if self.Meta.id_field:
field = self.fields[self.Meta.id_field]
value = field.get_attribute(instance)
feature["id"] = field.to_representation(value)
fields.remove(field)
processed_fields.add(self.Meta.id_field)

# required type attribute
# must be "Feature" according to GeoJSON spec
Expand All @@ -115,7 +114,8 @@ def to_representation(self, instance):
field = self.fields[self.Meta.geo_field]
geo_value = field.get_attribute(instance)
feature["geometry"] = field.to_representation(geo_value)
fields.remove(field)
processed_fields.add(self.Meta.geo_field)

# Bounding Box
# if auto_bbox feature is enabled
# bbox will be determined automatically automatically
Expand All @@ -126,7 +126,15 @@ def to_representation(self, instance):
field = self.fields[self.Meta.bbox_geo_field]
value = field.get_attribute(instance)
feature["bbox"] = value.extent if hasattr(value, 'extent') else None
fields.remove(field)
processed_fields.add(self.Meta.bbox_geo_field)

# the list of fields that will be processed by get_properties
# we will remove fields that have been already processed
# to increase performance on large numbers
fields = [
field_value for field_key, field_value in self.fields.items()
if field_key not in processed_fields
]

# GeoJSON properties
feature["properties"] = self.get_properties(instance, fields)
Expand Down