QGIS插件开发实战:手把手教你用Python调用高德地图API做路径规划(附坐标转换避坑指南)
QGIS插件开发实战Python集成高德地图API的路径规划全流程解析当我们需要在QGIS中实现路径规划功能时高德地图API提供了丰富的服务接口。本文将带你从零开始开发一个能够调用高德地图API进行路径规划的QGIS插件并重点解决开发过程中最棘手的坐标转换问题。1. 开发环境准备与插件框架搭建在开始开发前我们需要配置好开发环境。QGIS插件开发主要依赖以下工具链QGIS 3.x建议使用LTR长期支持版本Python 3.x与QGIS版本匹配的Python环境PyCharm作为主要开发IDEQt Designer用于设计插件界面首先我们需要在PyCharm中配置QGIS的Python解释器。这可以通过创建一个批处理文件来实现echo off SET OSGEO4W_ROOTC:\Program Files\QGIS 3.x call %OSGEO4W_ROOT%\bin\o4w_env.bat call %OSGEO4W_ROOT%\apps\qt5\bin\qt5_env.bat call %OSGEO4W_ROOT%\apps\Python37\Scripts\pyqt5_env.bat path %PATH%;%OSGEO4W_ROOT%\apps\qgis\bin path %PATH%;%OSGEO4W_ROOT%\apps\Python37\Scripts set PYTHONPATH%OSGEO4W_ROOT%\apps\qgis\python;%PYTHONPATH% set QT_PLUGIN_PATH%OSGEO4W_ROOT%\apps\qgis\qtplugins;%QT_PLUGIN_PATH% start PyCharm aware of QGIS /B C:\Program Files\JetBrains\PyCharm\bin\pycharm64.exe %*使用QGIS的Plugin Builder工具可以快速生成插件框架python pb_tool.py create这会生成以下主要文件结构MyPlugin/ ├── __init__.py ├── metadata.txt ├── my_plugin.py ├── my_plugin_dialog.py ├── resources.qrc ├── resources.py └── ui_my_plugin.py2. 高德地图API集成与调用高德地图提供了多种路径规划API接口我们需要先申请开发者密钥。API主要支持以下几种出行方式出行方式API端点必需参数驾车/v3/direction/drivingorigin, destination步行/v3/direction/walkingorigin, destination骑行/v4/direction/bicyclingorigin, destination公交/v3/direction/transit/integratedorigin, destination, cityAPI调用的基本流程如下获取起点和终点的坐标构造API请求URL发送HTTP请求并处理响应解析返回的路径数据以下是驾车路径规划的示例代码def get_driving_route(origin, destination, api_key): url fhttps://restapi.amap.com/v3/direction/driving?origin{origin[0]},{origin[1]}destination{destination[0]},{destination[1]}key{api_key} response requests.get(url) data response.json() if data[status] 1 and data[route][paths]: route data[route][paths][0] distance float(route[distance]) # 单位米 duration float(route[duration]) # 单位秒 steps [] polyline [] for step in route[steps]: step_info { instruction: step[instruction], distance: step[distance], duration: step[duration], road: step.get(road, ) } steps.append(step_info) # 解析polyline坐标 for point in step[polyline].split(;): lon, lat map(float, point.split(,)) polyline.append((lon, lat)) return distance, duration, polyline, steps else: raise Exception(fAPI调用失败: {data.get(info, 未知错误)})3. 坐标转换GCJ-02与WGS84互转高德地图API返回的坐标使用GCJ-02坐标系俗称火星坐标而QGIS通常使用WGS84坐标系。要实现坐标的准确叠加我们需要进行坐标系转换。3.1 WGS84转GCJ-02发送请求前我们需要将QGIS中的WGS84坐标转换为GCJ-02def wgs84_to_gcj02(lon, lat, api_key): url fhttps://restapi.amap.com/v3/assistant/coordinate/convert?locations{lon},{lat}coordsysgpskey{api_key} response requests.get(url) data response.json() if data[status] 1: return map(float, data[locations].split(,)) else: raise Exception(f坐标转换失败: {data.get(info, 未知错误)})3.2 GCJ-02转WGS84高德地图没有提供官方的GCJ-02转WGS84接口我们需要自己实现转换算法。以下是基于迭代法的转换实现import math # 定义常量 a 6378245.0 # 长半轴 f 1 / 298.3 # 扁率 ee 1 - (1 - f) * (1 - f) # 第一偏心率平方 PI math.pi def out_of_china(lon, lat): 判断坐标是否在中国境外 return not (72.004 lon 137.8347 and 0.8293 lat 55.8271) def transform_lat(x, y): 纬度转换 ret -100.0 2.0 * x 3.0 * y 0.2 * y * y 0.1 * x * y 0.2 * math.sqrt(abs(x)) ret (20.0 * math.sin(6.0 * x * PI) 20.0 * math.sin(2.0 * x * PI)) * 2.0 / 3.0 ret (20.0 * math.sin(y * PI) 40.0 * math.sin(y / 3.0 * PI)) * 2.0 / 3.0 ret (160.0 * math.sin(y / 12.0 * PI) 320.0 * math.sin(y * PI / 30.0)) * 2.0 / 3.0 return ret def transform_lon(x, y): 经度转换 ret 300.0 x 2.0 * y 0.1 * x * x 0.1 * x * y 0.1 * math.sqrt(abs(x)) ret (20.0 * math.sin(6.0 * x * PI) 20.0 * math.sin(2.0 * x * PI)) * 2.0 / 3.0 ret (20.0 * math.sin(x * PI) 40.0 * math.sin(x / 3.0 * PI)) * 2.0 / 3.0 ret (150.0 * math.sin(x / 12.0 * PI) 300.0 * math.sin(x * PI / 30.0)) * 2.0 / 3.0 return ret def wgs84_to_gcj02(lon, lat): WGS84转GCJ02 if out_of_china(lon, lat): return lon, lat dLat transform_lat(lon - 105.0, lat - 35.0) dLon transform_lon(lon - 105.0, lat - 35.0) radLat lat / 180.0 * PI magic math.sin(radLat) magic 1 - ee * magic * magic sqrtMagic math.sqrt(magic) dLat (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * PI) dLon (dLon * 180.0) / (a / sqrtMagic * math.cos(radLat) * PI) gcjLat lat dLat gcjLon lon dLon return gcjLon, gcjLat def gcj02_to_wgs84(lon, lat): GCJ02转WGS84迭代法 g0 (lon, lat) w0 g0 g1 wgs84_to_gcj02(w0[0], w0[1]) w1 (w0[0] - (g1[0] - g0[0]), w0[1] - (g1[1] - g0[1])) delta (w1[0] - w0[0], w1[1] - w0[1]) while abs(delta[0]) 1e-6 or abs(delta[1]) 1e-6: w0 w1 g1 wgs84_to_gcj02(w0[0], w0[1]) w1 (w0[0] - (g1[0] - g0[0]), w0[1] - (g1[1] - g0[1])) delta (w1[0] - w0[0], w1[1] - w0[1]) return w14. 插件功能实现与优化4.1 多线程处理为了避免界面卡顿我们需要使用QThread来实现后台任务处理from PyQt5.QtCore import QThread, pyqtSignal class RoutePlanningWorker(QThread): finished pyqtSignal(list) progress pyqtSignal(int, int) error pyqtSignal(str) log pyqtSignal(str) def __init__(self, od_pairs, mode, api_key, crs_src, parentNone): super().__init__(parent) self.od_pairs od_pairs self.mode mode self.api_key api_key self.crs_src crs_src self.stop_flag False def run(self): try: results [] total len(self.od_pairs) for idx, (origin, destination) in enumerate(self.od_pairs): if self.stop_flag: self.log.emit(用户中断操作) break try: # 坐标转换和路径规划处理 # ... self.progress.emit(idx 1, total) except Exception as e: self.log.emit(f路径规划失败: {str(e)}) continue self.finished.emit(results) except Exception as e: self.error.emit(f任务执行出错: {str(e)})4.2 路径可视化将规划好的路径在QGIS中可视化def create_route_layer(polyline, crs, name路径规划): 创建路径图层 from qgis.core import QgsVectorLayer, QgsFeature, QgsGeometry, QgsLineString # 创建线图层 layer QgsVectorLayer(fLineString?crs{crs.authid()}, name, memory) provider layer.dataProvider() # 创建要素 feature QgsFeature() points [QgsPointXY(lon, lat) for lon, lat in polyline] line QgsLineString(points) feature.setGeometry(QgsGeometry(line)) # 添加要素到图层 provider.addFeature(feature) layer.updateExtents() return layer4.3 结果导出支持将结果导出为CSV文件def export_to_csv(results, file_path): 导出结果到CSV import csv with open(file_path, w, newline, encodingutf-8) as f: writer csv.writer(f) writer.writerow([起点ID, 终点ID, 距离(米), 时间(秒)]) for result in results: writer.writerow([ result[origin_id], result[destination_id], result[distance], result[duration] ])5. 常见问题与调试技巧在开发过程中可能会遇到以下典型问题坐标偏移问题现象路径显示位置与实际位置有偏差解决确保正确执行了GCJ-02和WGS84之间的转换调试方法使用已知坐标点测试转换函数API调用限制高德地图API有调用频率限制解决方案实现请求间隔控制import time for od in od_pairs: # 调用API get_route(od) time.sleep(0.2) # 控制请求频率多线程同步问题现象界面卡顿或崩溃解决确保所有UI操作都在主线程执行使用信号槽机制进行线程间通信内存泄漏长时间运行后QGIS变慢解决及时释放不再使用的图层和对象使用QGIS内置的垃圾回收机制坐标系不一致现象路径显示在错误的位置解决检查所有图层的坐标系设置使用QgsCoordinateTransform进行坐标系转换# 坐标系转换示例 def transform_coordinates(point, src_crs, dest_crs): transform QgsCoordinateTransform(src_crs, dest_crs, QgsProject.instance()) return transform.transform(point)开发QGIS插件集成高德地图API时坐标转换是最关键的技术难点。通过本文介绍的迭代转换算法可以较好地解决GCJ-02与WGS84之间的转换问题。在实际项目中建议对转换结果进行抽样验证确保精度满足需求。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2458365.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!