Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - GeoDjango read coordinates in the wrong order

first of all thanks for your help.

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.

Example of PgAdmin map with the geometric field data.

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']

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:

Editing the same entry we saw in the previous image

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.

Thanks in advance

like image 408
SbManolo Avatar asked Jun 09 '20 16:06

SbManolo


1 Answers

I had the same problem.

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.

Upgrading Django did correct both OSMWidget and OSMGeoAdmin for PointField.

I'm note sure you have exactly the same configuration problem though, as multipolygons seemed unaffected on my app...

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.

like image 180
tgrandje Avatar answered Oct 19 '22 05:10

tgrandje