python使用shapely判断两条线是否相交

  • 安装shapely模块
    pip install shapely

实现代码

import shapely.wkt as wkt


if __name__ == '__main__':
    line1 = wkt.loads("LINESTRING (116.23760690616871 40.17090730499467, 116.23760690616871 40.17091615348095)")
    line2 = wkt.loads("LINESTRING (116.23761690616871 40.17092015348095, 116.23760690616871 40.17092015348095)")
    # 判断线线是否相交
    print(line1.intersects(line2))
    # 增加0.3米的buffer, 这里0.3/100000后的单位是米
    polygon1 = line1.buffer(0.3/100000)
    polygon2 = line2.buffer(0.3/100000)
    # 判断线段扩展buffer后的范围是否相交
    print(polygon1.intersects(polygon2))

结果

False
True

python使用shapely判断两条线是否相交_第1张图片




BaseGeometry.buffer()方法参数说明


1. cap_style-----端点扩展方式

  • 1(CAP_STYLE.round) 默认,半圆形扩展
  • 2(CAP_STYLE.flat) 不扩展
  • 3(CAP_STYLE.square) 方形扩展

端点按半圆形扩展

line = wkt.loads("LINESTRING (116.23760690616871 40.17090730499467, 116.23760690616871 40.17091615348095)")
polygon = line.buffer(0.5/100000)

python使用shapely判断两条线是否相交_第2张图片

端点不扩展

line = wkt.loads("LINESTRING (116.23761690616871 40.17092015348095, 116.23760690616871 40.17092015348095)")
polygon = line.buffer(0.2/100000, cap_style=2)

python使用shapely判断两条线是否相交_第3张图片

端点按方形扩展

line = wkt.loads("LINESTRING (116.23761690616871 40.17092015348095, 116.23760690616871 40.17092015348095)")
polygon = line.buffer(0.2/100000, cap_style=3)

python使用shapely判断两条线是否相交_第4张图片

2. join_style-----折角连接方式

  • 1(JOIN_STYLE.round) 默认,圆弧连接
  • 2(JOIN_STYLE.mitre) 折角连接
  • 3(JOIN_STYLE.bevel)斜面连接

圆弧连接

line = wkt.loads("LINESTRING (116.23761690616871 40.17092015348095, 116.23760690616871 40.17092015348095, 116.23759890616871 40.17091530499467)")
polygon = line.buffer(0.2/100000, join_style=1)

python使用shapely判断两条线是否相交_第5张图片

折角连接

line = wkt.loads("LINESTRING (116.23761690616871 40.17092015348095, 116.23760690616871 40.17092015348095, 116.23759890616871 40.17091530499467)")
polygon = line.buffer(0.2/100000, join_style=2)

python使用shapely判断两条线是否相交_第6张图片

斜面连接

line = wkt.loads("LINESTRING (116.23761690616871 40.17092015348095, 116.23760690616871 40.17092015348095, 116.23759890616871 40.17091530499467)")
polygon = line.buffer(0.2/100000, join_style=3)

python使用shapely判断两条线是否相交_第7张图片

你可能感兴趣的:(Goemetry,python)