/** * Create a geometry from GeoJSON * * @param string|array|object $geojson the GeoJSON object either in a JSON string or a pre-parsed array/object * * @throws \ErrorException * @return GeometryInterface|FeatureAbstract */ public static function fromGeoJson($geojson) { if (is_string($geojson)) { $geojson = json_decode($geojson, true); } if (is_object($geojson)) { $geojson = json_decode(json_encode($geojson), true); } $type = $geojson['type']; if ($type === 'GeometryCollection') { $geometries = []; foreach ($geojson['geometries'] as $geom) { $geometries[] = self::fromGeoJson($geom); } $geometry = self::createGeometry($type, $geometries); } elseif (strtolower($type) === 'feature') { $geometry = new Feature(); if (isset($geojson['geometry'])) { $geometry->setGeometry(self::fromGeoJson($geojson['geometry'])); } if (isset($geojson['properties'])) { $geometry->setProperties($geojson['properties']); } } elseif (strtolower($type) === 'featurecollection') { $geometry = new FeatureCollection(); foreach ($geojson['features'] as $feature) { /** @noinspection PhpParamsInspection */ $geometry->addFeature(self::fromGeoJson($feature)); } } else { $coordinates = $geojson['coordinates']; $geometry = self::createGeometry($type, $coordinates); } return $geometry; }
public function testBBox() { $line = new LineString([[2, 3], [4, 5]]); $feature = new Feature(); $feature->setGeometry($line); $feature->enableBBox(); $json = $feature->jsonSerialize(); $this->assertTrue(is_array($json['bbox'])); }