博客断更很久了,来水一篇(惭愧惭愧😭)…….
最近在GIS群看到有人在谈论北斗网格编码,我还是对北斗高精导航很有好感的,于是乎就看了这个编码,嗯嗯,确实是过于简单了,然后就练手写了个demo
如果你熟悉GIS中的RTree、四叉树、八叉树、GeoHash等,那原理几乎是不变的
我能想到它的唯一优点,应该是做政企项目的时候,政企统一了编码规范
代码如下,从Github上的java代码翻译得到,简单起见,忽略了高度编码

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))