public function centroid() { if ($this->isEmpty()) { return null; } if ($this->geos()) { return FOX_geo::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 FOX_point($cn['x'], $cn['y']); return $centroid; }
public function getBBox() { if ($this->isEmpty()) { return null; } if ($this->geos()) { $envelope = $this->geos()->envelope(); if ($envelope->typeName() == 'Point') { return FOX_geo::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 // ===================================================================== $first_item = true; $maxx = 0; $maxy = 0; $minx = 0; $miny = 0; foreach ($this->components as $component) { $component_bbox = $component->getBBox(); // On the first run through, set the bbox to the component bbox if ($first_item) { $maxx = $component_bbox['maxx']; $maxy = $component_bbox['maxy']; $minx = $component_bbox['minx']; $miny = $component_bbox['miny']; $first_item = false; } else { if ($component_bbox['maxx'] > $maxx) { $maxx = $component_bbox['maxx']; } if ($component_bbox['maxy'] > $maxy) { $maxy = $component_bbox['maxy']; } if ($component_bbox['minx'] < $minx) { $minx = $component_bbox['minx']; } if ($component_bbox['miny'] < $miny) { $miny = $component_bbox['miny']; } } } return array('maxy' => $maxy, 'miny' => $miny, 'maxx' => $maxx, 'minx' => $minx); }
/** * 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 (FOX_geo::geosInstalled()) { $reader = new GEOSWKTReader(); if ($srid) { $geom = FOX_geo::geosToGeometry($reader->read($wkt)); $geom->setSRID($srid); return $geom; } else { return FOX_geo::geosToGeometry($reader->read($wkt)); } } $wkt = str_replace(', ', ',', $wkt); // For each geometry type, check to see if we have a match at the // beggining of the string. If we do, then parse using that type foreach (FOX_geo::geometryList() as $geom_type) { $wkt_geom = strtoupper($geom_type); if (strtoupper(substr($wkt, 0, strlen($wkt_geom))) == $wkt_geom) { $data_string = $this->getDataString($wkt, $wkt_geom); $method = 'parse' . $geom_type; if ($srid) { $geom = $this->{$method}($data_string); $geom->setSRID($srid); return $geom; } else { return $this->{$method}($data_string); } } } }
public function simplify($tolerance, $preserveTopology = false) { if ($this->geos()) { return FOX_geo::geosToGeometry($this->geos()->simplify($tolerance, $preserveTopology)); } }