【问题标题】:GeoDjango: How to create a circle based on point and radiusGeoDjango:如何根据点和半径创建一个圆
【发布时间】:2011-02-13 16:09:27
【问题描述】:

我有以下(简化的)模型:

class Zone(gismodels.Model):
    name = gismodels.CharField()
    poly = gismodels.PolygonField()

我想根据给定的点和半径创建并保存一个表示圆的多边形。

我能弄清楚如何实现这一点的唯一方法是使用原始 SQL 调用 postgis ST_Buffer 函数。我真的希望有另一种方法。

是否可以访问 GEOS 缓冲区方法?

【问题讨论】:

    标签: django gis geodjango geos


    【解决方案1】:

    是的,可以使用geos buffer method

    >>> from django.contrib.gis import geos
    >>> center = geos.Point(5, 5)
    >>> radius = 2
    >>> circle = center.buffer(radius)
    >>> circle
    <Polygon object at 0x1029d8370>
    

    这里的半径与点的坐标单位相同。这适用于某些坐标系,如 UTM,但不适用于其他坐标系。

    此外,虽然这适用于构造圆形几何,但PostGIS documentation 指出,ST_DWithin 进行半径搜索更有效。

    【讨论】:

    • 感谢您的回答。我的半径以公里为单位,我的点是使用以度为单位的经度/纬度对创建的。我是否需要将半径转换为度数才能获得有用的结果,或者是否有内置功能来处理这个问题?
    • @MattRowbum - 您需要从 km 转换为度数。此外,这会让你的椭圆形越远,你离赤道的距离就越远。 PostGIS 现在允许使用您想要的“地理”类型,但我找不到通过 GeoDjango 中的 GEOS API 访问它的方法。
    • 如何将公里换算成度数:stackoverflow.com/questions/5217348/…
    【解决方案2】:

    我花了很多时间试图让这个工作正常进行。由于这是排名第一的谷歌搜索结果,这对我有用:

    radius_km = radius*1.609 # convert miles to km
    point = target.geolocation # a django PointField using SRID 4326
    # re-project point to a flat coordinate system 
    # so we can use meters instead of degrees below, 
    # AND get an actual circle instead of oval    
    point.transform(6347) 
    poly = point.buffer(radius_km*1000) # get a circular polygon from radius
    poly.transform(4326)# re-project the resulting polygon back
    

    奖励:如果你这样做是为了在谷歌静态地图上获得一个圆圈,请抓住polyline

    import polyline
    import ast
    
    geo = ast.literal_eval(poly.geojson) # turn the text into a dict
    points = geo['coordinates'][0]
    pl = polyline.encode(points, geojson=True, precision=5) # make a polyline out of the polygon for google
    map_url += '&path=color:0x00000000%7Cfillcolor:0x0000AA33%7Cweight:1%7Cenc:' + pl
    

    【讨论】:

    • 谢谢!神奇的数字 6347 是什么意思?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多