Django - GeoDjango 以错误的顺序读取坐标

SbM*_*olo 7 python django geodjango coordinates leaflet

首先感谢您的帮助。

我正在用 Django 创建一个表单,它使用 OSMWidget 将坐标(多边形、线和点)保存到 PostgreSQL 数据库中的几何字段。它运行良好,我可以毫无问题地将信息保存在数据库中。当我使用 PgAdmin 进行查询时,我可以正确地看到 Leaflet 地图中显示的几何字段数据。

具有几何字段数据的 PgAdmin 地图示例.

以下是我在 forms.py 中的一些内容:

from django import forms
from django_select2 import forms as select2_forms
from django.contrib.gis import forms as osmforms

from django.forms import ModelForm
from .models import Dataset


class SessionForm(forms.ModelForm):

    at_choices = [(item.title, item.title) for item in Dataset.objects.all()]
    key_choices = [(item.keywords_d, item.keywords_d) for item in Dataset.objects.all()]

    uuid = forms.CharField(label='', max_length=10 , widget=forms.TextInput(attrs={'class': "form-control left-half"}))
    title = forms.CharField(label='Title', max_length=65536 , widget=forms.TextInput(attrs={'class': "form-control full-size-field"}))
    abstract = forms.CharField(label='Abstract', max_length=65536 , widget=forms.Textarea(attrs={'class': "form-control full-size-field", 'title': 'Your name'}))
    keywords_d = forms.MultipleChoiceField(label='Keywords', widget=select2_forms.Select2MultipleWidget(attrs={'class': "form-control left-half",'style': 'width:100%'}), choices=key_choices)
    activity_type = forms.MultipleChoiceField(label='Activity type', widget=select2_forms.Select2MultipleWidget(attrs={'class': "form-control right-half",'style': 'width:100%'}), choices=at_choices)
    related_site_we = forms.CharField(label='Related Site', max_length=256 , widget=forms.TextInput(attrs={'class': "form-control full-size-field"}))
    bounding_box = osmforms.GeometryCollectionField(label='Bounding Box', widget=osmforms.OSMWidget(attrs={'class': "form-control full-size-field",'map_width': 992, 'map_height': 500}))

    class Meta:
        model = Dataset
        fields = ['uuid','title','abstract','keywords_d','activity_type','related_site_we','bounding_box']
Run Code Online (Sandbox Code Playgroud)

这是views.py的一部分:

def editor(request):
    if request.method == 'GET':
        if request.GET['uuid'] != '0':
            session = Dataset.objects.get(uuid=request.GET['uuid'])
            form = SessionForm(instance=session)
        else:
            form = SessionForm()
        return render(request, 'form.html',
            {'form': form,})
Run Code Online (Sandbox Code Playgroud)

无需详细说明,表单的目的之一是部分填写,以便其他人以后可以对其进行编辑。编辑表单时,这会加载数据库中该条目的现有数据以及我们之前输入的坐标,这就是问题出现的地方,因为它似乎颠倒了纬度和经度的顺序,以这种方式出现:

编辑我们在上一张图片中看到的相同条目

刚才说了,坐标保存的很好,我觉得只是OSMWidget读取坐标的顺序有问题。有什么办法可以纠正这个问题吗?我已经阅读了几个小时的文档,并查看了 StackOverFlow 和其他论坛中的其他主题,但我找不到解决方案。

提前致谢

tgr*_*dje 6

我有同样的问题。

就我而言,这是由于 Django 和 GDAL 之间不兼容造成的,正如这里也提到的:如果您使用 GDAL 3,那么请务必使用 Django 3.1。

升级 Django 确实更正了 PointField 的 OSMWidget 和 OSMGeoAdmin。

我注意到您肯定有完全相同的配置问题,因为多重多边形似乎对我的应用程序不受影响......

注意: 我通常不会复制现有的票证作为 SO 的答案,但我只花了 2 天来弄清楚这一点(并找到正确的信息),这将有助于更多地引用这一点。