Polygon::isSimple()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 6
nop 0
dl 0
loc 18
rs 9.0444
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Polygon: A polygon is a plane figure that is bounded by a closed path, 
5
 * composed of a finite sequence of straight line segments
6
 */
7
class Polygon extends Collection
8
{
9
  protected $geom_type = 'Polygon';
10
11
  public function area($exterior_only = FALSE, $signed = FALSE) {
12
    if ($this->isEmpty()) return 0;
13
    
14
    if ($this->geos() && $exterior_only == FALSE) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
15
      return $this->geos()->area();
16
    }
17
    
18
    $exterior_ring = $this->components[0];
19
    $pts = $exterior_ring->getComponents();
20
    
21
    $c = count($pts);
22
    if((int)$c == '0') return NULL;
23
    $a = '0';
24
    foreach($pts as $k => $p){
25
      $j = ($k + 1) % $c;
26
      $a = $a + ($p->getX() * $pts[$j]->getY()) - ($p->getY() * $pts[$j]->getX());
27
    }
28
    
29
    if ($signed) $area = ($a / 2);
30
    else $area = abs(($a / 2));
31
    
32
    if ($exterior_only == TRUE) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
33
      return $area;
34
    }
35
    foreach ($this->components as $delta => $component) {
36
      if ($delta != 0) {
37
        $inner_poly = new Polygon(array($component));
38
        $area -= $inner_poly->area();
39
      }
40
    }
41
    return $area;
42
  }
43
  
44
  public function centroid() {
45
    if ($this->isEmpty()) return NULL;
46
    
47
    if ($this->geos()) {
48
      return geoPHP::geosToGeometry($this->geos()->centroid());
49
    }
50
    
51
    $exterior_ring = $this->components[0];
52
    $pts = $exterior_ring->getComponents();
53
    
54
    $c = count($pts);
55
    if((int)$c == '0') return NULL;
56
    $cn = array('x' => '0', 'y' => '0');
57
    $a = $this->area(TRUE, TRUE);
58
    
59
    // If this is a polygon with no area. Just return the first point.
60
    if ($a == 0) {
61
      return $this->exteriorRing()->pointN(1);
62
    }
63
    
64
    foreach($pts as $k => $p){
65
      $j = ($k + 1) % $c;
66
      $P = ($p->getX() * $pts[$j]->getY()) - ($p->getY() * $pts[$j]->getX());
67
      $cn['x'] = $cn['x'] + ($p->getX() + $pts[$j]->getX()) * $P;
68
      $cn['y'] = $cn['y'] + ($p->getY() + $pts[$j]->getY()) * $P;
69
    }
70
    
71
    $cn['x'] = $cn['x'] / ( 6 * $a);
72
    $cn['y'] = $cn['y'] / ( 6 * $a);
73
    
74
    $centroid = new Point($cn['x'], $cn['y']);
0 ignored issues
show
Documentation introduced by
$cn['y'] is of type integer|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...
75
    return $centroid;
76
  }
77
78
	/**
79
	 * Find the outermost point from the centroid
80
	 *
81
	 * @returns Point The outermost point
82
	 */
83
  public function outermostPoint() {
84
		$centroid = $this->getCentroid();
85
86
		$max = array('length' => 0, 'point' => null);
87
88
		foreach($this->getPoints() as $point) {
89
			$lineString = new LineString(array($centroid, $point));
90
91
			if($lineString->length() > $max['length']) {
92
				$max['length'] = $lineString->length();
93
				$max['point'] = $point;
94
			}
95
		}
96
97
		return $max['point'];
98
  }
99
100
  public function exteriorRing() {
101
    if ($this->isEmpty()) return new LineString();
102
    return $this->components[0];
103
  }
104
  
105
  public function numInteriorRings() {
106
    if ($this->isEmpty()) return 0;
107
    return $this->numGeometries()-1;
108
  }
109
  
110
  public function interiorRingN($n) {
111
    return $this->geometryN($n+1);
112
  }
113
  
114
  public function dimension() {
115
    if ($this->isEmpty()) return 0;
116
    return 2;
117
  }
118
119
  public function isSimple() {
120
    if ($this->geos()) {
121
      return $this->geos()->isSimple();
122
    }
123
    
124
    $segments = $this->explode();
125
    
126
    foreach ($segments as $i => $segment) {
127
      foreach ($segments as $j => $check_segment) {
128
        if ($i != $j) {
129
          if ($segment->lineSegmentIntersect($check_segment)) {
130
            return FALSE;
131
          }
132
        }
133
      }
134
    }
135
    return TRUE;
136
  }
137
138
  /**
139
   * For a given point, determine whether it's bounded by the given polygon.
140
   * Adapted from http://www.assemblysys.com/dataServices/php_pointinpolygon.php
141
   * @see http://en.wikipedia.org/wiki/Point%5Fin%5Fpolygon
142
   *
143
   * @param Point $point 
144
   * @param boolean $pointOnBoundary - whether a boundary should be considered "in" or not
145
   * @param boolean $pointOnVertex - whether a vertex should be considered "in" or not
146
   * @return boolean
147
   */
148
  public function pointInPolygon($point, $pointOnBoundary = true, $pointOnVertex = true) {
149
    $vertices = $this->getPoints();
150
151
    // Check if the point sits exactly on a vertex
152
    if ($this->pointOnVertex($point, $vertices)) {
0 ignored issues
show
Unused Code introduced by
The call to Polygon::pointOnVertex() has too many arguments starting with $vertices.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
153
      return $pointOnVertex ? TRUE : FALSE;
154
    }
155
  
156
    // Check if the point is inside the polygon or on the boundary
157
    $intersections = 0; 
158
    $vertices_count = count($vertices);
159
160
    for ($i=1; $i < $vertices_count; $i++) {
161
      $vertex1 = $vertices[$i-1]; 
162
      $vertex2 = $vertices[$i];
163
      if ($vertex1->y() == $vertex2->y() 
164
      && $vertex1->y() == $point->y() 
165
      && $point->x() > min($vertex1->x(), $vertex2->x()) 
166
      && $point->x() < max($vertex1->x(), $vertex2->x())) {
167
        // Check if point is on an horizontal polygon boundary
168
        return $pointOnBoundary ? TRUE : FALSE;
169
      }
170
      if ($point->y() > min($vertex1->y(), $vertex2->y())
171
      && $point->y() <= max($vertex1->y(), $vertex2->y())
172
      && $point->x() <= max($vertex1->x(), $vertex2->x())
173
      && $vertex1->y() != $vertex2->y()) {
174
        $xinters = 
175
          ($point->y() - $vertex1->y()) * ($vertex2->x() - $vertex1->x())
176
          / ($vertex2->y() - $vertex1->y()) 
177
          + $vertex1->x();
178
        if ($xinters == $point->x()) {
179
          // Check if point is on the polygon boundary (other than horizontal)
180
          return $pointOnBoundary ? TRUE : FALSE;
181
        }
182
        if ($vertex1->x() == $vertex2->x() || $point->x() <= $xinters) {
183
          $intersections++;
184
        }
185
      } 
186
    } 
187
    // If the number of edges we passed through is even, then it's in the polygon.
188
    if ($intersections % 2 != 0) {
189
      return TRUE;
190
    }
191
    else {
192
      return FALSE;
193
    }
194
  }
195
  
196
  public function pointOnVertex($point) {
197
    foreach($this->getPoints() as $vertex) {
198
      if ($point->equals($vertex)) {
199
        return true;
200
      }
201
    }
202
  }
203
204
205
  // Not valid for this geometry type
206
  // --------------------------------
207
  public function length() { return NULL; }
208
  
209
}
210
211