From c827566d03e55e4422129dcf57d070fbca3bf959 Mon Sep 17 00:00:00 2001 From: luckykoi Date: Fri, 7 Mar 2025 00:24:33 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8D=95=E7=A4=BA=E4=BE=8B=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- amap_route_planning.html | 181 +++++++++++++++++++++++++++++++++++++++ baidu_map.py | 92 ++++++++++++++++++++ crc16_test.py | 57 ++++++++++++ ups_info.py | 78 +++++++++++++++++ 4 files changed, 408 insertions(+) create mode 100644 amap_route_planning.html create mode 100644 baidu_map.py create mode 100644 crc16_test.py create mode 100644 ups_info.py diff --git a/amap_route_planning.html b/amap_route_planning.html new file mode 100644 index 0000000..a602a1c --- /dev/null +++ b/amap_route_planning.html @@ -0,0 +1,181 @@ + + + + + + + 地点关键字 + 驾车路线规划 // 手动初始化定位版本 + + + + + + +
+
+ + + +
+
+ + + + + + diff --git a/baidu_map.py b/baidu_map.py new file mode 100644 index 0000000..f53c012 --- /dev/null +++ b/baidu_map.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python +# -*-coding:utf-8 -*- + +''' +# @Author :幸运锦鲤 +# @Time : 2025-03-07 00:14:56 +# @version : python3 +# @Update time : +# @Description : 百度地图坐标转换示例代码 +''' + +import math + +def bd09_to_gcj02(bd_lon, bd_lat): + """ + 将 BD-09 坐标系转换为 GCJ-02 坐标系 + :param bd_lon: BD-09 经度 + :param bd_lat: BD-09 纬度 + :return: GCJ-02 坐标 (lon, lat) + """ + x = bd_lon - 0.0065 + y = bd_lat - 0.006 + z = math.sqrt(x * x + y * y) - 0.00002 * math.sin(y * math.pi) + theta = math.atan2(y, x) - 0.000003 * math.cos(x * math.pi) + gcj_lon = z * math.cos(theta) + gcj_lat = z * math.sin(theta) + return gcj_lon, gcj_lat + +def gcj02_to_wgs84(gcj_lon, gcj_lat): + """ + 将 GCJ-02 坐标系转换为 WGS-84 坐标系 + :param gcj_lon: GCJ-02 经度 + :param gcj_lat: GCJ-02 纬度 + :return: WGS-84 坐标 (lon, lat) + """ + a = 6378245.0 # 克拉索夫斯基椭球参数长半轴 + ee = 0.00669342162296594323 # 克拉索夫斯基椭球参数第一偏心率平方 + PI = math.pi + + # 转换公式 + dLat = transform_lat(gcj_lon - 105.0, gcj_lat - 35.0) + dLon = transform_lon(gcj_lon - 105.0, gcj_lat - 35.0) + radLat = gcj_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) + wgs_lon = gcj_lon - dLon + wgs_lat = gcj_lat - dLat + return wgs_lon, wgs_lat + +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 * math.pi) + 20.0 * math.sin(2.0 * x * math.pi)) * 2.0 / 3.0 + ret += (20.0 * math.sin(y * math.pi) + 40.0 * math.sin(y / 3.0 * math.pi)) * 2.0 / 3.0 + ret += (160.0 * math.sin(y / 12.0 * math.pi) + 320 * math.sin(y * math.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 * math.pi) + 20.0 * math.sin(2.0 * x * math.pi)) * 2.0 / 3.0 + ret += (20.0 * math.sin(x * math.pi) + 40.0 * math.sin(x / 3.0 * math.pi)) * 2.0 / 3.0 + ret += (150.0 * math.sin(x / 12.0 * math.pi) + 300.0 * math.sin(x / 30.0 * math.pi)) * 2.0 / 3.0 + return ret + +def bd09_to_wgs84(bd_lon, bd_lat): + """ + 将 BD-09 坐标系转换为 WGS-84 坐标系 + :param bd_lon: BD-09 经度 + :param bd_lat: BD-09 纬度 + :return: WGS-84 坐标 (lon, lat) + """ + # 先将 BD-09 转换为 GCJ-02 + gcj_lon, gcj_lat = bd09_to_gcj02(bd_lon, bd_lat) + # 再将 GCJ-02 转换为 WGS-84 + wgs_lon, wgs_lat = gcj02_to_wgs84(gcj_lon, gcj_lat) + return wgs_lon, wgs_lat + +# 示例 +bd_lon, bd_lat = 116.510484,39.896936 # 百度地图坐标 +wgs_lon, wgs_lat = bd09_to_wgs84(bd_lon, bd_lat) +print(f"WGS-84 坐标: ({wgs_lon}, {wgs_lat})") + + + diff --git a/crc16_test.py b/crc16_test.py new file mode 100644 index 0000000..db455ea --- /dev/null +++ b/crc16_test.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python +# -*-coding:utf-8 -*- + +''' +# @Author :幸运锦鲤 +# @Time : 2025-03-07 00:22:47 +# @version : python3 +# @Update time : +# @Description : crc16校验码计算示例代码 +''' + + +def crc16(data: str) -> str: + # 将输入字符串按空格分割,并将每个部分转换为字节 + byte_list = [int(byte, 16) for byte in data.split()] + + # 计算CRC16校验码 + crc = 0xFFFF + for b in byte_list: + cur_byte = 0xFF & b + for _ in range(0, 8): + if (crc & 0x0001) ^ (cur_byte & 0x0001): + crc = (crc >> 1) ^ 0xA001 + else: + crc >>= 1 + cur_byte >>= 1 + + # 将CRC16校验码转换为大端序(高位在前) + crc = ((crc & 0xFF) << 8) | (crc >> 8) + + # 将CRC16校验码转换为两位十六进制字符串 + crc_high = (crc >> 8) & 0xFF + crc_low = crc & 0xFF + crc_str = f"{crc_high:02X}{crc_low:02X}" + + # 返回原始数据加上CRC16校验码的字符串(无空格) + return f"{data.replace(' ', '')}{crc_str}" + +# 示例用法 +input_data = "01 03 00 00 00 01" +result = crc16(input_data) +print(result) # 输出: "010300000001840A" + +input_data = "07 04 00 00 00 02" +result = crc16(input_data) +print(result) + + + + +input_data = "05 06 00 00 00 01" +result = crc16(input_data) +print(result) + +spaced_hex_str = ' '.join(result[i:i+2] for i in range(0, len(result), 2)) +print("result:",spaced_hex_str) + diff --git a/ups_info.py b/ups_info.py new file mode 100644 index 0000000..eb58680 --- /dev/null +++ b/ups_info.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python +# -*-coding:utf-8 -*- + +''' +# @Author :幸运锦鲤 +# @Time : 2025-02-02 16:09:53 +# @version : python3 +# @Update time : +# @Description : UPS信息获取,用于后期处理示例代码 +''' + +from pynut2 import nut2 as PyNUT +import json + + +class UPSInfo: + def __init__(self, nut_server='127.0.0.1', nut_port=3493, ups_name='my_ups_name'): + """初始化UPSInfo实例""" + self.nut_server = nut_server + self.nut_port = nut_port + self.ups_name = ups_name + + def build_nested_dict(self, d, keys, value): + """辅助函数,用于递归构建嵌套字典""" + if len(keys) == 1: + d[keys[0]] = value + else: + if keys[0] not in d: + d[keys[0]] = {} + self.build_nested_dict(d[keys[0]], keys[1:], value) + + def get_ups_data(self): + """获取UPS数据并返回JSON格式""" + try: + client = PyNUT.PyNUTClient(host=self.nut_server, port=self.nut_port) + ups_vars = client.list_vars(self.ups_name) + + # 组织数据为字典形式 + ups_data = {} + for key, value in ups_vars.items(): + parts = key.split(':') + self.build_nested_dict(ups_data, parts, value) + + # 返回JSON格式字符串 + return json.dumps(ups_data, indent=4) + except Exception as e: + # 返回错误信息的JSON + error_response = { + "error": "Failed to retrieve UPS data", + "details": str(e) + } + return json.dumps(error_response, indent=4) + + def get_ups_data_as_dict(self): + """获取UPS数据并返回字典格式""" + try: + client = PyNUT.PyNUTClient(host=self.nut_server, port=self.nut_port) + ups_vars = client.list_vars(self.ups_name) + + # 组织数据为字典形式 + ups_data = {} + for key, value in ups_vars.items(): + parts = key.split(':') + self.build_nested_dict(ups_data, parts, value) + + # 返回字典 + return ups_data + except Exception as e: + # 返回错误信息的字典 + return { + "error": "Failed to retrieve UPS data", + "details": str(e) + } + +if __name__ == '__main__': + # 示例调用,方便测试 + ups_info = UPSInfo(nut_server="192.168.100.100", nut_port=3493, ups_name="ups") + print(ups_info.get_ups_data())