Django-GeoDjango以错误的顺序读取坐标 [英] Django - GeoDjango read coordinates in the wrong order

查看:37
本文介绍了Django-GeoDjango以错误的顺序读取坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先感谢您的帮助.

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

I'm making a form with Django which uses the OSMWidget to save coordinates (Polygons, Lines and Points) to a Geometry field in a PostgreSQL database. It works well, I can save the information in the database without any problem. And when I make a query with PgAdmin I can see the geometric fields data displayed in a Leaflet map correctly.

.

这是我表格中的一些内容.py:

Here's some of what I have in my 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的一部分:

And this is part of the 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,})

不赘述过多,表单的目的之一是部分填写表单,以便其他人以后可以编辑它.编辑表单时,这会将数据库中该条目的现有数据以及我们先前输入的坐标加载到数据库中,这就是问题所在,因为它似乎颠倒了纬度和经度的顺序,显示方式如下:

Without going into too much detail, one of the purposes of the form is to partially fill it out so that others can edit it later. When editing the form, this loads the existing data in the database for that entry, along with the coordinates we have previously entered, and this is where the problem appears, as it seems to reverse the order of latitude and longitude, appearing this way:

正如我所说,坐标存储得很好,当OSMWidget读取它们时,我认为这只是坐标顺序的问题.有什么办法可以纠正这个问题?我已经阅读了数小时的文档,并在StackOverFlow和其他论坛中回顾了其他线程,但我找不到解决方案.

As I said, the coordinates are stored well, I think it's just a problem in the order of the coordinates when OSMWidget reads them. Is there any way to correct this? I've been reading documentation for hours, as well as reviewing other threads in StackOverFlow and other forums, and I can't find a solution to this.

预先感谢

推荐答案

我遇到了同样的问题.

就我而言,这是由于Django和GDAL之间的不兼容,也曾在此处:如果您使用的是GDAL 3,请确保使用Django 3.1.

In my case, it was due to incompatibility between Django and GDAL, as has been also mentionned here : if you are using GDAL 3, then be sure to use Django 3.1.

升级Django确实纠正了PointField的OSMWidget和OSMGeoAdmin.

Upgrading Django did correct both OSMWidget and OSMGeoAdmin for PointField.

我要确保您确实遇到了相同的配置问题,因为多多边形在我的应用中似乎没有受到影响...

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

Note : I wouldn't normally reproduce an existing ticket as an answer on SO, but I just spent 2 days figuring this out (and finding the right information) and it will help to have this more referenced.

这篇关于Django-GeoDjango以错误的顺序读取坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆