Class: GeoHex::Zone
- Inherits:
-
Object
- Object
- GeoHex::Zone
- Defined in:
- lib/geohex.rb
Instance Attribute Summary collapse
-
#code ⇒ Object
Returns the value of attribute code.
-
#lat ⇒ Object
Returns the value of attribute lat.
-
#lon ⇒ Object
Returns the value of attribute lon.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Class Method Summary collapse
- .calcHexSize(level) ⇒ Object
-
.decode(code) ⇒ Object
geohex to latlon.
-
.encode(lat, lon, level = 7) ⇒ Object
latlon to geohex.
- .loc2xy(_lon, _lat) ⇒ Object
- .xy2loc(_x, _y) ⇒ Object
Instance Method Summary collapse
- #hexCoords ⇒ Object
- #hexSize ⇒ Object
-
#initialize(*params) ⇒ Zone
constructor
A new instance of Zone.
- #level ⇒ Object
Constructor Details
#initialize(*params) ⇒ Zone
Returns a new instance of Zone.
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/geohex.rb', line 20 def initialize(*params) raise ArgumentError, "latlng or code is needed" if params.size == 0 if params.first.is_a?(Float) @lat,@lon = params[0],params[1] @level= params[2]||7 @code= GeoHex::Zone.encode(@lat,@lon,@level) else @code = params.first @lat,@lon,@level=GeoHex::Zone.decode(@code) end end |
Instance Attribute Details
#code ⇒ Object
Returns the value of attribute code.
19 20 21 |
# File 'lib/geohex.rb', line 19 def code @code end |
#lat ⇒ Object
Returns the value of attribute lat.
19 20 21 |
# File 'lib/geohex.rb', line 19 def lat @lat end |
#lon ⇒ Object
Returns the value of attribute lon.
19 20 21 |
# File 'lib/geohex.rb', line 19 def lon @lon end |
#x ⇒ Object
Returns the value of attribute x.
19 20 21 |
# File 'lib/geohex.rb', line 19 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
19 20 21 |
# File 'lib/geohex.rb', line 19 def y @y end |
Class Method Details
.calcHexSize(level) ⇒ Object
32 33 34 |
# File 'lib/geohex.rb', line 32 def self.calcHexSize(level) H_BASE/(2**level)/3 end |
.decode(code) ⇒ Object
geohex to latlon
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/geohex.rb', line 146 def self.decode(code) c_length = code.length; level = H_KEY.index(code[0,1]); scl = level; h_size = self.calcHexSize(level); unit_x = 6.0 * h_size; unit_y = 6.0 * h_size * H_K; h_max = (H_BASE / unit_x + H_BASE / unit_y).round; h_x = 0; h_y = 0; if (h_max >= 12960000 / 2) h_x = H_KEY.index(code[1,1]) * 12960000 + H_KEY.index(code[3,1]) * 216000 + H_KEY.index(code[5,1]) * 3600 + H_KEY.index(code[7,1]) * 60 + H_KEY.index(code[9,1]); h_y = H_KEY.index(code[2,1]) * 12960000 + H_KEY.index(code[4,1]) * 216000 + H_KEY.index(code[6,1]) * 3600 + H_KEY.index(code[8,1]) * 60 + H_KEY.index(code[10,1]); elsif (h_max >= 216000 / 2) h_x = H_KEY.index(code[1,1]) * 216000 + H_KEY.index(code[3,1]) * 3600 + H_KEY.index(code[5,1]) * 60 + H_KEY.index(code[7,1]); h_y = H_KEY.index(code[2,1]) * 216000 + H_KEY.index(code[4,1]) * 3600 + H_KEY.index(code[6,1]) * 60 + H_KEY.index(code[8,1]); elsif (h_max >= 3600 / 2) h_x = H_KEY.index(code[1,1]) * 3600 + H_KEY.index(code[3,1]) * 60 + H_KEY.index(code[5,1]); h_y = H_KEY.index(code[2,1]) * 3600 + H_KEY.index(code[4,1]) * 60 + H_KEY.index(code[6,1]); elsif (h_max >= 60 / 2) h_x = H_KEY.index(code[1,1]) * 60 + H_KEY.index(code[3,1]); h_y = H_KEY.index(code[2,1]) * 60 + H_KEY.index(code[4,1]); else h_x = H_KEY.index(code[1,1]); h_y = H_KEY.index(code[2,1]); end h_x = (h_x % 2 == 1) ? -(h_x - 1) / 2 : h_x / 2; h_y = (h_y % 2 == 1) ? -(h_y - 1) / 2 : h_y / 2; h_lat_y = (H_K * h_x * unit_x + h_y * unit_y) / 2; h_lon_x = (h_lat_y - h_y * unit_y) / H_K; h_loc = xy2loc(h_lon_x, h_lat_y); return [h_loc.lat, h_loc.lon, level] end |
.encode(lat, lon, level = 7) ⇒ Object
latlon to geohex
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/geohex.rb', line 70 def self.encode(lat,lon,level=7) raise ArgumentError, "latitude must be double" unless (lat.is_a?(Numeric)) raise ArgumentError, "latitude must be between -85 and 85" if (lat < -85.1 || lat > 85.1) raise ArgumentError, "latitude must be between -85 and 85" if (lat < -85.1 || lat > 85.1) raise ArgumentError, "longitude must be between -180 and 180" if (lon < -180 || lon > 180) raise ArgumentError, "level must be between 0 and 24" if (level < 0 || level > 24) h_size = self.calcHexSize(level) z_xy = loc2xy(lon,lat) lon_grid = z_xy.x lat_grid = z_xy.y unit_x = 6.0* h_size; unit_y = 6.0* h_size*H_K; h_pos_x = (lon_grid + lat_grid / H_K) / unit_x; h_pos_y = (lat_grid - H_K*lon_grid) / unit_y; h_x_0 = h_pos_x.floor; h_y_0 = h_pos_y.floor; h_x_q = h_pos_x - h_x_0 h_y_q = h_pos_y - h_y_0 h_x = h_pos_x.round; h_y = h_pos_y.round; h_max=(H_BASE/unit_x + H_BASE/unit_y).round; if ( h_y_q > -h_x_q + 1 ) if ( h_y_q < (2 * h_x_q ) and h_y_q > (0.5 * h_x_q ) ) h_x = h_x_0 + 1 h_y = h_y_0 + 1 end elsif ( h_y_q < -h_x_q + 1 ) if ( (h_y_q > (2 * h_x_q ) - 1 ) && ( h_y_q < ( 0.5 * h_x_q ) + 0.5 ) ) h_x = h_x_0 h_y = h_y_0 end end h_lat = (H_K * h_x * unit_x + h_y * unit_y) / 2; h_lon = (h_lat - h_y * unit_y)/H_K; z_loc = xy2loc(h_lon,h_lat) z_loc_x = z_loc.lon z_loc_y = z_loc.lat if (H_BASE - h_lon <h_size) z_loc_x = 180.0; h_xy = h_x; h_x = h_y; h_y = h_xy; end h_x_p = (h_x<0) ? 1 : 0; h_y_p = (h_y<0) ? 1 : 0; h_x_abs = ((h_x).abs * 2 + h_x_p).to_f; h_y_abs = ((h_y).abs * 2 + h_y_p).to_f; h_x_10000 = ((h_x_abs%777600000)/12960000).floor; h_x_1000 = ((h_x_abs%12960000)/216000).floor; h_x_100 = ((h_x_abs%216000)/3600).floor; h_x_10 = ((h_x_abs%3600)/60).floor; h_x_1 = ((h_x_abs%3600)%60).floor; h_y_10000 = ((h_y_abs%77600000)/12960000).floor; h_y_1000 = ((h_y_abs%12960000)/216000).floor; h_y_100 = ((h_y_abs%216000)/3600).floor; h_y_10 = ((h_y_abs%3600)/60).floor; h_y_1 = ((h_y_abs%3600)%60).floor; h_code = H_KEY[level % 60, 1]; h_code += H_KEY[h_x_10000, 1]+H_KEY[h_y_10000, 1] if(h_max >=12960000/2) ; h_code += H_KEY[h_x_1000, 1]+H_KEY[h_y_1000, 1] if(h_max >=216000/2) ; h_code += H_KEY[h_x_100, 1]+H_KEY[h_y_100, 1] if(h_max >=3600/2) ; h_code += H_KEY[h_x_10, 1]+H_KEY[h_y_10, 1] if(h_max >=60/2) ; h_code += H_KEY[h_x_1, 1]+H_KEY[h_y_1, 1]; return h_code; end |
.loc2xy(_lon, _lat) ⇒ Object
205 206 207 208 209 210 |
# File 'lib/geohex.rb', line 205 def loc2xy(_lon,_lat) x=_lon*H_BASE/180; y= Math.log(Math.tan((90+_lat)*Math::PI/360)) / (Math::PI / 180 ); y= y * H_BASE / 180; return OpenStruct.new("x"=>x, "y"=>y); end |
.xy2loc(_x, _y) ⇒ Object
212 213 214 215 216 217 |
# File 'lib/geohex.rb', line 212 def xy2loc(_x,_y) lon=(_x/H_BASE)*180; lat=(_y/H_BASE)*180; lat=180.0/Math::PI*(2.0*Math.atan(Math.exp(lat*Math::PI/180))-Math::PI/2); return OpenStruct.new("lon" => lon,"lat" => lat); end |
Instance Method Details
#hexCoords ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/geohex.rb', line 43 def hexCoords h_lat = self.lat; h_lon = self.lon; h_xy = GeoHex::Zone.loc2xy(h_lon, h_lat); h_x = h_xy.x h_y = h_xy.y h_deg = Math.tan(Math::PI * (60 / 180)); h_size = self.hexSize h_top = xy2loc(h_x, h_y + h_deg * h_size).lat; h_btm = xy2loc(h_x, h_y - h_deg * h_size).lat; h_l = xy2loc(h_x - 2 * h_size, h_y).lon; h_r = xy2loc(h_x + 2 * h_size, h_y).lon; h_cl = xy2loc(h_x - 1 * h_size, h_y).lon; h_cr = xy2loc(h_x + 1 * h_size, h_y).lon; [ {:lat => h_lat, :lon => h_l}, {:lat => h_top, :lon => h_cl}, {:lat => h_top, :lon => h_cr}, {:lat => h_lat, :lon => h_r}, {:lat => h_btm, :lon => h_cr}, {:lat => h_btm, :lon => h_cl} ] end |
#hexSize ⇒ Object
39 40 41 |
# File 'lib/geohex.rb', line 39 def hexSize self.calcHexSize(self.level) end |
#level ⇒ Object
36 37 38 |
# File 'lib/geohex.rb', line 36 def level H_KEY.index(code[0,1]) end |