使用 Django 序列化程序在 Leaflet 地图上添加 geoJSON 数据时对象无效

Invalid object when adding geoJSON data on a Leaflet map with Django serializer

我正在使用 Leaflet 和 Postgresql / PostGIS 开发 Django 应用程序。当我尝试在发送 GeoJSON Feature Collection 对象的地图上添加 MultiLineString 图层时,它会引发无效对象错误。 一开始我的 views.py:

from geonode.geoloc.models import Transport
from django.template import RequestContext
from django.core.serializers import serialize

class LookupView(FormView):
    template_name = 'geoloc/lookupresults.html'
    form_class = LookupForm

    def get(self, request):
        return render_to_response('geoloc/lookup.html',      RequestContext(request))

def form_valid(self, form):
    # Get data
    latitude = form.cleaned_data['latitude']
    longitude = form.cleaned_data['longitude']

    # Look up roads
    roads =  Transport.objects.all()[0:5]

    roads_json = serialize('geojson', roads,
               fields=('geom',))

    # Render the template
    return self.render_to_response({
                              'roads_json': roads_json
                             }, content_type = 'json')

表单有效时我的模板:

{% extends "geoloc/geoloc_base.html" %}
{% block content %}
{% load leaflet_tags %}
{% leaflet_js %}
{% leaflet_css %}

<div id="mapid" style="width: 600px; height: 400px;"></div>
<script>
  var geojsonFeature = "{{ roads_json }}";
  var mymap = L.map('mapid').setView([51.505, -0.09], 13);
  L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
  attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'}).addTo(mymap);
  var myroads = L.geoJSON().addTo(mymap);
  myroads.addData(geojsonFeature);


 </script>
{% endblock %}

当我在 http://geojsonlint.com/ 中测试 geoJSON 对象(Django 序列化程序发送的)时,我意识到该对象是无效的,因为以下几行:

 "crs": {
"type": "name",
"properties": {
  "name": "EPSG:4326"
}

我读到的是 "an old-style crs member and is not recommended"。我是否要自定义序列化器的输出以便不提取上述行或者有更有效的方法来成功?

我按照以下步骤解决了上述问题:

  • 我在 Python 字典中转换了 geoJSON 对象
  • 我使用 pop() 方法从字典中删除了 'crs' 键
  • 我再次将字典转换为 geoJSON 对象

views.py中的修改代码:

roads =  Transport.objects.all()[0:5]

roads_json = serialize('geojson', roads,
           fields=('geom',))

new_roads_json = json.loads(roads_json)
new_roads_json.pop('crs', None)
new_roads_json = json.dumps(new_roads_json)

# Render the template
return self.render_to_response({
                     'new_roads_json': new_roads_json
                      }, content_type = 'json')

我遇到了同样的问题,我发现您可以而且应该完全删除 crs 行。 如前所述 here in RFC7946 所有 GeoJSON 坐标都被视为 WGS 84。

  1. Coordinate Reference System

The coordinate reference system for all GeoJSON coordinates is a
geographic coordinate reference system, using the World Geodetic
System 1984 (WGS 84) [WGS84] datum, with longitude and latitude units of decimal degrees. This is equivalent to the coordinate reference
system identified by the Open Geospatial Consortium (OGC) URN
urn:ogc:def:crs:OGC::CRS84. An OPTIONAL third-position element SHALL be the height in meters above or below the WGS 84 reference
ellipsoid. In the absence of elevation values, applications
sensitive to height or depth SHOULD interpret positions as being at
local ground or sea level.

Note: the use of alternative coordinate reference systems was
specified in [GJ2008], but it has been removed from this version of
the specification because the use of different coordinate reference
systems -- especially in the manner specified in [GJ2008] -- has
proven to have interoperability issues. In general, GeoJSON
processing software is not expected to have access to coordinate
reference system databases or to have network access to coordinate
reference system transformation parameters. However, where all
involved parties have a prior arrangement, alternative coordinate
reference systems can be used without risk of data being
misinterpreted.