5๐
โ
You can use a combination of the following methods:
AsGeoJSON
, which
Accepts a single geographic field or expression and returns a GeoJSON representation of the geometry.
Centroid()
which
Accepts a single geographic field or expression and returns the centroid value of the geometry.
.annotate()
which
Annotates each object in the QuerySet with the provided list of query expressions.
[โฆ]
Each argument toannotate()
is an annotation that will be added to each object in the QuerySet that is returned.
Example:
The following query:
Polygon.objects.annotate(geometry=AsGeoJSON(Centroid('coordinates')))
will add a field named 'geometry'
to the Polygon
queryset which will contain the centroid calculated from the coordinates
field of every Polygon
object of your given model.
๐คJohn Moutafis
Source:stackexchange.com