/** * Given an object or a string, return a Geometry * * @param mixed $input The GeoJSON string or object * * @return object Geometry */ public function read($input) { if (is_string($input)) { $input = json_decode($input); } if (!is_object($input)) { throw new FOX_exception(array('numeric' => 1, 'text' => "Invalid input parameter. Must be an object.", 'data' => $input, 'file' => __FILE__, 'class' => __CLASS__, 'function' => __FUNCTION__, 'line' => __LINE__, 'child' => null)); } if (!is_string($input->type)) { throw new FOX_exception(array('numeric' => 2, 'text' => "Invalid input parameter 'type' specifier. Must be a string.", 'data' => $input, 'file' => __FILE__, 'class' => __CLASS__, 'function' => __FUNCTION__, 'line' => __LINE__, 'child' => null)); } // Check to see if it's a FeatureCollection if ($input->type == 'FeatureCollection') { $geoms = array(); foreach ($input->features as $feature) { $geoms[] = $this->read($feature); } return FOX_geo::geometryReduce($geoms); } // Check to see if it's a Feature if ($input->type == 'Feature') { return $this->read($input->geometry); } // It's a geometry - process it return $this->objToGeom($input); }
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 geomFromXML() { $geometries = array(); $geometries = array_merge($geometries, $this->parseWaypoints()); $geometries = array_merge($geometries, $this->parseTracks()); $geometries = array_merge($geometries, $this->parseRoutes()); if (empty($geometries)) { throw new FOX_exception(array('numeric' => 1, 'text' => "Invalid GPX data string", 'data' => $geometries, 'file' => __FILE__, 'class' => __CLASS__, 'function' => __FUNCTION__, 'line' => __LINE__, 'child' => null)); } return FOX_geo::geometryReduce($geometries); }
public function geomFromXML() { $geometries = array(); $geometries = array_merge($geometries, $this->parsePoints()); $geometries = array_merge($geometries, $this->parseLines()); $geometries = array_merge($geometries, $this->parsePolygons()); $geometries = array_merge($geometries, $this->parseBoxes()); $geometries = array_merge($geometries, $this->parseCircles()); if (empty($geometries)) { throw new FOX_exception(array('numeric' => 1, 'text' => "Invalid or empty GeoRSS data", 'data' => $geometries, 'file' => __FILE__, 'class' => __CLASS__, 'function' => __FUNCTION__, 'line' => __LINE__, 'child' => null)); } return FOX_geo::geometryReduce($geometries); }
public function simplify($tolerance, $preserveTopology = false) { if ($this->geos()) { return FOX_geo::geosToGeometry($this->geos()->simplify($tolerance, $preserveTopology)); } }
public function parseGeometryCollection($xml) { $components = array(); $geom_types = FOX_geo::geometryList(); foreach ($xml->childNodes as $child) { $nodeName = $child->nodeName == 'linearring' ? 'linestring' : $child->nodeName; if (array_key_exists($nodeName, $geom_types)) { $function = 'parse' . $geom_types[$nodeName]; $components[] = $this->{$function}($child); } } return new FOX_geometryCollection($components); }
/** * Serialize geometries into a WKT string. * * @param Geometry $geometry * * @return string The WKT string representation of the input geometries */ public function write(FOX_geometry $geometry) { // If geos is installed, then we take a shortcut and let it write the WKT if (FOX_geo::geosInstalled()) { $writer = new GEOSWKTWriter(); $writer->setTrim(true); return $writer->write($geometry->geos()); } if ($geometry->isEmpty()) { return strtoupper($geometry->geometryType()) . ' EMPTY'; } else { $data = $this->extractData($geometry); if ($data) { return strtoupper($geometry->geometryType()) . ' (' . $data . ')'; } else { return null; } } }
public function boundary() { if ($this->isEmpty()) { return new LineString(); } if ($this->geos()) { return $this->geos()->boundary(); } $components_boundaries = array(); foreach ($this->components as $component) { $components_boundaries[] = $component->boundary(); } return FOX_geo::geometryReduce($components_boundaries); }