|
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, GeometryObjectCallback $geometryObjectCallback = null): GeometryCollection |
|
18
|
|
|
{ |
|
19
|
5 |
|
$geometryCollection = new GeometryCollection(); |
|
20
|
|
|
|
|
21
|
5 |
|
if(!is_null($geometryObjectCallback)) { |
|
22
|
|
|
$geometryCollection->setGeometryObjectCallback($geometryObjectCallback); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
5 |
|
if ('FeatureCollection' !== $geoJSONArray['type']) { |
|
26
|
1 |
|
throw new NotImplementedException('Only FeatureCollections are currently supported'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
4 |
|
foreach ($geoJSONArray['features'] ?? [] as $feature) { |
|
30
|
4 |
|
if ('Feature' !== $feature['type']) { |
|
31
|
1 |
|
throw new NotImplementedException('Only features of type "Feature" are supported.'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
3 |
|
$geometryObject = GeometryObjectFactory::createForGeoJsonFeatureGeometry($feature['geometry']); |
|
35
|
3 |
|
if (null === $geometryObject) { |
|
36
|
1 |
|
continue; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
2 |
|
if (array_key_exists('properties', $feature) && array_key_exists('featurecla', $feature['properties'])) { |
|
40
|
1 |
|
$geometryObject->setFeatureClass($feature['properties']['featurecla']); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
2 |
|
if(array_key_exists('properties', $feature) && !empty($feature['properties'])) { |
|
44
|
1 |
|
$geometryObject->setProperties($feature['properties']); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
2 |
|
$geometryCollection->addGeometryObject($geometryObject); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
3 |
|
return $geometryCollection; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @throws JsonException|NotImplementedException|InvalidPositionException |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public static function createFromGeoJsonString(string $geoJsonString, GeometryObjectCallback $geometryObjectCallback = null): GeometryCollection |
|
57
|
|
|
{ |
|
58
|
1 |
|
return self::createFromGeoJSONArray(json_decode($geoJsonString, true, 512, JSON_THROW_ON_ERROR), $geometryObjectCallback); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @throws JsonException|NotImplementedException|InvalidPositionException |
|
63
|
|
|
*/ |
|
64
|
1 |
|
public static function createFromGeoJSONFilePath(string $path, GeometryObjectCallback $geometryObjectCallback = null): GeometryCollection |
|
65
|
|
|
{ |
|
66
|
1 |
|
return self::createFromGeoJsonString(file_get_contents($path), $geometryObjectCallback); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|