makinacorpus / django-leaflet

Use Leaflet in your Django projects
GNU Lesser General Public License v3.0
708 stars 281 forks source link

Why are lat/long reversed in widget #203

Open jhotujec opened 6 years ago

jhotujec commented 6 years ago

So my understanding is that leaflet.js expects coordinates in order of lat/long but the widget is saving and displaying them in x,y.

I'm using SRID 4326 in my database and when i save the geometry with django-leaflet widget the coordinates are reversed.

Is there any way to change this?

EDIT Even when specifically setting SRID in widget it doesn't seem to work.

widgets = {
            'garage': LeafletWidget(attrs={
                'settings_overrides': {
                    'SRID': 4326,
                }
            }),
        }

SO: link

Thanks

Gagaro commented 6 years ago

The widget save the geometries as GeoJSON ones. GeoJSON uses the coordinates as x/y ones.

jhotujec commented 6 years ago

@Gagaro thanks for the explanation. Is there a quick way to change this without having to rewrite huge chunks of the widget?

Gagaro commented 6 years ago

What issue do you have exactly? The default SRID should be 4326. How do you use the data afterwards ?

jhotujec commented 6 years ago

My issue is that, as I understand, according to SRID4326 geographical data is supposed to be saved in order of lat/long and with the widget coordinates are reversed. That leads to having to reverse Point coordinates everytime I wanna do some GeoDjango lookups (for example checking if Polygon contains Point)

This is how I initiate widget (forms.py)

    widgets = {
       'garage': LeafletWidget(),
       'airports': LeafletWidget(),
    }

This is how they are defined (models.py)

    garage = models.PointField(default=(40, 40))
    airports = models.MultiPolygonField(null=True)

So when data is saved the coordinates are (x,y) instead of (lat, long) in the database. I'm using default settings for django-leaflet - not overriding anything.

Thanks!

Gagaro commented 6 years ago

They should be stored correctly in the DB. How do you do the GeoDjango lookup?

jhotujec commented 6 years ago

Hm.. well for example, saving London in the database looks like this: SRID=4326;POINT(-0.118092 51.509865) but lat/long of London is 51.509865/-0.118092. But what's interesting is that the widget represent location correctly.

Database (field structure)

screen shot 2017-09-23 at 00 26 12

(value in db)

screen shot 2017-09-23 at 00 25 27

Rendered widget in HTML

screen shot 2017-09-23 at 00 22 18

For GeoDjango lookup im doing <Polygon>.contains(<Point>) but because the coordinates are reversed it returns False

Gagaro commented 6 years ago

Because they are stored as x y coordinates as well. Where do you get your polygon? Are you sure it's not the one having reversed coordinates?