Translated using Weblate (Portuguese (Brazil))
[phpmyadmin.git] / src / Gis / GisPoint.php
blobdbc4e29510669a5e78c4428bd4d085e2f2f121f6
1 <?php
2 /**
3 * Handles actions related to GIS POINT objects
4 */
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;
13 use TCPDF;
15 use function json_encode;
16 use function mb_substr;
17 use function round;
18 use function sprintf;
20 /**
21 * Handles actions related to GIS POINT objects
23 class GisPoint extends GisGeometry
25 private static self $instance;
27 /**
28 * A private constructor; prevents direct creation of object.
30 private function __construct()
34 /**
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;
48 /**
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);
63 /**
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(
72 string $spatial,
73 string $label,
74 array $color,
75 ScaleData $scaleData,
76 ImageWrapper $image,
77 ): void {
78 // allocate colors
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] == '') {
88 return;
91 $image->arc(
92 (int) round($pointsArr[0]),
93 (int) round($pointsArr[1]),
97 360,
98 $pointColor,
100 if ($label === '') {
101 return;
104 // print label if applicable
105 $image->string(
107 (int) round($pointsArr[0]),
108 (int) round($pointsArr[1]),
109 $label,
110 $black,
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(
123 string $spatial,
124 string $label,
125 array $color,
126 ScaleData $scaleData,
127 TCPDF $pdf,
128 ): void {
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] == '') {
136 return;
139 // draw a small circle to mark the point
140 $pdf->Circle($pointsArr[0], $pointsArr[1], 2, 0, 360, 'D', $line);
141 if ($label === '') {
142 return;
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
163 $pointOptions = [
164 'name' => $label,
165 'id' => $label . $this->getRandomId(),
166 'class' => 'point vector',
167 'fill' => 'white',
168 'stroke' => sprintf('#%02x%02x%02x', ...$color),
169 'stroke-width' => 2,
172 // Trim to remove leading 'POINT(' and trailing ')'
173 $point = mb_substr($spatial, 6, -1);
174 $pointsArr = $this->extractPoints1dLinear($point, $scaleData);
176 $row = '';
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 . '"';
184 $row .= '/>';
187 return $row;
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(
202 string $spatial,
203 int $srid,
204 string $label,
205 array $color,
206 ): string {
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) . '),'
213 . 'radius: 3'
214 . '})';
215 if ($label !== '') {
216 $textStyle = ['text' => $label, 'offsetY' => -9];
217 $style .= ',text: new ol.style.Text(' . json_encode($textStyle) . ')';
220 $style .= '})';
222 // Trim to remove leading 'POINT(' and trailing ')'
223 $point = mb_substr($spatial, 6, -1);
224 $olGeometry = $this->toOpenLayersObject(
225 'ol.geom.Point',
226 $this->extractPoints1dLinear($point, null),
227 $srid,
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
280 return 'POINT';