Geohash.net é uma biblioteca C# leve e de alto desempenho para codificar e decodificar string e geohashes inteiros.
PENDÊNCIA
Aqui está um exemplo rápido de como usar a biblioteca Geohash.net:
using NetGeohash ;
// Encode latitude and longitude coordinates into a geohash string
string geohash = Geohash . Encode ( 37.421999 , - 122.084057 , 9 ) ;
// Decode the geohash string into latitude and longitude coordinates
( double latitude , double longitude ) = Geohash . Decode ( geohash ) ;
// Encode the geohash string into an integer format
ulong geohashInt = Geohash . EncodeToInt64 ( 37.421999 , - 122.084057 , 9 ) ;
// Decode the integer geohash into latitude and longitude coordinates
( latitude , longitude ) = Geohash . DecodeFromInt64 ( geohashInt ) ; O GeoHash é um sistema de geocodificação que permite representar uma localização geográfica usando uma corda ou número inteiro alfanumérico curto. Foi desenvolvido por Gustavo Niemeyer em 2008 e, desde então, tornou -se um método popular para codificar e indexar dados espaciais.
O conceito por trás da geohanking é tomar coordenadas de latitude e longitude e convertê -los em uma forma mais compacta. Isso é conseguido subdividindo recursivamente a superfície da Terra em células retangulares, que então atribuem um identificador único. O comprimento do geohash determina o nível de precisão, com geohashes mais longos representando áreas menores.
As células geohash têm algumas propriedades notáveis:
Por exemplo, para gerar uma corda geohash para o local com latitude 37.7749 e longitude -122.4194 No nível de precisão de 12, podemos seguir estes três estágios:
Quantize a latitude e longitude para números inteiros de 32 bits, mapeando o intervalo unitário [0, 1] e multiplicando por 2^32 ( exp2(32) ):
Latitude leva o intervalo [-90, 90], portanto a representação inteira de 32 bits de latitude é
lat32 = floor ( exp2 ( 32 ) * ( latitude + 90 ) / 180 ) Que no nosso caso é igual a 3048827870 ( 0b10110101101110010110101111011110 )
Da mesma forma, para longitude [-180, 180]:
lng32 = floor ( exp2 ( 32 ) * ( longitude + 180 ) / 360 ) Que no nosso caso é igual a 686963316 ( 0b101000111100100011101001110100 )
A latitude e longitude quantizadas de 32 bits são internas de bits para produzir um valor de 64 bits (o inteiro geohash ).
O diagrama abaixo ilustra a operação.

Nota: Latitude e longitude ocupam bits pares e estranhos, respectivamente .
Em nosso exemplo em execução, o Inteiro Geohash é: 5589511709871930228 ( 0b0100110110010001111011110100100100011110110011010111101101110100 )
Este número deve ser tomado com menor precisão. A precisão do Geohash inteiro consiste nos n bits altos , por isso precisamos truncar bits em excesso. Podemos fazer isso pela operação de mudança de bits:
hash = interleaved >> ( sizeof ( ulong ) * BITS_PER_BYTE - log2 ( 32 ) * precision ) // 64 - 5 * precision Que é 349344481866995639 ( 0b010011011001000111101111010010010001111011001101011110110111 ) para o nosso exemplo.
NOTA: Esta etapa fornece a propriedade crítica de Geohash: o geohash de um ponto a uma precisão mais baixa é um prefixo do geohash a uma precisão mais alta. Portanto, as pesquisas de proximidade podem ser reduzidas a prefixos geohash correspondentes (como uma primeira passagem).
A string geohash é obtida da codificação inteira geohash por base32. Isso é padrão, exceto para a escolha do alfabeto. Cada caractere geohash representa um valor binário de 5 bits ( log2(32) ), e toda a string geohash é composta de vários valores concatenados de 5 bits.
0123456789bcdefghjkmnpqrstuvwxyz
A codificação começa nos bits altos, consumindo 5 bits do geohash inteiro para cada caractere de precisão. Como resultado, a precisão máxima é de 12 caracteres ou 60 bits .
Divida o valor binário em grupos de 5 bits e converta cada grupo em seu caractere de base 32 correspondente:
01001 ( 9) - 9
10110 (22) - q
01000 ( 8) - 8
11110 (30) - y
11110 (30) - y
10010 (18) - k
01000 ( 8) - 8
11110 (30) - y
11001 (25) - t
10101 (21) - p
11101 (29) - x
10111 (23) - r
Concatenar todos os caracteres base-32 juntos para formar a string geohash final: 9q8yyk8ytpxr
Para casos de uso interno, o geohash inteiro geralmente é suficiente . A etapa base32 foi incluída aqui para completar.
BenchmarkDotNet =v0.13.5, OS =Windows 11 (10.0.22000.1696/21H2/SunValley)
AMD Ryzen 9 3900X, 1 CPU, 24 logical and 12 physical cores
.NET SDK =8.0.100-preview.1.23115.2
[Host] : .NET 8.0.0 (8.0.23.11008), X64 RyuJIT AVX2
Job-KKMPKU : .NET 8.0.0 (8.0.23.11008), X64 RyuJIT AVX2
Jit =RyuJit Platform =X64 Runtime =.NET 8.0
| Método | Entrada | Significar | Erro | Stddev | Tamanho do código | Gen0 | Alocado |
|---|---|---|---|---|---|---|---|
| Netgeohash | k | 8.112 ns | 0,1881 ns | 0,2090 ns | 1.717 b | - | - |
| Netcoregeohash | k | 145.271 ns | 0,8736 ns | 0,7295 ns | 18 b | 0,0038 | 32 b |
| Ngeohashlib | k | 143.414 ns | 0,8529 ns | 0,7561 ns | 9 b | 0,0229 | 192 b |
| Netgeohash | yd | 8.442 ns | 0,1032 ns | 0,0965 ns | 1.717 b | - | - |
| Netcoregeohash | yd | 166.305 ns | 0,8682 ns | 0,8121 ns | 18 b | 0,0038 | 32 b |
| Ngeohashlib | yd | 169.663 ns | 1.0957 ns | 1.0249 ns | 9 b | 0,0229 | 192 b |
| Netgeohash | 6n3 | 9.442 ns | 0,1746 ns | 0,1633 ns | 1.717 b | - | - |
| Netcoregeohash | 6n3 | 192.502 ns | 2.0566 ns | 1.9238 ns | 18 b | 0,0038 | 32 b |
| Ngeohashlib | 6n3 | 202.150 ns | 1.9676 ns | 1.8405 ns | 9 b | 0,0229 | 192 b |
| Netgeohash | zvgk | 10.183 ns | 0,1023 ns | 0,0907 ns | 1.717 b | - | - |
| Netcoregeohash | zvgk | 218.244 ns | 1.4455 ns | 1.3521 ns | 18 b | 0,0038 | 32 b |
| Ngeohashlib | zvgk | 223.580 ns | 0,7643 ns | 0,6776 ns | 9 b | 0,0229 | 192 b |
| Netgeohash | T05KH | 10.726 ns | 0,2016 ns | 0,1886 ns | 1.717 b | - | - |
| Netcoregeohash | T05KH | 310.208 ns | 0,9773 ns | 0,8663 ns | 18 b | 0,0038 | 32 b |
| Ngeohashlib | T05KH | 263.364 ns | 2.2418 ns | 2.0970 ns | 9 b | 0,0229 | 192 b |
| Netgeohash | B5CV2H | 11.413 ns | 0,0796 ns | 0,0706 ns | 1.717 b | - | - |
| Netcoregeohash | B5CV2H | 328.490 ns | 2.4518 ns | 2.1735 ns | 18 b | 0,0038 | 32 b |
| Ngeohashlib | B5CV2H | 283.862 ns | 2.9657 ns | 2.7741 ns | 9 b | 0,0229 | 192 b |
| Netgeohash | vveyj80 | 12.114 ns | 0,1749 ns | 0,1636 ns | 1.717 b | - | - |
| Netcoregeohash | vveyj80 | 358.143 ns | 0,9470 ns | 0,7908 ns | 18 b | 0,0038 | 32 b |
| Ngeohashlib | vveyj80 | 318.725 ns | 1.7533 ns | 1.6400 ns | 9 b | 0,0229 | 192 b |
| Netgeohash | f7y53xjt | 12.676 ns | 0,2287 ns | 0,2140 ns | 1.717 b | - | - |
| Netcoregeohash | f7y53xjt | 380.157 ns | 2.2520 ns | 2.1065 ns | 18 b | 0,0038 | 32 b |
| Ngeohashlib | f7y53xjt | 334.277 ns | 1.0733 ns | 1.0039 ns | 9 b | 0,0229 | 192 b |
| Netgeohash | TRM92JKBV | 12.990 ns | 0,1217 ns | 0,1139 ns | 1.717 b | - | - |
| Netcoregeohash | TRM92JKBV | 404.136 ns | 1.9322 ns | 1.7128 ns | 18 b | 0,0038 | 32 b |
| Ngeohashlib | TRM92JKBV | 374.209 ns | 1.0807 ns | 1.0109 ns | 9 b | 0,0229 | 192 b |
| Netgeohash | drMq3GX6ZT | 13.861 ns | 0,1824 ns | 0,1706 ns | 1.717 b | - | - |
| Netcoregeohash | drMq3GX6ZT | 429.735 ns | 1.6946 ns | 1.4151 ns | 18 b | 0,0038 | 32 b |
| Ngeohashlib | drMq3GX6ZT | 400.585 ns | 1.9080 ns | 1.7847 ns | 9 b | 0,0229 | 192 b |
| Netgeohash | 9zefgnuj7dw | 14.265 ns | 0,2997 ns | 0,2803 ns | 1.717 b | - | - |
| Netcoregeohash | 9zefgnuj7dw | 455.781 ns | 3.3098 ns | 2.9341 ns | 18 b | 0,0038 | 32 b |
| Ngeohashlib | 9zefgnuj7dw | 416.747 ns | 2.7821 ns | 2.6023 ns | 9 b | 0,0229 | 192 b |
| Netgeohash | K9M2H7T1N0C2 | 14.864 ns | 0,3210 ns | 0,3153 ns | 1.717 b | - | - |
| Netcoregeohash | K9M2H7T1N0C2 | 479.624 ns | 1.5628 ns | 1.3854 ns | 18 b | 0,0038 | 32 b |
| Ngeohashlib | K9M2H7T1N0C2 | 467.252 ns | 1.3795 ns | 1.2904 ns | 9 b | 0,0229 | 192 b |

| Método | latitude | longitude | precisão | Significar | Erro | Stddev | Tamanho do código | Gen0 | Alocado |
|---|---|---|---|---|---|---|---|---|---|
| Netgeohash | -89.127865 | -179.438962 | 7 | 20,69 ns | 0,146 ns | 0,129 ns | 906 b | 0,0048 | 40 b |
| Netcoregeohash | -89.127865 | -179.438962 | 7 | 195.04 ns | 0,632 ns | 0,528 ns | 14 b | 0,0172 | 144 b |
| Ngeohashlib | -89.127865 | -179.438962 | 7 | 324.45 ns | 3.044 ns | 2.848 ns | 19 b | 0,0458 | 384 b |
| Netgeohash | -41.2858 | 174.7868 | 12 | 24,77 ns | 0,352 ns | 0,329 ns | 906 b | 0,0057 | 48 b |
| Netcoregeohash | -41.2858 | 174.7868 | 12 | 318.08 ns | 2.079 ns | 1.944 ns | 14 b | 0,0181 | 152 b |
| Ngeohashlib | -41.2858 | 174.7868 | 12 | 493.78 ns | 2.879 ns | 2.552 ns | 19 b | 0,0687 | 576 b |
| Netgeohash | -12.347856 | 34.890273 | 3 | 17,95 ns | 0,215 ns | 0,201 ns | 906 b | 0,0038 | 32 b |
| Netcoregeohash | -12.347856 | 34.890273 | 3 | 104,91 ns | 0,616 ns | 0,546 ns | 14 b | 0,0162 | 136 b |
| Ngeohashlib | -12.347856 | 34.890273 | 3 | 167,74 ns | 2.523 ns | 2.360 ns | 19 b | 0,0277 | 232 b |
| Netgeohash | 45.678912 | 92.45236 | 4 | 18.39 ns | 0,313 ns | 0,293 ns | 906 b | 0,0038 | 32 b |
| Netcoregeohash | 45.678912 | 92.45236 | 4 | 127,05 ns | 1.053 ns | 0,985 ns | 14 b | 0,0162 | 136 b |
| Ngeohashlib | 45.678912 | 92.45236 | 4 | 209.17 ns | 1.549 ns | 1.449 ns | 19 b | 0.0305 | 256 b |
| Netgeohash | 52.5174 | 13.409 | 12 | 24,64 ns | 0,299 ns | 0,280 ns | 906 b | 0,0057 | 48 b |
| Netcoregeohash | 52.5174 | 13.409 | 12 | 310.05 ns | 1.637 ns | 1.451 ns | 14 b | 0,0181 | 152 b |
| Ngeohashlib | 52.5174 | 13.409 | 12 | 498.72 ns | 5.374 ns | 5.027 ns | 19 b | 0,0687 | 576 b |
| Netgeohash | 80.294617 | 19.543821 | 5 | 18.82 ns | 0,228 ns | 0,191 ns | 906 b | 0,0038 | 32 b |
| Netcoregeohash | 80.294617 | 19.543821 | 5 | 152.24 ns | 0,561 ns | 0,498 ns | 14 b | 0,0162 | 136 b |
| Ngeohashlib | 80.294617 | 19.543821 | 5 | 242,98 ns | 2.265 ns | 2.008 ns | 19 b | 0,0391 | 328 b |

Encorajo contribuições para o geohash.net! Se você encontrar um bug ou ter uma sugestão para um novo recurso, não hesite em abrir um problema ou enviar uma solicitação de tração no Github. Estou constantemente buscando maneiras de aprimorar a biblioteca e fornecer mais valor à comunidade.
Geohash.net é baseado na pesquisa e descobertas de Michael McLoughlin. Seu trabalho sobre geohanking e a implementação de um algoritmo geohing de alto desempenho em Go podem ser encontrados neste artigo Geohash na Assembléia de Golang.