简体   繁体   English

Geodjango + PostGIS。 聚合多边形面积(以平方米为单位)

[英]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.我正在使用 geodjango(3.1) + postgis,我想接收以平方米为单位的多边形面积。 Therefore, I am using the Geographic Database Functions of Django .因此,我正在使用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.当我以相同的方式使用距离 function时,我得到了以平方米为单位的正确结果。

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.当我将ST_Area(geom, use_spheroid=true)作为 RawSQL 执行时,结果也适合并且以平方米为单位,但我希望避免使用 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.请注意,如果您geom字段存储为地理类型,则Area function 应返回平方米而不是度数。

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)", []))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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