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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
<?php
/**
* Handles actions related to GIS LINESTRING objects
*/
declare(strict_types=1);
namespace PhpMyAdmin\Gis;
use PhpMyAdmin\Image\ImageWrapper;
use TCPDF;
use function count;
use function hexdec;
use function json_encode;
use function mb_substr;
use function round;
use function trim;
/**
* Handles actions related to GIS LINESTRING objects
*/
class GisLineString extends GisGeometry
{
/** @var self */
private static $instance;
/**
* A private constructor; prevents direct creation of object.
*/
private function __construct()
{
}
/**
* Returns the singleton.
*
* @return GisLineString the singleton
*/
public static function singleton()
{
if (! isset(self::$instance)) {
self::$instance = new GisLineString();
}
return self::$instance;
}
/**
* Scales each row.
*
* @param string $spatial spatial data of a row
*
* @return array an array containing the min, max values for x and y coordinates
*/
public function scaleRow($spatial)
{
// Trim to remove leading 'LINESTRING(' and trailing ')'
$linestring = mb_substr($spatial, 11, -1);
return $this->setMinMax($linestring, []);
}
/**
* Adds to the PNG image object, the data related to a row in the GIS dataset.
*
* @param string $spatial GIS POLYGON object
* @param string|null $label Label for the GIS POLYGON object
* @param string $line_color Color for the GIS POLYGON object
* @param array $scale_data Array containing data related to scaling
*/
public function prepareRowAsPng(
$spatial,
?string $label,
$line_color,
array $scale_data,
ImageWrapper $image
): ImageWrapper {
// allocate colors
$black = $image->colorAllocate(0, 0, 0);
$red = (int) hexdec(mb_substr($line_color, 1, 2));
$green = (int) hexdec(mb_substr($line_color, 3, 2));
$blue = (int) hexdec(mb_substr($line_color, 4, 2));
$color = $image->colorAllocate($red, $green, $blue);
$label = trim($label ?? '');
// Trim to remove leading 'LINESTRING(' and trailing ')'
$lineString = mb_substr($spatial, 11, -1);
$points_arr = $this->extractPoints($lineString, $scale_data);
foreach ($points_arr as $point) {
if (isset($temp_point)) {
// draw line section
$image->line(
(int) round($temp_point[0]),
(int) round($temp_point[1]),
(int) round($point[0]),
(int) round($point[1]),
$color
);
}
$temp_point = $point;
}
// print label if applicable
if ($label !== '') {
$image->string(
1,
(int) round($points_arr[1][0]),
(int) round($points_arr[1][1]),
$label,
$black
);
}
return $image;
}
/**
* Adds to the TCPDF instance, the data related to a row in the GIS dataset.
*
* @param string $spatial GIS LINESTRING object
* @param string|null $label Label for the GIS LINESTRING object
* @param string $line_color Color for the GIS LINESTRING object
* @param array $scale_data Array containing data related to scaling
* @param TCPDF $pdf TCPDF instance
*
* @return TCPDF the modified TCPDF instance
*/
public function prepareRowAsPdf($spatial, ?string $label, $line_color, array $scale_data, $pdf)
{
// allocate colors
$red = hexdec(mb_substr($line_color, 1, 2));
$green = hexdec(mb_substr($line_color, 3, 2));
$blue = hexdec(mb_substr($line_color, 4, 2));
$line = [
'width' => 1.5,
'color' => [
$red,
$green,
$blue,
],
];
$label = trim($label ?? '');
// Trim to remove leading 'LINESTRING(' and trailing ')'
$linesrting = mb_substr($spatial, 11, -1);
$points_arr = $this->extractPoints($linesrting, $scale_data);
foreach ($points_arr as $point) {
if (isset($temp_point)) {
// draw line section
$pdf->Line($temp_point[0], $temp_point[1], $point[0], $point[1], $line);
}
$temp_point = $point;
}
// print label
if ($label !== '') {
$pdf->setXY($points_arr[1][0], $points_arr[1][1]);
$pdf->setFontSize(5);
$pdf->Cell(0, 0, $label);
}
return $pdf;
}
/**
* Prepares and returns the code related to a row in the GIS dataset as SVG.
*
* @param string $spatial GIS LINESTRING object
* @param string $label Label for the GIS LINESTRING object
* @param string $line_color Color for the GIS LINESTRING object
* @param array $scale_data Array containing data related to scaling
*
* @return string the code related to a row in the GIS dataset
*/
public function prepareRowAsSvg($spatial, $label, $line_color, array $scale_data)
{
$line_options = [
'name' => $label,
'id' => $label . $this->getRandomId(),
'class' => 'linestring vector',
'fill' => 'none',
'stroke' => $line_color,
'stroke-width' => 2,
];
// Trim to remove leading 'LINESTRING(' and trailing ')'
$linesrting = mb_substr($spatial, 11, -1);
$points_arr = $this->extractPoints($linesrting, $scale_data);
$row = '<polyline points="';
foreach ($points_arr as $point) {
$row .= $point[0] . ',' . $point[1] . ' ';
}
$row .= '"';
foreach ($line_options as $option => $val) {
$row .= ' ' . $option . '="' . trim((string) $val) . '"';
}
$row .= '/>';
return $row;
}
/**
* Prepares JavaScript related to a row in the GIS dataset
* to visualize it with OpenLayers.
*
* @param string $spatial GIS LINESTRING object
* @param int $srid Spatial reference ID
* @param string $label Label for the GIS LINESTRING object
* @param array $line_color Color for the GIS LINESTRING object
* @param array $scale_data Array containing data related to scaling
*
* @return string JavaScript related to a row in the GIS dataset
*/
public function prepareRowAsOl($spatial, int $srid, $label, $line_color, array $scale_data)
{
$stroke_style = [
'color' => $line_color,
'width' => 2,
];
$result = 'var style = new ol.style.Style({'
. 'stroke: new ol.style.Stroke(' . json_encode($stroke_style) . ')';
if (trim($label) !== '') {
$text_style = ['text' => trim($label)];
$result .= ', text: new ol.style.Text(' . json_encode($text_style) . ')';
}
$result .= '});';
if ($srid === 0) {
$srid = 4326;
}
$result .= $this->getBoundsForOl($srid, $scale_data);
// Trim to remove leading 'LINESTRING(' and trailing ')'
$linesrting = mb_substr($spatial, 11, -1);
$points_arr = $this->extractPoints($linesrting, null);
return $result . 'var line = new ol.Feature({geometry: '
. $this->getLineForOpenLayers($points_arr, $srid) . '});'
. 'line.setStyle(style);'
. 'vectorLayer.addFeature(line);';
}
/**
* Generate the WKT with the set of parameters passed by the GIS editor.
*
* @param array $gis_data GIS data
* @param int $index Index into the parameter object
* @param string|null $empty Value for empty points
*
* @return string WKT with the set of parameters passed by the GIS editor
*/
public function generateWkt(array $gis_data, $index, $empty = '')
{
$no_of_points = $gis_data[$index]['LINESTRING']['no_of_points'] ?? 2;
if ($no_of_points < 2) {
$no_of_points = 2;
}
$wkt = 'LINESTRING(';
for ($i = 0; $i < $no_of_points; $i++) {
$wkt .= (isset($gis_data[$index]['LINESTRING'][$i]['x'])
&& trim((string) $gis_data[$index]['LINESTRING'][$i]['x']) != ''
? $gis_data[$index]['LINESTRING'][$i]['x'] : $empty)
. ' ' . (isset($gis_data[$index]['LINESTRING'][$i]['y'])
&& trim((string) $gis_data[$index]['LINESTRING'][$i]['y']) != ''
? $gis_data[$index]['LINESTRING'][$i]['y'] : $empty) . ',';
}
$wkt = mb_substr($wkt, 0, -1);
return $wkt . ')';
}
/**
* Generate parameters for the GIS data editor from the value of the GIS column.
*
* @param string $value of the GIS column
* @param int $index of the geometry
*
* @return array params for the GIS data editor from the value of the GIS column
*/
public function generateParams($value, $index = -1)
{
$params = [];
if ($index == -1) {
$index = 0;
$data = GisGeometry::generateParams($value);
$params['srid'] = $data['srid'];
$wkt = $data['wkt'];
} else {
$params[$index]['gis_type'] = 'LINESTRING';
$wkt = $value;
}
// Trim to remove leading 'LINESTRING(' and trailing ')'
$linestring = mb_substr($wkt, 11, -1);
$points_arr = $this->extractPoints($linestring, null);
$no_of_points = count($points_arr);
$params[$index]['LINESTRING']['no_of_points'] = $no_of_points;
for ($i = 0; $i < $no_of_points; $i++) {
$params[$index]['LINESTRING'][$i]['x'] = $points_arr[$i][0];
$params[$index]['LINESTRING'][$i]['y'] = $points_arr[$i][1];
}
return $params;
}
}
|