UPDATE 4.4.0.0
[phpmyadmin.git] / libraries / gis / GIS_Multipoint.class.php
blob0d9692402ae63c50b91bcb46da4d4cd78ba63574
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Handles actions related to GIS MULTIPOINT objects
6 * @package PhpMyAdmin-GIS
7 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
14 * Handles actions related to GIS MULTIPOINT objects
16 * @package PhpMyAdmin-GIS
18 class PMA_GIS_Multipoint extends PMA_GIS_Geometry
20 // Hold the singleton instance of the class
21 private static $_instance;
23 /**
24 * A private constructor; prevents direct creation of object.
26 * @access private
28 private function __construct()
32 /**
33 * Returns the singleton.
35 * @return PMA_GIS_Multipoint the singleton
36 * @access public
38 public static function singleton()
40 if (!isset(self::$_instance)) {
41 $class = __CLASS__;
42 self::$_instance = new $class;
45 return self::$_instance;
48 /**
49 * Scales each row.
51 * @param string $spatial spatial data of a row
53 * @return array an array containing the min, max values for x and y coordinates
54 * @access public
56 public function scaleRow($spatial)
58 // Trim to remove leading 'MULTIPOINT(' and trailing ')'
59 $multipoint = /*overload*/mb_substr(
60 $spatial,
61 11,
62 /*overload*/mb_strlen($spatial) - 12
64 return $this->setMinMax($multipoint, array());
67 /**
68 * Adds to the PNG image object, the data related to a row in the GIS dataset.
70 * @param string $spatial GIS MULTIPOINT object
71 * @param string $label Label for the GIS MULTIPOINT object
72 * @param string $point_color Color for the GIS MULTIPOINT object
73 * @param array $scale_data Array containing data related to scaling
74 * @param object $image Image object
76 * @return object the modified image object
77 * @access public
79 public function prepareRowAsPng($spatial, $label, $point_color,
80 $scale_data, $image
81 ) {
82 // allocate colors
83 $black = imagecolorallocate($image, 0, 0, 0);
84 $red = hexdec(/*overload*/mb_substr($point_color, 1, 2));
85 $green = hexdec(/*overload*/mb_substr($point_color, 3, 2));
86 $blue = hexdec(/*overload*/mb_substr($point_color, 4, 2));
87 $color = imagecolorallocate($image, $red, $green, $blue);
89 // Trim to remove leading 'MULTIPOINT(' and trailing ')'
90 $multipoint = /*overload*/mb_substr(
91 $spatial,
92 11,
93 /*overload*/mb_strlen($spatial) - 12
95 $points_arr = $this->extractPoints($multipoint, $scale_data);
97 foreach ($points_arr as $point) {
98 // draw a small circle to mark the point
99 if ($point[0] != '' && $point[1] != '') {
100 imagearc($image, $point[0], $point[1], 7, 7, 0, 360, $color);
103 // print label for each point
104 if ((isset($label) && trim($label) != '')
105 && ($points_arr[0][0] != '' && $points_arr[0][1] != '')
107 imagestring(
108 $image, 1, $points_arr[0][0], $points_arr[0][1], trim($label), $black
111 return $image;
115 * Adds to the TCPDF instance, the data related to a row in the GIS dataset.
117 * @param string $spatial GIS MULTIPOINT object
118 * @param string $label Label for the GIS MULTIPOINT object
119 * @param string $point_color Color for the GIS MULTIPOINT object
120 * @param array $scale_data Array containing data related to scaling
121 * @param TCPDF $pdf TCPDF instance
123 * @return TCPDF the modified TCPDF instance
124 * @access public
126 public function prepareRowAsPdf($spatial, $label, $point_color,
127 $scale_data, $pdf
129 // allocate colors
130 $red = hexdec(/*overload*/mb_substr($point_color, 1, 2));
131 $green = hexdec(/*overload*/mb_substr($point_color, 3, 2));
132 $blue = hexdec(/*overload*/mb_substr($point_color, 4, 2));
133 $line = array('width' => 1.25, 'color' => array($red, $green, $blue));
135 // Trim to remove leading 'MULTIPOINT(' and trailing ')'
136 $multipoint = /*overload*/mb_substr(
137 $spatial,
139 /*overload*/mb_strlen($spatial) - 12
141 $points_arr = $this->extractPoints($multipoint, $scale_data);
143 foreach ($points_arr as $point) {
144 // draw a small circle to mark the point
145 if ($point[0] != '' && $point[1] != '') {
146 $pdf->Circle($point[0], $point[1], 2, 0, 360, 'D', $line);
149 // print label for each point
150 if ((isset($label) && trim($label) != '')
151 && ($points_arr[0][0] != '' && $points_arr[0][1] != '')
153 $pdf->SetXY($points_arr[0][0], $points_arr[0][1]);
154 $pdf->SetFontSize(5);
155 $pdf->Cell(0, 0, trim($label));
157 return $pdf;
161 * Prepares and returns the code related to a row in the GIS dataset as SVG.
163 * @param string $spatial GIS MULTIPOINT object
164 * @param string $label Label for the GIS MULTIPOINT object
165 * @param string $point_color Color for the GIS MULTIPOINT object
166 * @param array $scale_data Array containing data related to scaling
168 * @return string the code related to a row in the GIS dataset
169 * @access public
171 public function prepareRowAsSvg($spatial, $label, $point_color, $scale_data)
173 $point_options = array(
174 'name' => $label,
175 'class' => 'multipoint vector',
176 'fill' => 'white',
177 'stroke' => $point_color,
178 'stroke-width'=> 2,
181 // Trim to remove leading 'MULTIPOINT(' and trailing ')'
182 $multipoint = /*overload*/mb_substr(
183 $spatial,
185 /*overload*/mb_strlen($spatial) - 12
187 $points_arr = $this->extractPoints($multipoint, $scale_data);
189 $row = '';
190 foreach ($points_arr as $point) {
191 if ($point[0] != '' && $point[1] != '') {
192 $row .= '<circle cx="' . $point[0] . '" cy="'
193 . $point[1] . '" r="3"';
194 $point_options['id'] = $label . rand();
195 foreach ($point_options as $option => $val) {
196 $row .= ' ' . $option . '="' . trim($val) . '"';
198 $row .= '/>';
202 return $row;
206 * Prepares JavaScript related to a row in the GIS dataset
207 * to visualize it with OpenLayers.
209 * @param string $spatial GIS MULTIPOINT object
210 * @param int $srid Spatial reference ID
211 * @param string $label Label for the GIS MULTIPOINT object
212 * @param string $point_color Color for the GIS MULTIPOINT object
213 * @param array $scale_data Array containing data related to scaling
215 * @return string JavaScript related to a row in the GIS dataset
216 * @access public
218 public function prepareRowAsOl($spatial, $srid, $label,
219 $point_color, $scale_data
221 $style_options = array(
222 'pointRadius' => 3,
223 'fillColor' => '#ffffff',
224 'strokeColor' => $point_color,
225 'strokeWidth' => 2,
226 'label' => $label,
227 'labelYOffset' => -8,
228 'fontSize' => 10,
230 if ($srid == 0) {
231 $srid = 4326;
233 $result = $this->getBoundsForOl($srid, $scale_data);
235 // Trim to remove leading 'MULTIPOINT(' and trailing ')'
236 $multipoint = /*overload*/mb_substr(
237 $spatial,
239 /*overload*/mb_strlen($spatial) - 12
241 $points_arr = $this->extractPoints($multipoint, null);
243 $result .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector('
244 . 'new OpenLayers.Geometry.MultiPoint('
245 . $this->getPointsArrayForOpenLayers($points_arr, $srid)
246 . '), null, ' . json_encode($style_options) . '));';
247 return $result;
251 * Generate the WKT with the set of parameters passed by the GIS editor.
253 * @param array $gis_data GIS data
254 * @param int $index Index into the parameter object
255 * @param string $empty Multipoint does not adhere to this
257 * @return string WKT with the set of parameters passed by the GIS editor
258 * @access public
260 public function generateWkt($gis_data, $index, $empty = '')
262 $no_of_points = isset($gis_data[$index]['MULTIPOINT']['no_of_points'])
263 ? $gis_data[$index]['MULTIPOINT']['no_of_points'] : 1;
264 if ($no_of_points < 1) {
265 $no_of_points = 1;
267 $wkt = 'MULTIPOINT(';
268 for ($i = 0; $i < $no_of_points; $i++) {
269 $wkt .= ((isset($gis_data[$index]['MULTIPOINT'][$i]['x'])
270 && trim($gis_data[$index]['MULTIPOINT'][$i]['x']) != '')
271 ? $gis_data[$index]['MULTIPOINT'][$i]['x'] : '')
272 . ' ' . ((isset($gis_data[$index]['MULTIPOINT'][$i]['y'])
273 && trim($gis_data[$index]['MULTIPOINT'][$i]['y']) != '')
274 ? $gis_data[$index]['MULTIPOINT'][$i]['y'] : '') . ',';
277 $wkt = /*overload*/mb_substr($wkt, 0, /*overload*/mb_strlen($wkt) - 1);
278 $wkt .= ')';
279 return $wkt;
283 * Generate the WKT for the data from ESRI shape files.
285 * @param array $row_data GIS data
287 * @return string the WKT for the data from ESRI shape files
288 * @access public
290 public function getShape($row_data)
292 $wkt = 'MULTIPOINT(';
293 for ($i = 0; $i < $row_data['numpoints']; $i++) {
294 $wkt .= $row_data['points'][$i]['x'] . ' '
295 . $row_data['points'][$i]['y'] . ',';
298 $wkt = /*overload*/mb_substr($wkt, 0, /*overload*/mb_strlen($wkt) - 1);
299 $wkt .= ')';
300 return $wkt;
304 * Generate parameters for the GIS data editor from the value of the GIS column.
306 * @param string $value Value of the GIS column
307 * @param integer $index Index of the geometry
309 * @return array params for the GIS data editor from the value of the GIS column
310 * @access public
312 public function generateParams($value, $index = -1)
314 $params = array();
315 if ($index == -1) {
316 $index = 0;
317 $data = PMA_GIS_Geometry::generateParams($value);
318 $params['srid'] = $data['srid'];
319 $wkt = $data['wkt'];
320 } else {
321 $params[$index]['gis_type'] = 'MULTIPOINT';
322 $wkt = $value;
325 // Trim to remove leading 'MULTIPOINT(' and trailing ')'
326 $points = /*overload*/mb_substr($wkt, 11, /*overload*/mb_strlen($wkt) - 12);
327 $points_arr = $this->extractPoints($points, null);
329 $no_of_points = count($points_arr);
330 $params[$index]['MULTIPOINT']['no_of_points'] = $no_of_points;
331 for ($i = 0; $i < $no_of_points; $i++) {
332 $params[$index]['MULTIPOINT'][$i]['x'] = $points_arr[$i][0];
333 $params[$index]['MULTIPOINT'][$i]['y'] = $points_arr[$i][1];
336 return $params;
340 * Overridden to make sure that only the points having valid values
341 * for x and y coordinates are added.
343 * @param array $points_arr x and y coordinates for each point
344 * @param string $srid spatial reference id
346 * @return string JavaScript for adding an array of points to OpenLayers
347 * @access protected
349 protected function getPointsArrayForOpenLayers($points_arr, $srid)
351 $ol_array = 'new Array(';
352 foreach ($points_arr as $point) {
353 if ($point[0] != '' && $point[1] != '') {
354 $ol_array .= $this->getPointForOpenLayers($point, $srid) . ', ';
358 $olArrayLength = /*overload*/mb_strlen($ol_array);
359 if (/*overload*/mb_substr($ol_array, $olArrayLength - 2) == ', ') {
360 $ol_array = /*overload*/mb_substr($ol_array, 0, $olArrayLength - 2);
362 $ol_array .= ')';
364 return $ol_array;