geohash 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ V�r�ZM�`��kE#^���@5g�q%`q|]�� ��vo����T-P�]���<ơt�"�`w�E�6�l(�h۩n�>�?j}��|�h����0��D��|�\��;ج�]^�zZsS�7�Y�<�/����x�̊�/��㐵��R�k6����u��9f�����u�+�>�l�
@@ -0,0 +1,5 @@
1
+ === 1.0.0 / 2008-04-23
2
+
3
+ * Released by David Troy (C) Under the MIT License
4
+
5
+
@@ -0,0 +1,82 @@
1
+ = geohash
2
+
3
+ * http://rubyforge.org/projects/geohash
4
+
5
+ == DESCRIPTION:
6
+
7
+ GeoHash Gem for Ruby (c) 2008 David Troy
8
+ dave@roundhousetech.com
9
+
10
+ Geohash is a latitude/longitude encoding system invented by Gustavo Niemeyer when writing the
11
+ web service at geohash.org, and put into the public domain. Geohashes offer properties like
12
+ arbitrary precision, similar prefixes for nearby positions, and the possibility of gradually
13
+ removing characters from the end of the code to reduce its size (and gradually lose precision).
14
+
15
+ See these resources online:
16
+ http://geohash.org
17
+ http://en.wikipedia.org/wiki/Geohash
18
+
19
+ == FEATURES/PROBLEMS:
20
+
21
+ * Encode to Geohash Format to an arbitrary level of precision
22
+ * Decode from Geohash Format to an arbitrary level of precision
23
+ * C implementation is over 10 times faster than native Ruby code (we checked)
24
+ * No known problems
25
+
26
+ == SYNOPSIS:
27
+
28
+ GeoHash is very easy to use (and fast) because it's written in C with Ruby bindings.
29
+
30
+ require 'rubygems'
31
+ require 'geohash'
32
+ include GeoHash
33
+
34
+ GeoHash.decode('f77')
35
+ => [63.98438, -73.82813]
36
+
37
+ GeoHash.encode(39.51, -76.24)
38
+ => "dr1bc0edrj"
39
+
40
+ You can encode or decode to an arbitrary level of precision:
41
+
42
+ # Encode latitude and longitude to a geohash with precision digits
43
+ encode(lat, lon, precision=10)
44
+
45
+ # Decode a geohash to a latitude and longitude with decimals digits
46
+ decode(geohash, decimals=5)
47
+
48
+ Have fun with this! GeoHash is the new black.
49
+
50
+ == REQUIREMENTS:
51
+
52
+ * GCC and a Gnu-ish build environment (for native extensions)
53
+
54
+ == INSTALL:
55
+
56
+ 1. gem install geohash
57
+ 2. profit!
58
+
59
+ == LICENSE:
60
+
61
+ (The MIT License)
62
+
63
+ Copyright (c) 2008 David Troy, Roundhouse Technologies LLC
64
+
65
+ Permission is hereby granted, free of charge, to any person obtaining
66
+ a copy of this software and associated documentation files (the
67
+ 'Software'), to deal in the Software without restriction, including
68
+ without limitation the rights to use, copy, modify, merge, publish,
69
+ distribute, sublicense, and/or sell copies of the Software, and to
70
+ permit persons to whom the Software is furnished to do so, subject to
71
+ the following conditions:
72
+
73
+ The above copyright notice and this permission notice shall be
74
+ included in all copies or substantial portions of the Software.
75
+
76
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
77
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
78
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
79
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
80
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
81
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
82
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'hoe'
3
+ require './lib/geohash.rb'
4
+
5
+ Hoe.new('geohash', GeoHash::VERSION) do |p|
6
+ p.rubyforge_name = 'geohash' # if different than lowercase project name
7
+ p.developer('David Troy', 'dave@roundhousetech.com')
8
+ p.remote_rdoc_dir = '' # Release to root
9
+ p.spec_extras = {:extensions => 'extconf.rb', :require_paths => ['lib']}
10
+ end
File without changes
@@ -0,0 +1,3 @@
1
+ require 'mkmf'
2
+ dir_config("geohash_native")
3
+ create_makefile("geohash_native")
@@ -0,0 +1,152 @@
1
+ // geohash_native.c
2
+ // (c) 2008 David Troy
3
+ // dave@roundhousetech.com
4
+ //
5
+ // (The MIT License)
6
+ //
7
+ // Copyright (c) 2008 David Troy, Roundhouse Technologies LLC
8
+ //
9
+ // Permission is hereby granted, free of charge, to any person obtaining
10
+ // a copy of this software and associated documentation files (the
11
+ // 'Software'), to deal in the Software without restriction, including
12
+ // without limitation the rights to use, copy, modify, merge, publish,
13
+ // distribute, sublicense, and/or sell copies of the Software, and to
14
+ // permit persons to whom the Software is furnished to do so, subject to
15
+ // the following conditions:
16
+ //
17
+ // The above copyright notice and this permission notice shall be
18
+ // included in all copies or substantial portions of the Software.
19
+ //
20
+ // THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
21
+ // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
+ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23
+ // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24
+ // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25
+ // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26
+ // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
+
28
+ #include "ruby.h"
29
+
30
+ static VALUE rb_mGeoHash;
31
+
32
+ #define BASE32 "0123456789bcdefghjkmnpqrstuvwxyz"
33
+
34
+ void refine_interval(double *interval, char cd, char mask) {
35
+ if (cd&mask)
36
+ interval[0] = (interval[0] + interval[1])/2;
37
+ else
38
+ interval[1] = (interval[0] + interval[1])/2;
39
+ }
40
+
41
+ float *decode_geohash(char *geohash, double *point) {
42
+ int i, j, hashlen, is_even=1;
43
+ double lat[2], lon[2], lat_err, lon_err;
44
+ char c, cd, mask;
45
+ char bits[] = {16,8,4,2,1};
46
+
47
+ lat[0] = -90.0; lat[1] = 90.0;
48
+ lon[0] = -180.0; lon[1] = 180.0;
49
+ lat_err = 90.0; lon_err = 180.0;
50
+ hashlen = strlen(geohash);
51
+
52
+ for (i=0; i<hashlen; i++) {
53
+ c = tolower(geohash[i]);
54
+ cd = strchr(BASE32, c)-BASE32;
55
+ for (j=0; j<5; j++) {
56
+ mask = bits[j];
57
+ if (is_even) {
58
+ lon_err /= 2;
59
+ refine_interval(lon, cd, mask);
60
+ } else {
61
+ lat_err /= 2;
62
+ refine_interval(lat, cd, mask);
63
+ }
64
+ is_even = !is_even;
65
+ }
66
+ }
67
+
68
+ point[0] = (lat[0] + lat[1]) / 2;
69
+ point[1] = (lon[0] + lon[1]) / 2;
70
+
71
+ return 0;
72
+ }
73
+
74
+ void *encode_geohash(double latitude, double longitude, int precision, char *geohash) {
75
+ int is_even=1, i=0;
76
+ double lat[2], lon[2], mid;
77
+ char bits[] = {16,8,4,2,1}, bit=0, ch=0;
78
+
79
+ lat[0] = -90.0; lat[1] = 90.0;
80
+ lon[0] = -180.0; lon[1] = 180.0;
81
+
82
+ while (i < precision) {
83
+ if (is_even) {
84
+ mid = (lon[0] + lon[1]) / 2;
85
+ if (longitude > mid) {
86
+ ch |= bits[bit];
87
+ lon[0] = mid;
88
+ } else
89
+ lon[1] = mid;
90
+ } else {
91
+ mid = (lat[0] + lat[1]) / 2;
92
+ if (latitude > mid) {
93
+ ch |= bits[bit];
94
+ lat[0] = mid;
95
+ } else
96
+ lat[1] = mid;
97
+ }
98
+
99
+ is_even = !is_even;
100
+ if (bit < 4)
101
+ bit++;
102
+ else {
103
+ geohash[i++] = BASE32[ch];
104
+ bit = 0;
105
+ ch = 0;
106
+ }
107
+ }
108
+ geohash[i] = 0;
109
+ }
110
+
111
+ static VALUE encode(VALUE self, VALUE lat, VALUE lon, VALUE precision)
112
+ {
113
+ VALUE geohash;
114
+ char str[15];
115
+ int digits=10;
116
+
117
+ digits = NUM2INT(precision);
118
+
119
+ Check_Type(lat, T_FLOAT);
120
+ Check_Type(lon, T_FLOAT);
121
+ if (digits <3 || digits > 12)
122
+ digits = 12;
123
+
124
+ encode_geohash(RFLOAT(lat)->value, RFLOAT(lon)->value, digits, str);
125
+
126
+ geohash = rb_str_new2(str);
127
+ return geohash;
128
+ }
129
+
130
+
131
+ static VALUE decode(VALUE self, VALUE str)
132
+ {
133
+ VALUE ary;
134
+ double point[2];
135
+ Check_Type(str, T_STRING);
136
+
137
+ decode_geohash(RSTRING(str)->ptr, point);
138
+
139
+ ary = rb_ary_new2(2);
140
+ rb_ary_store(ary, 0, rb_float_new(point[0]));
141
+ rb_ary_store(ary, 1, rb_float_new(point[1]));
142
+ return ary;
143
+ }
144
+
145
+ void Init_geohash_native()
146
+ {
147
+ rb_mGeoHash = rb_define_module("GeoHash");
148
+ rb_define_method(rb_mGeoHash, "decode_base", decode, 1);
149
+ rb_define_method(rb_mGeoHash, "encode_base", encode, 3);
150
+ }
151
+
152
+ // end
@@ -0,0 +1,24 @@
1
+ require 'geohash_native'
2
+
3
+ class Float
4
+ def decimals(places)
5
+ n = (self * (10 ** places)).round
6
+ n.to_f/(10**places)
7
+ end
8
+ end
9
+
10
+ module GeoHash
11
+
12
+ VERSION = '1.0.0'
13
+
14
+ # Encode latitude and longitude to a geohash with precision digits
15
+ def encode(lat, lon, precision=10)
16
+ encode_base(lat, lon, precision)
17
+ end
18
+
19
+ # Decode a geohash to a latitude and longitude with decimals digits
20
+ def decode(geohash, decimals=5)
21
+ lat, lon = decode_base(geohash)
22
+ [lat.decimals(decimals), lon.decimals(decimals)]
23
+ end
24
+ end
@@ -0,0 +1,33 @@
1
+ require 'rubygems'
2
+ require "#{File.dirname(__FILE__)}/../lib/geohash"
3
+ require 'test/unit'
4
+
5
+ class GeoHashTest < Test::Unit::TestCase
6
+ include GeoHash
7
+
8
+ def test_decoding
9
+ assert_equal [39.02474, -76.51100], decode("dqcw4bnrs6s7")
10
+ assert_equal [37.791562, -122.398541], decode("9q8yyz8pg3bb", 6)
11
+ assert_equal [37.791562, -122.398541], decode("9Q8YYZ8PG3BB", 6)
12
+ assert_equal [42.60498046875, -5.60302734375], decode("ezs42", 11)
13
+ assert_equal [-25.382708, -49.265506], decode("6gkzwgjzn820",6)
14
+ assert_equal [-25.383, -49.266], decode("6gkzwgjz", 3)
15
+ assert_equal [37.8565, -122.2554], decode("9q9p658642g7", 4)
16
+ end
17
+
18
+ def test_encoding
19
+ assert_equal "dqcw4bnrs6s7", encode(39.0247389581054, -76.5110040642321, 12)
20
+ assert_equal "dqcw4bnrs6", encode(39.0247389581054, -76.5110040642321, 10)
21
+ assert_equal "6gkzmg1u", encode(-25.427, -49.315, 8)
22
+ assert_equal "ezs42", encode(42.60498046875, -5.60302734375, 5)
23
+ end
24
+
25
+ # require 'benchmark'
26
+ # def test_multiple
27
+ # Benchmark.bmbm(30) do |bm|
28
+ # bm.report("encoding") {30000.times { test_encoding }}
29
+ # bm.report("decoding") {30000.times { test_encoding }}
30
+ # end
31
+ # end
32
+ end
33
+
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: geohash
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - David Troy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIDPDCCAiSgAwIBAgIBADANBgkqhkiG9w0BAQUFADBEMQ0wCwYDVQQDDARkYXZl
14
+ MR4wHAYKCZImiZPyLGQBGRYOcm91bmRob3VzZXRlY2gxEzARBgoJkiaJk/IsZAEZ
15
+ FgNjb20wHhcNMDgwNDI0MTA1MDI5WhcNMDkwNDI0MTA1MDI5WjBEMQ0wCwYDVQQD
16
+ DARkYXZlMR4wHAYKCZImiZPyLGQBGRYOcm91bmRob3VzZXRlY2gxEzARBgoJkiaJ
17
+ k/IsZAEZFgNjb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC7bDsN
18
+ GJwEzgouHmAliXQup133AFX1eTz1gyfMQ1oqFQWEtswhAIgQk3yuqF7nAnjUlS4C
19
+ dnpD/IRsXLbEIUBEjWNjYNwBzsRKNm2jumBbMYFGUrM9tsHuABCRy/jG0XOI5ea1
20
+ qW1Qmva4fbgoUfnzmvvPJwar1i2TcRml+UQ8y98iCT9CQ125mAGcsJh6OJsBcTPk
21
+ p8bSX+XKfyq5p+A/pRw3OGi5Ky6b5MmOJUMm8CaZddskAJBbnkfOfEqms66yxLyg
22
+ ipJwB3/x60wF0G63y2moLD89vyRsvzwPnSY69HuMB313FU1LJNFifG50DGqUtDQD
23
+ oT4FBWIhK4NAKxRdAgMBAAGjOTA3MAkGA1UdEwQCMAAwCwYDVR0PBAQDAgSwMB0G
24
+ A1UdDgQWBBSRKSB0wLoqAi3rcW0UhGLTrRxCpDANBgkqhkiG9w0BAQUFAAOCAQEA
25
+ cLJVV+CZLRd//zGvjDUmWtI8T0csXfRJvcVJrm6NTeFnoZyIVWV/Yoleg6v/X3Eg
26
+ +XtnNUtzFwiyO7hmYBb3UtXK/FLYg1ixmqV1WZ57BRj6jmAnv1aAu4kCZuUP1NS1
27
+ kllZY+Rj7NpItVROr1QQEFhJeXgu7kAw24r//BUYB4QjDfYCNa3OUy40SY7uHZwG
28
+ wEVKyY3rIZ7iS6xuS0LfdDOSeyYxKmuLXKqkfKoRpYezMsCOZicc+aBeqjDTkdZj
29
+ UAsGF9/uTVKURgR3bVO7o9qX002avEhaNDPeNYiym4lwoIIB1UBvbA5t1deVc4RA
30
+ K80lsu167t5ZwbXh+Day+g==
31
+ -----END CERTIFICATE-----
32
+
33
+ date: 2008-04-24 00:00:00 -04:00
34
+ default_executable:
35
+ dependencies:
36
+ - !ruby/object:Gem::Dependency
37
+ name: hoe
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 1.5.1
44
+ version:
45
+ description: "GeoHash Gem for Ruby (c) 2008 David Troy dave@roundhousetech.com Geohash is a latitude/longitude encoding system invented by Gustavo Niemeyer when writing the web service at geohash.org, and put into the public domain. Geohashes offer properties like arbitrary precision, similar prefixes for nearby positions, and the possibility of gradually removing characters from the end of the code to reduce its size (and gradually lose precision). See these resources online: http://geohash.org http://en.wikipedia.org/wiki/Geohash"
46
+ email:
47
+ - dave@roundhousetech.com
48
+ executables:
49
+ - geohash
50
+ extensions:
51
+ - extconf.rb
52
+ extra_rdoc_files:
53
+ - History.txt
54
+ - README.txt
55
+ files:
56
+ - History.txt
57
+ - README.txt
58
+ - Rakefile
59
+ - bin/geohash
60
+ - extconf.rb
61
+ - geohash_native.c
62
+ - lib/geohash.rb
63
+ - test/test_geohash.rb
64
+ has_rdoc: true
65
+ homepage: http://rubyforge.org/projects/geohash
66
+ post_install_message:
67
+ rdoc_options:
68
+ - --main
69
+ - README.txt
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: "0"
77
+ version:
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: "0"
83
+ version:
84
+ requirements: []
85
+
86
+ rubyforge_project: geohash
87
+ rubygems_version: 1.0.1
88
+ signing_key:
89
+ specification_version: 2
90
+ summary: GeoHash Gem for Ruby (c) 2008 David Troy dave@roundhousetech.com Geohash is a latitude/longitude encoding system invented by Gustavo Niemeyer when writing the web service at geohash.org, and put into the public domain
91
+ test_files:
92
+ - test/test_geohash.rb
Binary file