/**
  * Converts an stdClass object into a Geometry based on its 'type' property
  * Converts an stdClass object into a Geometry, based on its 'type' property
  *
  * @param stdClass $obj Object resulting from json decoding a GeoJSON string
  *
  * @return object Object from class eometry
  */
 private static function toInstance($obj)
 {
     if (is_null($obj)) {
         return null;
     }
     if (!isset($obj->type)) {
         self::checkType($obj);
     }
     if ($obj->type == 'Feature') {
         $instance = self::toGeomInstance($obj->geometry);
     } else {
         if ($obj->type == 'FeatureCollection') {
             $geometries = array();
             foreach ($obj->features as $feature) {
                 $geometries[] = self::toGeomInstance($feature->geometry);
             }
             // Get a geometryCollection or MultiGeometry out of the the provided geometries
             $instance = geoPHP::geometryReduce($geometries);
         } else {
             // It's a geometry
             $instance = self::toGeomInstance($obj);
         }
     }
     return $instance;
 }
Example #2
2
 protected 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 Exception("Invalid / Empty GPX");
     }
     return geoPHP::geometryReduce($geometries);
 }
Example #3
0
 /**
  * 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);
 }
Example #4
0
/**
 * Convert KML -> WKT.
 *
 * @param string $wkt The coverage.
 * @return string|null The WKT.
 */
function nl_kml2wkt($kml)
{
    $wkt = null;
    // Is the input valid KML?
    if (geoPHP::detectFormat($kml) == 'kml') {
        $geo = geoPHP::load($kml);
        $wkt = geoPHP::geometryReduce($geo)->out('wkt');
    }
    return $wkt;
}
Example #5
0
 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);
 }
Example #6
0
 /**
  * Convert an object into a GeoPHP Geometry object.
  *
  * The following rules will dictate the conversion:
  *  - Multi-value geofields of different types will be reduced to a
  *    GEOMETRYCOLLECTION.
  *  - Multi-value geofields of the same 'simple type' (POINT, LINESTRING or
  *    POLYGON) will be reduced to the MULTI* equivalent.
  *  - GEOMETRYCOLLECTION's containing multiple values of only one 'simple type'
  *    will be reduced to the MULTI* equvalent.
  *  - GEOMETRYCOLLECTION's or MULTI* values containing only one geometry will be
  *    reduced to that geometery.
  *
  * @param Traversable $object
  *    An object that represents a geometry or a (traversable) collection of
  *    such objects.
  *
  * @param Callable $to_geom
  *    A callback that takes your object and returns a GeoPHP Geometry object.
  *
  * @return
  *    A GeoPHP Geometry object representing the $object value
  *    converted according to the above rules.
  */
 protected function createGeoPHPGeometry($object, $to_geom = NULL)
 {
     geophp_load();
     if (empty($object)) {
         return NULL;
     }
     if (!$to_geom) {
         $to_geom = function ($wkt) {
             return \geoPHP::load($wkt, 'wkt');
         };
     }
     // TODO: This reflection sucks.
     if (is_array($object) || $object instanceof \Traversable && $object instanceof \Countable && $object instanceof \ArrayAccess) {
         foreach ($object as $delta => $value) {
             $geometry = $this->createGeoPHPGeometry($value, $to_geom);
             if ($geometry) {
                 $geom[] = $geometry;
             }
         }
     } else {
         $geom = $to_geom($object);
     }
     return \geoPHP::geometryReduce($geom);
 }
Example #7
0
 /**
  *
  */
 protected function geometry()
 {
     if (!$this->processed || $reset) {
         $this->process();
     }
     geophp_load();
     return \geoPHP::geometryReduce(array_map(function ($feature) {
         // TODO: GeoPHP cannot parse an array-based representation of a GeoJSON
         //       object. We have to encode the array then parse the JSON string.
         $json = json_encode($feature->geometry);
         return \geoPHP::load($json, 'json');
     }, $this->features));
 }
Example #8
0
 static function geometryReduce($geometry)
 {
     // If it's an array of one, then just parse the one
     if (is_array($geometry)) {
         if (count($geometry) == 1) {
             return geoPHP::geometryReduce($geometry[0]);
         }
     }
     // 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 (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 (count($geom_types) == 1) {
         if (count($geometries) == 1) {
             return $geometries[0];
         } else {
             $class = 'Multi' . $geom_types[0];
             return new $class($geometries);
         }
     } else {
         return new GeometryCollection($geometries);
     }
 }
Example #9
0
File: KML.php Project: kitbs/geoPHP
 protected function geomFromXML()
 {
     $geometries = array();
     $geom_types = geoPHP::geometryList();
     $placemark_elements = $this->xmlobj->getElementsByTagName('placemark');
     if ($placemark_elements->length) {
         foreach ($placemark_elements as $placemark) {
             foreach ($placemark->childNodes as $child) {
                 // Node names are all the same, except for MultiGeometry, which maps to GeometryCollection
                 $node_name = $child->nodeName == 'multigeometry' ? 'geometrycollection' : $child->nodeName;
                 if (array_key_exists($node_name, $geom_types)) {
                     $function = 'parse' . $geom_types[$node_name];
                     $geometries[] = $this->{$function}($child);
                 }
             }
         }
     } else {
         // The document does not have a placemark, try to create a valid geometry from the root element
         $node_name = $this->xmlobj->documentElement->nodeName == 'multigeometry' ? 'geometrycollection' : $this->xmlobj->documentElement->nodeName;
         if (array_key_exists($node_name, $geom_types)) {
             $function = 'parse' . $geom_types[$node_name];
             $geometries[] = $this->{$function}($this->xmlobj->documentElement);
         }
     }
     return geoPHP::geometryReduce($geometries);
 }
 public function boundary()
 {
     if ($this->geos()) {
         return $this->geos()->boundary();
     }
     $components_boundaries = array();
     foreach ($this->components as $component) {
         $components_boundaries[] = $component->boundary();
     }
     return geoPHP::geometryReduce($components_boundaries);
 }