class GeoHash

Constants

NEIGHBOR_DIRECTIONS
VERSION

Public Class Methods

calculate_adjacent(p1, p2) click to toggle source
static VALUE calculate_adjacent(VALUE self, VALUE geohash, VALUE dir)
{
        char *str;
        VALUE ret_val;
        Check_Type(geohash, T_STRING);
        Check_Type(dir, T_FIXNUM);
        str = RSTRING_PTR(geohash);
        if (!strlen(str)) return Qnil;
        ret_val = rb_str_new(str,strlen(str));
        get_neighbor(RSTRING_PTR(ret_val), NUM2INT(dir), strlen(str));
        return ret_val;
}
decode(geohash, decimals=5) click to toggle source

Decode a geohash to a latitude and longitude with decimals digits

# File lib/geohash.rb, line 26
def self.decode(geohash, decimals=5)
  lat, lon = decode_base(geohash)
  [lat.decimals(decimals), lon.decimals(decimals)]
end
decode_base(p1) click to toggle source
static VALUE decode(VALUE self, VALUE str)
{
        VALUE ary;
        double point[2];
        Check_Type(str, T_STRING);
        
        decode_geohash(RSTRING_PTR(str), point);
        
        ary = rb_ary_new2(2);
        rb_ary_store(ary, 0, rb_float_new(point[0]));
        rb_ary_store(ary, 1, rb_float_new(point[1]));
        return ary;
}
decode_bbox(p1) click to toggle source
static VALUE decode_bbox(VALUE self, VALUE str)
{
        VALUE ary, ret;
        double lat[2], lon[2];
        Check_Type(str, T_STRING);

        decode_geohash_bbox(RSTRING_PTR(str), lat, lon);

        ret = rb_ary_new2(2); /* [[lat[0], lon[0]], [lat[1], lon[1]]] */

        ary = rb_ary_new2(2); /* [lat[0], lon[0]] */
        rb_ary_store(ary, 0, rb_float_new(lat[0]));
        rb_ary_store(ary, 1, rb_float_new(lon[0]));
        rb_ary_store(ret, 0, ary);

        ary = rb_ary_new2(2); /* [lat[1], lon[1]] */
        rb_ary_store(ary, 0, rb_float_new(lat[1]));
        rb_ary_store(ary, 1, rb_float_new(lon[1]));
        rb_ary_store(ret, 1, ary);

        return ret;
}
encode(lat, lon, precision=10) click to toggle source

Encode latitude and longitude to a geohash with precision digits

# File lib/geohash.rb, line 21
def self.encode(lat, lon, precision=10)
  encode_base(lat, lon, precision)
end
encode_base(p1, p2, p3) click to toggle source
static VALUE encode(VALUE self, VALUE lat, VALUE lon, VALUE precision)
{
        VALUE geohash;
        char str[15];
        int digits=10;
        
        digits = NUM2INT(precision);
        
        Check_Type(lat, T_FLOAT);
        Check_Type(lon, T_FLOAT);
        if (digits <3 || digits > 12)
                digits = 12;

        encode_geohash(RFLOAT_VALUE(lat), RFLOAT_VALUE(lon), digits, str);

        geohash = rb_str_new2(str);
        return geohash;
}
new(*params) click to toggle source

Create a new GeoHash object from a geohash or from a latlon

# File lib/geohash.rb, line 32
def initialize(*params)
  if params.first.is_a?(Float)
    @value = GeoHash.encode(*params)
    @latitude, @longitude = params
  else
    @value = params.first
    @latitude, @longitude = GeoHash.decode(@value)
  end
  @bounding_box = GeoHash.decode_bbox(@value)
end

Public Instance Methods

neighbor(dir) click to toggle source
# File lib/geohash.rb, line 51
def neighbor(dir)
  GeoHash.calculate_adjacent(@value, dir)
end
neighbors() click to toggle source
# File lib/geohash.rb, line 55
def neighbors
  immediate = NEIGHBOR_DIRECTIONS.flatten.map do |d|
    neighbor(d)
  end
  diagonals = NEIGHBOR_DIRECTIONS.first.map do |y|
    NEIGHBOR_DIRECTIONS.last.map do |x|
      GeoHash.calculate_adjacent(GeoHash.calculate_adjacent(@value, x), y)
    end
  end.flatten
  immediate + diagonals
end
to_bbox() click to toggle source
# File lib/geohash.rb, line 47
def to_bbox
  GeoHash.decode_bbox(@value)
end
to_s() click to toggle source
# File lib/geohash.rb, line 43
def to_s
  @value
end