获取直线与多边形相交点的坐标

2024-01-03

我想找到一条线与多边形相交的点。我使用凹轮廓计算获得了这个多边形thread https://stackoverflow.com/questions/57260352/python-concave-hull-polygon-of-a-set-of-lines.

import alphashape
from shapely.geometry import LineString
import matplotlib.pyplot as plt
from descartes import PolygonPatch

points = [(17, 158),(15, 135),(38, 183),(43, 19),(93, 88),(96, 140),(149, 163),(128, 248),(216, 265),(248, 210),(223, 167),(256, 151),(331, 214),(340, 187),(316, 53),(298, 35),(182, 0),(121, 42)]
points = np.array(points)

alpha = 0.99 * alphashape.optimizealpha(points)
hull = alphashape.alphashape(points, alpha)
hull_pts = hull.exterior.coords.xy
path = PolygonPatch(hull, fill=False, color='green')
print(path.contains_point([128,248]))

fig, ax = plt.subplots()
ax.scatter(hull_pts[0], hull_pts[1], color='red')
ax.scatter(points[:,0], points[:,1], color='red')

p = np.array([[350, 100],[0, 100]])
ax.plot(p[:, 0], p[:, 1], color='blue')
ax.add_patch(path)

到目前为止,我尝试用以下命令定义一条线:

l = LineString(p)
inters = l.intersection(hull)

but inters.xy返回 NotImplemented 错误,因此我不确定如何获取线与多边形交叉点的坐标。


交集返回一个MultilineString,这是一个奇特的词,表示列表LineStrings。我们可以从每个线串中检索坐标,例如:

import alphashape
from shapely.geometry import LineString
import matplotlib.pyplot as plt
import numpy as np

#replicating your example
fig, ax = plt.subplots()

line_xy = [[350, 100],[0, 120]]
points = np.asarray([(17, 158),(15, 135),(38, 183),(43, 19),(93, 88),(96, 140),(149, 163),(128, 248),(216, 265),(248, 210),(223, 167),(256, 151),(331, 214),(340, 187),(316, 53),(298, 35),(182, 0),(121, 42)])

alpha = 0.99 * alphashape.optimizealpha(points)
hull = alphashape.alphashape(points, alpha)
hull_pts = hull.exterior.coords.xy

p = LineString(line_xy)

ax.plot(*hull_pts, c="green")
ax.scatter(points[:,0], points[:,1], marker="o", color="red")
ax.scatter(*hull_pts, marker="s", color="red")

ax.plot(*p.coords.xy, color='blue')

#retrieving intersection 
inters = hull.intersection(p)

#checking for object type to retrieve all intersection coordinates
if inters.type == "LineString":
    coords = np.asarray([inters.coords.xy])
elif inters.type == "MultiLineString":
    coords = np.asarray([l.coords.xy for l in inters.geoms])
    
#reshaping array point coordinates into a form that does not make my head hurt
coords = coords.transpose(1, 0, 2).reshape(2, -1)
print(coords)

plt.show()

with coords[:, i]返回交点的 x-y 值i.

示例输出:

[[324.67707894 234.24811338 176.4217078   18.88111888]
 [101.44702406 106.61439352 109.91875955 118.92107892]]

奇怪的是,shapely考虑像这样的线点300, 100在船体内作为交叉点。严格来说,必须检查所有已识别的点是否位于船体多边形内。

alphashape(此处使用 1.3.1)应该更新他们的例程,因为alphashape.alphashape(points, alpha)生成错误消息ShapelyDeprecationWarning: Iteration over multi-part geometries is deprecated and will be removed in Shapely 2.0. Use the geoms property to access the constituent parts of a multi-part geometry.

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

获取直线与多边形相交点的坐标 的相关文章

随机推荐

  • 使用 python panda 按月、年计算出现频率

    假设我有以下数据系列 Date Category 2014 8 Facebook 2014 8 Vimeo 2014 8 Facebook 2014 8 Facebook 2014 9 Facebook 2014 9 Orkut 2014
  • 在 PyQt5 中,我们如何获取焦点小部件的名称/对象名称?

    在我们的 PyQt5 程序中 我们多次使用 Qline Edits QcheckBox QListwidget 如何知道 我们的程序中使用的小部件有哪些小部件名称 如何获取 返回当前具有焦点的小部件的名称 用户指定的名称 正如 Heike所
  • 在 C++ 中将函数模板作为参数传递

    例如 我想从两个序列中获取最大值列表 left and right 并将结果保存在max seq 它们都是先前定义和分配的 std transform left begin left end right begin max seq begi
  • “py.test”与“pytest”命令

    The py test在我的例子中 命令失败了 而pytest运行完全正常 我使用 pytest flask 插件 platform linux Python 3 5 2 pytest 3 0 2 py 1 4 31 pluggy 0 3
  • JavaFX TableView 通过单击和自动插入行进行编辑?

    我在程序中使用了 tableview 来显示几行 这很好 我的程序用于会计 正如你所知 其中插入了大量数据 我使用了表视图 但我遇到了很多问题 问题 1 当用户想要修改值时 他将双击以编辑单元格 我希望我的用户选择任何行并开始输入 问题2
  • 如何检查MySQL中是否存在某行? (即检查 MySQL 中是否存在用户名或电子邮件)

    我需要帮助检查数据库中是否存在行 就我而言 该行包含一个电子邮件地址 我得到结果 email no longer exists email protected cdn cgi l email protection 这是我当前使用的代码 if
  • 如何在 iPhone 的 Objective-C 中本地播放视频?

    我想通过将视频存储在应用程序中来在 iPhone 上本地播放视频 我能怎么做 NSString path NSBundle mainBundle pathForResource myVideo ofType mp4 MPMoviePlaye
  • Cypress - 从 json 响应正文获取值

    我正在使用 Cypress 进行一些 API 测试 但我很难访问 JSON 响应正文中的值 但是我可以对身体进行断言 表明它正确接收了它 下面我尝试分配 JSON 主体 response body 然后从中获取 id 的值 describe
  • Excel 文件的 DictReader

    我有一个当前保存为 csv 的文件 但它最初是一个 Excel 文件 Excel 2010 其内容是这样的 Name Category Address McFood Fast Food Street 1 BurgerEmperor Fast
  • 限制网络请求

    我想执行一堆 WebRequest 但设置了可以同时启动的阈值 我遇到了这个LimitedConcurrencyTaskScheduler 示例 https msdn microsoft com en us library system t
  • WPF 数据网格文本省略号不起作用

    我有一个专栏 里面有很长的用户评论 我使用以下代码加载它
  • TabItem 绑定 WPF

    我刚刚学习 WPF 我需要一些帮助 我有一个使用 TabControl 并动态生成新选项卡的应用程序 在每个选项卡上我有一个 TextBox 现在我想向工具栏添加一个撤消按钮 该工具栏不是选项卡的一部分 类似 VisualStudio 撤消
  • 在 Groovy 中将地图与递归嵌套地图合并

    我想知道是否有人有一种简单的方法将 2 个深层嵌套地图合并在一起 例如 我想得到 a 1 animals cat blue b 2 animals dog red a 1 b 2 animals cat blue dog red 有人有简单
  • 合并重叠的数字范围

    我需要将重叠的数字范围合并为单个范围 所以我有一个包含子列表的列表 例如 83 77 103 97 82 76 101 95 78 72 97 91 72 66 89 83 63 57 78 72 53 47 65 59 41 35 50
  • Cipher 是线程安全的吗?

    很简单 一个实例可以javax crypto Cipher e g Cipher getInstance RSA 可以从多个线程中使用 或者我是否需要将多个线程粘在一个线程中ThreadLocal 就我而言 不 不是 该实例是有状态的 因此
  • 用于二维数组的 ASP.NET MVC 5 编辑器

    我有一个包含二维数组的模型 public class Matrix public ValidInt Data Range 0 8 ErrorMessage public int Width get set Range 0 8 ErrorMe
  • 宏操作符列表

    我知道宏的字符串化运算符 并且我最近了解到 连接运算符 带有字符串文字前缀的预处理器字符串化运算符 https stackoverflow com questions 29850088 preprocessor stringizing op
  • 我可以在垫子扩展行内显示另一个表格吗?

    我有一张带扩展行的垫子桌 如果我单击一行 它会展开并显示硬编码字符串 我想在展开的行内显示另一个表格 是否可以 或者 是否有任何其他技术或方法来实现我想要做的事情 我正在尝试显示给定时间段内已执行作业的列表 在主行上 我只想显示类似 01
  • 在 Spring Boot 中的 application.properties 中使用 Maven 属性

    我正在尝试将 pom xml 中的属性加载到 application properties 中 我想创建两个配置文件 dev 和 prod 以使用不同的数据库 url 我使用 Jenkins 作为 CI 在我的所有应用程序 主要是 Spri
  • 获取直线与多边形相交点的坐标

    我想找到一条线与多边形相交的点 我使用凹轮廓计算获得了这个多边形thread https stackoverflow com questions 57260352 python concave hull polygon of a set o