Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get coordinates from Point Django

I use class with PointField to store coordinates in database.

# models.py
from django.contrib.gis.db import models


class MapPoint(models.Model):
    p = models.PointField()

    def __str__(self):
        return self.p

Coordinates I get from template using AJAX.

# views.py

def mapper(request):
    if request.method == "GET":
        if request.is_ajax():
            lat = float(request.GET.get('lat'))
            lng = float(request.GET.get('lng'))
            pnt = Point(lat, lng)
            MapPoint.objects.create(p=pnt)

    return render(request, 'map_in.html')

Then I want to show all points in other template

# views.py

def mapper_done(request):
    query = MapPoint.objects.all().values()
    out_list = list(query)
    return render(request, 'map_out.html', {'out_list': out_list})

It returns:

{'id': 1, 'p': <Point object at 0x7f42c712b670>}

How can I get coordinates from Point? I want something like this:

ID: 1, lat: 42.326565 lng: 52.325874

As I see, I need to iterate through querySet. But how?

And sorry for bad English:)

like image 477
Dmitry Avatar asked Jul 17 '26 04:07

Dmitry


1 Answers

>>> pnt = Point(5, 23)
>>> [coord for coord in pnt]
[5.0, 23.0]
>>> pnt.coords
(5.0, 23.0)

add your variable pnt = Point(your point variable)

like image 168
Gorkhali Khadka Avatar answered Jul 18 '26 17:07

Gorkhali Khadka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!