Example #1
0
 public function centroid()
 {
     if ($this->isEmpty()) {
         return NULL;
     }
     if ($this->geos()) {
         return GeoPHP::geosToGeometry($this->geos()->centroid());
     }
     $exterior_ring = $this->components[0];
     $pts = $exterior_ring->getComponents();
     $c = count($pts);
     if ((int) $c == '0') {
         return NULL;
     }
     $cn = array('x' => '0', 'y' => '0');
     $a = $this->area(TRUE, TRUE);
     // If this is a polygon with no area. Just return the first point.
     if ($a == 0) {
         return $this->exteriorRing()->pointN(1);
     }
     foreach ($pts as $k => $p) {
         $j = ($k + 1) % $c;
         $P = $p->getX() * $pts[$j]->getY() - $p->getY() * $pts[$j]->getX();
         $cn['x'] = $cn['x'] + ($p->getX() + $pts[$j]->getX()) * $P;
         $cn['y'] = $cn['y'] + ($p->getY() + $pts[$j]->getY()) * $P;
     }
     $cn['x'] = $cn['x'] / (6 * $a);
     $cn['y'] = $cn['y'] / (6 * $a);
     $centroid = new Point($cn['x'], $cn['y']);
     return $centroid;
 }
Example #2
0
File: WKT.php Project: kitbs/geoPHP
 /**
  * Read WKT string into geometry objects
  *
  * @param string $WKT A WKT string
  *
  * @return Geometry
  */
 public function read($wkt)
 {
     $wkt = trim($wkt);
     // If it contains a ';', then it contains additional SRID data
     if (strpos($wkt, ';')) {
         $parts = explode(';', $wkt);
         $wkt = $parts[1];
         $eparts = explode('=', $parts[0]);
         $srid = $eparts[1];
     } else {
         $srid = NULL;
     }
     // If geos is installed, then we take a shortcut and let it parse the WKT
     if (GeoPHP::geosInstalled()) {
         $reader = new GEOSWKTReader();
         if ($srid) {
             $geom = GeoPHP::geosToGeometry($reader->read($wkt));
             $geom->setSRID($srid);
             return $geom;
         } else {
             return GeoPHP::geosToGeometry($reader->read($wkt));
         }
     }
     $wkt = str_replace(', ', ',', $wkt);
     // For each geometry type, check to see if we have a match at the
     // beginning of the string. If we do, then parse using that type
     foreach (GeoPHP::geometryList() as $geom_type) {
         $wkt_geom = strtoupper($geom_type);
         if (strtoupper(substr($wkt, 0, strlen($wkt_geom))) == $wkt_geom) {
             $data_string = $this->getDataString($wkt);
             $method = 'parse' . $geom_type;
             if ($srid) {
                 $geom = $this->{$method}($data_string);
                 $geom->setSRID($srid);
                 return $geom;
             } else {
                 return $this->{$method}($data_string);
             }
         }
     }
 }
Example #3
0
 public function getBBox()
 {
     if ($this->isEmpty()) {
         return NULL;
     }
     if ($this->geos()) {
         $envelope = $this->geos()->envelope();
         if ($envelope->typeName() == 'Point') {
             return GeoPHP::geosToGeometry($envelope)->getBBOX();
         }
         $geos_ring = $envelope->exteriorRing();
         return array('maxy' => $geos_ring->pointN(3)->getY(), 'miny' => $geos_ring->pointN(1)->getY(), 'maxx' => $geos_ring->pointN(1)->getX(), 'minx' => $geos_ring->pointN(3)->getX());
     }
     // Go through each component and get the max and min x and y
     $i = 0;
     foreach ($this->components as $component) {
         $component_bbox = $component->getBBox();
         // On the first run through, set the bbox to the component bbox
         if ($i == 0) {
             $maxx = $component_bbox['maxx'];
             $maxy = $component_bbox['maxy'];
             $minx = $component_bbox['minx'];
             $miny = $component_bbox['miny'];
         }
         // Do a check and replace on each boundary, slowly growing the bbox
         $maxx = $component_bbox['maxx'] > $maxx ? $component_bbox['maxx'] : $maxx;
         $maxy = $component_bbox['maxy'] > $maxy ? $component_bbox['maxy'] : $maxy;
         $minx = $component_bbox['minx'] < $minx ? $component_bbox['minx'] : $minx;
         $miny = $component_bbox['miny'] < $miny ? $component_bbox['miny'] : $miny;
         $i++;
     }
     return array('maxy' => $maxy, 'miny' => $miny, 'maxx' => $maxx, 'minx' => $minx);
 }
Example #4
0
 public function simplify($tolerance, $preserveTopology = FALSE)
 {
     if ($this->geos()) {
         return GeoPHP::geosToGeometry($this->geos()->simplify($tolerance, $preserveTopology));
     }
 }