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

Django - GeoDjango read coordinates in the wrong order

首先感谢您的帮助。

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

这是我 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']

这是 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,})

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

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

提前致谢

基于: 在模型中使用 属性 并将 Y,X 的顺序更改为 X,Y

class Line(models.Model):
"""
    _____________   line in the map
"""

line = models.LineStringField(blank=False, null=False)

  def __str__(self):
    return str(self.line)

  @property
  def coordinates(self):
    
    return [list([coords_pair[1],coords_pair[0]]) for coords_pair in   self.line.coords]

我遇到了同样的问题。

在我的例子中,这是由于 Django 和 GDAL 之间的不兼容,正如也提到的那样here:如果您使用的是 GDAL 3,那么一定要使用 Django 3.1。

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

我注意到您确实遇到了完全相同的配置问题,因为多面体在我的应用程序上似乎没有受到影响...

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