1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| import math from dataclasses import dataclass
@dataclass class Scale: x: float y: float height: float
map_scales = [ Scale(6 * 3600, 4 * 3600, 445280), Scale(30 * 60, 30 * 60, 55660), Scale(15 * 60, 10 * 60, 27830), Scale(60, 60, 1850), Scale(4, 4, 123.69), Scale(2, 2, 61.84), Scale(1 / 4, 1 / 4, 7.73), Scale(1 / 32, 1 / 32, 0.97), Scale(1 / 256, 1 / 256, 0.121), Scale(1 / 2048, 1 / 2048, 0.015) ]
lat = 77.99316382222223 lon = 116.31260311944445
def beidou(lat, lon, level): code = "" code += "N" if lat >= 0 else "S" lat = abs(lat)
minLon = -180 * 3600 minLat = 0 lon = lon * 3600 lat = lat * 3600
for i in range(level): scale = map_scales[i] scaleX, scaleY = scale.x, scale.y i += 1
if i == 1: x = math.floor((lon - minLon) / scaleX) + 1 code += str(x) minLon = minLon + (x - 1) * scaleX
y = math.floor((lat - minLat) / scaleY) + 65 code += chr(y) minLat = minLat + (y - 65) * scaleY else: x = math.floor((lon - minLon) / scaleX) y = math.floor((lat - minLat) / scaleY) if level in (3, 6): code += "{:X}".format(x + y * 2) else: code += "{:X}{:X}".format(x, y) minLon = minLon + x * scaleX minLat = minLat + y * scaleY return code
print(beidou(lat, lon, 10))
|