GeoHash::encodePoint()   B
last analyzed

Complexity

Conditions 7
Paths 12

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 12
nop 2
dl 0
loc 47
rs 8.223
c 0
b 0
f 0
1
<?php
2
/**
3
 * PHP Geometry GeoHash encoder/decoder.
4
 *
5
 * @author prinsmc
6
 * @see http://en.wikipedia.org/wiki/Geohash
7
 *
8
 */
9
class GeoHash extends GeoAdapter{
10
  private $table = "0123456789bcdefghjkmnpqrstuvwxyz";
11
12
  /**
13
   * Convert the geohash to a Point. The point is 2-dimensional.
14
   * @return Point the converted geohash
15
   * @param string $hash a geohash
16
   * @see GeoAdapter::read()
17
   */
18
  public function read($hash, $as_grid = FALSE) {
19
    $ll = $this->decode($hash);
20
    if (!$as_grid) {
21
      return new Point($ll['medlon'], $ll['medlat']);
0 ignored issues
show
Documentation introduced by
$ll['medlon'] is of type double, but the function expects a object<numeric>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
22
    }
23
    else {
24
      return new Polygon(array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Polygon(arra...'], $ll['maxlat']))))); (Polygon) is incompatible with the return type documented by GeoHash::read of type Point.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
25
        new LineString(array(
26
          new Point($ll['minlon'], $ll['maxlat']),
27
          new Point($ll['maxlon'], $ll['maxlat']),
28
          new Point($ll['maxlon'], $ll['minlat']),
29
          new Point($ll['minlon'], $ll['minlat']),
30
          new Point($ll['minlon'], $ll['maxlat']),
31
        ))
32
      ));
33
    }
34
  }
35
36
  /**
37
   * Convert the geometry to geohash.
38
   * @return string the geohash or null when the $geometry is not a Point
39
   * @param Point $geometry
40
   * @see GeoAdapter::write()
41
   */
42
  public function write(Geometry $geometry, $precision = NULL){
43
    if ($geometry->isEmpty()) return '';
44
45
    if($geometry->geometryType() === 'Point'){
46
      return $this->encodePoint($geometry, $precision);
0 ignored issues
show
Compatibility introduced by
$geometry of type object<Geometry> is not a sub-type of object<Point>. It seems like you assume a child class of the class Geometry to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
47
    }
48
    else {
49
      // The geohash is the hash grid ID that fits the envelope
50
      $envelope = $geometry->envelope();
51
      $geohashes = array();
52
      $geohash = '';
53
      foreach ($envelope->getPoints() as $point) {
54
        $geohashes[] = $this->encodePoint($point, 0.0000001);
55
      }
56
      $i = 0;
57
      while ($i < strlen($geohashes[0])) {
58
        $char = $geohashes[0][$i];
59
        foreach ($geohashes as $hash) {
60
          if ($hash[$i] != $char) {
61
            return $geohash;
62
          }
63
        }
64
        $geohash .= $char;
65
        $i++;
66
      }
67
      return $geohash;
68
    }
69
  }
70
71
  /**
72
   * @return string geohash
73
   * @param Point $point
74
   * @author algorithm based on code by Alexander Songe <[email protected]>
75
   * @see https://github.com/asonge/php-geohash/issues/1
76
   */
77
  private function encodePoint($point, $precision = NULL){
78
    if ($precision === NULL) {
79
      $lap = strlen($point->y())-strpos($point->y(),".");
80
      $lop = strlen($point->x())-strpos($point->x(),".");
81
      $precision = pow(10,-max($lap-1,$lop-1,0))/2;
82
    }
83
84
    $minlat =  -90;
85
    $maxlat =   90;
86
    $minlon = -180;
87
    $maxlon =  180;
88
    $latE   =   90;
89
    $lonE   =  180;
90
    $i = 0;
91
    $error = 180;
92
    $hash='';
93
    while($error>=$precision) {
94
      $chr = 0;
95
      for($b=4;$b>=0;--$b) {
96
        if((1&$b) == (1&$i)) {
97
          // even char, even bit OR odd char, odd bit...a lon
98
          $next = ($minlon+$maxlon)/2;
99
          if($point->x()>$next) {
100
            $chr |= pow(2,$b);
101
            $minlon = $next;
102
          } else {
103
            $maxlon = $next;
104
          }
105
          $lonE /= 2;
106
        } else {
107
          // odd char, even bit OR even char, odd bit...a lat
108
          $next = ($minlat+$maxlat)/2;
109
          if($point->y()>$next) {
110
            $chr |= pow(2,$b);
111
            $minlat = $next;
112
          } else {
113
            $maxlat = $next;
114
          }
115
          $latE /= 2;
116
        }
117
      }
118
      $hash .= $this->table[$chr];
119
      $i++;
120
      $error = min($latE,$lonE);
121
    }
122
    return $hash;
123
  }
124
125
  /**
126
   * @param string $hash a geohash
127
   * @author algorithm based on code by Alexander Songe <[email protected]>
128
   * @see https://github.com/asonge/php-geohash/issues/1
129
   */
130
  private function decode($hash){
131
    $ll = array();
132
    $minlat =  -90;
133
    $maxlat =   90;
134
    $minlon = -180;
135
    $maxlon =  180;
136
    $latE   =   90;
137
    $lonE   =  180;
138
    for($i=0,$c=strlen($hash);$i<$c;$i++) {
139
      $v = strpos($this->table,$hash[$i]);
140
      if(1&$i) {
141
        if(16&$v)$minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2;
142
        if(8&$v) $minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2;
143
        if(4&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2;
144
        if(2&$v) $minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2;
145
        if(1&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2;
146
        $latE /= 8;
147
        $lonE /= 4;
148
      } else {
149
        if(16&$v)$minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2;
150
        if(8&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2;
151
        if(4&$v) $minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2;
152
        if(2&$v) $minlat = ($minlat+$maxlat)/2; else $maxlat = ($minlat+$maxlat)/2;
153
        if(1&$v) $minlon = ($minlon+$maxlon)/2; else $maxlon = ($minlon+$maxlon)/2;
154
        $latE /= 4;
155
        $lonE /= 8;
156
      }
157
    }
158
    $ll['minlat'] = $minlat;
159
    $ll['minlon'] = $minlon;
160
    $ll['maxlat'] = $maxlat;
161
    $ll['maxlon'] = $maxlon;
162
    $ll['medlat'] = round(($minlat+$maxlat)/2, max(1, -round(log10($latE)))-1);
163
    $ll['medlon'] = round(($minlon+$maxlon)/2, max(1, -round(log10($lonE)))-1);
164
    return $ll;
165
  }
166
}
167