简体   繁体   中英

Geodjango + PostGIS. Aggregate polygon area in square meters

I am using geodjango(3.1) + postgis and I want to receive the area of a polygon in square meteres. Therefore, I am using the Geographic Database Functions of Django .

My code looks the following:

City.objects.annotate(area=Area('geom'))

I think the result I am reciving is in degree. When I use the distance function the same way, I get the right result in square meters.

When I am executing ST_Area(geom, use_spheroid=true) as RawSQL, the result also fits and is in square meteres but I would like the avoid RawSQL.

Thanks for any help =)

Note that if you store your geom field as a geography type , the Area function should return square meters instead of degrees.

from django.contrib.gis.db import models as gis_models

class City(gis_models.Model):
    geom = gis_models.PolygonField(geography=True)

…
cities = City.objects.annotate(area=Area('geom'))
cities[0].area.sq_m  # square meters

This worked for me:

City.objects.annotate(area=RawSQL("ST_AREA(geom, true)", []))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM