1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PrinsFrank\PhpGeoSVG\Geometry\GeometryObject; |
6
|
|
|
|
7
|
|
|
use PrinsFrank\PhpGeoSVG\Exception\InvalidPositionException; |
8
|
|
|
use PrinsFrank\PhpGeoSVG\Exception\NotImplementedException; |
9
|
|
|
use PrinsFrank\PhpGeoSVG\Geometry\Position\Position; |
10
|
|
|
|
11
|
|
|
class GeometryObjectFactory |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @throws NotImplementedException |
15
|
|
|
* @throws InvalidPositionException |
16
|
|
|
*/ |
17
|
2 |
|
public static function createForGeoJsonFeatureGeometry(array $featureGeometry): ?GeometryObject |
18
|
|
|
{ |
19
|
2 |
|
return match ($featureGeometry['type']) { |
20
|
1 |
|
'LineString' => self::createForLineStringCoordinates($featureGeometry['coordinates']), |
21
|
1 |
|
'MultiLineString' => self::createForMultiLineStringCoordinates($featureGeometry['coordinates']), |
22
|
|
|
'MultiPoint' => self::createForMultiPointCoordinates($featureGeometry['coordinates']), |
23
|
1 |
|
'MultiPolygon' => self::createForMultiPolygonCoordinates($featureGeometry['coordinates']), |
24
|
1 |
|
'Point' => self::createForPointCoordinates($featureGeometry['coordinates']), |
25
|
1 |
|
'Polygon' => self::createForPolygonCoordinates($featureGeometry['coordinates']), |
26
|
2 |
|
default => throw new NotImplementedException('Feature geometries of type "' . $featureGeometry['type'] . '" are currently not supported') |
27
|
|
|
}; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @throws InvalidPositionException |
32
|
|
|
*/ |
33
|
2 |
|
public static function createForLineStringCoordinates(array $coordinates): LineString |
34
|
|
|
{ |
35
|
2 |
|
$lineString = new LineString(); |
36
|
2 |
|
foreach ($coordinates as $coordinate) { |
37
|
1 |
|
$lineString->addPosition(new Position($coordinate[0], $coordinate[1], $coordinate[2] ?? null)); |
38
|
|
|
} |
39
|
|
|
|
40
|
2 |
|
return $lineString; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @throws InvalidPositionException |
45
|
|
|
*/ |
46
|
2 |
|
public static function createForMultiLineStringCoordinates(array $coordinates): MultiLineString |
47
|
|
|
{ |
48
|
2 |
|
$multiLineString = new MultiLineString(); |
49
|
2 |
|
foreach ($coordinates as $lineStringCoordinates) { |
50
|
1 |
|
$multiLineString->addLineString(self::createForLineStringCoordinates($lineStringCoordinates)); |
51
|
|
|
} |
52
|
|
|
|
53
|
2 |
|
return $multiLineString; |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
public static function createForMultiPointCoordinates(array $coordinates): MultiPoint |
57
|
|
|
{ |
58
|
1 |
|
$multiPoint = new MultiPoint(); |
59
|
1 |
|
foreach ($coordinates as $coordinate) { |
60
|
1 |
|
$multiPoint->addPoint(self::createForPointCoordinates($coordinate)); |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
return $multiPoint; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @throws InvalidPositionException |
68
|
|
|
*/ |
69
|
2 |
|
public static function createForMultiPolygonCoordinates(array $coordinates): MultiPolygon |
70
|
|
|
{ |
71
|
2 |
|
$multiPolygon = new MultiPolygon(); |
72
|
2 |
|
foreach ($coordinates as $polygonCoordinates) { |
73
|
1 |
|
$polygon = self::createForPolygonCoordinates($polygonCoordinates); |
74
|
1 |
|
if (null === $polygon) { |
75
|
1 |
|
continue; |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
$multiPolygon->addPolygon($polygon); |
79
|
|
|
} |
80
|
|
|
|
81
|
2 |
|
return $multiPolygon; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @throws InvalidPositionException |
86
|
|
|
*/ |
87
|
1 |
|
public static function createForPointCoordinates(array $coordinates): Point |
88
|
|
|
{ |
89
|
1 |
|
return new Point(new Position($coordinates[0], $coordinates[1], $coordinates[2] ?? null)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @throws InvalidPositionException |
94
|
|
|
*/ |
95
|
3 |
|
public static function createForPolygonCoordinates(array $coordinates): ?Polygon |
96
|
|
|
{ |
97
|
3 |
|
$exteriorCoordinates = array_shift($coordinates); |
98
|
3 |
|
if (null === $exteriorCoordinates) { |
99
|
1 |
|
return null; |
100
|
|
|
} |
101
|
|
|
|
102
|
2 |
|
$polygon = new Polygon(self::createForLineStringCoordinates($exteriorCoordinates)); |
103
|
2 |
|
foreach ($coordinates as $lineStringData) { |
104
|
1 |
|
$polygon->addInteriorRing(self::createForLineStringCoordinates($lineStringData)); |
105
|
|
|
} |
106
|
|
|
|
107
|
2 |
|
return $polygon; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|