6

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.

  • 3
    Not many features will contain a point at 99 degrees south. It helps if you always think in terms of X,Y order, and even write it "longitude and latitude". – Vince May 15 '16 at 19:56
  • but i'm doing an intersection, surely any point within the USA will intersect with the USA polygon. so are points longitude and latitude? or are they some abitrary x,y coordinate system in geodjango – Dr Manhattan May 16 '16 at 6:16
  • What Vince is saying is that -99 latitude does not exist. The South Pole is -90. You have reversed the coordinates in your two examples. – John Powell May 16 '16 at 9:10
  • Please see updated question – Dr Manhattan May 16 '16 at 14:57
12

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.

  • 1
    so, if you're coming from google maps, and experienced to latlng objects and the like, x and y are like switched. this caused my some headache! – benzkji Dec 15 '18 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.