问题描述
我一直在寻找解决方案,该解决方案是最新的并且针对我的问题,但是还没有找到解决方案或清晰的文档来证明我真正需要做的事情,以使关系扁平化以符合geojson规范
I have searched long and far for a solution that is up to date and specific to my problem but have yet not found a solution or a clear documentation on what I really need to do in order to flatten a relationship to become geojson compliant.
这个问题与我的几乎相同,但是解决方案或答案无法解决问题,仍然会产生无效的GeoJSON.
This question is almost identical to mine, however the solutions or answersdoes not solve the problem and still produces invalid GeoJSON.
- Set serializer geo_field as PointField from another model - Django
- Django REST Framework: define fields in nested object?
我有一个Location模型,其中装有SRID = 4326的pointfield.我也有一个TrainStation模型,该模型具有location字段作为的外键.当我通过GeoFeatureModelSerializer序列化TrainStation时,它将生成无效的GeoJSON(请参见无效的geojson"下面的示例).
I have a Location model that holds a pointfield with SRID=4326. I also have a TrainStation model that has location field as foreign key to Location. When I serialize the TrainStation via the GeoFeatureModelSerializer it produces invalid GeoJSON (see example below "invalid geojson").
(如果我将PointField存储在TrainStation模型中,当然可以获得有效的GeoJSON,但就我而言,我不能这样做,所以我需要以某种方式对其进行展平.)
(Valid GeoJSON can, of course, be obtained if I where to store the PointField in the TrainStation model, but in my case, I cannot do that so I need to flatten it somehow.)
- 如何实现类似下面的"Valid GeoJSON"示例的输出?
我是Python和Django的新手,因此我还不太擅长阅读其他人的源代码,但是我想我可以得出结论,我需要以某种方式重写to_representation()方法以获取我想要的东西,但到目前为止,我的搜索无济于事,所以被困了.
I am a newcomer to both Python and Django thus I am yet not very good at reading other people's source code, however I think I can conclude that I need to somehow override the to_representation() method in order to get what I want, but my searches are so far fruitless so I am stuck.
class Location(models.Model): point = models.PointField() class TrainStation(models.Model): location_signature = models.CharField(primary_key=True, max_length=32) advertised_location_name = models.CharField(max_length=32) country_code = models.ForeignKey(Country) county_no = models.ForeignKey(County) location = models.ForeignKey(Location, null=True)
serializers.py
class LocationSerializer(ModelSerializer): class Meta: model = Location geo_field = 'point' fields = [ 'point', ] class TrainStationSerializer(GeoFeatureModelSerializer): location_signature = PrimaryKeyRelatedField(read_only=True) location = LocationSerializer(read_only=True) country_code = StringRelatedField(read_only=True) county_no = StringRelatedField(read_only=True) class Meta: model = TrainStation geo_field = 'location' fields = [ 'location_signature', 'advertised_location_name', 'country_code', 'county_no', ]
GeoJSON输出示例:
我已经验证了 http://geojson.io 上的输出,以确定其是否有效.
GeoJSON Output examples:
I have verified the output on http://geojson.io to determine if its valid or not.
{ "type": "FeatureCollection", "features": [ { "id": "Ak", "type": "Feature", "geometry": { "point": { <------+------ offending lines "type": "Point", | "coordinates": [ | 18.8303462142963, | 68.3486410812835 | ] | } <------+ }, "properties": { "advertised_location_name": "Abisko Östra", "country_code": "Sverige", "county_no": "Norrbottens län" } } ] }
有效的GeoJSON
这是我正在寻找的输出.
Valid GeoJSON
This is the output I am looking for.
{ "type": "FeatureCollection", "features": [ { "id": "Ak", "type": "Feature", "geometry": { "type": "Point", "coordinates": [ 18.8303462142963, 68.3486410812835 ] }, "properties": { "advertised_location_name": "Abisko Östra", "country_code": "Sverige", "county_no": "Norrbottens län" } } ] }
推荐答案
我现在使用以下代码解决了这个问题:
I have now solved this issue with the following code:
class LocationSerializer(ModelSerializer): def to_representation(self, obj): representation = super().to_representation(obj) point_representation = representation.pop('point') for key in point_representation: representation[key] = point_representation[key] return representation class Meta: model = Location geo_field = 'point' fields = [ 'point', ]
这确实会产生有效的GeoJSON:
And this does indeed produce valid GeoJSON:
{ "type": "FeatureCollection", "features": [ { "id": "Ak", "type": "Feature", "geometry": { "type": "Point", "coordinates": [ 18.8303462142963, 68.3486410812835 ] }, "properties": { "advertised_location_name": "Abisko Östra", "country_code": "Sverige", "county_no": "Norrbottens län" } } ] }
如果有人对此有任何投入,请随时做出贡献并添加答案:-)
If someone has any inputs on this feel free to contribute and add an answer :-)
这篇关于如何使用django-rest-framework-(gis)压平外键对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!