Skip to content

Commit 5d10439

Browse files
MoonEMauricioFauth
authored andcommitted
Refactor OpenLayer visualization code generation
Signed-off-by: Maximilian Krög <maxi_kroeg@web.de>
1 parent 680e952 commit 5d10439

20 files changed

Lines changed: 191 additions & 509 deletions

js/src/ol.mjs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { Attribution, MousePosition, Zoom } from 'ol/control.js';
22
import { createStringXY } from 'ol/coordinate.js';
33
import { isEmpty } from 'ol/extent.js';
4-
import { LineString, LinearRing, MultiLineString, MultiPoint, MultiPolygon, Point, Polygon } from 'ol/geom.js';
4+
import { LineString, MultiLineString, MultiPoint, MultiPolygon, Point, Polygon } from 'ol/geom.js';
55
import { Tile, Vector as VectorLayer } from 'ol/layer.js';
6-
import { get } from 'ol/proj.js';
76
import { OSM, Vector as VectorSource } from 'ol/source.js';
87
import { Circle, Fill, Stroke, Style, Text } from 'ol/style.js';
98
import { Feature, Map, View } from 'ol';
@@ -19,14 +18,11 @@ const ol = {
1918
isEmpty
2019
},
2120
geom: {
22-
LineString, LinearRing, MultiLineString, MultiPoint, MultiPolygon, Point, Polygon
21+
LineString, MultiLineString, MultiPoint, MultiPolygon, Point, Polygon
2322
},
2423
layer: {
2524
Tile, Vector: VectorLayer
2625
},
27-
proj: {
28-
get
29-
},
3026
source: {
3127
OSM, Vector: VectorSource
3228
},

libraries/classes/Gis/GisGeometry.php

Lines changed: 32 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use function array_map;
1414
use function defined;
1515
use function explode;
16+
use function json_encode;
1617
use function mb_strripos;
1718
use function mb_substr;
1819
use function mt_getrandmax;
@@ -252,7 +253,7 @@ private function extractPointsInternal(string $point_set, array|null $scale_data
252253
*
253254
* @return float[][] scaled points
254255
*/
255-
protected function extractPoints(string $wktCoords, array|null $scale_data): array
256+
protected function extractPoints1d(string $wktCoords, array|null $scale_data): array
256257
{
257258
/** @var float[][] $points_arr */
258259
$points_arr = $this->extractPointsInternal($wktCoords, $scale_data, false);
@@ -268,7 +269,7 @@ protected function extractPoints(string $wktCoords, array|null $scale_data): arr
268269
*
269270
* @return float[] scaled points
270271
*/
271-
protected function extractPointsLinear(string $wktCoords, array|null $scale_data): array
272+
protected function extractPoints1dLinear(string $wktCoords, array|null $scale_data): array
272273
{
273274
/** @var float[] $points_arr */
274275
$points_arr = $this->extractPointsInternal($wktCoords, $scale_data, true);
@@ -277,121 +278,56 @@ protected function extractPointsLinear(string $wktCoords, array|null $scale_data
277278
}
278279

279280
/**
280-
* Generates JavaScript for adding an array of polygons to OpenLayers.
281-
*
282-
* @param array $polygons x and y coordinates for each polygon
283-
* @param int $srid spatial reference id
281+
* @param string $wktCoords string of ),( separated points
282+
* @param array|null $scale_data data related to scaling
284283
*
285-
* @return string JavaScript for adding an array of polygons to OpenLayers
284+
* @return float[][][] scaled points
286285
*/
287-
protected function getPolygonArrayForOpenLayers(array $polygons, int $srid): string
286+
protected function extractPoints2d(string $wktCoords, array|null $scale_data): array
288287
{
289-
$ol_array = 'var polygonArray = [];';
290-
foreach ($polygons as $polygon) {
291-
$rings = explode('),(', $polygon);
292-
$ol_array .= $this->getPolygonForOpenLayers($rings, $srid);
293-
$ol_array .= 'polygonArray.push(polygon);';
294-
}
288+
$parts = explode('),(', $wktCoords);
295289

296-
return $ol_array;
290+
return array_map(function ($coord) use ($scale_data) {
291+
return $this->extractPoints1d($coord, $scale_data);
292+
}, $parts);
297293
}
298294

299295
/**
300-
* Generates JavaScript for adding points for OpenLayers polygon.
301-
*
302-
* @param array $polygon x and y coordinates for each line
303-
* @param int $srid spatial reference id
296+
* @param string $wktCoords string of )),(( separated points
297+
* @param array|null $scale_data data related to scaling
304298
*
305-
* @return string JavaScript for adding points for OpenLayers polygon
299+
* @return float[][][][] scaled points
306300
*/
307-
protected function getPolygonForOpenLayers(array $polygon, int $srid): string
301+
protected function extractPoints3d(string $wktCoords, array|null $scale_data): array
308302
{
309-
return $this->getLineArrayForOpenLayers($polygon, $srid, false)
310-
. 'var polygon = new ol.geom.Polygon(arr);';
311-
}
312-
313-
/**
314-
* Generates JavaScript for adding an array of LineString
315-
* or LineRing to OpenLayers.
316-
*
317-
* @param array $lines x and y coordinates for each line
318-
* @param int $srid spatial reference id
319-
* @param bool $is_line_string whether it's an array of LineString
320-
*
321-
* @return string JavaScript for adding an array of LineString
322-
* or LineRing to OpenLayers
323-
*/
324-
protected function getLineArrayForOpenLayers(
325-
array $lines,
326-
int $srid,
327-
$is_line_string = true,
328-
): string {
329-
$ol_array = 'var arr = [];';
330-
foreach ($lines as $line) {
331-
$ol_array .= 'var lineArr = [];';
332-
$points_arr = $this->extractPoints($line, null);
333-
$ol_array .= 'var line = ' . $this->getLineForOpenLayers($points_arr, $srid, $is_line_string) . ';';
334-
$ol_array .= 'var coord = line.getCoordinates();';
335-
$ol_array .= 'for (var i = 0; i < coord.length; i++) lineArr.push(coord[i]);';
336-
$ol_array .= 'arr.push(lineArr);';
337-
}
303+
$parts = explode(')),((', $wktCoords);
338304

339-
return $ol_array;
305+
return array_map(function ($coord) use ($scale_data) {
306+
return $this->extractPoints2d($coord, $scale_data);
307+
}, $parts);
340308
}
341309

342310
/**
343-
* Generates JavaScript for adding a LineString or LineRing to OpenLayers.
344-
*
345-
* @param array $points_arr x and y coordinates for each point
346-
* @param int $srid spatial reference id
347-
* @param bool $is_line_string whether it's a LineString
348-
*
349-
* @return string JavaScript for adding a LineString or LineRing to OpenLayers
311+
* @param string $constructor
312+
* OpenLayers geometry constructor string
313+
* @param float[]|float[][]|float[][][]|float[][][][] $coordinates
314+
* Array of coordintes 1-4 dimensions
350315
*/
351-
protected function getLineForOpenLayers(
352-
array $points_arr,
353-
int $srid,
354-
$is_line_string = true,
355-
): string {
356-
return 'new ol.geom.'
357-
. ($is_line_string ? 'LineString' : 'LinearRing') . '('
358-
. $this->getPointsArrayForOpenLayers($points_arr, $srid)
359-
. ')';
360-
}
361-
362-
/**
363-
* Generates JavaScript for adding an array of points to OpenLayers.
364-
*
365-
* @param array $points_arr x and y coordinates for each point
366-
* @param int $srid spatial reference id
367-
*
368-
* @return string JavaScript for adding an array of points to OpenLayers
369-
*/
370-
protected function getPointsArrayForOpenLayers(array $points_arr, int $srid): string
316+
protected function toOpenLayersObject(string $constructor, array $coordinates, int $srid): string
371317
{
372-
$ol_array = 'new Array(';
373-
foreach ($points_arr as $point) {
374-
$ol_array .= $this->getPointForOpenLayers($point, $srid) . '.getCoordinates(), ';
318+
$ol = 'new ' . $constructor . '(' . json_encode($coordinates) . ')';
319+
if ($srid != 3857) {
320+
$ol .= '.transform(\'EPSG:' . ($srid ?: 4326) . '\', \'EPSG:3857\')';
375321
}
376322

377-
$ol_array = mb_substr($ol_array, 0, -2);
378-
379-
return $ol_array . ')';
323+
return $ol;
380324
}
381325

382-
/**
383-
* Generates JavaScript for adding a point to OpenLayers.
384-
*
385-
* @param array $point array containing the x and y coordinates of the point
386-
* @param int $srid spatial reference id
387-
*
388-
* @return string JavaScript for adding points to OpenLayers
389-
*/
390-
protected function getPointForOpenLayers(array $point, int $srid): string
326+
protected function addGeometryToLayer(string $olGeometry, string $style): string
391327
{
392-
return '(new ol.geom.Point([' . $point[0] . ',' . $point[1] . '])'
393-
. '.transform(ol.proj.get("EPSG:' . $srid . '")'
394-
. ', ol.proj.get(\'EPSG:3857\')))';
328+
return 'var feature = new ol.Feature(' . $olGeometry . ');'
329+
. 'feature.setStyle(' . $style . ');'
330+
. 'vectorSource.addFeature(feature);';
395331
}
396332

397333
protected function getRandomId(): int

libraries/classes/Gis/GisLineString.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function prepareRowAsPng(
8181

8282
// Trim to remove leading 'LINESTRING(' and trailing ')'
8383
$lineString = mb_substr($spatial, 11, -1);
84-
$points_arr = $this->extractPoints($lineString, $scale_data);
84+
$points_arr = $this->extractPoints1d($lineString, $scale_data);
8585

8686
foreach ($points_arr as $point) {
8787
if (isset($temp_point)) {
@@ -132,7 +132,7 @@ public function prepareRowAsPdf($spatial, string $label, array $color, array $sc
132132

133133
// Trim to remove leading 'LINESTRING(' and trailing ')'
134134
$linesrting = mb_substr($spatial, 11, -1);
135-
$points_arr = $this->extractPoints($linesrting, $scale_data);
135+
$points_arr = $this->extractPoints1d($linesrting, $scale_data);
136136

137137
foreach ($points_arr as $point) {
138138
if (isset($temp_point)) {
@@ -176,7 +176,7 @@ public function prepareRowAsSvg($spatial, string $label, array $color, array $sc
176176

177177
// Trim to remove leading 'LINESTRING(' and trailing ')'
178178
$linesrting = mb_substr($spatial, 11, -1);
179-
$points_arr = $this->extractPoints($linesrting, $scale_data);
179+
$points_arr = $this->extractPoints1d($linesrting, $scale_data);
180180

181181
$row = '<polyline points="';
182182
foreach ($points_arr as $point) {
@@ -211,27 +211,24 @@ public function prepareRowAsOl(string $spatial, int $srid, string $label, array
211211
'width' => 2,
212212
];
213213

214-
$result = 'var style = new ol.style.Style({'
214+
$style = 'new ol.style.Style({'
215215
. 'stroke: new ol.style.Stroke(' . json_encode($stroke_style) . ')';
216216
if ($label !== '') {
217217
$text_style = ['text' => $label];
218-
$result .= ', text: new ol.style.Text(' . json_encode($text_style) . ')';
218+
$style .= ', text: new ol.style.Text(' . json_encode($text_style) . ')';
219219
}
220220

221-
$result .= '});';
222-
223-
if ($srid === 0) {
224-
$srid = 4326;
225-
}
221+
$style .= '})';
226222

227223
// Trim to remove leading 'LINESTRING(' and trailing ')'
228-
$linesrting = mb_substr($spatial, 11, -1);
229-
$points_arr = $this->extractPoints($linesrting, null);
230-
231-
return $result . 'var line = new ol.Feature({geometry: '
232-
. $this->getLineForOpenLayers($points_arr, $srid) . '});'
233-
. 'line.setStyle(style);'
234-
. 'vectorLayer.addFeature(line);';
224+
$wktCoordinates = mb_substr($spatial, 11, -1);
225+
$olGeometry = $this->toOpenLayersObject(
226+
'ol.geom.LineString',
227+
$this->extractPoints1d($wktCoordinates, null),
228+
$srid,
229+
);
230+
231+
return $this->addGeometryToLayer($olGeometry, $style);
235232
}
236233

237234
/**
@@ -276,7 +273,7 @@ protected function getCoordinateParams(string $wkt): array
276273
{
277274
// Trim to remove leading 'LINESTRING(' and trailing ')'
278275
$linestring = mb_substr($wkt, 11, -1);
279-
$points_arr = $this->extractPoints($linestring, null);
276+
$points_arr = $this->extractPoints1d($linestring, null);
280277

281278
$no_of_points = count($points_arr);
282279
$coords = ['no_of_points' => $no_of_points];

libraries/classes/Gis/GisMultiLineString.php

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function prepareRowAsPng(
9595

9696
$first_line = true;
9797
foreach ($linestirngs as $linestring) {
98-
$points_arr = $this->extractPoints($linestring, $scale_data);
98+
$points_arr = $this->extractPoints1d($linestring, $scale_data);
9999
foreach ($points_arr as $point) {
100100
if (isset($temp_point)) {
101101
// draw line section
@@ -154,7 +154,7 @@ public function prepareRowAsPdf($spatial, string $label, array $color, array $sc
154154

155155
$first_line = true;
156156
foreach ($linestirngs as $linestring) {
157-
$points_arr = $this->extractPoints($linestring, $scale_data);
157+
$points_arr = $this->extractPoints1d($linestring, $scale_data);
158158
foreach ($points_arr as $point) {
159159
if (isset($temp_point)) {
160160
// draw line section
@@ -205,7 +205,7 @@ public function prepareRowAsSvg($spatial, string $label, array $color, array $sc
205205

206206
$row = '';
207207
foreach ($linestirngs as $linestring) {
208-
$points_arr = $this->extractPoints($linestring, $scale_data);
208+
$points_arr = $this->extractPoints1d($linestring, $scale_data);
209209

210210
$row .= '<polyline points="';
211211
foreach ($points_arr as $point) {
@@ -242,29 +242,24 @@ public function prepareRowAsOl(string $spatial, int $srid, string $label, array
242242
'width' => 2,
243243
];
244244

245-
$row = 'var style = new ol.style.Style({'
245+
$style = 'new ol.style.Style({'
246246
. 'stroke: new ol.style.Stroke(' . json_encode($stroke_style) . ')';
247247
if ($label !== '') {
248248
$text_style = ['text' => $label];
249-
$row .= ', text: new ol.style.Text(' . json_encode($text_style) . ')';
249+
$style .= ', text: new ol.style.Text(' . json_encode($text_style) . ')';
250250
}
251251

252-
$row .= '});';
253-
254-
if ($srid === 0) {
255-
$srid = 4326;
256-
}
252+
$style .= '})';
257253

258254
// Trim to remove leading 'MULTILINESTRING((' and trailing '))'
259-
$multilinestirng = mb_substr($spatial, 17, -2);
260-
// Separate each linestring
261-
$linestirngs = explode('),(', $multilinestirng);
262-
263-
return $row . $this->getLineArrayForOpenLayers($linestirngs, $srid)
264-
. 'var multiLineString = new ol.geom.MultiLineString(arr);'
265-
. 'var feature = new ol.Feature({geometry: multiLineString});'
266-
. 'feature.setStyle(style);'
267-
. 'vectorLayer.addFeature(feature);';
255+
$wktCoordinates = mb_substr($spatial, 17, -2);
256+
$olGeometry = $this->toOpenLayersObject(
257+
'ol.geom.MultiLineString',
258+
$this->extractPoints2d($wktCoordinates, null),
259+
$srid,
260+
);
261+
262+
return $this->addGeometryToLayer($olGeometry, $style);
268263
}
269264

270265
/**
@@ -351,7 +346,7 @@ protected function getCoordinateParams(string $wkt): array
351346
$coords = ['no_of_lines' => count($wkt_linestrings)];
352347

353348
foreach ($wkt_linestrings as $j => $wkt_linestring) {
354-
$points = $this->extractPoints($wkt_linestring, null);
349+
$points = $this->extractPoints1d($wkt_linestring, null);
355350
$no_of_points = count($points);
356351
$coords[$j] = ['no_of_points' => $no_of_points];
357352
for ($i = 0; $i < $no_of_points; $i++) {

0 commit comments

Comments
 (0)