Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django postgresql postgis incorrect spatial lookup

I have the following model:

class Item(Model):
    lat_lng = PointField(geography=True, null=True)

Item.objects.create(lat_lng=Point(-95.864468, 36.075450))
bbox1 = (-168.3984375, 25.16517336866393, -52.03125, 75.32002523220804)
bbox2 = (-145.1953125, 25.16517336866393, -52.03125, 63.07486569058663)

bbox1 contains bbox2, but Item.objects.filter(lat_lng__coveredby=Polygon.from_bbox(bbox1)) doesn't return the item, while Item.objects.filter(lat_lng__coveredby=Polygon.from_bbox(bbox2)) returns it.

Am I missing something?

UPDATE:

If I remove geographic=True, then everything works. What does this flag stands for?

like image 323
Andrew Fount Avatar asked Feb 02 '26 02:02

Andrew Fount


1 Answers

When using geography, great circle arcs are used instead of straight lines. This remains true for the lines connecting the bounding box corners.

The premise that box1 contains box2 is false. They may intersect, but one is not wholly contained in the other.

Box1 is wider than box 2, so its southern boundary, represented as a great circle arc, will go norther than box 2 boundary, and therefore the point is located south of box 1 but within box 2.

like image 131
JGH Avatar answered Feb 03 '26 19:02

JGH