libgeohash

Category: c library
Tags: C library

A static library for encoding/decoding geohashes

History

It started as a service to offer short URL for coordinates.

FAQ

What is it?

Encoder/Decoder for coordinates with a 'variable' precision

How cool is it?

Rather.

Competitors?

postgis and probably others

When to use it?

When you want to have a readable, 'rough' position, and easy access to it's neighbours.

Is it dead?

Nope.

Where is it?

https://github.com/simplegeo/libgeohash

How to use it

The documentation and the examples are rather good.

Download

file: 1_download.sh

#!/bin/bash

mkdir -p tmp
cd ./tmp
git clone https://github.com/simplegeo/libgeohash.git
cd libgeohash
make library

Init

file: main.c

#include <stdio.h>
#include <stdlib.h>

#include <geohash.h>

int main() {
    size_t i;
    const int precision = 5;
    const char grid[8][3] = { "N \0", "NE\0", "E \0", "SE\0", "S \0", "SW\0", "W \0", "NW\0" };
    char* hash;
    char** neighbours;
    GeoBoxDimension dim;
    GeoCoord coord;

    dim = geohash_dimensions_for_precision(precision);
    printf("precision: %f x %f meters\n", dim.height, dim.width);

    hash = geohash_encode(51.183385, 5.989092 , precision);
    neighbours = geohash_neighbors(hash);

    for( i = 0; i < 8; i++) {
        coord = geohash_decode(neighbours[i]);
        printf("%s; hash:%s; center:(%f, %f); (NE, SW):((%f, %f), (%f, %f))\n",
                    grid[i], neighbours[i],
                    coord.latitude, coord.longitude,
                    coord.north, coord.east, coord.south, coord.west);
    }

    free(hash);
    free(neighbours);

    return 0;
}

file: 2_compile_and_run.sh

#!/bin/bash

gcc -std=c99 -o ./tmp/main main.c -I./tmp/libgeohash ./tmp/libgeohash/libgeohash.a 
./tmp/main

Output

precision: 0.043945 x 0.043945 meters
N ; hash:u1h72; center:(51.218262, 5.998535); (NE, SW):((51.240234, 6.020508), (51.196289, 5.976562))
NE; hash:u1h73; center:(51.218262, 6.042480); (NE, SW):((51.240234, 6.064453), (51.196289, 6.020508))
E ; hash:u1h71; center:(51.174316, 6.042480); (NE, SW):((51.196289, 6.064453), (51.152344, 6.020508))
SE; hash:u1h6c; center:(51.130371, 6.042480); (NE, SW):((51.152344, 6.064453), (51.108398, 6.020508))
S ; hash:u1h6b; center:(51.130371, 5.998535); (NE, SW):((51.152344, 6.020508), (51.108398, 5.976562))
SW; hash:u1h4z; center:(51.130371, 5.954590); (NE, SW):((51.152344, 5.976562), (51.108398, 5.932617))
W ; hash:u1h5p; center:(51.174316, 5.954590); (NE, SW):((51.196289, 5.976562), (51.152344, 5.932617))
NW; hash:u1h5r; center:(51.218262, 5.954590); (NE, SW):((51.240234, 5.976562), (51.196289, 5.932617))

Further reading:

We only tipped the top of the iceberg here. There is much more:

https://en.wikipedia.org/wiki/Geohash https://news.ycombinator.com/item?id=9772419