hancho
hancho

Reputation: 1437

GeoDjango - distance between a point and a queryset of multipolygons

I'm familiar with Django, but new to GeoDjango and PostGIS.

I have a problem where I want to find the nearest MuliPolygon to a point. The point can be outside or within the MultiPolygon. Nearest means the nearest boundary point.

I know that I can calculate the distance between two points with from django.contrib.gis.db.models.functions import Distance - but I don't want to use the centroid because it is possible the border of a MultiPolygon is closer to one point than the centroid of another.

I have a model Land with a field surface_area which is a MultiPolygon. I have a point object created with from django.contrib.gis.geos import Point. This is the data I'm using to try and build a query.

Any help with best practices would be appreciated.

Upvotes: 0

Views: 1416

Answers (1)

John Moutafis
John Moutafis

Reputation: 23134

GeoDjango's distance lookups utilize the corresponding database's distance function.

Since you are using PostgreSQL with PostGIS the corresponding ST_Distance method:

For geometry types returns the minimum 2D Cartesian (planar) distance between two geometries, in projected units (spatial ref units).

Therefore you can use the distance lookups for your calculations.

If you want to implement it a bit differently (for example using the bounding boxes of the polygons) you can refer to this Q&A: How do I get the k nearest neighbors for geodjango?

Upvotes: 1

Related Questions