• 【附代码】使用Shapely计算点面关系



    作者:小猪快跑

    基础数学&计算数学,从事优化领域5年+,主要研究方向:MIP求解器、整数规划、随机规划、智能优化算法

    本文档介绍如何使用 Shapely Python 包 计算几何点面关系&距离。

    如有错误,欢迎指正。如有更好的算法,也欢迎交流!!!——@小猪快跑

    相关文献

    基础

    Point、LineString、Polygon的通用属性

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

    创建Point、LineString、Polygon:

    from shapely import Point, Polygon, GeometryCollection,LineString
    
    point = Point(3, 3)
    polygon = Polygon([(0, 5), (1, 1), (3, 0)])
    circ = Point(4, 0).buffer(2)
    line = LineString([(0, 0), (2, 2)])
    polygon.intersection(circ)
    
    GeometryCollection([point, polygon, circ,line])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    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)  # 
    print(line.coords[:], type(line.coords[:]))  # [(0.0, 0.0), (1.0, 1.0)] 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    Polygon对象

    # 可以传入两个参数,第一个是有序的外边缘轮廓,第二个是无序的内边缘轮廓,多边形内所有的环形最多只能有一个焦点,否则都是无效的
    polygon = Polygon([(0, 0), (-1, 1), (2, 1), (3, -2)])
    print(polygon.area)  # 5.0
    print(polygon.length)  # 11.182042498005464
    print(polygon.bounds)  # (-1.0, -2.0, 3.0, 1.0)
    x_min, y_min, x_max, y_max = polygon.bounds
    print(x_min, y_min, x_max, y_max)  # -1.0 -2.0 3.0 1.0
    print(polygon.geom_type)  # Polygon
    print(polygon.exterior.coords)  # 
    print(polygon.exterior.coords[:])  # [(0.0, 0.0), (-1.0, 1.0), (2.0, 1.0), (3.0, -2.0), (0.0, 0.0)]
    print(polygon.interiors)  # 
    print(polygon.interiors[:])  # []
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    一元判定

    has_z 判断是否有z坐标

    p = Point(0, 0)
    print(p.has_z)  # False
    p = Point(0, 0, 0)
    print(p.has_z)  # True
    
    • 1
    • 2
    • 3
    • 4

    is_ccw 判断是否是逆时针的

    lr = LinearRing([(1, 0), (1, 1), (0, 0)])
    print(lr.is_ccw)  # True
    lr = LinearRing([(1, 0), (0, 0), (1, 1)])
    print(lr.is_ccw)  # False
    
    • 1
    • 2
    • 3
    • 4

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述

    is_empty 返回TRUE如果内部和边界都是空

    p = Point()
    print(p.is_empty)  # True
    p = Point(0, 0)
    print(p.is_empty)  # False
    
    • 1
    • 2
    • 3
    • 4

    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
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    二元判定

    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,完全不相交才返回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不相交,也就是说在边界上
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    contains

    a = Point(1, 1)
    b = Point(3, 3)
    print(a.contains(b))
    a = Point(2, 2)
    b = Point(2, 2)
    print(a.contains(b))
    a = Point(2, 1)
    b = LineString([(1, 2), (3, 2)])
    print(a.contains(b))
    a = Point(2, 2)
    b = LineString([(1, 2), (3, 2)])
    print(a.contains(b))
    a = Point(3, 1)
    b = Point(2, 2).buffer(1)
    print(a.contains(b))
    a = Point(2, 2)
    b = Point(2, 2).buffer(1)
    print(a.contains(b))
    a = Point(2, 1)
    b = Point(2, 2).buffer(1)
    print(a.contains(b))
    a = LineString([(1, 1), (3, 1)])
    b = LineString([(1, 3), (3, 3)])
    print(a.contains(b))
    a = LineString([(1, 2), (3, 2)])
    b = LineString([(2, 1), (2, 3)])
    print(a.contains(b))
    a = LineString([(1, 0.5), (3, 0.5)])
    b = Point(2, 2).buffer(1)
    print(a.contains(b))
    a = LineString([(1, 1), (3, 1)])
    b = Point(2, 2).buffer(1)
    print(a.contains(b))
    a = LineString([(1, 1), (2, 2)])
    b = Point(2, 2).buffer(1)
    print(a.contains(b))
    a = Point(1, 1).buffer(1)
    b = Point(3, 3).buffer(1)
    print(a.contains(b))
    a = Polygon([(2, 2), (2, 3), (3, 3), (3, 2)])
    b = Polygon([(1, 1), (1, 2), (2, 2), (2, 1)])
    print(a.contains(b))
    a = Point(1, 1).buffer(1)
    b = Point(2, 2).buffer(1.5)
    print(a.contains(b))
    a = Point(2, 2).buffer(1)
    b = Point(2, 2).buffer(1.5)
    print(a.contains(b))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    在这里插入图片描述
    在这里插入图片描述

    crosses

    a = Point(1, 1)
    b = Point(3, 3)
    print(a.crosses(b))
    a = Point(2, 2)
    b = Point(2, 2)
    print(a.crosses(b))
    a = Point(2, 1)
    b = LineString([(1, 2), (3, 2)])
    print(a.crosses(b))
    a = Point(2, 2)
    b = LineString([(1, 2), (3, 2)])
    print(a.crosses(b))
    a = Point(3, 1)
    b = Point(2, 2).buffer(1)
    print(a.crosses(b))
    a = Point(2, 2)
    b = Point(2, 2).buffer(1)
    print(a.crosses(b))
    a = Point(2, 1)
    b = Point(2, 2).buffer(1)
    print(a.crosses(b))
    a = LineString([(1, 1), (3, 1)])
    b = LineString([(1, 3), (3, 3)])
    print(a.crosses(b))
    a = LineString([(1, 2), (3, 2)])
    b = LineString([(2, 1), (2, 3)])
    print(a.crosses(b))
    a = LineString([(1, 0.5), (3, 0.5)])
    b = Point(2, 2).buffer(1)
    print(a.crosses(b))
    a = LineString([(1, 1), (3, 1)])
    b = Point(2, 2).buffer(1)
    print(a.crosses(b))
    a = LineString([(1, 1), (2, 2)])
    b = Point(2, 2).buffer(1)
    print(a.crosses(b))
    a = Point(1, 1).buffer(1)
    b = Point(3, 3).buffer(1)
    print(a.crosses(b))
    a = Polygon([(2, 2), (2, 3), (3, 3), (3, 2)])
    b = Polygon([(1, 1), (1, 2), (2, 2), (2, 1)])
    print(a.crosses(b))
    a = Point(1, 1).buffer(1)
    b = Point(2, 2).buffer(1.5)
    print(a.crosses(b))
    a = Point(2, 2).buffer(1)
    b = Point(2, 2).buffer(1.5)
    print(a.crosses(b))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    在这里插入图片描述

    disjoint

    a = Point(1, 1)
    b = Point(3, 3)
    print(a.disjoint(b))
    a = Point(2, 2)
    b = Point(2, 2)
    print(a.disjoint(b))
    a = Point(2, 1)
    b = LineString([(1, 2), (3, 2)])
    print(a.disjoint(b))
    a = Point(2, 2)
    b = LineString([(1, 2), (3, 2)])
    print(a.disjoint(b))
    a = Point(3, 1)
    b = Point(2, 2).buffer(1)
    print(a.disjoint(b))
    a = Point(2, 2)
    b = Point(2, 2).buffer(1)
    print(a.disjoint(b))
    a = Point(2, 1)
    b = Point(2, 2).buffer(1)
    print(a.disjoint(b))
    a = LineString([(1, 1), (3, 1)])
    b = LineString([(1, 3), (3, 3)])
    print(a.disjoint(b))
    a = LineString([(1, 2), (3, 2)])
    b = LineString([(2, 1), (2, 3)])
    print(a.disjoint(b))
    a = LineString([(1, 0.5), (3, 0.5)])
    b = Point(2, 2).buffer(1)
    print(a.disjoint(b))
    a = LineString([(1, 1), (3, 1)])
    b = Point(2, 2).buffer(1)
    print(a.disjoint(b))
    a = LineString([(1, 1), (2, 2)])
    b = Point(2, 2).buffer(1)
    print(a.disjoint(b))
    a = Point(1, 1).buffer(1)
    b = Point(3, 3).buffer(1)
    print(a.disjoint(b))
    a = Polygon([(2, 2), (2, 3), (3, 3), (3, 2)])
    b = Polygon([(1, 1), (1, 2), (2, 2), (2, 1)])
    print(a.disjoint(b))
    a = Point(1, 1).buffer(1)
    b = Point(2, 2).buffer(1.5)
    print(a.disjoint(b))
    a = Point(2, 2).buffer(1)
    b = Point(2, 2).buffer(1.5)
    print(a.disjoint(b))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    在这里插入图片描述

    intersects

    a = Point(1, 1)
    b = Point(3, 3)
    print(a.intersects(b))
    a = Point(2, 2)
    b = Point(2, 2)
    print(a.intersects(b))
    a = Point(2, 1)
    b = LineString([(1, 2), (3, 2)])
    print(a.intersects(b))
    a = Point(2, 2)
    b = LineString([(1, 2), (3, 2)])
    print(a.intersects(b))
    a = Point(3, 1)
    b = Point(2, 2).buffer(1)
    print(a.intersects(b))
    a = Point(2, 2)
    b = Point(2, 2).buffer(1)
    print(a.intersects(b))
    a = Point(2, 1)
    b = Point(2, 2).buffer(1)
    print(a.intersects(b))
    a = LineString([(1, 1), (3, 1)])
    b = LineString([(1, 3), (3, 3)])
    print(a.intersects(b))
    a = LineString([(1, 2), (3, 2)])
    b = LineString([(2, 1), (2, 3)])
    print(a.intersects(b))
    a = LineString([(1, 0.5), (3, 0.5)])
    b = Point(2, 2).buffer(1)
    print(a.intersects(b))
    a = LineString([(1, 1), (3, 1)])
    b = Point(2, 2).buffer(1)
    print(a.intersects(b))
    a = LineString([(1, 1), (2, 2)])
    b = Point(2, 2).buffer(1)
    print(a.intersects(b))
    a = Point(1, 1).buffer(1)
    b = Point(3, 3).buffer(1)
    print(a.intersects(b))
    a = Polygon([(2, 2), (2, 3), (3, 3), (3, 2)])
    b = Polygon([(1, 1), (1, 2), (2, 2), (2, 1)])
    print(a.intersects(b))
    a = Point(1, 1).buffer(1)
    b = Point(2, 2).buffer(1.5)
    print(a.intersects(b))
    a = Point(2, 2).buffer(1)
    b = Point(2, 2).buffer(1.5)
    print(a.intersects(b))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    在这里插入图片描述

    overlaps

    a = Point(1, 1)
    b = Point(3, 3)
    print(a.overlaps(b))
    a = Point(2, 2)
    b = Point(2, 2)
    print(a.overlaps(b))
    a = Point(2, 1)
    b = LineString([(1, 2), (3, 2)])
    print(a.overlaps(b))
    a = Point(2, 2)
    b = LineString([(1, 2), (3, 2)])
    print(a.overlaps(b))
    a = Point(3, 1)
    b = Point(2, 2).buffer(1)
    print(a.overlaps(b))
    a = Point(2, 2)
    b = Point(2, 2).buffer(1)
    print(a.overlaps(b))
    a = Point(2, 1)
    b = Point(2, 2).buffer(1)
    print(a.overlaps(b))
    a = LineString([(1, 1), (3, 1)])
    b = LineString([(1, 3), (3, 3)])
    print(a.overlaps(b))
    a = LineString([(1, 2), (3, 2)])
    b = LineString([(2, 1), (2, 3)])
    print(a.overlaps(b))
    a = LineString([(1, 0.5), (3, 0.5)])
    b = Point(2, 2).buffer(1)
    print(a.overlaps(b))
    a = LineString([(1, 1), (3, 1)])
    b = Point(2, 2).buffer(1)
    print(a.overlaps(b))
    a = LineString([(1, 1), (2, 2)])
    b = Point(2, 2).buffer(1)
    print(a.overlaps(b))
    a = Point(1, 1).buffer(1)
    b = Point(3, 3).buffer(1)
    print(a.overlaps(b))
    a = Polygon([(2, 2), (2, 3), (3, 3), (3, 2)])
    b = Polygon([(1, 1), (1, 2), (2, 2), (2, 1)])
    print(a.overlaps(b))
    a = Point(1, 1).buffer(1)
    b = Point(2, 2).buffer(1.5)
    print(a.overlaps(b))
    a = Point(2, 2).buffer(1)
    b = Point(2, 2).buffer(1.5)
    print(a.overlaps(b))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    在这里插入图片描述

    touches

    a = Point(1, 1)
    b = Point(3, 3)
    print(a.touches(b))
    a = Point(2, 2)
    b = Point(2, 2)
    print(a.touches(b))
    a = Point(2, 1)
    b = LineString([(1, 2), (3, 2)])
    print(a.touches(b))
    a = Point(2, 2)
    b = LineString([(1, 2), (3, 2)])
    print(a.touches(b))
    a = Point(3, 1)
    b = Point(2, 2).buffer(1)
    print(a.touches(b))
    a = Point(2, 2)
    b = Point(2, 2).buffer(1)
    print(a.touches(b))
    a = Point(2, 1)
    b = Point(2, 2).buffer(1)
    print(a.touches(b))
    a = LineString([(1, 1), (3, 1)])
    b = LineString([(1, 3), (3, 3)])
    print(a.touches(b))
    a = LineString([(1, 2), (3, 2)])
    b = LineString([(2, 1), (2, 3)])
    print(a.touches(b))
    a = LineString([(1, 0.5), (3, 0.5)])
    b = Point(2, 2).buffer(1)
    print(a.touches(b))
    a = LineString([(1, 1), (3, 1)])
    b = Point(2, 2).buffer(1)
    print(a.touches(b))
    a = LineString([(1, 1), (2, 2)])
    b = Point(2, 2).buffer(1)
    print(a.touches(b))
    a = Point(1, 1).buffer(1)
    b = Point(3, 3).buffer(1)
    print(a.touches(b))
    a = Polygon([(2, 2), (2, 3), (3, 3), (3, 2)])
    b = Polygon([(1, 1), (1, 2), (2, 2), (2, 1)])
    print(a.touches(b))
    a = Point(1, 1).buffer(1)
    b = Point(2, 2).buffer(1.5)
    print(a.touches(b))
    a = Point(2, 2).buffer(1)
    b = Point(2, 2).buffer(1.5)
    print(a.touches(b))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    在这里插入图片描述

    计算距离

    distance

    from shapely.geometry import Point, LineString, Polygon
    
    a = Point(1, 1)
    b = Point(3, 3)
    print(a.distance(b))
    a = Point(2, 2)
    b = Point(2, 2)
    print(a.distance(b))
    a = Point(2, 1)
    b = LineString([(1, 2), (3, 2)])
    print(a.distance(b))
    a = Point(2, 2)
    b = LineString([(1, 2), (3, 2)])
    print(a.distance(b))
    a = Point(3, 1)
    b = Point(2, 2).buffer(1)
    print(a.distance(b))
    a = Point(2, 2)
    b = Point(2, 2).buffer(1)
    print(a.distance(b))
    a = Point(2, 1)
    b = Point(2, 2).buffer(1)
    print(a.distance(b))
    a = LineString([(1, 1), (3, 1)])
    b = LineString([(1, 3), (3, 2)])
    print(a.distance(b))
    a = LineString([(1, 2), (3, 2)])
    b = LineString([(2, 1), (2, 3)])
    print(a.distance(b))
    a = LineString([(1, 0.5), (3, 0.5)])
    b = Point(2, 2).buffer(1)
    print(a.distance(b))
    a = LineString([(1, 1), (3, 1)])
    b = Point(2, 2).buffer(1)
    print(a.distance(b))
    a = LineString([(1, 1), (2, 2)])
    b = Point(2, 2).buffer(1)
    print(a.distance(b))
    a = Point(1, 1).buffer(1)
    b = Point(3, 3).buffer(1)
    print(a.distance(b))
    a = Polygon([(2, 2), (2, 3), (3, 3), (3, 2)])
    b = Polygon([(1, 1), (1, 2), (2, 2), (2, 1)])
    print(a.distance(b))
    a = Point(1, 1).buffer(1)
    b = Point(2, 2).buffer(1.5)
    print(a.distance(b))
    a = Point(2, 2).buffer(1)
    b = Point(2, 2).buffer(1.5)
    print(a.distance(b))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    在这里插入图片描述

    点面关系图代码

    # figures.py
    from math import sqrt
    from shapely import affinity
    
    GM = (sqrt(5)-1.0)/2.0
    W = 8.0
    H = W*GM
    SIZE = (W, H)
    
    BLUE = '#6699cc'
    GRAY = '#999999'
    DARKGRAY = '#333333'
    YELLOW = '#ffcc33'
    GREEN = '#339933'
    RED = '#ff3333'
    BLACK = '#000000'
    
    
    def add_origin(ax, geom, origin):
        x, y = xy = affinity.interpret_origin(geom, origin, 2)
        ax.plot(x, y, 'o', color=GRAY, zorder=1)
        ax.annotate(str(xy), xy=xy, ha='center',
                    textcoords='offset points', xytext=(0, 8))
    
    
    def set_limits(ax, x0, xN, y0, yN):
        ax.set_xlim(x0, xN)
        ax.set_xticks(range(x0, xN+1))
        ax.set_ylim(y0, yN)
        ax.set_yticks(range(y0, yN+1))
        ax.set_aspect("equal")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    # main.py
    import matplotlib.pyplot as plt
    from shapely.geometry import Point, LineString, Polygon
    from shapely.plotting import plot_polygon, plot_points, plot_line
    from figures import BLUE, GRAY, set_limits
    
    fig = plt.figure(1, figsize=(9, 9), dpi=300)
    fig.subplots_adjust(wspace=0.5, hspace=0.5)  # 调整边距和子图的间距
    
    ax = fig.add_subplot(4, 4, 1)
    a = Point(1, 1)
    b = Point(3, 3)
    plot_points(a, ax=ax, color=GRAY)
    plot_points(b, ax=ax, color=GRAY)
    plot_points(a.intersection(b), ax=ax, color=BLUE)
    ax.set_title(f'a.touches(b):{a.touches(b)}')
    set_limits(ax, 0, 4, 0, 4)
    
    ax = fig.add_subplot(4, 4, 2)
    a = Point(2, 2)
    b = Point(2, 2)
    plot_points(a, ax=ax, color=GRAY)
    plot_points(b, ax=ax, color=GRAY)
    plot_points(a.intersection(b), ax=ax, color=BLUE)
    ax.set_title(f'a.touches(b):{a.touches(b)}')
    set_limits(ax, 0, 4, 0, 4)
    
    ax = fig.add_subplot(4, 4, 3)
    a = Point(2, 1)
    b = LineString([(1, 2), (3, 2)])
    plot_points(a, ax=ax, color=GRAY)
    plot_line(b, ax=ax, color=GRAY)
    plot_points(a.intersection(b), ax=ax, color=BLUE)
    ax.set_title(f'a.touches(b):{a.touches(b)}')
    set_limits(ax, 0, 4, 0, 4)
    
    ax = fig.add_subplot(4, 4, 4)
    a = Point(2, 2)
    b = LineString([(1, 2), (3, 2)])
    plot_points(a, ax=ax, color=GRAY)
    plot_line(b, ax=ax, color=GRAY)
    plot_points(a.intersection(b), ax=ax, color=BLUE)
    ax.set_title(f'a.touches(b):{a.touches(b)}')
    set_limits(ax, 0, 4, 0, 4)
    
    ax = fig.add_subplot(4, 4, 5)
    a = Point(3, 1)
    b = Point(2, 2).buffer(1)
    plot_points(a, ax=ax, color=GRAY)
    plot_polygon(b, ax=ax, add_points=False, color=GRAY, alpha=0.2)
    plot_points(a.intersection(b), ax=ax, color=BLUE)
    ax.set_title(f'a.touches(b):{a.touches(b)}')
    set_limits(ax, 0, 4, 0, 4)
    
    ax = fig.add_subplot(4, 4, 6)
    a = Point(2, 2)
    b = Point(2, 2).buffer(1)
    plot_points(a, ax=ax, color=GRAY)
    plot_polygon(b, ax=ax, add_points=False, color=GRAY, alpha=0.2)
    plot_points(a.intersection(b), ax=ax, color=BLUE)
    ax.set_title(f'a.touches(b):{a.touches(b)}')
    set_limits(ax, 0, 4, 0, 4)
    
    ax = fig.add_subplot(4, 4, 7)
    a = Point(2, 1)
    b = Point(2, 2).buffer(1)
    plot_points(a, ax=ax, color=GRAY)
    plot_polygon(b, ax=ax, add_points=False, color=GRAY, alpha=0.2)
    plot_points(a.intersection(b), ax=ax, color=BLUE)
    ax.set_title(f'a.touches(b):{a.touches(b)}')
    set_limits(ax, 0, 4, 0, 4)
    
    ax = fig.add_subplot(4, 4, 8)
    a = LineString([(1, 1), (3, 1)])
    b = LineString([(1, 3), (3, 3)])
    plot_line(a, ax=ax, color=GRAY)
    plot_line(b, ax=ax, color=GRAY)
    plot_points(a.intersection(b), ax=ax, color=BLUE)
    ax.set_title(f'a.touches(b):{a.touches(b)}')
    set_limits(ax, 0, 4, 0, 4)
    
    ax = fig.add_subplot(4, 4, 9)
    a = LineString([(1, 2), (3, 2)])
    b = LineString([(2, 1), (2, 3)])
    plot_line(a, ax=ax, color=GRAY)
    plot_line(b, ax=ax, color=GRAY)
    plot_points(a.intersection(b), ax=ax, color=BLUE)
    ax.set_title(f'a.touches(b):{a.touches(b)}')
    set_limits(ax, 0, 4, 0, 4)
    
    ax = fig.add_subplot(4, 4, 10)
    a = LineString([(1, 0.5), (3, 0.5)])
    b = Point(2, 2).buffer(1)
    plot_line(a, ax=ax, color=GRAY)
    plot_polygon(b, ax=ax, add_points=False, color=GRAY, alpha=0.2)
    plot_points(a.intersection(b), ax=ax, color=BLUE)
    ax.set_title(f'a.touches(b):{a.touches(b)}')
    set_limits(ax, 0, 4, 0, 4)
    
    ax = fig.add_subplot(4, 4, 11)
    a = LineString([(1, 1), (3, 1)])
    b = Point(2, 2).buffer(1)
    plot_line(a, ax=ax, color=GRAY)
    plot_polygon(b, ax=ax, add_points=False, color=GRAY, alpha=0.2)
    plot_line(a.intersection(b), ax=ax, color=BLUE)
    ax.set_title(f'a.touches(b):{a.touches(b)}')
    set_limits(ax, 0, 4, 0, 4)
    
    ax = fig.add_subplot(4, 4, 12)
    a = LineString([(1, 1), (2, 2)])
    b = Point(2, 2).buffer(1)
    plot_line(a, ax=ax, color=GRAY)
    plot_polygon(b, ax=ax, add_points=False, color=GRAY, alpha=0.2)
    plot_line(a.intersection(b), ax=ax, color=BLUE)
    ax.set_title(f'a.touches(b):{a.touches(b)}')
    set_limits(ax, 0, 4, 0, 4)
    
    ax = fig.add_subplot(4, 4, 13)
    a = Point(1, 1).buffer(1)
    b = Point(3, 3).buffer(1)
    plot_polygon(a, ax=ax, add_points=False, color=GRAY, alpha=0.2)
    plot_polygon(b, ax=ax, add_points=False, color=GRAY, alpha=0.2)
    plot_points(a.intersection(b), ax=ax, color=BLUE)
    ax.set_title(f'a.touches(b):{a.touches(b)}')
    set_limits(ax, 0, 4, 0, 4)
    
    ax = fig.add_subplot(4, 4, 14)
    a = Polygon([(2, 2), (2, 3), (3, 3), (3, 2)])
    b = Polygon([(1, 1), (1, 2), (2, 2), (2, 1)])
    plot_polygon(a, ax=ax, add_points=False, color=GRAY, alpha=0.2)
    plot_polygon(b, ax=ax, add_points=False, color=GRAY, alpha=0.2)
    plot_points(a.intersection(b), ax=ax, color=BLUE)
    ax.set_title(f'a.touches(b):{a.touches(b)}')
    set_limits(ax, 0, 4, 0, 4)
    
    ax = fig.add_subplot(4, 4, 15)
    a = Point(1, 1).buffer(1)
    b = Point(2, 2).buffer(1.5)
    plot_polygon(a, ax=ax, add_points=False, color=GRAY, alpha=0.2)
    plot_polygon(b, ax=ax, add_points=False, color=GRAY, alpha=0.2)
    plot_polygon(a.intersection(b), ax=ax, add_points=False, color=BLUE)
    ax.set_title(f'a.touches(b):{a.touches(b)}')
    set_limits(ax, 0, 4, 0, 4)
    
    ax = fig.add_subplot(4, 4, 16)
    a = Point(2, 2).buffer(1)
    b = Point(2, 2).buffer(1.5)
    plot_polygon(a, ax=ax, add_points=False, color=GRAY, alpha=0.2)
    plot_polygon(b, ax=ax, add_points=False, color=GRAY, alpha=0.2)
    plot_polygon(a.intersection(b), ax=ax, add_points=False, color=BLUE)
    ax.set_title(f'a.touches(b):{a.touches(b)}')
    set_limits(ax, 0, 4, 0, 4)
    
    plt.savefig('output.png')
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
  • 相关阅读:
    springboot - 2.7.3版本 - (七)整合Kafka
    Java抽象类和接口
    scratch班级成绩处理 电子学会图形化编程scratch等级考试四级真题和答案解析2022年9月
    百题千解计划【CSDN每日一练】收件邮箱(使用Python、Java、JavaScript解决)无敌的Python正则表达式、零宽负向断言
    APS在印刷行业的应用前景和应用效益
    HBase在大数据集群的安装部署及整合Phoenix
    Python学习笔记——存储容器
    开源实时数仓 Apache Doris 毕业了,未来如何走得更远?
    Java基础语法
    128-根据给定的字符串,建立二叉树
  • 原文地址:https://blog.csdn.net/ymzhu385/article/details/133613409