简体   繁体   English

GeoDjango多边形区域

[英]GeoDjango polygon area

I have implemented GeoDjango using postgis.我已经使用 postgis 实现了 GeoDjango。

Here is my model:这是我的模型:

  ...
  geometria = models.PolygonField(srid=4326, null=True)
  ...

When I call data.area it returns a float, but I don't have any clues about it's measurement units, and it's a problem because I want to test if it's bigger of a pre-set area in squared meters.当我调用data.area 时,它返回一个浮点数,但我对它的测量单位没有任何线索,这是一个问题,因为我想测试它是否比以平方米为单位的预设区域更大。

Can you help me?你能帮助我吗?

If you are dealing with large areas on the map, you should set如果您正在处理地图上的大面积区域,您应该设置

geometria = models.PolygonField(srid=4326, null=True, geography=True)

As mentioned in geodjango's documentation https://docs.djangoproject.com/en/dev/ref/contrib/gis/model-api/#geography正如 geodjango 的文档中提到的https://docs.djangoproject.com/en/dev/ref/contrib/gis/model-api/#geography

Geography Type In PostGIS 1.5, the geography type was introduced -- it provides native support for spatial features represented with geographic coordinates (eg, WGS84 longitude/latitude).地理类型 在 PostGIS 1.5 中,引入了地理类型——它为用地理坐标(例如,WGS84 经度/纬度)表示的空间特征提供本机支持。 [7] Unlike the plane used by a geometry type, the geography type uses a spherical representation of its data. [7] 与几何类型使用的平面不同,地理类型使用其数据的球形表示。 Distance and measurement operations performed on a geography column automatically employ great circle arc calculations and return linear units.在地理柱上执行的距离和测量操作会自动采用大​​圆弧计算并返回线性单位。 In other words, when ST_Distance is called on two geographies, a value in meters is returned (as opposed to degrees if called on a geometry column in WGS84).换句话说,当在两个地理区域上调用 ST_Distance 时,将返回一个以米为单位的值(如果在 WGS84 中的几何列上调用则与度相反)。

If you do not have geography=True , we are storing things as plain geometries, we will need to do conversion from square degrees (the floating point result you are getting) into a unit of measure you prefer because we cannot calculate area from geographic coordinates.如果您没有geography=True ,我们将事物存储为普通几何图形,我们需要将square degrees (您获得的浮点结果)转换为您喜欢的度量单位,因为我们无法根据地理坐标计算面积. We can instead add a helper method which is in a projected coordinate space to do the transformation:我们可以在投影坐标空间中添加一个辅助方法来进行转换:

def get_acres(self): 
    """ 
    Returns the area in acres. 
    """ 
    # Convert our geographic polygons (in WGS84)
    # into a local projection for New York (here EPSG:32118) 
    self.polygon.transform(32118) 
    meters_sq = self.polygon.area.sq_m

    acres = meters_sq * 0.000247105381 # meters^2 to acres

    return acres

Which projection we use depends on the extent of the data, and how accurate we need the results: here I've illustrated with a specific projection for part of New York, but if your data isn't particularly accurate, you could easily substitute a global projection or just use a simple formula.我们使用哪种投影取决于数据的范围,以及我们需要结果的准确程度:这里我用纽约部分地区的特定投影进行了说明,但如果您的数据不是特别准确,您可以轻松替换全局投影或只是使用一个简单的公式。

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

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