-
Notifications
You must be signed in to change notification settings - Fork 1
/
geohash.sql
291 lines (249 loc) · 6.07 KB
/
geohash.sql
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
DROP FUNCTION IF EXISTS [dbo].[geohash_bit]
GO
CREATE FUNCTION [dbo].[geohash_bit](
@_bit TINYINT
)
RETURNS TINYINT
AS
BEGIN
DECLARE @bit TINYINT = NULL;
RETURN CASE @_bit
WHEN 0 THEN 16
WHEN 1 THEN 8
WHEN 2 THEN 4
WHEN 3 THEN 2
WHEN 4 THEN 1
END
END
GO
DROP FUNCTION IF EXISTS [dbo].[geohash_base32]
GO
CREATE FUNCTION [dbo].[geohash_base32] (
@_index TINYINT
)
RETURNS CHAR(1)
AS
BEGIN
RETURN CASE @_index
WHEN 0 THEN '0'
WHEN 1 THEN '1'
WHEN 2 THEN '2'
WHEN 3 THEN '3'
WHEN 4 THEN '4'
WHEN 5 THEN '5'
WHEN 6 THEN '6'
WHEN 7 THEN '7'
WHEN 8 THEN '8'
WHEN 9 THEN '9'
WHEN 10 THEN 'b'
WHEN 11 THEN 'c'
WHEN 12 THEN 'd'
WHEN 13 THEN 'e'
WHEN 14 THEN 'f'
WHEN 15 THEN 'g'
WHEN 16 THEN 'h'
WHEN 17 THEN 'j'
WHEN 18 THEN 'k'
WHEN 19 THEN 'm'
WHEN 20 THEN 'n'
WHEN 21 THEN 'p'
WHEN 22 THEN 'q'
WHEN 23 THEN 'r'
WHEN 24 THEN 's'
WHEN 25 THEN 't'
WHEN 26 THEN 'u'
WHEN 27 THEN 'v'
WHEN 28 THEN 'w'
WHEN 29 THEN 'x'
WHEN 30 THEN 'y'
WHEN 31 THEN 'z'
END
END
GO
DROP FUNCTION IF EXISTS [dbo].[geohash_base32_index]
GO
CREATE FUNCTION [dbo].[geohash_base32_index] (
@_ch CHAR(1)
)
RETURNS TINYINT
AS
BEGIN
RETURN CASE @_ch
WHEN '0' THEN 0
WHEN '1' THEN 1
WHEN '2' THEN 2
WHEN '3' THEN 3
WHEN '4' THEN 4
WHEN '5' THEN 5
WHEN '6' THEN 6
WHEN '7' THEN 7
WHEN '8' THEN 8
WHEN '9' THEN 9
WHEN 'b' THEN 10
WHEN 'c' THEN 11
WHEN 'd' THEN 12
WHEN 'e' THEN 13
WHEN 'f' THEN 14
WHEN 'g' THEN 15
WHEN 'h' THEN 16
WHEN 'j' THEN 17
WHEN 'k' THEN 18
WHEN 'm' THEN 19
WHEN 'n' THEN 20
WHEN 'p' THEN 21
WHEN 'q' THEN 22
WHEN 'r' THEN 23
WHEN 's' THEN 24
WHEN 't' THEN 25
WHEN 'u' THEN 26
WHEN 'v' THEN 27
WHEN 'w' THEN 28
WHEN 'x' THEN 29
WHEN 'y' THEN 30
WHEN 'z' THEN 31
END
END
GO
DROP FUNCTION IF EXISTS [dbo].[geohash_encode]
GO
CREATE FUNCTION [dbo].[geohash_encode] (
@_latitude DECIMAL(10, 7),
@_longitude DECIMAL(10, 7),
@_precision TINYINT
)
RETURNS VARCHAR(12)
AS
BEGIN
IF @_latitude IS NULL OR @_longitude IS NULL
RETURN NULL
DECLARE @is_even TINYINT = 1
DECLARE @i TINYINT = 0
DECLARE @latL DECIMAL(38, 35) = -90.0
DECLARE @latR DECIMAL(38, 35) = 90.0
DECLARE @lonT DECIMAL(38, 34) = -180.0
DECLARE @lonB DECIMAL(38, 34) = 180.0
DECLARE @bit INT = 0
DECLARE @ch INT = 0
DECLARE @mid DECIMAL(12, 8) = NULL
DECLARE @geohash VARCHAR(12) = ''
IF @_precision IS NULL
SET @_precision = 12
WHILE LEN(@geohash) < @_precision
BEGIN
IF @is_even = 1
BEGIN
SET @mid = (@lonT + @lonB) / 2;
--SET @mid = ROUND(@mid, 7, 1);
IF @_longitude > @mid
BEGIN
SET @ch = @ch | dbo.geohash_bit(@bit)
SET @lonT = @mid;
END
ELSE
BEGIN
SET @lonB = @mid;
END
END
ELSE
BEGIN
SET @mid = (@latL + @latR) / 2;
--SET @mid = ROUND(@mid, 7, 1);
IF @mid < @_latitude
BEGIN
SET @ch = @ch | dbo.geohash_bit(@bit);
SET @latL = @mid;
END
ELSE
BEGIN
SET @latR = @mid
END
END
IF @is_even = 0
SET @is_even = 1
ELSE
SET @is_even = 0
IF @bit < 4
SET @bit = @bit + 1;
ELSE
BEGIN
SET @geohash = CONCAT(@geohash, dbo.geohash_base32(@ch));
SET @bit = 0;
SET @ch = 0;
END
END
RETURN @geohash
END
GO
DROP FUNCTION IF EXISTS [dbo].[geohash_decode]
GO
CREATE FUNCTION [dbo].[geohash_decode] (
@_geohash VARCHAR(12)
)
RETURNS @result TABLE(
LatL DECIMAL(10, 7),
LatR DECIMAL(10, 7),
LngT DECIMAL(10, 7),
LngB DECIMAL(10, 7),
LatC DECIMAL(10,7),
LngC DECIMAL(10, 7),
LatError DECIMAL(10,7),
LngError DECIMAL(10, 7)
)
AS
BEGIN
DECLARE @is_even bit = 1
DECLARE @latL DECIMAL(10,7) = -90.0
DECLARE @latR DECIMAL(10,7) = 90.0
DECLARE @latC DECIMAL(10,7)
DECLARE @lonT DECIMAL(10,7) = -180
DECLARE @lonB DECIMAL(10,7) = 180
DECLARE @lonC DECIMAL(10,7)
DECLARE @lat_err DECIMAL(10,7) = 90.0
DECLARE @lon_err DECIMAL(10,7) = 180.0
DECLARE @i tinyint = 0
DECLARE @len tinyint = LEN(@_geohash)
DECLARE @c CHAR(1) = ''
DECLARE @cd TINYINT = 0
DECLARE @j TINYINT = 0
DECLARE @mask TINYINT = 0
DECLARE @masked_val TINYINT = 0
WHILE @i < @len
BEGIN
SET @c = SUBSTRING(@_geohash, @i + 1, 1)
SET @cd = dbo.geohash_base32_index(@c)
SET @j = 0
WHILE @j < 5
BEGIN
SET @mask = dbo.geohash_bit(@j)
SET @masked_val = @cd & @mask
IF @is_even = 1
BEGIN
SET @lon_err = @lon_err / 2
IF @masked_val != 0
SET @lonT = (@lonT + @lonB) / 2
ELSE
SET @lonB = (@lonT + @lonB) / 2
END
ELSE
BEGIN
SET @lat_err = @lat_err / 2
IF @masked_val != 0
SET @latL = (@latL + @latR) / 2
ELSE
SET @latR = (@latL + @latR) / 2
END
IF @is_even = 0
SET @is_even = 1
ELSE
SET @is_even = 0
SET @j = @j + 1
END
SET @i = @i + 1
END
SET @latC = (@latL + @latR) / 2
SET @lonC = (@lonT + @lonB) / 2
INSERT @result
SELECT @latL, @latR, @lonT, @lonB, @latC, @lonC, @lat_err, @lon_err
RETURN
END
GO