Shapely计算LineString交点不在线上的问题求助
问题:Shapely计算LineString交点无法用于路网拆分
我有一个道路路网,需要在两条LineString道路的交点处对路网进行拆分。但通过Shapely计算得到的交点并不与任意一条线相交,该点与两条线的距离均不为0。我尝试了查找线上最近点、使用line.interpolate(line.project(pt))方法、snap工具等方案,但均未解决问题。
以下是可复现的示例代码:
from shapely.geometry import Point, LineString from shapely.ops import nearest_points from shapely.ops import snap # 定义LineString的坐标 coords1 = [ (73.0867321, 33.6644942), (73.08678639972806, 33.66455575008311), (73.08684069953381, 33.66461730014245), (73.08689499941724, 33.66467885017808), (73.08694929937838, 33.66474040018994), (73.08700359941724, 33.66480195017806), (73.08705789953379, 33.664863500142445), (73.08711219972804, 33.6649250500831), (73.0871665, 33.6649866) ] coords2 = [ (73.08690239999999, 33.6650841), (73.08694961275046, 33.665018912562815), (73.08699682542931, 33.664953725107694), (73.08704403803665, 33.664888537634624), (73.08709125057243, 33.66482335014359), (73.08713846303664, 33.66475816263463), (73.08718567542934, 33.6646929751077), (73.08723288775042, 33.66462778756282), (73.08728009999999, 33.6645626), (73.0873316, 33.664484), (73.08737451271202, 33.66442327505189), (73.0899173, 33.6613607), (73.08996137509759, 33.661297200023455), (73.09000545013011, 33.66123370003129), (73.09004952509757, 33.66117020002346), (73.0900936, 33.6611067), (73.09013767509688, 33.661043400023274), (73.09018175012917, 33.660980100031026), (73.0902258250969, 33.66091680002327), (73.09026989999998, 33.6608535), (73.09031404732161, 33.66081331574375), (73.09035819461442, 33.66077313147986), (73.0904023418784, 33.66073294720829), (73.09157271456951, 33.65874581072471), (73.09160897388341, 33.65869747008384), (73.09164523315653, 33.658649129432376), (73.09168149238894, 33.658600788770315), (73.09171775158057, 33.65855244809768), (73.0917540107315, 33.658504107414444), (73.09179026984168, 33.658455766720635), (73.09182652891113, 33.658407426016254), (73.09186278793982, 33.65835908530127), (73.09189904692778, 33.658310744575715), (73.09193530587498, 33.65826240383956), (73.09197156478149, 33.65821406309283), (73.09200782364721, 33.6581657223355), (73.09204408247223, 33.65811738156759), (73.09208034125646, 33.65806904078909), (73.0921166, 33.6580207) ] # 创建LineString对象 line2 = LineString(coords2) line1 = LineString(coords1) print(line1.intersects(line2)) intersection = line1.intersection(line2) # 修正原代码笔误:intersect改为intersection print(intersection.intersects(line1)) print(line2.project(intersection)) p = line2.interpolate(line2.project(intersection)) print(p.intersects(line2)) snap_precision = 0.0001 snap_line1 = snap(intersection, line1, snap_precision) snap_line2 = snap(intersection, line2, snap_precision) np0, np1 = nearest_points(intersection, line1) print(np1.intersects(line1))
我参考了相关帖子尝试line1.interpolate(line1.project(pt))方法,但也未能解决问题。
内容的提问来源于stack exchange,提问作者Carla S.
相关产品推荐
相关产品推荐

