1

I've created a simple model like this:

class AddPoint(models.Model):
    name = models.CharField(max_length=100)
    geom = geomodels.PointField(srid=3857)

    def __str__(self):
        return self.name

With this view I export the db contents as GeoJSON:

from django.core.serializers import serialize

def addPointGeoJson(request):
    vectors = serialize(
        'geojson',
        AddPoint.objects.all(),
        )
    content_type = 'json'
    return HttpResponse(vectors, content_type)

When I see the exported GeoJSON the SRID is not 3857 but 4326. Why happen this?

{"type": "FeatureCollection", "crs": {"type": "name", "properties": {"name": "EPSG:4326"}}, "features": [{"type": "Feature", "properties": {"name": "Napoli", "publishing_date": "2019-07-27T18:12:36.831", "updating_date": "2019-07-27T18:12:36.843", "timestamp": "2019-07-27T18:12:36.843", "pk": "30"}, "geometry": {"type": "Point", "coordinates": [14.291931129992006, 40.84290487729676]}}]}

I know that GeoJSON Serializer use 4326 as default SRID but I expected it to change to 3857 as in the model.

  • 2
    GeoJSON isn't valid GeoJSON unless it uses longitude,latitude pairs. – Vince Jul 27 at 16:55
  • What do you mean? – Massimiliano Moraca Jul 27 at 20:53
  • 1
    GeoJson standard deprecated CRS tag, and the only standard compliant coordinate system is 4326. – Michael Jul 27 at 23:36
  • Here there are the specification and I read that the crs is necessary geojson.org/… – Massimiliano Moraca Jul 28 at 6:32
1

Pass SRID as option in serialize

from django.core.serializers import serialize

def addPointGeoJson(request):
    vectors = serialize(
        'geojson',
        AddPoint.objects.all(),
        srid= 'EPSG:3857',
        )
    content_type = 'json'
    return HttpResponse(vectors, content_type)

`

  • For me was not clear that SRID must be put after the query. I've already tried to put it inside the serialize but before AddPoint.objects.all() and I seen an error: SyntaxError: positional argument follows keyword argument – Massimiliano Moraca Jul 27 at 21:01
  • I've noticed a trouble with your solution. Now addPointGeoJson give %d format: a number is required, not str – Massimiliano Moraca Jul 27 at 21:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.