I searched long and far for a solution that was relevant and specific to my problem, but so far I have not found a solution or clear documentation on what I really need to do in order to smooth out relations in order to become compatible with geojson.
This question is almost identical to mine, however, solutions or answers do not solve the problem and still creates invalid GeoJSON.
Related
Problem
I have a model Locationthat contains pointfieldwith SRID = 4326. I also have a model TrainStationthat has a Locationfield as a foreign key for Location. When I serialize TrainStationthrough GeoFeatureModelSerializer, it throws an invalid GeoJSON (see the example below "invalid geojson").
(A valid GeoJSON can, of course, be obtained if I store it somewhere pointfieldin the model TrainStation, but in my case I cannot do this, so I need to smooth it out somehow.)
Question
- How to achieve a result, for example, the example "Valid GeoJSON" below?
Research
Python, Django, , , , - to_representation(), , , , .
models.py
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
{
"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
, .
{
"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"
}
}
]
}