【问题标题】:How can I take a list of points that create a line and extend them into polygons inward of a certain length?如何获取创建线的点列表并将它们扩展到一定长度内的多边形?
【发布时间】:2014-01-07 16:04:02
【问题描述】:

我有 GPS 坐标。它们只是在几个不同的地方创建轮廓的点,。我希望能够有一个脚本将这些点转换为向内延伸 X 距离的多边形,以及一种向内和向外延伸的方式。

如果我有这样的事情:

(点是点)

00000000000000000
00000000000000000
00............000
00.0000000000.000
00.0000000000.000
00.0000000000.000
00.0000000000.000
00.0000000000.000
00............000
00000000000000000
00000000000000000

我可以在距离为 1 和“向内”的情况下运行这个程序,我最终会得到一个 # 形状的多边形:

00000000000000000
00000000000000000
00&&&&&&&&&&&&000
00&&&&&&&&&&&&000
00&&00000000&&000
00&&00000000&&000
00&&00000000&&000
00&&&&&&&&&&&&000
00&&&&&&&&&&&&000
00000000000000000
00000000000000000

到目前为止,我已经尝试过使用圆圈然后缩小它们,但这似乎是错误的/不太可行。这不是在网格上执行的,实际上它使用浮点数作为坐标。

任何可以做到这一点的图书馆都值得赞赏。

【问题讨论】:

  • 不,是一个列表。每个封闭的形状也都是自己独立的。
  • 当然,它们都有数百个点。不一定是顺时针或逆时针,只是从一个点到下一个风格的旅行。 -87.591604621628392,45.096611340705195, -87.58854379668621,45.090842359416861, -87.589388527669584,45.086618423968154, -87.592979043889585,45.085140007205247 ....

标签: python geometry polygon


【解决方案1】:

GDAL/OGR 是另一种选择。最终你想要做的是一个缓冲区。要向外扩展多边形形状,请使用具有正缓冲距离的缓冲区,向内它将是负缓冲距离。以下是使用 shapefile 的简单示例。不确定您的数据是什么格式,但如果 GDAL/OGR 无法读取它,我会感到惊讶。

import osgeo.ogr
# using ESRI Shape file in this example but there are a number
# of different files this lib supports: http://www.gdal.org/ogr/ogr_formats.html
driver = osgeo.ogr.GetDriverByName('ESRI Shapefile')
osgeo.ogr.UseExceptions()

# Create a data source using the driver...
dataSource = driver.Open("/home/user1/data.shp")

# Get the layer
lyr = dataSource.GetLayer()

# Select the feature in this case using an attribute query
lyr.SetAttributeFilter("column = 'value'")

# verify that you have a feature selected
print 'features in layer:', lyr.GetFeatureCount()

# get the firest feature from the layer
feature = lyr.GetNextFeature()

# get the geometry from the feature
geom = feature.GetGeometryRef()

# perform a 100 unit buffer, not sure what units the coordinates of the 
# the data you have are in.
bufferGeom = geom.buffer(100)

# bufferGeom is a geometry object, which is described here:
# <http://cosmicproject.org/OGR/ogr_classes.html#Geometry>

以下是开始使用 GDAL/ORG 处理空间数据的绝佳资源:http://www.gis.usu.edu/~chrisg/python/2009/

API 文档:http://cosmicproject.org/OGR/ogr_classes.html

最后是 GDAL/OGR 页面的链接。 http://www.gdal.org/

【讨论】:

    【解决方案2】:

    https://pypi.python.org/pypi/Shapely

    Shapely 是一个非常好的二维计算几何库;按照我的理解,它将您的问题简化为一行代码。

    【讨论】:

    • 你说的是腐蚀/膨胀方法吗?
    • 是的;查看示例。您可以充分控制如何处理角落等。在没有库的情况下从头开始以一种通用且稳健的方式做这些事情是非常困难的,而 afaik shapely 是目前最好的库,所以看看你是否可以把它哄到你的细节中是值得的。
    猜你喜欢
    • 1970-01-01
    • 2021-05-10
    • 1970-01-01
    • 2019-08-14
    • 2014-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多