Class: GeoHash
- Inherits:
-
Object
- Object
- GeoHash
- Defined in:
- lib/geohash.rb,
ext/geohash_native.c
Constant Summary collapse
- VERSION =
'1.1.0'- NEIGHBOR_DIRECTIONS =
[ [0, 1], [2, 3] ]
Class Method Summary collapse
-
.calculate_adjacent(geohash, dir) ⇒ Object
Acts as Ruby API wrapper to get_neighbor function, which is recursive and does nasty C things.
-
.decode(geohash, decimals = 5) ⇒ Object
Decode a geohash to a latitude and longitude with decimals digits.
- .decode_base(str) ⇒ Object
- .decode_bbox(str) ⇒ Object
-
.encode(lat, lon, precision = 10) ⇒ Object
Encode latitude and longitude to a geohash with precision digits.
- .encode_base(lat, lon, precision) ⇒ Object
Instance Method Summary collapse
-
#initialize(*params) ⇒ GeoHash
constructor
Create a new GeoHash object from a geohash or from a latlon.
- #neighbor(dir) ⇒ Object
- #neighbors ⇒ Object
- #to_bbox ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(*params) ⇒ GeoHash
Create a new GeoHash object from a geohash or from a latlon
32 33 34 35 36 37 38 39 40 41 |
# 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 |
Class Method Details
.calculate_adjacent(geohash, dir) ⇒ Object
Acts as Ruby API wrapper to get_neighbor function, which is recursive and does nasty C things
190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'ext/geohash_native.c', line 190 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) ⇒ Object
Decode a geohash to a latitude and longitude with decimals digits
26 27 28 29 |
# 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(str) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'ext/geohash_native.c', line 152 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(str) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'ext/geohash_native.c', line 129 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) ⇒ Object
Encode latitude and longitude to a geohash with precision digits
21 22 23 |
# File 'lib/geohash.rb', line 21 def self.encode(lat, lon, precision=10) encode_base(lat, lon, precision) end |
.encode_base(lat, lon, precision) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'ext/geohash_native.c', line 110 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; } |
Instance Method Details
#neighbor(dir) ⇒ Object
51 52 53 |
# File 'lib/geohash.rb', line 51 def neighbor(dir) GeoHash.calculate_adjacent(@value, dir) end |
#neighbors ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# 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 ⇒ Object
47 48 49 |
# File 'lib/geohash.rb', line 47 def to_bbox GeoHash.decode_bbox(@value) end |
#to_s ⇒ Object
43 44 45 |
# File 'lib/geohash.rb', line 43 def to_s @value end |