Labels for polygons and multipolygons
[phpmyadmin/crack.git] / libraries / gis / pma_gis_multilinestring.php
blob32a89403fd623fc48e9f1adb752da50741527a7d
1 <?php
2 /**
3 * Handles the visualization of GIS MULTILINESTRING objects.
5 * @package phpMyAdmin-GIS
6 */
7 class PMA_GIS_Multilinestring extends PMA_GIS_Geometry
9 // Hold the singleton instance of the class
10 private static $_instance;
12 /**
13 * A private constructor; prevents direct creation of object.
15 private function __construct()
19 /**
20 * Returns the singleton.
22 * @return the singleton
24 public static function singleton()
26 if (!isset(self::$_instance)) {
27 $class = __CLASS__;
28 self::$_instance = new $class;
31 return self::$_instance;
34 /**
35 * Scales each row.
37 * @param string $spatial spatial data of a row
39 * @return array containing the min, max values for x and y cordinates
41 public function scaleRow($spatial)
43 $min_max = array();
45 // Trim to remove leading 'MULTILINESTRING((' and trailing '))'
46 $multilinestirng = substr($spatial, 17, (strlen($spatial) - 19));
47 // Seperate each linestring
48 $linestirngs = explode("),(", $multilinestirng);
50 foreach ($linestirngs as $linestring) {
51 $min_max = $this->setMinMax($linestring, $min_max);
54 return $min_max;
57 /**
58 * Adds to the PNG image object, the data related to a row in the GIS dataset.
60 * @param string $spatial GIS MULTILINESTRING object
61 * @param string $label Label for the GIS MULTILINESTRING object
62 * @param string $line_color Color for the GIS MULTILINESTRING object
63 * @param array $scale_data Array containing data related to scaling
64 * @param image $image Image object
66 * @return the modified image object
68 public function prepareRowAsPng($spatial, $label, $line_color, $scale_data, $image)
70 // allocate colors
71 $black = imagecolorallocate($image, 0, 0, 0);
72 $red = hexdec(substr($line_color, 1, 2));
73 $green = hexdec(substr($line_color, 3, 2));
74 $blue = hexdec(substr($line_color, 4, 2));
75 $color = imagecolorallocate($image, $red, $green, $blue);
77 // Trim to remove leading 'MULTILINESTRING((' and trailing '))'
78 $multilinestirng = substr($spatial, 17, (strlen($spatial) - 19));
79 // Seperate each linestring
80 $linestirngs = explode("),(", $multilinestirng);
82 foreach ($linestirngs as $linestring) {
83 $points_arr = $this->extractPoints($linestring, $scale_data);
84 foreach ($points_arr as $point) {
85 if (! isset($temp_point)) {
86 $temp_point = $point;
87 } else {
88 // draw line section
89 imageline($image, $temp_point[0], $temp_point[1], $point[0], $point[1], $color);
90 $temp_point = $point;
93 unset($temp_point);
94 // print label if applicable
95 if (isset($label) && trim($label) != '') {
96 imagestring($image, 2, $points_arr[1][0], $points_arr[1][1], trim($label), $black);
99 return $image;
103 * Adds to the TCPDF instance, the data related to a row in the GIS dataset.
105 * @param string $spatial GIS MULTILINESTRING object
106 * @param string $label Label for the GIS MULTILINESTRING object
107 * @param string $line_color Color for the GIS MULTILINESTRING object
108 * @param array $scale_data Array containing data related to scaling
109 * @param image $pdf TCPDF instance
111 * @return the modified TCPDF instance
113 public function prepareRowAsPdf($spatial, $label, $line_color, $scale_data, $pdf)
115 // allocate colors
116 $red = hexdec(substr($line_color, 1, 2));
117 $green = hexdec(substr($line_color, 3, 2));
118 $blue = hexdec(substr($line_color, 4, 2));
119 $line = array('width' => 1.5, 'color' => array($red, $green, $blue));
121 // Trim to remove leading 'MULTILINESTRING((' and trailing '))'
122 $multilinestirng = substr($spatial, 17, (strlen($spatial) - 19));
123 // Seperate each linestring
124 $linestirngs = explode("),(", $multilinestirng);
126 foreach ($linestirngs as $linestring) {
127 $points_arr = $this->extractPoints($linestring, $scale_data);
128 foreach ($points_arr as $point) {
129 if (! isset($temp_point)) {
130 $temp_point = $point;
131 } else {
132 // draw line section
133 $pdf->Line($temp_point[0], $temp_point[1], $point[0], $point[1], $line);
134 $temp_point = $point;
137 unset($temp_point);
138 // print label
139 if (isset($label) && trim($label) != '') {
140 $pdf->SetXY($points_arr[1][0], $points_arr[1][1]);
141 $pdf->SetFontSize(7);
142 $pdf->Cell(0, 0, trim($label));
145 return $pdf;
149 * Prepares and returns the code related to a row in the GIS dataset as SVG.
151 * @param string $spatial GIS MULTILINESTRING object
152 * @param string $label Label for the GIS MULTILINESTRING object
153 * @param string $line_color Color for the GIS MULTILINESTRING object
154 * @param array $scale_data Array containing data related to scaling
156 * @return the code related to a row in the GIS dataset
158 public function prepareRowAsSvg($spatial, $label, $line_color, $scale_data)
160 $line_options = array(
161 'name' => $label,
162 'class' => 'linestring vector',
163 'fill' => 'none',
164 'stroke' => $line_color,
165 'stroke-width'=> 2,
168 // Trim to remove leading 'MULTILINESTRING((' and trailing '))'
169 $multilinestirng = substr($spatial, 17, (strlen($spatial) - 19));
170 // Seperate each linestring
171 $linestirngs = explode("),(", $multilinestirng);
173 $row = '';
174 foreach ($linestirngs as $linestring) {
175 $points_arr = $this->extractPoints($linestring, $scale_data);
177 $row .= '<polyline points="';
178 foreach ($points_arr as $point) {
179 $row .= $point[0] . ',' . $point[1] . ' ';
181 $row .= '"';
182 $line_options['id'] = $label . rand();
183 foreach ($line_options as $option => $val) {
184 $row .= ' ' . $option . '="' . trim($val) . '"';
186 $row .= '/>';
189 return $row;
193 * Prepares JavaScript related to a row in the GIS dataset
194 * to visualize it with OpenLayers.
196 * @param string $spatial GIS MULTILINESTRING object
197 * @param int $srid Spatial reference ID
198 * @param string $label Label for the GIS MULTILINESTRING object
199 * @param string $line_color Color for the GIS MULTILINESTRING object
200 * @param array $scale_data Array containing data related to scaling
202 * @return JavaScript related to a row in the GIS dataset
204 public function prepareRowAsOl($spatial, $srid, $label, $line_color, $scale_data)
206 $style_options = array(
207 'strokeColor' => $line_color,
208 'strokeWidth' => 2,
209 'label' => $label,
210 'fontSize' => 10,
212 if ($srid == 0) {
213 $srid = 4326;
215 $row = $this->getBoundsForOl($srid, $scale_data);
217 // Trim to remove leading 'MULTILINESTRING((' and trailing '))'
218 $multilinestirng = substr($spatial, 17, (strlen($spatial) - 19));
219 // Seperate each linestring
220 $linestirngs = explode("),(", $multilinestirng);
222 $row .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector('
223 . 'new OpenLayers.Geometry.MultiLineString(new Array(';
224 foreach ($linestirngs as $linestring) {
225 $points_arr = $this->extractPoints($linestring, null);
226 $row .= 'new OpenLayers.Geometry.LineString(new Array(';
227 foreach ($points_arr as $point) {
228 $row .= '(new OpenLayers.Geometry.Point(' . $point[0] . ', '
229 . $point[1] . ')).transform(new OpenLayers.Projection("EPSG:'
230 . $srid . '"), map.getProjectionObject()), ';
232 $row = substr($row, 0, strlen($row) - 2);
233 $row .= ')), ';
235 $row = substr($row, 0, strlen($row) - 2);
236 $row .= ')), null, ' . json_encode($style_options) . '));';
237 return $row;
241 * Generate the WKT with the set of parameters passed by the GIS editor.
243 * @param array $gis_data GIS data
244 * @param int $index Index into the parameter object
245 * @param string $empty Value for empty points
247 * @return WKT with the set of parameters passed by the GIS editor
249 public function generateWkt($gis_data, $index, $empty = '')
251 $no_of_lines = isset($gis_data[$index]['MULTILINESTRING']['no_of_lines'])
252 ? $gis_data[$index]['MULTILINESTRING']['no_of_lines'] : 1;
253 if ($no_of_lines < 1) {
254 $no_of_lines = 1;
256 $wkt = 'MULTILINESTRING(';
257 for ($i = 0; $i < $no_of_lines; $i++) {
258 $no_of_points = isset($gis_data[$index]['MULTILINESTRING'][$i]['no_of_points'])
259 ? $gis_data[$index]['MULTILINESTRING'][$i]['no_of_points'] : 2;
260 if ($no_of_points < 2) {
261 $no_of_points = 2;
263 $wkt .= '(';
264 for ($j = 0; $j < $no_of_points; $j++) {
265 $wkt .= ((isset($gis_data[$index]['MULTILINESTRING'][$i][$j]['x'])
266 && trim($gis_data[$index]['MULTILINESTRING'][$i][$j]['x']) != '')
267 ? $gis_data[$index]['MULTILINESTRING'][$i][$j]['x'] : $empty)
268 . ' ' . ((isset($gis_data[$index]['MULTILINESTRING'][$i][$j]['y'])
269 && trim($gis_data[$index]['MULTILINESTRING'][$i][$j]['y']) != '')
270 ? $gis_data[$index]['MULTILINESTRING'][$i][$j]['y'] : $empty) . ',';
272 $wkt = substr($wkt, 0, strlen($wkt) - 1);
273 $wkt .= '),';
275 $wkt = substr($wkt, 0, strlen($wkt) - 1);
276 $wkt .= ')';
277 return $wkt;
281 * Generate the WKT for the data from ESRI shape files.
283 * @param array $row_data GIS data
285 * @return the WKT for the data from ESRI shape files
287 public function getShape($row_data) {
288 $wkt = 'MULTILINESTRING(';
289 for ($i = 0; $i < $row_data['numparts']; $i++) {
290 $wkt .= '(';
291 foreach($row_data['parts'][$i]['points'] as $point) {
292 $wkt .= $point['x'] . ' ' . $point['y'] . ',';
294 $wkt = substr($wkt, 0, strlen($wkt) - 1);
295 $wkt .= '),';
297 $wkt = substr($wkt, 0, strlen($wkt) - 1);
298 $wkt .= ')';
299 return $wkt;
303 * Generate parameters for the GIS data editor from the value of the GIS column.
305 * @param string $value of the GIS column
306 * @param index $index of the geometry
308 * @return parameters for the GIS data editor from the value of the GIS column
310 public function generateParams($value, $index = -1)
312 if ($index == -1) {
313 $index = 0;
314 $params = array();
315 $data = PMA_GIS_Geometry::generateParams($value);
316 $params['srid'] = $data['srid'];
317 $wkt = $data['wkt'];
318 } else {
319 $params[$index]['gis_type'] = 'MULTILINESTRING';
320 $wkt = $value;
323 // Trim to remove leading 'MULTILINESTRING((' and trailing '))'
324 $multilinestirng = substr($wkt, 17, (strlen($wkt) - 19));
325 // Seperate each linestring
326 $linestirngs = explode("),(", $multilinestirng);
327 $params[$index]['MULTILINESTRING']['no_of_lines'] = count($linestirngs);
329 $j = 0;
330 foreach ($linestirngs as $linestring) {
331 $points_arr = $this->extractPoints($linestring, null);
332 $no_of_points = count($points_arr);
333 $params[$index]['MULTILINESTRING'][$j]['no_of_points'] = $no_of_points;
334 for ($i = 0; $i < $no_of_points; $i++) {
335 $params[$index]['MULTILINESTRING'][$j][$i]['x'] = $points_arr[$i][0];
336 $params[$index]['MULTILINESTRING'][$j][$i]['y'] = $points_arr[$i][1];
338 $j++;
340 return $params;