3 * Handles actions related to GIS POINT objects
6 declare(strict_types
=1);
8 namespace PhpMyAdmin\Gis
;
10 use PhpMyAdmin\Gis\Ds\Extent
;
11 use PhpMyAdmin\Gis\Ds\ScaleData
;
12 use PhpMyAdmin\Image\ImageWrapper
;
15 use function json_encode
;
16 use function mb_substr
;
21 * Handles actions related to GIS POINT objects
23 class GisPoint
extends GisGeometry
25 private static self
$instance;
28 * A private constructor; prevents direct creation of object.
30 private function __construct()
35 * Returns the singleton.
37 * @return GisPoint the singleton
39 public static function singleton(): GisPoint
41 if (! isset(self
::$instance)) {
42 self
::$instance = new GisPoint();
45 return self
::$instance;
49 * Get coordinate extent for this wkt.
51 * @param string $wkt Well Known Text represenatation of the geometry
53 * @return Extent the min, max values for x and y coordinates
55 public function getExtent(string $wkt): Extent
57 // Trim to remove leading 'POINT(' and trailing ')'
58 $point = mb_substr($wkt, 6, -1);
60 return $this->getCoordinatesExtent($point);
64 * Adds to the PNG image object, the data related to a row in the GIS dataset.
66 * @param string $spatial GIS POLYGON object
67 * @param string $label Label for the GIS POLYGON object
68 * @param int[] $color Color for the GIS POLYGON object
69 * @param ScaleData $scaleData Array containing data related to scaling
71 public function prepareRowAsPng(
79 $black = $image->colorAllocate(0, 0, 0);
80 $pointColor = $image->colorAllocate(...$color);
82 // Trim to remove leading 'POINT(' and trailing ')'
83 $point = mb_substr($spatial, 6, -1);
84 $pointsArr = $this->extractPoints1dLinear($point, $scaleData);
86 // draw a small circle to mark the point
87 if ($pointsArr[0] == '' ||
$pointsArr[1] == '') {
92 (int) round($pointsArr[0]),
93 (int) round($pointsArr[1]),
104 // print label if applicable
107 (int) round($pointsArr[0]),
108 (int) round($pointsArr[1]),
115 * Adds to the TCPDF instance, the data related to a row in the GIS dataset.
117 * @param string $spatial GIS POINT object
118 * @param string $label Label for the GIS POINT object
119 * @param int[] $color Color for the GIS POINT object
120 * @param ScaleData $scaleData Array containing data related to scaling
122 public function prepareRowAsPdf(
126 ScaleData
$scaleData,
129 $line = ['width' => 1.25, 'color' => $color];
131 // Trim to remove leading 'POINT(' and trailing ')'
132 $point = mb_substr($spatial, 6, -1);
133 $pointsArr = $this->extractPoints1dLinear($point, $scaleData);
135 if ($pointsArr[0] == '' ||
$pointsArr[1] == '') {
139 // draw a small circle to mark the point
140 $pdf->Circle($pointsArr[0], $pointsArr[1], 2, 0, 360, 'D', $line);
145 // print label if applicable
146 $pdf->setXY($pointsArr[0], $pointsArr[1]);
147 $pdf->setFontSize(5);
148 $pdf->Cell(0, 0, $label);
152 * Prepares and returns the code related to a row in the GIS dataset as SVG.
154 * @param string $spatial GIS POINT object
155 * @param string $label Label for the GIS POINT object
156 * @param int[] $color Color for the GIS POINT object
157 * @param ScaleData $scaleData Array containing data related to scaling
159 * @return string the code related to a row in the GIS dataset
161 public function prepareRowAsSvg(string $spatial, string $label, array $color, ScaleData
$scaleData): string
165 'id' => $label . $this->getRandomId(),
166 'class' => 'point vector',
168 'stroke' => sprintf('#%02x%02x%02x', ...$color),
172 // Trim to remove leading 'POINT(' and trailing ')'
173 $point = mb_substr($spatial, 6, -1);
174 $pointsArr = $this->extractPoints1dLinear($point, $scaleData);
177 if ($pointsArr[0] !== 0.0 && $pointsArr[1] !== 0.0) {
178 $row .= '<circle cx="' . $pointsArr[0]
179 . '" cy="' . $pointsArr[1] . '" r="3"';
180 foreach ($pointOptions as $option => $val) {
181 $row .= ' ' . $option . '="' . $val . '"';
191 * Prepares JavaScript related to a row in the GIS dataset
192 * to visualize it with OpenLayers.
194 * @param string $spatial GIS POINT object
195 * @param int $srid Spatial reference ID
196 * @param string $label Label for the GIS POINT object
197 * @param int[] $color Color for the GIS POINT object
199 * @return string JavaScript related to a row in the GIS dataset
201 public function prepareRowAsOl(
207 $fillStyle = ['color' => 'white'];
208 $strokeStyle = ['color' => $color, 'width' => 2];
209 $style = 'new ol.style.Style({'
210 . 'image: new ol.style.Circle({'
211 . 'fill: new ol.style.Fill(' . json_encode($fillStyle) . '),'
212 . 'stroke: new ol.style.Stroke(' . json_encode($strokeStyle) . '),'
216 $textStyle = ['text' => $label, 'offsetY' => -9];
217 $style .= ',text: new ol.style.Text(' . json_encode($textStyle) . ')';
222 // Trim to remove leading 'POINT(' and trailing ')'
223 $point = mb_substr($spatial, 6, -1);
224 $olGeometry = $this->toOpenLayersObject(
226 $this->extractPoints1dLinear($point, null),
230 return $this->addGeometryToLayer($olGeometry, $style);
234 * Generate the WKT with the set of parameters passed by the GIS editor.
236 * @param mixed[] $gisData GIS data
237 * @param int $index Index into the parameter object
238 * @param string $empty Point does not adhere to this parameter
240 * @return string WKT with the set of parameters passed by the GIS editor
242 public function generateWkt(array $gisData, int $index, string $empty = ''): string
244 $wktCoord = $this->getWktCoord($gisData[$index]['POINT'] ??
null, '');
246 return 'POINT(' . $wktCoord . ')';
250 * Generate the WKT for the data from ESRI shape files.
252 * @param mixed[] $rowData GIS data
254 * @return string the WKT for the data from ESRI shape files
256 public function getShape(array $rowData): string
258 return 'POINT(' . ($rowData['x'] ??
'')
259 . ' ' . ($rowData['y'] ??
'') . ')';
263 * Generate coordinate parameters for the GIS data editor from the value of the GIS column.
265 * @param string $wkt Value of the GIS column
267 * @return mixed[] Coordinate params for the GIS data editor from the value of the GIS column
269 protected function getCoordinateParams(string $wkt): array
271 // Trim to remove leading 'POINT(' and trailing ')'
272 $wktPoint = mb_substr($wkt, 6, -1);
273 $points = $this->extractPoints1d($wktPoint, null);
275 return ['x' => $points[0][0], 'y' => $points[0][1]];
278 protected function getType(): string