如何在Shapely中获得线上等距点

2023-11-26

我试图(大致)将一条线的点均匀地间隔到预定义的距离。

距离之间有一定的公差是可以的,但最好尽可能接近。

我知道我可以手动迭代线路中的每个点并检查 p1 与 p2 的距离,并根据需要添加更多点。

但我想知道是否有人知道是否有办法用 shapely 来实现这一点,因为我已经在 LineString 中拥有了坐标。

enter image description here


一种方法是使用interpolate返回沿线指定距离处的点的方法。您只需首先以某种方式生成距离列表。以输入行为例罗伊2012年的回答:

import numpy as np
from shapely.geometry import LineString
from shapely.ops import unary_union

line = LineString(([0, 0], [2, 1], [3, 2], [3.5, 1], [5, 2]))

enter image description here

以指定距离分裂:

distance_delta = 0.9
distances = np.arange(0, line.length, distance_delta)
# or alternatively without NumPy:
# points_count = int(line.length // distance_delta) + 1
# distances = (distance_delta * i for i in range(points_count))
points = [line.interpolate(distance) for distance in distances] + [line.boundary[1]]
multipoint = unary_union(points)  # or new_line = LineString(points)

enter image description here
Note that since the distance is fixed you can have problems at the end of the line as shown in the image. Depending on what you want you can include/exclude the [line.boundary[1]] part which adds the line's endpoint or use distances = np.arange(0, line.length, distance_delta)[:-1] to exclude the penultimate point.

另请注意,unary_union我正在使用应该比调用更有效object.union(other)在循环内,如另一个答案所示。

分割为固定数量的点:

n = 7
# or to get the distances closest to the desired one:
# n = round(line.length / desired_distance_delta)
distances = np.linspace(0, line.length, n)
# or alternatively without NumPy:
# distances = (line.length * i / (n - 1) for i in range(n))
points = [line.interpolate(distance) for distance in distances]
multipoint = unary_union(points)  # or new_line = LineString(points)

enter image description here

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在Shapely中获得线上等距点 的相关文章

随机推荐

  • 错误CS0266:无法将类型“object”隐式转换为“int”

    错误CS0266 无法将类型 object 隐式转换为 int 一个 存在显式转换 您是否缺少强制转换 int dd 6000 sqlCmdDefaultTime new SqlCommand myQuery sqlCon sqlDefau
  • Microsoft Azure 应用服务存储

    I have a doubt for purchase a microsoft azure app service to host my app I have already tested the free profile and i am
  • Android 收据/zigzag 可绘制/布局

    我需要在 Android 中创建收据布局 这个想法非常简单 一个带有锯齿形底部边缘的矩形布局 最好的结果是具有固定锯齿形尺寸的可绘制 布局 即固定的半三角形尺寸 这将根据形状的实际宽度实时乘以三角形的数量 也许可以用夹子 必要时可以剪掉三角
  • 如何用 d3.js 绘制*简单*线段?

    In the 文档对于 d3 js 我找不到直接的方法来绘制simple两点之间的线段 我能找到的唯一方法是需要为 x 和 y 等创建回调 Amajor制作时只是画一条简单的线段 有没有更简单的东西 最简单的是 d3 select svg
  • Angular POST 到 Web API 不传递数据

    我已经使用 jQuery 针对 ASP NET Web API 编写代码有一段时间了 并且我正在 Angular 中开始一些新的东西 针对相同的 Web API 后端编写 我正在发布到一个方法 该方法将返回系统中实体的一些搜索结果 它看起来
  • php包含错误未找到路径

    我有这个小代码 实际上是一个登录脚本 它检查寄存器是否打开 并在登录按钮后显示它
  • 如何在lxml中查找元素的直接子元素

    我找到了一个具有特定类的对象 THREAD TREE find class thread 0 现在我想得到所有 p 其直接子元素 I tired THREAD findall p THREAD xpath div class thread
  • 为什么 CSS 可以使用假元素?

    在我的课堂上 我在玩耍时发现 CSS 可以处理虚构的元素 Example imsocool color blue
  • 我可以在 Qt-Creator 中看到程序输出吗?

    我正在使用 Qt Creator 编写一个简单的 OpenGL 程序 它基本上创建一个 QGLWidget 显示它并运行应用程序循环 我通常喜欢通过使用实际调试器和监视等的预处理器符号打开和关闭诊断消息来进行更多调试 在 Qt Creato
  • MongoDB 区分 undefined 和 null

    我正在检查查询非值的逻辑 并在使用时注意到mongoshell 它区分undefined and null values gt use test gt db test insert a 1 b null c undefined gt db
  • HQL 加入 - 加入所需的路径!休眠

    我是休眠新手 遇到了以下问题 当我尝试运行此查询时 出现 连接所需的路径 异常 String hql select avg t price from Ticket t JOIN Flight f WHERE f number flightN
  • 如何增加 MS Access 2007 数据库大小?

    我开发了一个Windows应用程序 后端数据库是Access 2007 我听说Access 2007的最大限制是2GB 现在我的问题是 有什么办法可以将大小增加到超出该限制吗 如何为应用程序创建多个数据库以增加大小和性能 您可以将数据分区到
  • 在 .js 文件中调用 URL 参数

    我正在 HTML 文件中调用 js 文件 在 js 文件的 URL 上 我想包含一个可供 js 文件内的代码访问的参数 例如 我希望能够在 jQuery 的帮助下将 ID 值传递给 jquery widget js 文件中的函数 这是怎么做
  • Groovy 地图点键嵌套地图

    我有一个带有点表示法键的地图 但我需要它作为嵌套地图 test key one value1 text key two value2 现在结果应该是 test key one value1 two value2 这是我的代码想法 def e
  • C# ListView 项目图像

    如何使用 foreach 语句将图像 指定图像 添加到 listview 中 例如 foreach Video entry in videoFeed Entries listview1 items add entry listview1 i
  • Sprite Kit 中可重用的多线程实现

    我正在开发 Sprite Kit 游戏 我需要做一些多线程来保持健康的 fps 更新时 我调用一个函数来创建大量 UIBezierPaths 并使用 C 静态库合并它们 如果我有超过 10 个形状 帧速率会急剧下降 因此我决定尝试一下 GC
  • 更好的地图构造器

    有没有更简化的方法来执行以下操作 Map
  • 我在哪里可以了解经过验证的共享加密密钥的方法?

    假设一个群组想要加密一些信息 然后以需要群组共识的方式在群组成员之间共享加密密钥来解密该信息 我对各种场景感兴趣 其中共识的广度范围从一致到绝对多数 有用的技术可以应用于对称密钥 私钥或两者 我可以尝试推出自己的方法 我相信许多 SO 成员
  • KCFinder '您无权列出文件。'

    我在 ckeditor 中集成 KCFinder 时遇到问题 我的ckeditor版本是4 0 另一个 KCFinder版本 是2 52 dev 您好 像这样配置 ckeditor 的 config js CKEDITOR config b
  • 如何在Shapely中获得线上等距点

    我试图 大致 将一条线的点均匀地间隔到预定义的距离 距离之间有一定的公差是可以的 但最好尽可能接近 我知道我可以手动迭代线路中的每个点并检查 p1 与 p2 的距离 并根据需要添加更多点 但我想知道是否有人知道是否有办法用 shapely