shapely

  • shapely介绍
  • shapely安装
  • shapely的导入
  • Point、LineString、Polygon的通用属性
  • Point对象
  • LineString对象
  • Polygon对象
  • box对象
  • 一元判定
  • 二元判定

shapely介绍

shapely是专门做图形计算的包,基本上图形线段,点的判断包里都有,shapely里主要由Point,LineString,Polygon这三类组成,在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)  # <shapely.coords.CoordinateSequence object at 0x000001E64DE38518>
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.4142135623730951
print(line.bounds)  # (0.0, 0.0, 1.0, 1.0)
print(line.geom_type)  # LineString
print(line.coords)  # <shapely.coords.CoordinateSequence object at 0x0000028D59BC0518>
print(line.coords[:], type(line.coords[:]))  # [(0.0, 0.0), (1.0, 1.0)] <class 'list'>
0.0
1.4142135623730951
(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.182042498005464
(-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) # Trueis_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

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

  1. 7_2判断两个单链表是否相交,若相交,求出第一个交点

    转载请注明出处:http://www.cnblogs.com/wuzetiandaren/p/4251372.html 声明:现大部分文章为寻找问题时在网上相互转载,此博是为自己做个记录记录,方便自己 ...

  2. 判断两个单链表是否相交及找到第一个交点

    题目:给两个单链表,如何判断两个单链表是否相交?若相交,则找出第一个相交的节点.  这道题的思路和解法有很多,在这把这道题的解法做一个详细的总结. 解这道题之前,我们需要首先明确一个概念:  如果两个 ...

  3. [leetcode]242. Valid Anagram判断两个字符串是不是包含相同字符的重排列

    /*思路是判断26个字符在两个字符串中出现的次数是不是都一样,如果一样就返回true.记住这个方法*/if (s.length()!=t.length())return false;int[] wor ...

  4. 判断两个单向链表是否相交

    链接:http://www.cnblogs.com/mengdd/archive/2013/03/14/2958642.html 题目来源 <编程之美>3.6节. 给出两个单向链表的头指针 ...

  5. 判断两个ListInteger是否包含同样的值,不考虑位置关系

    2019独角兽企业重金招聘Python工程师标准>>> 其实很简单,先排序,然后对比就是了 public static boolean compare(List<Integer ...

  6. java 判断两个单链表是否相交

    文章目录 题目 思考 源码 环的入口 题目 单链表可能有环,也可能无环.给定两个单链表的头节点 head1 和 head2, 这两个链表可能相交,也可能不相交.请实现一个函数,如果两个链表相交,请返回 ...

  7. 判断两个单链表是否相交--java实现

    题目描述:单链表可能有环,也可能无环.给定两个单链表的头节点 head1 和 head2, 这两个链表可能相交,也可能不相交.请实现一个函数,如果两个链表相交,请返回相交 的第一个节点;如果不相交,返 ...

  8. 判断两个圆相切或相交

    bool is_relate(node a,node b) { int dx=a.x-b.x; int dy=a.y-b.y; int  ir=a.r+b.r; return dx*dx+dy*dy- ...

  9. 如何判断链表有环、如何判断两个链表相交

    如何判断单链表是否存在环 有一个单向链表,链表当中有可能出现"环",就像题图这样.如何用程序判断出这个链表是有环链表? 不允许修改链表结构. 时间复杂度O(n),空间复杂度O(1) ...

最新文章

  1. Python下Selenium PhantomJs设置header的方法
  2. 算法导论学习笔记 第7章 快速排序
  3. unity 角度限制_喵的Unity游戏开发之路 推球:游戏中的物理
  4. 浅谈js与jq给新增元素动态绑定事件(事件委托)
  5. LeetCode - 16. 3Sum Closest
  6. golang学习笔记14 golang substring 截取字符串
  7. 多元统计分析朱建平pdf_应用多元统计分析 朱建平 课后答案.pdf
  8. mariadb 卸载 Kali_Adobe官方卸载工具软件安装教程
  9. 离职通知邮件主题写什么好_辞职邮件怎么写,需要包含哪些内容
  10. 前端性能和加载体验优化实践(附:PWA、离线包、内存优化、预渲染)
  11. 关于RPN中proposal的坐标回归参数的一点理解及Faster R-CNN的学习资料
  12. 相干层析模型计算matlab,MATLAB 空间计量模型的实现
  13. 微信小程序识别二维码功能
  14. 【Spring教程】2.spring入门
  15. 攻击JavaWeb应用————2、CS交互安全
  16. pdf文档转换后出现大量乱码怎么办
  17. python try/except与try/finally使用
  18. 中金电信 文思海辉 pactera email 邮箱设置
  19. 晋中市中等职业学校技能大赛
  20. python pandas 去重_Pandas 数据框增、删、改、查、去重、抽样基本操作方法

热门文章

  1. 第十七章 再论没有银弹
  2. JSON.parse报错: Unexpected identifier
  3. 如何让品牌进入元宇宙
  4. 逻辑回归阈值_逻辑回归算法
  5. 英语单词测试词汇量的软件,英语单词词汇量测试小程序!简直不要太准
  6. 本地服务(local Service)的实现
  7. 作为应届大学生的我和准职业人的差距
  8. 软件架构-nginx详解上
  9. html5 lang属性都有哪些语言,HTML5中的lang属性,zh
  10. Python3实现Two-Pass算法检测区域连通性