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
|
|
|
throw new NotImplementedException('Creating multiPoints is not supported yet'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @throws InvalidPositionException |
63
|
|
|
*/ |
64
|
|
|
public static function createForMultiPolygonCoordinates(array $coordinates): MultiPolygon |
65
|
|
|
{ |
66
|
|
|
$multiPolygon = new MultiPolygon(); |
67
|
|
|
foreach ($coordinates as $polygonCoordinates) { |
68
|
|
|
$polygon = self::createForPolygonCoordinates($polygonCoordinates); |
69
|
|
|
if (null === $polygon) { |
70
|
|
|
continue; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$multiPolygon->addPolygon($polygon); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $multiPolygon; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @throws InvalidPositionException |
81
|
|
|
*/ |
82
|
1 |
|
public static function createForPointCoordinates(array $coordinates): Point |
83
|
|
|
{ |
84
|
1 |
|
return new Point(new Position($coordinates[0], $coordinates[1], $coordinates[2] ?? null)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @throws InvalidPositionException |
89
|
|
|
*/ |
90
|
|
|
public static function createForPolygonCoordinates(array $coordinates): ?Polygon |
91
|
|
|
{ |
92
|
|
|
$exteriorCoordinates = array_shift($coordinates); |
93
|
|
|
if (null === $exteriorCoordinates) { |
94
|
|
|
return null; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$polygon = new Polygon(self::createForLineStringCoordinates($exteriorCoordinates)); |
98
|
|
|
foreach ($coordinates as $lineStringData) { |
99
|
|
|
$polygon->addInteriorRing(self::createForLineStringCoordinates($lineStringData)); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $polygon; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.