|
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
|
|
|
public static function createForLineStringCoordinates(array $coordinates): LineString |
|
34
|
|
|
{ |
|
35
|
|
|
$lineString = new LineString(); |
|
36
|
|
|
foreach ($coordinates as $coordinate) { |
|
37
|
|
|
$lineString->addPosition(new Position($coordinate[0], $coordinate[1], $coordinate[2] ?? null)); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return $lineString; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @throws InvalidPositionException |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function createForMultiLineStringCoordinates(array $coordinates): MultiLineString |
|
47
|
|
|
{ |
|
48
|
|
|
$multiLineString = new MultiLineString(); |
|
49
|
|
|
foreach ($coordinates as $lineStringCoordinates) { |
|
50
|
|
|
$multiLineString->addLineString(self::createForLineStringCoordinates($lineStringCoordinates)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $multiLineString; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public static function createForMultiPointCoordinates(array $coordinates): MultiPoint |
|
57
|
|
|
{ |
|
58
|
|
|
$multiPoint = new MultiPoint(); |
|
59
|
|
|
foreach ($coordinates as $coordinate) { |
|
60
|
|
|
$multiPoint->addPoint(self::createForPointCoordinates($coordinate)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $multiPoint; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @throws InvalidPositionException |
|
68
|
|
|
*/ |
|
69
|
|
|
public static function createForMultiPolygonCoordinates(array $coordinates): MultiPolygon |
|
70
|
|
|
{ |
|
71
|
|
|
$multiPolygon = new MultiPolygon(); |
|
72
|
|
|
foreach ($coordinates as $polygonCoordinates) { |
|
73
|
|
|
$polygon = self::createForPolygonCoordinates($polygonCoordinates); |
|
74
|
|
|
if (null === $polygon) { |
|
75
|
|
|
continue; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$multiPolygon->addPolygon($polygon); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
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
|
|
|
public static function createForPolygonCoordinates(array $coordinates): ?Polygon |
|
96
|
|
|
{ |
|
97
|
|
|
$exteriorCoordinates = array_shift($coordinates); |
|
98
|
|
|
if (null === $exteriorCoordinates) { |
|
99
|
|
|
return null; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$polygon = new Polygon(self::createForLineStringCoordinates($exteriorCoordinates)); |
|
103
|
|
|
foreach ($coordinates as $lineStringData) { |
|
104
|
|
|
$polygon->addInteriorRing(self::createForLineStringCoordinates($lineStringData)); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $polygon; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|