1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PrinsFrank\PhpGeoSVG; |
6
|
|
|
|
7
|
|
|
use PrinsFrank\PhpGeoSVG\Coordinator\Coordinator; |
8
|
|
|
use PrinsFrank\PhpGeoSVG\Exception\PhpGeoSVGException; |
9
|
|
|
use PrinsFrank\PhpGeoSVG\Geometry\BoundingBox\BoundingBox; |
10
|
|
|
use PrinsFrank\PhpGeoSVG\Geometry\BoundingBox\BoundingBoxPosition; |
11
|
|
|
use PrinsFrank\PhpGeoSVG\Geometry\GeometryCollection; |
12
|
|
|
use PrinsFrank\PhpGeoSVG\HTML\Factory\ElementFactory; |
13
|
|
|
use PrinsFrank\PhpGeoSVG\HTML\Rendering\ElementRenderer; |
14
|
|
|
use PrinsFrank\PhpGeoSVG\Projection\EquiRectangularProjection; |
15
|
|
|
use PrinsFrank\PhpGeoSVG\Projection\Projection; |
16
|
|
|
use PrinsFrank\PhpGeoSVG\Scale\Scale; |
17
|
|
|
|
18
|
|
|
class GeoSVG |
19
|
|
|
{ |
20
|
|
|
public const VENDOR_NAME = 'prinsfrank'; |
21
|
|
|
public const PACKAGE_NAME = 'php-geo-svg'; |
22
|
|
|
|
23
|
|
|
public const PACKAGE_PATH = self::VENDOR_NAME . '/' . self::PACKAGE_NAME; |
24
|
|
|
|
25
|
|
|
private ?Scale $scale = null; |
26
|
|
|
|
27
|
4 |
|
public function __construct(private ?Projection $projection = null, private ?BoundingBox $boundingBox = null) |
28
|
|
|
{ |
29
|
4 |
|
} |
30
|
|
|
|
31
|
1 |
|
public function setProjection(?Projection $projection): self |
32
|
|
|
{ |
33
|
1 |
|
$this->projection = $projection; |
34
|
|
|
|
35
|
1 |
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
3 |
|
public function getProjection(): Projection |
39
|
|
|
{ |
40
|
3 |
|
if (null === $this->projection) { |
41
|
1 |
|
$this->projection = new EquiRectangularProjection(); |
42
|
|
|
} |
43
|
|
|
|
44
|
3 |
|
return $this->projection; |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
public function setBoundingBox(?BoundingBox $boundingBox): self |
48
|
|
|
{ |
49
|
1 |
|
$this->boundingBox = $boundingBox; |
50
|
|
|
|
51
|
1 |
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @throws null suppress hinting on the thrown exception as the default positions and bounding box are correct so catching these is not useful |
56
|
|
|
*/ |
57
|
3 |
|
public function getBoundingBox(): BoundingBox |
58
|
|
|
{ |
59
|
3 |
|
if (null === $this->boundingBox) { |
60
|
1 |
|
$this->boundingBox = new BoundingBox(new BoundingBoxPosition(-180, -90), new BoundingBoxPosition(180, 90)); |
61
|
|
|
} |
62
|
|
|
|
63
|
3 |
|
return $this->boundingBox; |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
public function setScale(?Scale $scale): self |
67
|
|
|
{ |
68
|
1 |
|
$this->scale = $scale; |
69
|
|
|
|
70
|
1 |
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
public function getScale(): Scale |
74
|
|
|
{ |
75
|
1 |
|
if (null === $this->scale) { |
76
|
1 |
|
$this->scale = new Scale(1); |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
return $this->scale; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @throws PhpGeoSVGException |
84
|
|
|
*/ |
85
|
1 |
|
public function render(GeometryCollection $geometryCollection): string |
86
|
|
|
{ |
87
|
1 |
|
return ElementRenderer::renderElement( |
88
|
1 |
|
ElementFactory::buildForGeometryCollection($geometryCollection, new Coordinator($this->getProjection(), $this->getBoundingBox(), $this->getScale())) |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @throws PhpGeoSVGException |
94
|
|
|
*/ |
95
|
1 |
|
public function toFile(GeometryCollection $geometryCollection, string $path): void |
96
|
|
|
{ |
97
|
1 |
|
file_put_contents($path, $this->render($geometryCollection)); |
98
|
1 |
|
} |
99
|
|
|
} |
100
|
|
|
|