1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
4
|
|
|
|
5
|
|
|
namespace Maps\DataAccess\GeoJsonStore; |
6
|
|
|
|
7
|
|
|
use GeoJson\Feature\FeatureCollection; |
8
|
|
|
use GeoJson\GeoJson; |
9
|
|
|
use GeoJson\Geometry\Point; |
10
|
|
|
use Title; |
11
|
|
|
|
12
|
|
|
class SubObjectBuilder { |
13
|
|
|
|
14
|
|
|
private $subjectPage; |
15
|
|
|
private $pointCount = 0; |
16
|
|
|
|
17
|
6 |
|
public function __construct( Title $subjectPage ) { |
18
|
6 |
|
$this->subjectPage = $subjectPage; |
19
|
6 |
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @return SubObject[] |
23
|
|
|
*/ |
24
|
6 |
|
public function getSubObjectsFromGeoJson( string $jsonString ) { |
25
|
6 |
|
$json = json_decode( $jsonString ); |
26
|
6 |
|
$geoJson = GeoJson::jsonUnserialize( $json ); |
27
|
|
|
|
28
|
6 |
|
return iterator_to_array( $this->featureCollectionToSubObjects( $geoJson ) ); |
29
|
|
|
} |
30
|
|
|
|
31
|
6 |
|
private function featureCollectionToSubObjects( FeatureCollection $featureCollection ) { |
32
|
6 |
|
foreach ( $featureCollection->getFeatures() as $feature ) { |
33
|
2 |
|
$geometry = $feature->getGeometry(); |
34
|
|
|
|
35
|
2 |
|
if ( $geometry instanceof Point ) { |
36
|
2 |
|
yield $this->pointToSubobject( $geometry, $feature->getProperties() ?? [] ); |
37
|
|
|
} |
38
|
|
|
} |
39
|
6 |
|
} |
40
|
|
|
|
41
|
2 |
|
private function pointToSubobject( Point $point, array $properties ): SubObject { |
42
|
2 |
|
$subObject = new SubObject( 'Point_' . ++$this->pointCount ); |
43
|
|
|
|
44
|
2 |
|
$subObject->addPropertyValuePair( |
45
|
2 |
|
'HasCoordinates', |
46
|
2 |
|
new \SMWDIGeoCoord( $point->getCoordinates()[1], $point->getCoordinates()[0] ) |
47
|
|
|
); |
48
|
|
|
|
49
|
2 |
|
if ( array_key_exists( 'description', $properties ) ) { |
50
|
1 |
|
$subObject->addPropertyValuePair( |
51
|
1 |
|
'HasDescription', |
52
|
1 |
|
new \SMWDIBlob( $properties['description'] ) |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
if ( array_key_exists( 'title', $properties ) ) { |
57
|
1 |
|
$subObject->addPropertyValuePair( |
58
|
1 |
|
'HasTitle', |
59
|
1 |
|
new \SMWDIBlob( $properties['title'] ) |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
2 |
|
return $subObject; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
} |
68
|
|
|
|