Django - GeoDjango read coordinates in the wrong order
Asked Answered
S

2

9

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

Sickening answered 9/6, 2020 at 16:17 Comment(0)
W
6

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.

Wyler answered 3/3, 2021 at 13:6 Comment(0)
C
-1

based on: similar question Use Property in model and change the order of Y,X to 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]
Colwen answered 1/12, 2020 at 9:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.