Geodjango – How to Convert Latitude and Longitude to Point in GeoDjango?

geodjango

I am doing the geodjango tutorial and I went on google and chose a random longitude and latitude in Kansas, I am using srid=4326 for my polygons

So I got this random lat and long on google maps in Kansas USA

lat, long = 38.670049, -99.565798

Now I am trying to see where in the world it is

>>> from django.contrib.gis.geos import Point
>>> pnt = Point(38.670049, -99.565798)
>>> WorldBorder.objects.get(mpoly__intersects=pnt)
...
...
world.models.DoesNotExist: WorldBorder matching query does not exist.

Using the tutorial point I get

>>> pnt = Point(-95.3385, 29.7245)
>>> WorldBorder.objects.get(mpoly__intersects=pnt)
<WorldBorder: United States>

So my database is imported and working.

So clearly Points are not the same as latitude and longitude, how do I convert between the two


It seems google maps has the lat/long the other way around, see coordinates in image below at the bottom from google maps:

enter image description here

# Try enter the coords in the same order as in image above
# Get wrong intersection
>>> from django.contrib.gis.geos import Point
>>> from world.models import WorldBorder
>>> pnt = Point(38.907636 , -77.036905)
>>> WorldBorder.objects.get(mpoly__intersects=pnt)
<WorldBorder: Antarctica>


# Swap them around, and it works
>>> pnt = Point(-77.036905, 38.907636)
>>> WorldBorder.objects.get(mpoly__intersects=pnt)
<WorldBorder: United States>

Can anyone explain what is going on exactly. For a Point(x,y) which is the longitude and which is the latitude, is it

Point(x,y)

x=longitude

y=latitude

And in which order does Google maps present them in the picture above

I am not a GIS specialist or even novice.

Best Answer

Straight from the documentation:

Another option is to use the constructor for the specific geometry type that you wish to create. For example, a Point object may be created by passing in the X and Y coordinates into its constructor:

> >>> from django.contrib.gis.geos import Point
> >>> pnt = Point(5, 23)

Note "X and Y coordinates". X is longitude, Y is latitude. In this example 5 is longitude and 23 is latitude.