|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace PrinsFrank\PhpGeoSVG\Geometry; |
|
6
|
|
|
|
|
7
|
|
|
use JsonException; |
|
8
|
|
|
use PrinsFrank\PhpGeoSVG\Exception\InvalidPositionException; |
|
9
|
|
|
use PrinsFrank\PhpGeoSVG\Exception\NotImplementedException; |
|
10
|
|
|
use PrinsFrank\PhpGeoSVG\Geometry\GeometryObject\GeometryObjectFactory; |
|
11
|
|
|
|
|
12
|
|
|
class GeometryCollectionFactory |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @throws NotImplementedException|InvalidPositionException |
|
16
|
|
|
*/ |
|
17
|
5 |
|
public static function createFromGeoJSONArray(array $geoJSONArray): GeometryCollection |
|
18
|
|
|
{ |
|
19
|
5 |
|
$geometryCollection = new GeometryCollection(); |
|
20
|
5 |
|
if ('FeatureCollection' !== $geoJSONArray['type']) { |
|
21
|
1 |
|
throw new NotImplementedException('Only FeatureCollections are currently supported'); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
4 |
|
foreach ($geoJSONArray['features'] ?? [] as $feature) { |
|
25
|
4 |
|
if ('Feature' !== $feature['type']) { |
|
26
|
1 |
|
throw new NotImplementedException('Only features of type "Feature" are supported.'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
3 |
|
$geometryObject = GeometryObjectFactory::createForGeoJsonFeatureGeometry($feature['geometry']); |
|
30
|
3 |
|
if (null === $geometryObject) { |
|
31
|
1 |
|
continue; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
2 |
|
if (array_key_exists('properties', $feature) && array_key_exists('featurecla', $feature['properties'])) { |
|
35
|
1 |
|
$geometryObject->setFeatureClass($feature['properties']['featurecla']); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
2 |
|
$geometryCollection->addGeometryObject($geometryObject); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
3 |
|
return $geometryCollection; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @throws JsonException|NotImplementedException|InvalidPositionException |
|
46
|
|
|
*/ |
|
47
|
1 |
|
public static function createFromGeoJsonString(string $geoJsonString): GeometryCollection |
|
48
|
|
|
{ |
|
49
|
1 |
|
return self::createFromGeoJSONArray(json_decode($geoJsonString, true, 512, JSON_THROW_ON_ERROR)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @throws JsonException|NotImplementedException|InvalidPositionException |
|
54
|
|
|
*/ |
|
55
|
1 |
|
public static function createFromGeoJSONFilePath(string $path): GeometryCollection |
|
56
|
|
|
{ |
|
57
|
1 |
|
return self::createFromGeoJsonString(file_get_contents($path)); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|