用shapely判断两个图形的包含相交关系

用shapely判断两个图形的包含相交关系shapelyshape 介绍 shapely 安装 shapely 的导入 Point LineString Polygon 的通用属性 Point 对象 LineString 对象 Polygon 对象 box 对象一元判定二元判定 shapely 介绍 shapely 是专门做图形计算的包 基本上图形线段 点的判断包里都有 shapely 里主要由 Point LineString Polygon 这三类组成 在 shapely 里点 线 面之间都是可以做判断的 比如说计算点到线段的距离 点到面之间的距离 点与点之间的距离 点是否在一个图形

shapely介绍

  • z轴在实例化对象时可以使用,但图形分析中没有用,所有的操作都在x-y平面完成
  • shapely拥有(点、曲线、面 interior, boundary, exterior)这样的概念
    • 点的内部是一个点、没有边界、所有其他点都是外部
    • 曲线的内部是沿线的无数个电、边界是两个端点、外部是其他所有的点
    • 面的内部是面上的无数个点、边界是一个或多个曲线、外部是其他所有点

shapely安装

我之前的博客已经介绍过shapely的安装,有兴趣的朋友可以去看看。

shapely的导入

from shapely.geometry import Point, LineString, Polygon 

Point、LineString、Polygon的通用属性

object.area 返回对象的面积 object.bounds 返回包含对象的x、y的最大值最小值的元组 object.length 返回对象的长度 object.geom_type 返回图形的类型 字符串 object.distance(other) 返回两个对象的最小距离 object.hausdorff_distance(other) 返回两个对象之间的哈多夫距离 object.representative_point() 返回一个保证在对象上的点 

Point对象

point = Point(0, 1) # 创建点对象,可以是三维的 print(point.area) # 0.0 print(point.length) # 0.0 print(point.bounds) # (0.0, 1.0, 0.0, 1.0) print(point.geom_type) # Point print(point.coords) # 
    print(point.coords[:]) # [(0.0, 1.0)] print(list(point.coords)) # [(0.0, 1.0)] print(point.x, point.y) # 0.0 1.0 
0.0 0.0 (0.0, 1.0, 0.0, 1.0) Point <shapely.coords.CoordinateSequence object at 0x00000201F7A18518> [(0.0, 1.0)] [(0.0, 1.0)] 0.0 1.0 

LineString对象

# 可以自己相交 line = LineString([(0, 0), (1, 1)]) print(line.area) # 0.0 print(line.length) # 1.30951 print(line.bounds) # (0.0, 0.0, 1.0, 1.0) print(line.geom_type) # LineString print(line.coords) # 
    print(line.coords[:], type(line.coords[:])) # [(0.0, 0.0), (1.0, 1.0)] 
    
0.0 1.30951 (0.0, 0.0, 1.0, 1.0) LineString <shapely.coords.CoordinateSequence object at 0x0000028D59BC0518> [(0.0, 0.0), (1.0, 1.0)] <class 'list'> 

Polygon对象

# 可以传入两个参数,第一个是有序的外边缘轮廓,第二个是无序的内边缘轮廓,多边形内所有的环形最多只能有一个焦点,否则都是无效的 poly = Polygon([(0, 0), (-1, 1), (2, 1), (3, -2)]) print(poly.area) print(poly.length) print(poly.bounds) x_min, y_min, x_max, y_max = poly.bounds print(x_min, y_min, x_max, y_max) print(poly.geom_type) print(poly.exterior.coords) print(poly.exterior.coords[:]) print(poly.interiors) print(poly.interiors[:]) 
5.0 11.5464 (-1.0, -2.0, 3.0, 1.0) -1.0 -2.0 3.0 1.0 Polygon <shapely.coords.CoordinateSequence object at 0x0000018A43B629E8> [(0.0, 0.0), (-1.0, 1.0), (2.0, 1.0), (3.0, -2.0), (0.0, 0.0)] <shapely.geometry.polygon.InteriorRingSequence object at 0x0000018A4398DB70> [] 

box对象

from shapely.geometry import box # box可以穿件矩形多边形,ccw参数选择顺时针或逆时针 b = box(0, 1, 2, 3, ccw=False) # 是以前两个参数作为左下角的点,后两个参数作为右上角的点 print(b.geom_type) print(b.bounds) print(b.exterior.coords[:]) # 获取多边形点的顺序 print(sgp.orient(b, sign=1)) # 顺时针 print(sgp.orient(b, sign=-1)) # 逆时针 
Polygon (1.0, 0.0, 2.0, 3.0) [(1.0, 0.0), (1.0, 3.0), (2.0, 3.0), (2.0, 0.0), (1.0, 0.0)] POLYGON ((1 0, 2 0, 2 3, 1 3, 1 0)) POLYGON ((1 0, 1 3, 2 3, 2 0, 1 0)) 

一元判定

# has_z 判断是否有z坐标 p = Point(0, 0) print(p.has_z) # False p = Point(0, 0, 0) print(p.has_z) # True # is_ccw 判断是否是逆时针的 lr = LinearRing([(1, 0), (1, 1), (0, 0)]) print(lr.is_ccw) # True lr.coords = list(lr.coords)[::-1] print(lr.is_ccw) # False # is_empty 返回TRUE如果内部和边界都是空 p = Point() print(p.is_empty) # True p = Point(0, 0) print(p.is_empty) # False # is_ring 闭合返回TRUE  line = LineString([(0, 0), (1, 1), (1, 0)]) print(line.is_ring) # False line = LineString([(0, 0), (1, 1), (1, 0), (0, 0)]) print(line.is_ring) # True lr = LinearRing([(0, 0), (1, 1), (1, 0)]) print(lr.is_ring) # True lr = LinearRing([(0, 0), (1, 1), (1, 0), (0, 0)]) print(lr.is_ring) # True # is_simple 自己不相交,返回TRUE line = LineString([(0, 0), (1, 1), (1, -1), (0, 1)]) print(line.is_simple) # False line = LineString([(0, 0), (0, 1), (1, 1), (1, 0)]) print(line.is_simple) # True is_valid LinearRing 不相交或者只有一个交点是有效的 Polygon 同上 Note:可以写成验证装饰器,根据特定内容判断他是否是有效的 

二元判定

object.__eq__(other) # 类型和坐标点相同返回TRUE object.equals(other) #boundary, interior, and exterior完全相同返回TRUE object.almost_equals(other[, decimal=6]) # 近似相等返回TRUE decimal是小数点的位数完全相同 object.contains(other) # other里没有点在object的exterior,且other的interior里至少有一个点在object的interior object.crosses(other) # object的interior与other的interior相交,且不包含他 object.disjoint(other) # object的interior和boundary 和other的interior和boundary都不想交 返回True object.intersects(other) # object的interior或者boundary和other的interior或者boundary相交 返回TRUE object.overlaps(other) # object的interior或者boundary和other的interior或者boundary相交,且不包含他, 返回TRUE object.touches(other) # other和object至少有一个共同的点,且他们的interior不相交 

object.__eq__(other) # 类型和坐标点相同返回TRUE,和左边点顺序有关

object = LineString([(0, 0), (1, 1)]) other = LineString([(0, 0), (1, 1)]) print(object.__eq__(other)) # True object = Point((0, 0)) other = Point((0, 0)) print(object.__eq__(other)) # True object = Polygon([(0, 0), (0, 1), (1, 1)]) other = Polygon([(0, 0), (0, 1), (1, 1)]) print(object.__eq__(other)) # True object = Polygon([(0, 0), (0, 1), (1, 1)]) other = Polygon([(0, 1), (1, 1), (0, 0)]) print(object.equals(other)) # False 

object.equals(other) #boundary, interior, and exterior完全相同返回TRUE,和坐标点顺序无关

object = LineString([(0, 0), (1, 1)]) other = LineString([(0, 0), (1, 1)]) print(object.equals(other)) # True object = Point((0, 0)) other = Point((0, 0)) print(object.equals(other)) # True object = Polygon([(0, 0), (0, 1), (1, 1)]) other = Polygon([(0, 0), (0, 1), (1, 1)]) print(object.equals(other)) # True object = Polygon([(0, 0), (0, 1), (1, 1)]) other = Polygon([(0, 1), (1, 1), (0, 0)]) print(object.equals(other)) # True 

object.almost_equals(other[, decimal=6]) 近似相等返回TRUE decimal是小数点的位数完全相同,和坐标点的顺序有关

object = LineString([(0, 0), (1, 1)]) other = LineString([(0, 0), (1, 1)]) print(object.almost_equals(other)) # True object = Point((0, 0)) other = Point((0, 0)) print(object.almost_equals(other)) # True object = Polygon([(0, 0), (0, 1), (1, 1)]) other = Polygon([(0, 0), (0, 1), (1, 1)]) print(object.almost_equals(other)) # True object = Polygon([(0, 0), (0, 1), (1, 1)]) other = Polygon([(0, 1), (1, 1), (0, 0)]) print(object.almost_equals(other)) # False 

object.contains(other) other里没有点在object的exterior,且other的interior里至少有一个点在object的interior,和坐标点的顺序无关

object = LineString([(0, 0), (1, 1)]) other = LineString([(0, 0), (1, 1)]) print(object.contains(other)) # True object = Point((0, 0)) other = Point((0, 0)) print(object.contains(other)) # True object = Polygon([(0, 0), (0, 1), (1, 1)]) other = Polygon([(0, 0), (0, 1), (1, 1)]) print(object.contains(other)) # True object = Polygon([(0, 0), (0, 1), (1, 1)]) other = Polygon([(0, 1), (1, 2), (0, 0)]) print(object.contains(other)) # False 

object.crosses(other) object的interior与other的interior相交,且不包含他,该判断只适用于图形与线之间的判定

object = LineString([(0, 0), (1, 1)]) other = LineString([(0, 0), (1, 1)]) print(object.crosses(other)) # False object = LineString([(0, 0), (2, 2)]) other = LineString([(0, 0), (1, 1)]) print(object.crosses(other)) # False object = Point((0, 0)) other = Point((0, 0)) print(object.crosses(other)) # False object = Polygon([(0, 0), (0, 1), (1, 1)]) other = Polygon([(0, 0), (0, 1), (1, 1)]) print(object.crosses(other)) # False object = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]) other = LineString([(0.5, 0.5), (2, 2)]) print(object.crosses(other)) # True print(other.crosses(object)) # True object = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]) other = Polygon([(0.5, 0.5), (0.5, 2), (2, 2), (2, 0.5)]) print(object.crosses(other)) # False print(other.crosses(object)) # False 

object.disjoint(other) object的interior和boundary 和other的interior和boundary都不想交 返回True,完全不相交才返回True,有一个点都不行

object = LineString([(0, 0), (1, 1)]) other = LineString([(0, 0), (1, 1)]) print(object.disjoint(other)) # False object = LineString([(0, 0), (1, 1)]) other = LineString([(2, 2), (3, 3)]) print(object.disjoint(other)) # True object = Point((0, 0)) other = Point((1, 1)) print(object.disjoint(other)) # True object = Polygon([(0, 0), (0, 1), (1, 1)]) other = Polygon([(0, 0), (0, -1), (-1, -1)]) print(object.disjoint(other)) # False,,有一个点相交了,都不想交才是True object = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]) other = LineString([(2, 2), (3, 3)]) print(object.disjoint(other)) # True print(other.disjoint(object)) # True object = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]) other = Polygon([(2,2), (2,4), (4,4), (4, 2)]) print(object.disjoint(other)) # True print(other.disjoint(object)) # True 

object.intersects(other) object的interior或者boundary和other的interior或者boundary相交 返回TRUE

object = LineString([(0, 0), (1, 1)]) other = LineString([(0, 0), (1, 1)]) print(object.intersects(other)) # True object = LineString([(0, 0), (1, 1)]) other = LineString([(1, 1), (0, 0)]) print(object.intersects(other)) # True object = Point((0, 0)) other = Point((1, 1)) print(object.intersects(other)) # False object = Polygon([(0, 0), (0, 1), (1, 1)]) other = Polygon([(0, 0), (0, -1), (-1, -1)]) print(object.intersects(other)) # True object = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]) other = LineString([(0, 0), (3, 3)]) print(object.intersects(other)) # True object = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]) other = Polygon([(2,2), (2,4), (4,4), (4, 2)]) print(object.intersects(other)) # False 

object.touches(other) other和object至少有一个共同的点,且他们的interior不相交,也就是说在边界上

object = LineString([(0, 0), (1, 1)]) other = LineString([(1, 1), (2, 2)]) print(object.touches(other)) # True object = LineString([(0, 0), (1, 1)]) other = Point(0, 0) print(object.touches(other)) # True object = Point((0, 0)) other = Point((0, 0)) print(object.touches(other)) # False object = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]) other = Point((0, 0)) print(object.touches(other)) # True object = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]) other = LineString([(0.1, 0), (0.2, 0)]) print(object.touches(other)) # True print(other.touches(object)) # True 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/206956.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月19日 下午3:06
下一篇 2026年3月19日 下午3:07


相关推荐

  • 中文情感极性词典_形容寻找美食的词语

    中文情感极性词典_形容寻找美食的词语【实例简介】(全)包括知网Hownet情感词典,台湾大学NTUSD简体中文情感词典,情感词汇本体,情感词典及其分类,清华大学李军中文褒贬义词典,汉语情感词极值表,否定词典,褒贬词及其近义词【实例截图】【核心代码】SentimentAnalysisDic└──SentimentAnalysisDic├──知网Hownet情感词典│├──主张词语(中文).txt│├──主张词语(英…

    2022年8月23日
    12
  • mbus主站电路设计_proteus子电路模块

    mbus主站电路设计_proteus子电路模块1Mbus接收发送机制对于主从式通信系统,因从机之间不能直接交换信息,只能通过主机来转发,此时采用MBus可以实现对从机的相关数据进行采集,并传递至集中器,然后再传递至总站。它由主机从机和两线制总线组成。MBus总线是一种半双工通信总线,其可以通过集中器实现给终端仪表远程供电。1.1发送由集中器向终端仪表传输的信号采用电压值的变化来表示,即集中器向终端仪表发送的数据码流是一种电压脉冲序列,用+36V表示逻辑“1”,用+24V表示逻辑“0”。在稳态时,线路将保持“1”状态。

    2022年10月15日
    4
  • uniapp 复制粘贴_小程序封装键盘输入框

    uniapp 复制粘贴_小程序封装键盘输入框本篇没啥营养,就是告诉不熟悉uniapp的开发者怎么完成长按复制,懂得朋友别浪费时间1.给text组件设置对应平台的对应属性,在安卓手机上的效果2.直接设置剪切板的内容uni.setClipboardData(OBJECT)<textstyle=”@longpress=’copyText’>长按触发longpress事件</text>//对应事件copyText(){ uni.setClipboardData({ dat…

    2026年4月16日
    4
  • mfc循环后界面卡死_undefined reference to printf

    mfc循环后界面卡死_undefined reference to printf使用EnterCriticalSection时卡死问题产生原因:如下代码,在已经进入临界区时,再次进入其他临界区,会导致软件卡死EnterCriticalSection(&cs0);//进入临界区EnterCriticalSection(&cs1);//进入临界区LeaveCriticalSection(&cs1);//离开临界区

    2026年1月30日
    5
  • struts2.0的工作原理「建议收藏」

    struts2.0的工作原理「建议收藏」客户端发出一个请求,服务器端StrutsPreparedAndExceuteFilter接收请求,如果该请求是一个以.action结尾请求,则Struts2将请求转发至相应的Action,进行数

    2022年7月2日
    24
  • maven repositories配置_maven排除依赖

    maven repositories配置_maven排除依赖eclipsemaven配置修改:mavenrepository配置http://blog.csdn.net/joewolf/article/details/4876604Maven缺省

    2022年8月3日
    13

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号