sph*_*rak 5 django geodjango geojson django-rest-framework django-rest-framework-gis
我一直在寻找一个最新且特定于我的问题的解决方案,但是还没有找到解决方案或明确的文档来说明我真正需要做的事情,以便使关系变得平坦以符合geojson.
这个问题几乎与我的问题相同,但是解决方案或答案并没有解决问题,仍然会产生无效的GeoJSON.
我有一个Location模型,其中包含pointfieldSRID = 4326.我也有一个TrainStation有模型location字段作为外键来Location.当我序列化TrainStation经由GeoFeatureModelSerializer其产生无效GeoJSON的(参见实施例下面的"无效GeoJSON的").
(当然,如果我PointField将TrainStation模型存储在哪里,可以获得有效的GeoJSON ,但在我的情况下,我不能这样做,所以我需要以某种方式压扁它.)
我是Python和Django的新手,因此我还不太擅长阅读其他人的源代码,但我想我可以得出结论,我需要以某种方式覆盖该to_representation()方法以获得我想要的东西,但我的搜索是如此没有结果,所以我被卡住了.
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)
Run Code Online (Sandbox Code Playgroud)
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',
]
Run Code Online (Sandbox Code Playgroud)
我已经验证了http://geojson.io上的输出,以确定它是否有效.
{
"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"
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
这是我要找的输出.
{
"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"
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
我现在已经用以下代码解决了这个问题:
\n\nclass LocationSerializer(ModelSerializer):\n\n def to_representation(self, obj):\n\n representation = super().to_representation(obj)\n point_representation = representation.pop(\'point\')\n for key in point_representation:\n representation[key] = point_representation[key]\n\n return representation\n\n class Meta:\n model = Location\n geo_field = \'point\'\n fields = [\n \'point\',\n ]\nRun Code Online (Sandbox Code Playgroud)\n\n这确实产生了有效的 GeoJSON:
\n\n{\n "type": "FeatureCollection",\n "features": [\n {\n "id": "Ak",\n "type": "Feature",\n "geometry": {\n "type": "Point",\n "coordinates": [\n 18.8303462142963,\n 68.3486410812835\n ]\n },\n "properties": {\n "advertised_location_name": "Abisko \xc3\x96stra",\n "country_code": "Sverige",\n "county_no": "Norrbottens l\xc3\xa4n"\n }\n }\n ]\n}\nRun Code Online (Sandbox Code Playgroud)\n\n如果有人对此有任何意见,请随时贡献并添加答案:-)
\n| 归档时间: |
|
| 查看次数: |
531 次 |
| 最近记录: |