python - GeoDjango 多边形区域

标签 python django area geodjango units-of-measurement

我已经使用 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

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

关于python - GeoDjango 多边形区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13348250/

相关文章:

python - 文本冒险的轻量级 MIDI 播放?

jquery - iOS Safari Mobile 图像 map - 单击时区域显示框

html - Safari/IE 图像映射自定义光标无法正常工作

python - 子类化 Python 字典以覆盖 __setitem__

Python:删除软连字符

python - 无法通过管道命令访问 django shell 中的导入函数?

django - django 到底如何验证它的 cookie?

django:使用字符串选择特定模型

C# : How to get area between two series in chart in C#?

python - 如何在Tensorflow中计算矩阵乘积的对角线?