-
Notifications
You must be signed in to change notification settings - Fork 201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement custom feature properties source #71
Conversation
By default, when converting a django model instance with geometry to GeoJSON, the "properties" field will be filled with the fields defined in the corresponding serializer. In this commit we separate the code a bit in individual methods, which can be overriden by any subclass. In this way the user is able to define their own source for the properties field of the GeoJSON output.
Sample usage: class NetworkGeoSerializer(GeoFeatureModelSerializer):
class Meta:
model = models.Link
geo_field = 'geo'
auto_bbox = True
def get_feature_properties(self, instance):
return instance.metadata # This is a PostgreSQL HStore field, which django maps to a dict
def unformat_geojson(self, feature):
attribs = {
self.Meta.geo_field: feature["geometry"],
"metadata": feature["properties"]
}
if self.Meta.bbox_geo_field and "bbox" in feature:
attribs[self.Meta.bbox_geo_field] = Polygon.from_bbox(
feature["bbox"])
return attribs |
Feels like this functionality belongs to |
Hmm true, but this is still a little bit different, because we still have the geometry (and optionally the bounding box) which is part of the model/serializer. |
👍 I did not like the nested function declarations, a few months ago I also thought that they could make customization of the serializer more cumbersome. For the moment I'll only be able to ask you 2 things:
In a couple of weeks I'll test the feature properly and merge it if it's ready. |
if not field.write_only: | ||
value = field.get_attribute(instance) | ||
ret["bbox"] = (value.extent if hasattr(value, 'extent') else | ||
None) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
generally I do not like to enforce strict line breaking because in instances like this one I have a hard time reading the code. Could you please put None
on the line above?
Sorry for the delay, but those things you've mentioned should be fixed now! :) |
Thank you @sh4wn, I merged this manually in 5a82a00. I needed to change a few bits in order to avoid losing some performance gains I managed to introduce in the latest versions, but the behaviour you introduced should be unchanged (except for the name of the method Please test 0.9.5 alpha in your app and report any feedback. |
By default, when converting a django model instance with geometry
to GeoJSON, the "properties" field will be filled with the fields
defined in the corresponding serializer.
In this PR we separate the code a bit in individual methods,
which can be overridden by any subclass. In this way the user is
able to define their own source for the properties field of the
GeoJSON output.
Solves #70