如何获取折线(Polyline)两点间的所有坐标?
获取折线两点间全部坐标的解决方案
Hey there, let's figure out how to grab every coordinate along the polyline between your points A and B. I'll cover a couple of practical approaches depending on whether you prefer coding or using a GUI tool:
1. Python实现(用Shapely库)
This is great if you want to automate the process or integrate it into a script. First, we'll use the Shapely library, which makes geometric operations super straightforward.
- First, install the dependency:
pip install shapely - Then use this code snippet:
小提示:Shapely默认用经度在前的坐标顺序,所以我们定义点时调换了你给出的纬度和经度顺序。from shapely.geometry import LineString # 定义你的点——注意:Shapely采用「经度, 纬度」的顺序! point_a = (-9.263187, 39.091868) point_b = (-9.261857, 39.089815) # 创建两点间的折线对象(LineString) line = LineString([point_a, point_b]) # 调整步长来控制坐标点的密度(数值越小,生成的点越多) step_distance = 0.0001 # 单位为度 total_points = int(line.length / step_distance) + 1 # 生成折线上的所有坐标 all_coordinates = [line.interpolate(i / total_points, normalized=True).coords[0] for i in range(total_points)] # 转换回「纬度, 经度」的顺序输出 for lon, lat in all_coordinates: print(f"纬度: {lat:.6f}, 经度: {lon:.6f}")step_distance参数可以灵活调整,数值越小,生成的坐标点越密集。
2. GUI工具:QGIS
如果不想写代码,免费开源的QGIS是个省心的选择:
- 打开QGIS,新建一个点图层,将点A和点B添加进去
- 使用「两点连线」工具生成两点间的折线
- 选中这条折线,点击顶部菜单栏
矢量 > 几何工具 > 提取顶点 - 操作完成后会生成一个新图层,里面包含折线上的所有坐标点——你可以打开属性表查看经纬度,也可以导出为CSV文件备用。
关键注意事项
- 坐标顺序:不同工具对坐标顺序(纬度/经度 vs 经度/纬度)要求不同,一定要仔细核对,不然会出现位置偏移的问题!
- 点密度控制:如果需要更精细的坐标点,只需缩小Python代码里的步长,或者在QGIS的提取顶点工具里调整参数。
- 非直线折线:如果你的折线不是A到B的直线(比如有中间拐点),这些方法依然适用——只需传入完整的折线几何对象,而不是示例中的两点连线。
内容的提问来源于stack exchange,提问作者Rafi Ullah Patel




