【问题标题】:Django - GeoDjango read coordinates in the wrong orderDjango - GeoDjango 以错误的顺序读取坐标
【发布时间】:2020-06-09 16:17:28
【问题描述】:

首先感谢您的帮助。

我正在使用 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读取它们时坐标顺序的问题。有什么办法可以纠正这个吗?我已经阅读了几个小时的文档,并查看了 StackOverFlow 和其他论坛中的其他主题,但我找不到解决方案。

提前致谢

【问题讨论】:

    标签: python django leaflet coordinates geodjango


    【解决方案1】:

    我遇到了同样的问题。

    在我的情况下,这是由于 Django 和 GDAL 之间的不兼容,here 也提到过:如果您使用的是 GDAL 3,那么请务必使用 Django 3.1。

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

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

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

    【讨论】:

      【解决方案2】:

      基于: similar question 在模型中使用 Property 并将 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]
      

      【讨论】:

        猜你喜欢
        • 2020-11-06
        • 1970-01-01
        • 1970-01-01
        • 2019-08-11
        • 1970-01-01
        • 2018-09-25
        • 2022-10-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多