You are here

public function GeoJSON::read in geoPHP 8

Same name and namespace in other branches
  1. 7 geoPHP/lib/adapters/GeoJSON.class.php \GeoJSON::read()

Given an object or a string, return a Geometry

Parameters

mixed $input The GeoJSON string or object:

Return value

object Geometry

Overrides GeoAdapter::read

File

geoPHP/lib/adapters/GeoJSON.class.php, line 18

Class

GeoJSON
GeoJSON class : a geojson reader/writer.

Code

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);
}