Repository navigation
Expand file tree
/
Copy pathGeoJsonReproject.php
More file actions
184 lines (151 loc) · 5.73 KB
/
Copy pathGeoJsonReproject.php
File metadata and controls
184 lines (151 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
declare(strict_types=1);
namespace OpenMapsight;
use proj4php\Point;
use proj4php\Proj;
use proj4php\Proj4php;
class GeoJsonReproject
{
private static ?Proj4php $proj4php = null;
private static function getProj4Php(): ?Proj4php
{
if (is_null(self::$proj4php)) {
self::$proj4php = new Proj4php();
}
return self::$proj4php;
}
public static function getProjection(array $data): ?string
{
if (!isset($data['crs'])) {
return null;
}
if ($data['crs']['type'] === 'name' && isset($data['crs']['properties']['name'])) {
return $data['crs']['properties']['name'];
}
if (isset($data['crs']['type']) && isset($data['crs']['properties']['code'])) {
return $data['crs']['type'] . ':' . $data['crs']['properties']['code'];
}
return null;
}
/**
* @param array $data
* @param string $projection
*
* @return array
*/
public static function setProjection(array $data, string $projection): array
{
$data['crs'] = [
'type' => 'name',
'properties' => [
'name' => $projection,
],
];
return $data;
}
/**
* @param mixed $data
* @param string $destinationProjection
* @param array $options
*
* @return mixed reprojected data
*/
public static function reproject(mixed $data, string $destinationProjection = 'EPSG:4326', array $options = []): mixed
{
$options['destProj'] = $destinationProjection;
$srcProj = $options['srcProj'] ?? 'EPSG:4326';
$data = self::reprojectData($data, $srcProj, $options);
$data = self::setProjection($data, $destinationProjection);
return $data;
}
/**
* @param mixed $data
* @param string $sourceProjection
* @param array $options
*
* @return mixed reprojected data
*/
private static function reprojectData(mixed $data, string $sourceProjection, array $options): mixed
{
if ($data === null) {
return null;
}
$fileProjection = self::getProjection($data);
if ($fileProjection !== null) {
$sourceProjection = $fileProjection;
}
if (isset($data['crs'])) {
unset($data['crs']);
}
$type = $data['type'] ?? null;
switch ($type) {
case 'FeatureCollection':
$data['features'] = array_map(fn($feature): mixed => self::reprojectData($feature, $sourceProjection, $options), $data['features']);
break;
case 'Feature':
$data['geometry'] = self::reprojectData($data['geometry'], $sourceProjection, $options);
break;
case 'GeometryCollection':
$data['geometries'] = array_map(fn($geometry): mixed => self::reprojectData($geometry, $sourceProjection, $options), $data['geometries']);
break;
case 'MultiPolygon':
$data['coordinates'] = array_map(fn($polygonArray): array => array_map(fn($linearString): mixed => self::reprojectData($linearString, $sourceProjection, $options), $polygonArray), $data['coordinates']);
break;
case 'Polygon':
case 'MultiLineString':
$data['coordinates'] = array_map(fn($linearString): mixed => self::reprojectData($linearString, $sourceProjection, $options), $data['coordinates']);
break;
case 'Point':
$data['coordinates'] = self::reprojectCoordinates($sourceProjection, $data['coordinates'], $options);
break;
case 'LineString':
case 'MultiPoint':
$data['coordinates'] = array_map(fn(array $coordinate): array => self::reprojectCoordinates($sourceProjection, $coordinate, $options), $data['coordinates']);
break;
default:
$isPoint = !is_array($data[0]);
if ($isPoint) {
$data = self::reprojectCoordinates($sourceProjection, $data, $options);
} else {
$data = array_map(fn(array $coordinate): array => self::reprojectCoordinates($sourceProjection, $coordinate, $options), $data);
}
}
return $data;
}
/**
* @param number[] $coordinates
* @param string $sourceProjection
* @param string $destinationProjection
*
* @return Point
*/
private static function transformCoordinates(array $coordinates, string $sourceProjection, string $destinationProjection): Point
{
$proj4php = self::getProj4Php();
return $proj4php->transform(
new Proj($sourceProjection, $proj4php),
new Proj($destinationProjection, $proj4php),
new Point($coordinates[0], $coordinates[1], $coordinates[2])
);
}
/**
* @param string $sourceProjection
* @param number[] $coordinates
* @param array $options
*
* @return array reprojected coordinates
*/
private static function reprojectCoordinates(string $sourceProjection, array $coordinates, array $options): array
{
$coordinates[2] ??= null;
$point = self::transformCoordinates($coordinates, $sourceProjection, $options['destProj']);
$newCoordinates = [$point->x, $point->y];
if (isset($options['includeZCoordinate']) && $options['includeZCoordinate'] === true) {
$newCoordinates[2] = $point->z;
}
if (isset($options['useIntegerCoordinates']) && $options['useIntegerCoordinates'] === true) {
return array_map(intval(...), $newCoordinates);
}
return $newCoordinates;
}
}