[GIS] DatabaseError: Coordinate values are out of range [-180 -90, 180 90] for GEOGRAPHY type

geodjangopostgisspatial-database

I am using Django 1.3, PostGIS 2.0 , Postgresql 9.0 and GeoDjango.

I had a database with a table for places. This table had latitude and longitude for places in the US. I enabled this database with PostGIS. I added PointField to my model. Then, updated this table with

SELECT AddGeometryColumn('table', 'poly', 4326, 'POINT', 2)

CREATE INDEX "table_poly_id" ON "table" USING GIST ( "poly" )

UPDATE table
SET poly = ST_PointFromText('POINT(' || longitude ||' '|| latitude ||')', 4326)

Now, I want to make query to find places within 5 miles of given latitude and longitude of a user's location.
So, I did this

ref_pnt = fromstr("POINT(%s %s)" % ("-87.627778", "41.881944"))
records = Table.objects.filter(poly__distance_lte = (ref_pnt,D(mi=5)))

But, I am getting an error

DatabaseError: Coordinate values are out of range [-180 -90, 180 90] for GEOGRAPHY type

I also get an error when I do this

SELECT ST_PointFromText(poly) FROM table 

ERROR:  parse error - invalid geometry
HINT:  "0101000020E61000007E" <-- parse error at position 20 within geometry
CONTEXT:  SQL function "st_pointfromtext" statement 1

What could be the problem?

Best Answer

I had a similar problem, in my case, I was storing coordinates inverted.

In WGS84 and postgis, the order in point is longitude, latitude.

In Django, the point object is like: Point(x=longitude, y=latitude)