/** * 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 Exception('Invalid JSON'); } if (!is_string($input->type)) { throw new Exception('Invalid JSON'); } // Check to see if it's a FeatureCollection if ($input->type == 'FeatureCollection') { $geoms = array(); foreach ($input->features as $feature) { $geoms[] = $this->read($feature); } return GeoPHP::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); }
protected 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 Exception("Invalid / Empty GeoRSS"); } return GeoPHP::geometryReduce($geometries); }
public function reduce() { return GeoPHP::geometryReduce($this); }
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 GeoPHP::geometryReduce($components_boundaries); }
static function geometryReduce($geometry) { // If it's an array of one, then just parse the one if (is_array($geometry)) { if (empty($geometry)) { return FALSE; } if (count($geometry) == 1) { return GeoPHP::geometryReduce(array_shift($geometry)); } } // If the geometry cannot even theoretically be reduced more, then pass it back if (gettype($geometry) == 'object') { $passbacks = array('Point', 'LineString', 'Polygon'); if (in_array($geometry->geometryType(), $passbacks)) { return $geometry; } } // If it is a mutlti-geometry, check to see if it just has one member // If it does, then pass the member, if not, then just pass back the geometry if (gettype($geometry) == 'object') { $simple_collections = array('MultiPoint', 'MultiLineString', 'MultiPolygon'); if (in_array(get_class($geometry), $passbacks)) { $components = $geometry->getComponents(); if (count($components) == 1) { return $components[0]; } else { return $geometry; } } } // So now we either have an array of geometries, a GeometryCollection, or an array of GeometryCollections if (!is_array($geometry)) { $geometry = array($geometry); } $geometries = array(); $geom_types = array(); $collections = array('MultiPoint', 'MultiLineString', 'MultiPolygon', 'GeometryCollection'); foreach ($geometry as $item) { if ($item) { if (in_array(get_class($item), $collections)) { foreach ($item->getComponents() as $component) { $geometries[] = $component; $geom_types[] = $component->geometryType(); } } else { $geometries[] = $item; $geom_types[] = $item->geometryType(); } } } $geom_types = array_unique($geom_types); if (empty($geom_types)) { return FALSE; } if (count($geom_types) == 1) { if (count($geometries) == 1) { return $geometries[0]; } else { $class = 'GeoPHP\\Geometry\\Multi' . $geom_types[0]; return new $class($geometries); } } else { return new GeometryCollection($geometries); } }