Skip to main content
 首页 » 编程设计

python之GeoDjango 多边形区域

2024年08月29日41powertoolsteam

我已经使用 postgis 实现了 GeoDjango。

这是我的模型:

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

当我调用 data.area 时,它返回一个 float ,但我没有任何关于它的测量单位的线索,这是一个问题,因为我想测试它是否比预定义的更大以平方米为单位设置面积。

你能帮帮我吗?

请您参考如下方法:

如果你正在处理 map 上的大面积区域,你应该设置

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

正如 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 (e.g., WGS84 longitude/latitude). [7] Unlike the plane used by a geometry type, the geography type uses a spherical representation of its data. 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).

如果您没有geography=True,我们将把东西存储为简单的几何图形,我们将需要从square degrees(您的浮点结果将) 转换为您喜欢的度量单位,因为我们无法根据地理坐标计算面积。我们可以改为添加一个位于投影坐标空间中的辅助方法来进行转换:

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 

我们使用哪种投影取决于数据的范围以及我们需要结果的准确性:这里我用纽约部分地区的特定投影进行了说明,但如果您的数据不是特别准确,您可以轻松替换全局投影或仅使用简单的公式。