0

I loaded some Shapefile multipolygons into Geodjango to show them as a layer on a Leaflet map. But on the website, no layer appears, just the map itself.

The geometry data are stored in the Geodjango database like so:

from django.contrib.gis.db import models

class wgo(models.Model):
    (some more variables)
    poly = models.MultiPolygonField(srid=4326)

I pass the multipolygons with geojson and serialize like so:

wcrds = wgo.objects.filter(id=wid)
gridone = serialize('geojson', wcrds.all())

return render(request, 'result.html', {'gridone': gridone})

And when I inspect the page I can see that the geojson data indeed makes it to the html:

var Hlayer = new L.GeoJSON(

    {"type": "FeatureCollection", "crs": {"type": "name", "properties": {"name": "EPSG:4326"}}, "features": [{"type": "Feature", "properties": {"wijkcode": "WK036356", "wijknaam": "Middenmeer", "poly": "SRID=4326;MULTIPOLYGON (((4.93714288723798 52.3576900936898, 4.93742729807085 52.3577390397678,)))", "pk": "909"}, "geometry": null}]} 
    , {
        style: Hstyle
    }
);

var mymap = L.map('mapid').setView([52.3701, 4.8967], 13);

var OpenStreetMap_Mapnik = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  maxZoom: 19,
  attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(mymap);

(I took out most of the coordinates for brevity's sake). It looks like geojson did not pick up on the fact that the poly variable is the one with the geometry.

I see geojson coordinates with square brackets around them on example sites, as opposed to the round ones on mine.

I imported the shapefile directly via Geodjango's Layer Mapping, following the Geodjango tutorial. Also, I viewed and manipulated this Shapefile with ogrinfo, ogr2ogr and, in Python, GDAL osgeo without any problems.

Any idea how I might get Geodjango and geojson to pass the coordinates in the right format to Leaflet? Thank you in advance.

MSDMMM
  • 41
  • 5

1 Answers1

0

My mistake here. When serializing geojson, you should obviously tell it which variable contains geometry. Instead of

gridone = serialize('geojson', wcrds.all())

Made it:

gridone = serialize('geojson', wcrds.all(), geometry_field='poly')

And it works. Read the docs, as they say...

MSDMMM
  • 41
  • 5