GeoDjango:如何根据点和半径创建圆

发布于 2024-10-17 08:11:42 字数 275 浏览 7 评论 0原文

我有以下(简化)模型:

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

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

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

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

I have the following (simplified) Model:

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

I want to create and save a polygon that represents a circle, based upon a given point and radius.

The only way I can figure out how to achieve this, is to call the postgis ST_Buffer function using raw SQL. I'm really hoping that there is another way.

Is it possible to access the GEOS buffer methods?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自此以后,行同陌路 2024-10-24 08:11:42

是的,可以使用 geos 缓冲方法

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

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

另外,虽然这适合构建圆形几何图形,但 PostGIS 文档 指出,对于半径搜索ST_DWithin 效率更高。

Yes, it is possible to use the 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>

The radius here is in the same units as the coordinates of the points. This will work for some coordinate systems like UTM, but not as well for others.

Also, while this is appropriate for constructing a circular geometry, the PostGIS documentation notes that for doing radius searches ST_DWithin is more efficient.

揽月 2024-10-24 08:11:42

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

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

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

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

I spent a ridiculous amount of time trying to get this working. Since this is the number one google search result, here's what worked for me:

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

Bonus: If you're doing this so you can get a circle on a google static map, grab 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文