1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 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
 68
 69
 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
144
145
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//! An integer based geohash. Inspired by the geohashrust package.
//!
//! It uses the first 4 bits of the u64 to store the precision
//! then the subsequent bits are used to store the hash value.
//!
//! Currently there is a maximum precision of 15
//!

// todo: keep implementing stuff
// geohashrust/geohash.rs.html
// http://gis.stackexchange.com/questions/18330/would-it-be-possible-
// to-use-geohash-for-proximity-searches

use nalgebra::Vector2;

// first 4 bits is currently reserved for the precision with a range of 1 to 16

/// maximum value for precision of a geohash
pub static PRECISION_MAX: u8 = 58;

/// minimum value for precision of a geohash
pub static PRECISION_MIN: u8 = 1;

/// number of precision bits
pub static PRECISION_BITS: u8 = 6;

static PRECISION_MASK: u64 = 63; //mask 111111 for the 6 precision bits

/// An object which is able to be geohashed.
pub trait Geohashable<T> {
    /// decode this object of type `T`from an integer geohash
    fn integer_decode(geohash: u64) -> Result<T, String>;

    /// encode this object of type `T` into an unsigned integer geohash
    fn integer_encode(&self, precision: u8) -> Result<u64, String>;
}

/// A trait for a vector type object that can be rectified to fit
/// within a boundary.
pub trait Rectifiable {
    /// wrap this position around the bounds,
    /// such that it becomes the equivalent position
    /// within the bounds.
    fn spherical_rectify(&mut self, range: &Bounds);
}


// todo: figure out a way to implement this for coord so we don't have code and test duplication
impl Rectifiable for Vector2<f64> {
    /// wrap this position around the bounds,
    /// such that it becomes the equivalent position
    /// within the bounds.
    ///
    /// # Example
    ///
    /// ```
    /// #[macro_use]
    /// extern crate nalgebra;
    /// # extern crate oldnav_lib;
    /// # use oldnav_lib::navdata::geohash::*;
    /// use nalgebra::{Vector2, ApproxEq};
    /// # fn main() {
    /// let mut p = Vector2{x: 185.0, y: 30.0};
    /// p.spherical_rectify(&LATLON_BOUNDS);
    /// assert_approx_eq_eps!(p, Vector2{x: -175.0, y: 30.0}, 0.1);
    /// # }
    /// ```
    fn spherical_rectify(&mut self, range: &Bounds) {
        let x_range = range.x_range();
        let y_range = range.y_range();

        println!("x_range = {:#?}", x_range);
        println!("y_range = {:#?}", y_range);

        println!("self.y 0 = {:?}", self.y);

        while self.y < range.y_min - y_range {
            self.y += y_range;
        }

        println!("self.y 1 = {:#?}", self.y);

        while self.y > range.y_max + y_range {
            self.y -= y_range;
        }

        println!("self.y 2 = {:#?}", self.y);
        println!("self.x 1 = {:#?}", self.x);

        if self.y > range.y_max {
            self.y = y_range - self.y;
            self.x += x_range / 2.0;
        } else {
            if self.y < range.y_min {
                self.y = -(self.y + y_range);
                self.x += x_range / 2.0;
            }
        }

        while self.x > range.x_max {
            self.x -= x_range;
        }

        println!("self.x 3 = {:#?}", self.x);

        while self.x < range.x_min {
            self.x += x_range;
        }

        println!("self.x 4 = {:#?}", self.x);
    }
}

/// A rectangular boundary
#[derive(Clone, Debug, Copy)]
pub struct Bounds {
    /// x coordinate minimum
    pub x_min: f64,

    /// x coordinate maximum
    pub x_max: f64,

    /// y coordinate minimum
    pub y_min: f64,

    /// y coordinate maximum
    pub y_max: f64,
}

impl Bounds {
    /// Constructor for `Bounds`
    pub fn new(x_min: f64, x_max: f64, y_min: f64, y_max: f64) -> Bounds {
        Bounds {
            x_min: x_min,
            x_max: x_max,
            y_min: y_min,
            y_max: y_max,
        }
    }

    /// Get the midpoint of this `Bound`
    pub fn mid(&self) -> Vector2<f64> {
        Vector2 {
            x: (self.x_max + self.x_min) / 2.0,
            y: (self.y_max + self.y_min) / 2.0,
        }
    }

    /// Test whether a `Vector2` position falls within this `Bound`
    ///
    /// # Example
    ///
    /// A point which falls within a boundary:
    ///
    /// ```
    /// # extern crate nalgebra;
    /// # extern crate oldnav_lib;
    /// # use oldnav_lib::navdata::geohash::*;
    /// use nalgebra::Vector2;
    /// # fn main() {
    /// let b = Bounds::new(-21.0, 0.0, 2.0, 4.0);
    /// let p1 = Vector2{x: -2.1, y: 2.45};
    ///
    /// assert_eq!(true, b.contains(&p1));
    /// # }
    /// ```
    ///
    /// A point that falls outside a boundary:
    ///
    /// ```
    /// # extern crate nalgebra;
    /// # extern crate oldnav_lib;
    /// # use oldnav_lib::navdata::geohash::*;
    /// use nalgebra::Vector2;
    /// # fn main() {
    /// let b = Bounds::new(-21.0, 0.0, 2.0, 4.0);
    /// let p2: Vector2<f64> = Vector2{x: -100.0, y: 2.45};
    ///
    /// assert_eq!(false, b.contains(&p2));
    /// # }
    /// ```
    pub fn contains(&self, position: &Vector2<f64>) -> bool {
        if position.x <= self.x_max && position.x >= self.x_min && position.y <= self.y_max &&
           position.y >= self.y_min {
            return true;
        }
        return false;
    }

    /// get the width of the boundary
    pub fn x_range(&self) -> f64 {
        return (self.x_max - self.x_min).abs();
    }

    /// get the height of the boundary
    pub fn y_range(&self) -> f64 {
        return (self.y_max - self.y_min).abs();
    }
}

/// bounds for the lat/lon coordinate system
pub static LATLON_BOUNDS: Bounds = Bounds {
    y_min: -90.0,
    y_max: 90.0,
    x_min: -180.0,
    x_max: 180.0,
};


/// encode a `Vector2` position into an unsigned integer geohash.
/// wraps the position around the boundaries, to keep it within them.
///
/// # Example
///
/// ```
/// # extern crate nalgebra;
/// # extern crate oldnav_lib;
/// # use oldnav_lib::navdata::geohash::*;
/// use nalgebra::Vector2;
/// # fn main() {
/// let p = Vector2{y: 31.23, x: 121.473};
/// let bounds = LATLON_BOUNDS.clone();
/// let gh = encode(&p, 8, &bounds).unwrap();
/// assert_eq!(hash_to_string(gh).unwrap(), "11100110");
/// # }
/// ```
pub fn encode(position: &Vector2<f64>, precision: u8, range: &Bounds) -> Result<u64, String> {
    if precision > PRECISION_MAX || precision < PRECISION_MIN {
        return Err(format!("Precision must be in the range of {} to {}",
                           PRECISION_MIN,
                           PRECISION_MAX));
    }

    let mut current_range: Bounds = range.clone();

    let mut hash: u64 = (precision as u64) - 1;

    let mut do_x = true;

    // start leaving room for the precision bits
    let mut i = PRECISION_BITS;
    let end = precision + PRECISION_BITS;
    while i < end {
        let mid = current_range.mid();

        if do_x {
            if position.x > mid.x {
                hash |= 1 << i;
                current_range.x_min = mid.x;
            } else {
                current_range.x_max = mid.x;
            }
        } else {
            if position.y > mid.y {
                hash |= 1 << i;
                current_range.y_min = mid.y;
            } else {
                current_range.y_max = mid.y;
            }
        }

        do_x = !do_x;
        i += 1;
    }

    return Ok(hash);
}

/// Convert an integer geohash to a string representation of the binary values
///
/// # Example
///
/// ```
/// # use oldnav_lib::navdata::geohash::*;
/// let gh = hash_from_string("11101011").unwrap();
/// assert_eq!("11101011", hash_to_string(gh).unwrap());
/// ```
///
/// ```
/// # use oldnav_lib::navdata::geohash::*;
/// let gh = hash_from_string("1111").unwrap();
/// assert_eq!("1111", hash_to_string(gh).unwrap());
/// ```
///
pub fn hash_to_string(geohash: u64) -> Result<String, String> {
    let precision: u8 = hash_precision(geohash);
    let mut string = String::with_capacity(precision as usize);

    println!("Precision {}", precision);

    let mut i = PRECISION_BITS;
    let end = PRECISION_BITS + precision;
    while i < end {
        let val = geohash & 1 << i;
        string.push(if val > 0 { '1' } else { '0' });
        i += 1;
    }

    return Ok(string);
}

/// Create an integer geohash from a String
///
/// # Example
///
/// ```
/// # use oldnav_lib::navdata::geohash::*;
/// let gh = hash_from_string("1111").unwrap();
/// assert_eq!(4, hash_precision(gh));
/// assert_eq!("1111", hash_to_string(gh).unwrap());
/// ```
pub fn hash_from_string(string: &str) -> Result<u64, String> {
    let precision = string.len();

    if precision > (PRECISION_MAX as usize) || precision < (PRECISION_MIN as usize) {
        return Err(format!("String too long, its length must be in the range of {} to {}",
                           PRECISION_MIN,
                           PRECISION_MAX));
    }

    let mut hash: u64 = (precision as u64) - 1;

    let mut i = PRECISION_BITS;

    for char in string.chars() {
        if char == '1' {
            hash |= 1 << i;
        }
        i += 1;
    }

    return Ok(hash);
}

/// Decode integer geohash into a `Bounds`
///
/// # Example
///
/// ```
/// # extern crate nalgebra;
/// # extern crate oldnav_lib;
/// # use oldnav_lib::navdata::geohash::*;
/// use nalgebra::Vector2;
/// # fn main() {
/// let p = Vector2{y: -38.12, x: 148.234};
/// let gh = encode(&p, 40, &LATLON_BOUNDS).unwrap();
/// let b = decode(gh, &LATLON_BOUNDS).unwrap();
/// assert_eq!(true, b.contains(&p));
/// # }
/// ```
pub fn decode(geohash: u64, range: &Bounds) -> Result<Bounds, String> {
    let precision: u8 = hash_precision(geohash);
    return decode_precision_nocheck(geohash, precision, range);
}

/// decode an integer geohash with the specified precision.
pub fn decode_precision(geohash: u64, precision: u8, range: &Bounds) -> Result<Bounds, String> {
    let hp: u8 = hash_precision(geohash);
    if precision > hp {
        return Err(format!("Selected precision ({}) is greater than the max precision of the \
                            hash: ({})",
                           precision,
                           hp));
    }

    return decode_precision_nocheck(geohash, precision, range);
}

/// decode an integer geohash with specified precision, without checking
/// whether or not the precision is less than the hash's internal precision.
pub fn decode_precision_nocheck(geohash: u64,
                                precision: u8,
                                range: &Bounds)
                                -> Result<Bounds, String> {
    let mut current_range: Bounds = range.clone();

    let mut do_x = true;


    let mut i = PRECISION_BITS;
    let end = PRECISION_BITS + precision;

    while i < end {
        let mid = current_range.mid();

        if do_x {
            if geohash & 1 << i > 0 {
                current_range.x_min = mid.x;
            } else {
                current_range.x_max = mid.x;
            }
        } else {
            if geohash & 1 << i > 0 {
                current_range.y_min = mid.y;
            } else {
                current_range.y_max = mid.y;
            }
        }

        do_x = !do_x;
        i += 1;
    }

    return Ok(current_range);
}


/// Get the precision value for an integer geohash.
///
/// # Example
///
/// length of 4:
/// ```
/// # use oldnav_lib::navdata::geohash::*;
/// let gh = hash_from_string("1010").unwrap();
/// assert_eq!(4, hash_precision(gh));
/// ```
///
/// length of 8:
/// ```
/// # use oldnav_lib::navdata::geohash::*;
/// let gh = hash_from_string("00000000");
/// assert_eq!(8, hash_precision(gh));
/// ```
pub fn hash_precision(geohash: u64) -> u8 {
    return ((geohash & PRECISION_MASK) as u8) + 1;
}


/// Get the neighboring hash in the direction specified
/// spherical: is whether or not to use spherical rectification/bounds wrapping.
/// # Example
///
/// ```
///
/// ```
pub fn neighbor(geohash: u64,
                dir: (i8, i8),
                range: &Bounds,
                spherical: bool)
                -> Result<u64, String> {
    let b: Bounds = try!(decode(geohash, range));
    let precision = hash_precision(geohash);
    let (x_dir, y_dir) = dir;
    let midpoint = b.mid();

    let mut np = Vector2 {
        x: midpoint.x + b.x_range() * (x_dir as f64),
        y: midpoint.y + b.y_range() * (y_dir as f64),
    };

    if spherical {
        np.spherical_rectify(range)
    }

    return encode(&np, precision, range);
}