From 04d6d5ca99ebfd1cebb8ce06618fb3811fc1a8aa Mon Sep 17 00:00:00 2001 From: Charles Date: Thu, 9 Jan 2020 10:55:03 +0100 Subject: phpmyadmin working --- .../libraries/classes/Gis/GisFactory.php | 50 ++ .../libraries/classes/Gis/GisGeometry.php | 407 ++++++++++++ .../classes/Gis/GisGeometryCollection.php | 419 ++++++++++++ .../libraries/classes/Gis/GisLineString.php | 360 ++++++++++ .../libraries/classes/Gis/GisMultiLineString.php | 449 +++++++++++++ .../libraries/classes/Gis/GisMultiPoint.php | 416 ++++++++++++ .../libraries/classes/Gis/GisMultiPolygon.php | 617 +++++++++++++++++ srcs/phpmyadmin/libraries/classes/Gis/GisPoint.php | 363 +++++++++++ .../libraries/classes/Gis/GisPolygon.php | 618 ++++++++++++++++++ .../libraries/classes/Gis/GisVisualization.php | 726 +++++++++++++++++++++ 10 files changed, 4425 insertions(+) create mode 100644 srcs/phpmyadmin/libraries/classes/Gis/GisFactory.php create mode 100644 srcs/phpmyadmin/libraries/classes/Gis/GisGeometry.php create mode 100644 srcs/phpmyadmin/libraries/classes/Gis/GisGeometryCollection.php create mode 100644 srcs/phpmyadmin/libraries/classes/Gis/GisLineString.php create mode 100644 srcs/phpmyadmin/libraries/classes/Gis/GisMultiLineString.php create mode 100644 srcs/phpmyadmin/libraries/classes/Gis/GisMultiPoint.php create mode 100644 srcs/phpmyadmin/libraries/classes/Gis/GisMultiPolygon.php create mode 100644 srcs/phpmyadmin/libraries/classes/Gis/GisPoint.php create mode 100644 srcs/phpmyadmin/libraries/classes/Gis/GisPolygon.php create mode 100644 srcs/phpmyadmin/libraries/classes/Gis/GisVisualization.php (limited to 'srcs/phpmyadmin/libraries/classes/Gis') diff --git a/srcs/phpmyadmin/libraries/classes/Gis/GisFactory.php b/srcs/phpmyadmin/libraries/classes/Gis/GisFactory.php new file mode 100644 index 0000000..353d7e6 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Gis/GisFactory.php @@ -0,0 +1,50 @@ + $min_max['maxX']) { + $min_max['maxX'] = $x; + } + if (! isset($min_max['minX']) || $x < $min_max['minX']) { + $min_max['minX'] = $x; + } + $y = (float) $cordinates[1]; + if (! isset($min_max['maxY']) || $y > $min_max['maxY']) { + $min_max['maxY'] = $y; + } + if (! isset($min_max['minY']) || $y < $min_max['minY']) { + $min_max['minY'] = $y; + } + } + + return $min_max; + } + + /** + * Generates parameters for the GIS data editor from the value of the GIS column. + * This method performs common work. + * More specific work is performed by each of the geom classes. + * + * @param string $value value of the GIS column + * + * @return array parameters for the GIS editor from the value of the GIS column + * @access protected + */ + protected function generateParams($value) + { + $geom_types = '(POINT|MULTIPOINT|LINESTRING|MULTILINESTRING' + . '|POLYGON|MULTIPOLYGON|GEOMETRYCOLLECTION)'; + $srid = 0; + $wkt = ''; + + if (preg_match("/^'" . $geom_types . "\(.*\)',[0-9]*$/i", $value)) { + $last_comma = mb_strripos($value, ","); + $srid = trim(mb_substr($value, $last_comma + 1)); + $wkt = trim(mb_substr($value, 1, $last_comma - 2)); + } elseif (preg_match("/^" . $geom_types . "\(.*\)$/i", $value)) { + $wkt = $value; + } + + return [ + 'srid' => $srid, + 'wkt' => $wkt, + ]; + } + + /** + * Extracts points, scales and returns them as an array. + * + * @param string $point_set string of comma separated points + * @param array|null $scale_data data related to scaling + * @param boolean $linear if true, as a 1D array, else as a 2D array + * + * @return array scaled points + * @access protected + */ + protected function extractPoints($point_set, $scale_data, $linear = false) + { + $points_arr = []; + + // Separate each point + $points = explode(",", $point_set); + + foreach ($points as $point) { + $point = str_replace(['(', ')'], '', $point); + // Extract coordinates of the point + $cordinates = explode(" ", $point); + + if (isset($cordinates[0]) && trim($cordinates[0]) != '' + && isset($cordinates[1]) + && trim($cordinates[1]) != '' + ) { + if ($scale_data != null) { + $x = ($cordinates[0] - $scale_data['x']) * $scale_data['scale']; + $y = $scale_data['height'] + - ($cordinates[1] - $scale_data['y']) * $scale_data['scale']; + } else { + $x = floatval(trim($cordinates[0])); + $y = floatval(trim($cordinates[1])); + } + } else { + $x = 0; + $y = 0; + } + + if (! $linear) { + $points_arr[] = [ + $x, + $y, + ]; + } else { + $points_arr[] = $x; + $points_arr[] = $y; + } + } + + return $points_arr; + } + + /** + * Generates JavaScript for adding an array of polygons to OpenLayers. + * + * @param array $polygons x and y coordinates for each polygon + * @param string $srid spatial reference id + * + * @return string JavaScript for adding an array of polygons to OpenLayers + * @access protected + */ + protected function getPolygonArrayForOpenLayers(array $polygons, $srid) + { + $ol_array = 'new Array('; + foreach ($polygons as $polygon) { + $rings = explode("),(", $polygon); + $ol_array .= $this->getPolygonForOpenLayers($rings, $srid) . ', '; + } + + $ol_array + = mb_substr( + $ol_array, + 0, + mb_strlen($ol_array) - 2 + ); + $ol_array .= ')'; + + return $ol_array; + } + + /** + * Generates JavaScript for adding points for OpenLayers polygon. + * + * @param array $polygon x and y coordinates for each line + * @param string $srid spatial reference id + * + * @return string JavaScript for adding points for OpenLayers polygon + * @access protected + */ + protected function getPolygonForOpenLayers(array $polygon, $srid) + { + return 'new OpenLayers.Geometry.Polygon(' + . $this->getLineArrayForOpenLayers($polygon, $srid, false) + . ')'; + } + + /** + * Generates JavaScript for adding an array of LineString + * or LineRing to OpenLayers. + * + * @param array $lines x and y coordinates for each line + * @param string $srid spatial reference id + * @param bool $is_line_string whether it's an array of LineString + * + * @return string JavaScript for adding an array of LineString + * or LineRing to OpenLayers + * @access protected + */ + protected function getLineArrayForOpenLayers( + array $lines, + $srid, + $is_line_string = true + ) { + $ol_array = 'new Array('; + foreach ($lines as $line) { + $points_arr = $this->extractPoints($line, null); + $ol_array .= $this->getLineForOpenLayers( + $points_arr, + $srid, + $is_line_string + ); + $ol_array .= ', '; + } + + $ol_array + = mb_substr( + $ol_array, + 0, + mb_strlen($ol_array) - 2 + ); + $ol_array .= ')'; + + return $ol_array; + } + + /** + * Generates JavaScript for adding a LineString or LineRing to OpenLayers. + * + * @param array $points_arr x and y coordinates for each point + * @param string $srid spatial reference id + * @param bool $is_line_string whether it's a LineString + * + * @return string JavaScript for adding a LineString or LineRing to OpenLayers + * @access protected + */ + protected function getLineForOpenLayers( + array $points_arr, + $srid, + $is_line_string = true + ) { + return 'new OpenLayers.Geometry.' + . ($is_line_string ? 'LineString' : 'LinearRing') . '(' + . $this->getPointsArrayForOpenLayers($points_arr, $srid) + . ')'; + } + + /** + * Generates JavaScript for adding an array of points to OpenLayers. + * + * @param array $points_arr x and y coordinates for each point + * @param string $srid spatial reference id + * + * @return string JavaScript for adding an array of points to OpenLayers + * @access protected + */ + protected function getPointsArrayForOpenLayers(array $points_arr, $srid) + { + $ol_array = 'new Array('; + foreach ($points_arr as $point) { + $ol_array .= $this->getPointForOpenLayers($point, $srid) . ', '; + } + + $ol_array + = mb_substr( + $ol_array, + 0, + mb_strlen($ol_array) - 2 + ); + $ol_array .= ')'; + + return $ol_array; + } + + /** + * Generates JavaScript for adding a point to OpenLayers. + * + * @param array $point array containing the x and y coordinates of the point + * @param string $srid spatial reference id + * + * @return string JavaScript for adding points to OpenLayers + * @access protected + */ + protected function getPointForOpenLayers(array $point, $srid) + { + return '(new OpenLayers.Geometry.Point(' . $point[0] . ',' . $point[1] . '))' + . '.transform(new OpenLayers.Projection("EPSG:' + . intval($srid) . '"), map.getProjectionObject())'; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Gis/GisGeometryCollection.php b/srcs/phpmyadmin/libraries/classes/Gis/GisGeometryCollection.php new file mode 100644 index 0000000..dfd0cb9 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Gis/GisGeometryCollection.php @@ -0,0 +1,419 @@ +_explodeGeomCol($goem_col); + + foreach ($sub_parts as $sub_part) { + $type_pos = mb_strpos($sub_part, '('); + if ($type_pos === false) { + continue; + } + $type = mb_substr($sub_part, 0, $type_pos); + + $gis_obj = GisFactory::factory($type); + if (! $gis_obj) { + continue; + } + $scale_data = $gis_obj->scaleRow($sub_part); + + // Update minimum/maximum values for x and y coordinates. + $c_maxX = (float) $scale_data['maxX']; + if (! isset($min_max['maxX']) || $c_maxX > $min_max['maxX']) { + $min_max['maxX'] = $c_maxX; + } + + $c_minX = (float) $scale_data['minX']; + if (! isset($min_max['minX']) || $c_minX < $min_max['minX']) { + $min_max['minX'] = $c_minX; + } + + $c_maxY = (float) $scale_data['maxY']; + if (! isset($min_max['maxY']) || $c_maxY > $min_max['maxY']) { + $min_max['maxY'] = $c_maxY; + } + + $c_minY = (float) $scale_data['minY']; + if (! isset($min_max['minY']) || $c_minY < $min_max['minY']) { + $min_max['minY'] = $c_minY; + } + } + + return $min_max; + } + + /** + * 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 $color Color for the GIS POLYGON object + * @param array $scale_data Array containing data related to scaling + * @param resource $image Image object + * + * @return resource the modified image object + * @access public + */ + public function prepareRowAsPng($spatial, ?string $label, $color, array $scale_data, $image) + { + // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')' + $goem_col + = mb_substr( + $spatial, + 19, + mb_strlen($spatial) - 20 + ); + // Split the geometry collection object to get its constituents. + $sub_parts = $this->_explodeGeomCol($goem_col); + + foreach ($sub_parts as $sub_part) { + $type_pos = mb_strpos($sub_part, '('); + if ($type_pos === false) { + continue; + } + $type = mb_substr($sub_part, 0, $type_pos); + + $gis_obj = GisFactory::factory($type); + if (! $gis_obj) { + continue; + } + $image = $gis_obj->prepareRowAsPng( + $sub_part, + $label, + $color, + $scale_data, + $image + ); + } + + return $image; + } + + /** + * Adds to the TCPDF instance, the data related to a row in the GIS dataset. + * + * @param string $spatial GIS GEOMETRYCOLLECTION object + * @param string|null $label label for the GIS GEOMETRYCOLLECTION object + * @param string $color color for the GIS GEOMETRYCOLLECTION object + * @param array $scale_data array containing data related to scaling + * @param TCPDF $pdf TCPDF instance + * + * @return TCPDF the modified TCPDF instance + * @access public + */ + public function prepareRowAsPdf($spatial, ?string $label, $color, array $scale_data, $pdf) + { + // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')' + $goem_col + = mb_substr( + $spatial, + 19, + mb_strlen($spatial) - 20 + ); + // Split the geometry collection object to get its constituents. + $sub_parts = $this->_explodeGeomCol($goem_col); + + foreach ($sub_parts as $sub_part) { + $type_pos = mb_strpos($sub_part, '('); + if ($type_pos === false) { + continue; + } + $type = mb_substr($sub_part, 0, $type_pos); + + $gis_obj = GisFactory::factory($type); + if (! $gis_obj) { + continue; + } + $pdf = $gis_obj->prepareRowAsPdf( + $sub_part, + $label, + $color, + $scale_data, + $pdf + ); + } + + return $pdf; + } + + /** + * Prepares and returns the code related to a row in the GIS dataset as SVG. + * + * @param string $spatial GIS GEOMETRYCOLLECTION object + * @param string $label label for the GIS GEOMETRYCOLLECTION object + * @param string $color color for the GIS GEOMETRYCOLLECTION object + * @param array $scale_data array containing data related to scaling + * + * @return string the code related to a row in the GIS dataset + * @access public + */ + public function prepareRowAsSvg($spatial, $label, $color, array $scale_data) + { + $row = ''; + + // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')' + $goem_col + = mb_substr( + $spatial, + 19, + mb_strlen($spatial) - 20 + ); + // Split the geometry collection object to get its constituents. + $sub_parts = $this->_explodeGeomCol($goem_col); + + foreach ($sub_parts as $sub_part) { + $type_pos = mb_strpos($sub_part, '('); + if ($type_pos === false) { + continue; + } + $type = mb_substr($sub_part, 0, $type_pos); + + $gis_obj = GisFactory::factory($type); + if (! $gis_obj) { + continue; + } + $row .= $gis_obj->prepareRowAsSvg( + $sub_part, + $label, + $color, + $scale_data + ); + } + + return $row; + } + + /** + * Prepares JavaScript related to a row in the GIS dataset + * to visualize it with OpenLayers. + * + * @param string $spatial GIS GEOMETRYCOLLECTION object + * @param int $srid spatial reference ID + * @param string $label label for the GIS GEOMETRYCOLLECTION object + * @param string $color color for the GIS GEOMETRYCOLLECTION object + * @param array $scale_data array containing data related to scaling + * + * @return string JavaScript related to a row in the GIS dataset + * @access public + */ + public function prepareRowAsOl($spatial, $srid, $label, $color, array $scale_data) + { + $row = ''; + + // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')' + $goem_col + = mb_substr( + $spatial, + 19, + mb_strlen($spatial) - 20 + ); + // Split the geometry collection object to get its constituents. + $sub_parts = $this->_explodeGeomCol($goem_col); + + foreach ($sub_parts as $sub_part) { + $type_pos = mb_strpos($sub_part, '('); + if ($type_pos === false) { + continue; + } + $type = mb_substr($sub_part, 0, $type_pos); + + $gis_obj = GisFactory::factory($type); + if (! $gis_obj) { + continue; + } + $row .= $gis_obj->prepareRowAsOl( + $sub_part, + $srid, + $label, + $color, + $scale_data + ); + } + + return $row; + } + + /** + * Splits the GEOMETRYCOLLECTION object and get its constituents. + * + * @param string $geom_col geometry collection string + * + * @return array the constituents of the geometry collection object + * @access private + */ + private function _explodeGeomCol($geom_col) + { + $sub_parts = []; + $br_count = 0; + $start = 0; + $count = 0; + foreach (str_split($geom_col) as $char) { + if ($char == '(') { + $br_count++; + } elseif ($char == ')') { + $br_count--; + if ($br_count == 0) { + $sub_parts[] + = mb_substr( + $geom_col, + $start, + $count + 1 - $start + ); + $start = $count + 2; + } + } + $count++; + } + + return $sub_parts; + } + + /** + * Generates 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 $empty value for empty points + * + * @return string WKT with the set of parameters passed by the GIS editor + * @access public + */ + public function generateWkt(array $gis_data, $index, $empty = '') + { + $geom_count = isset($gis_data['GEOMETRYCOLLECTION']['geom_count']) + ? $gis_data['GEOMETRYCOLLECTION']['geom_count'] : 1; + $wkt = 'GEOMETRYCOLLECTION('; + for ($i = 0; $i < $geom_count; $i++) { + if (isset($gis_data[$i]['gis_type'])) { + $type = $gis_data[$i]['gis_type']; + $gis_obj = GisFactory::factory($type); + if (! $gis_obj) { + continue; + } + $wkt .= $gis_obj->generateWkt($gis_data, $i, $empty) . ','; + } + } + if (isset($gis_data[0]['gis_type'])) { + $wkt + = mb_substr( + $wkt, + 0, + mb_strlen($wkt) - 1 + ); + } + $wkt .= ')'; + + return $wkt; + } + + /** + * Generates parameters for the GIS data editor from the value of the GIS column. + * + * @param string $value of the GIS column + * + * @return array parameters for the GIS editor from the value of the GIS column + * @access public + */ + public function generateParams($value) + { + $params = []; + $data = GisGeometry::generateParams($value); + $params['srid'] = $data['srid']; + $wkt = $data['wkt']; + + // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')' + $goem_col + = mb_substr( + $wkt, + 19, + mb_strlen($wkt) - 20 + ); + // Split the geometry collection object to get its constituents. + $sub_parts = $this->_explodeGeomCol($goem_col); + $params['GEOMETRYCOLLECTION']['geom_count'] = count($sub_parts); + + $i = 0; + foreach ($sub_parts as $sub_part) { + $type_pos = mb_strpos($sub_part, '('); + if ($type_pos === false) { + continue; + } + $type = mb_substr($sub_part, 0, $type_pos); + /** + * @var GisMultiPolygon|GisPolygon|GisMultiPoint|GisPoint|GisMultiLineString|GisLineString $gis_obj + */ + $gis_obj = GisFactory::factory($type); + if (! $gis_obj) { + continue; + } + $params = array_merge($params, $gis_obj->generateParams($sub_part, $i)); + $i++; + } + + return $params; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Gis/GisLineString.php b/srcs/phpmyadmin/libraries/classes/Gis/GisLineString.php new file mode 100644 index 0000000..f309605 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Gis/GisLineString.php @@ -0,0 +1,360 @@ +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 + * @param resource $image Image object + * + * @return resource the modified image object + * @access public + */ + public function prepareRowAsPng( + $spatial, + ?string $label, + $line_color, + array $scale_data, + $image + ) { + // allocate colors + $black = imagecolorallocate($image, 0, 0, 0); + $red = hexdec(mb_substr($line_color, 1, 2)); + $green = hexdec(mb_substr($line_color, 3, 2)); + $blue = hexdec(mb_substr($line_color, 4, 2)); + $color = imagecolorallocate($image, $red, $green, $blue); + + // Trim to remove leading 'LINESTRING(' and trailing ')' + $linesrting + = mb_substr( + $spatial, + 11, + mb_strlen($spatial) - 12 + ); + $points_arr = $this->extractPoints($linesrting, $scale_data); + + foreach ($points_arr as $point) { + if (! isset($temp_point)) { + $temp_point = $point; + } else { + // draw line section + imageline( + $image, + $temp_point[0], + $temp_point[1], + $point[0], + $point[1], + $color + ); + $temp_point = $point; + } + } + // print label if applicable + if (isset($label) && trim($label) != '') { + imagestring( + $image, + 1, + $points_arr[1][0], + $points_arr[1][1], + trim($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 + * @access public + */ + 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, + ], + ]; + + // Trim to remove leading 'LINESTRING(' and trailing ')' + $linesrting + = mb_substr( + $spatial, + 11, + mb_strlen($spatial) - 12 + ); + $points_arr = $this->extractPoints($linesrting, $scale_data); + + foreach ($points_arr as $point) { + if (! isset($temp_point)) { + $temp_point = $point; + } else { + // draw line section + $pdf->Line( + $temp_point[0], + $temp_point[1], + $point[0], + $point[1], + $line + ); + $temp_point = $point; + } + } + // print label + if (isset($label) && trim($label) != '') { + $pdf->SetXY($points_arr[1][0], $points_arr[1][1]); + $pdf->SetFontSize(5); + $pdf->Cell(0, 0, trim($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 + * @access public + */ + public function prepareRowAsSvg($spatial, $label, $line_color, array $scale_data) + { + $line_options = [ + 'name' => $label, + 'id' => $label . mt_rand(), + 'class' => 'linestring vector', + 'fill' => 'none', + 'stroke' => $line_color, + 'stroke-width' => 2, + ]; + + // Trim to remove leading 'LINESTRING(' and trailing ')' + $linesrting + = mb_substr( + $spatial, + 11, + mb_strlen($spatial) - 12 + ); + $points_arr = $this->extractPoints($linesrting, $scale_data); + + $row = ' $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 string $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 + * @access public + */ + public function prepareRowAsOl($spatial, $srid, $label, $line_color, array $scale_data) + { + $style_options = [ + 'strokeColor' => $line_color, + 'strokeWidth' => 2, + 'label' => $label, + 'fontSize' => 10, + ]; + if ($srid == 0) { + $srid = 4326; + } + $result = $this->getBoundsForOl($srid, $scale_data); + + // Trim to remove leading 'LINESTRING(' and trailing ')' + $linesrting + = mb_substr( + $spatial, + 11, + mb_strlen($spatial) - 12 + ); + $points_arr = $this->extractPoints($linesrting, null); + + $result .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector(' + . $this->getLineForOpenLayers($points_arr, $srid) + . ', null, ' . json_encode($style_options) . '));'; + + return $result; + } + + /** + * 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 $empty Value for empty points + * + * @return string WKT with the set of parameters passed by the GIS editor + * @access public + */ + public function generateWkt(array $gis_data, $index, $empty = '') + { + $no_of_points = isset($gis_data[$index]['LINESTRING']['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, + mb_strlen($wkt) - 1 + ); + $wkt .= ')'; + + 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 + * @access public + */ + 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, + mb_strlen($wkt) - 12 + ); + $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; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Gis/GisMultiLineString.php b/srcs/phpmyadmin/libraries/classes/Gis/GisMultiLineString.php new file mode 100644 index 0000000..b4524f3 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Gis/GisMultiLineString.php @@ -0,0 +1,449 @@ +setMinMax($linestring, $min_max); + } + + return $min_max; + } + + /** + * 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 + * @param resource $image Image object + * + * @return resource the modified image object + * @access public + */ + public function prepareRowAsPng( + $spatial, + ?string $label, + $line_color, + array $scale_data, + $image + ) { + // allocate colors + $black = imagecolorallocate($image, 0, 0, 0); + $red = hexdec(mb_substr($line_color, 1, 2)); + $green = hexdec(mb_substr($line_color, 3, 2)); + $blue = hexdec(mb_substr($line_color, 4, 2)); + $color = imagecolorallocate($image, $red, $green, $blue); + + // Trim to remove leading 'MULTILINESTRING((' and trailing '))' + $multilinestirng + = mb_substr( + $spatial, + 17, + mb_strlen($spatial) - 19 + ); + // Separate each linestring + $linestirngs = explode("),(", $multilinestirng); + + $first_line = true; + foreach ($linestirngs as $linestring) { + $points_arr = $this->extractPoints($linestring, $scale_data); + foreach ($points_arr as $point) { + if (! isset($temp_point)) { + $temp_point = $point; + } else { + // draw line section + imageline( + $image, + $temp_point[0], + $temp_point[1], + $point[0], + $point[1], + $color + ); + $temp_point = $point; + } + } + unset($temp_point); + // print label if applicable + if (isset($label) && trim($label) != '' && $first_line) { + imagestring( + $image, + 1, + $points_arr[1][0], + $points_arr[1][1], + trim($label), + $black + ); + } + $first_line = false; + } + + return $image; + } + + /** + * Adds to the TCPDF instance, the data related to a row in the GIS dataset. + * + * @param string $spatial GIS MULTILINESTRING object + * @param string|null $label Label for the GIS MULTILINESTRING object + * @param string $line_color Color for the GIS MULTILINESTRING object + * @param array $scale_data Array containing data related to scaling + * @param TCPDF $pdf TCPDF instance + * + * @return TCPDF the modified TCPDF instance + * @access public + */ + 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, + ], + ]; + + // Trim to remove leading 'MULTILINESTRING((' and trailing '))' + $multilinestirng + = mb_substr( + $spatial, + 17, + mb_strlen($spatial) - 19 + ); + // Separate each linestring + $linestirngs = explode("),(", $multilinestirng); + + $first_line = true; + foreach ($linestirngs as $linestring) { + $points_arr = $this->extractPoints($linestring, $scale_data); + foreach ($points_arr as $point) { + if (! isset($temp_point)) { + $temp_point = $point; + } else { + // draw line section + $pdf->Line( + $temp_point[0], + $temp_point[1], + $point[0], + $point[1], + $line + ); + $temp_point = $point; + } + } + unset($temp_point); + // print label + if (isset($label) && trim($label) != '' && $first_line) { + $pdf->SetXY($points_arr[1][0], $points_arr[1][1]); + $pdf->SetFontSize(5); + $pdf->Cell(0, 0, trim($label)); + } + $first_line = false; + } + + return $pdf; + } + + /** + * Prepares and returns the code related to a row in the GIS dataset as SVG. + * + * @param string $spatial GIS MULTILINESTRING object + * @param string $label Label for the GIS MULTILINESTRING object + * @param string $line_color Color for the GIS MULTILINESTRING object + * @param array $scale_data Array containing data related to scaling + * + * @return string the code related to a row in the GIS dataset + * @access public + */ + public function prepareRowAsSvg($spatial, $label, $line_color, array $scale_data) + { + $line_options = [ + 'name' => $label, + 'class' => 'linestring vector', + 'fill' => 'none', + 'stroke' => $line_color, + 'stroke-width' => 2, + ]; + + // Trim to remove leading 'MULTILINESTRING((' and trailing '))' + $multilinestirng + = mb_substr( + $spatial, + 17, + mb_strlen($spatial) - 19 + ); + // Separate each linestring + $linestirngs = explode("),(", $multilinestirng); + + $row = ''; + foreach ($linestirngs as $linestring) { + $points_arr = $this->extractPoints($linestring, $scale_data); + + $row .= ' $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 MULTILINESTRING object + * @param int $srid Spatial reference ID + * @param string $label Label for the GIS MULTILINESTRING object + * @param string $line_color Color for the GIS MULTILINESTRING object + * @param array $scale_data Array containing data related to scaling + * + * @return string JavaScript related to a row in the GIS dataset + * @access public + */ + public function prepareRowAsOl($spatial, $srid, $label, $line_color, array $scale_data) + { + $style_options = [ + 'strokeColor' => $line_color, + 'strokeWidth' => 2, + 'label' => $label, + 'fontSize' => 10, + ]; + if ($srid == 0) { + $srid = 4326; + } + $row = $this->getBoundsForOl($srid, $scale_data); + + // Trim to remove leading 'MULTILINESTRING((' and trailing '))' + $multilinestirng + = mb_substr( + $spatial, + 17, + mb_strlen($spatial) - 19 + ); + // Separate each linestring + $linestirngs = explode("),(", $multilinestirng); + + $row .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector(' + . 'new OpenLayers.Geometry.MultiLineString(' + . $this->getLineArrayForOpenLayers($linestirngs, $srid) + . '), null, ' . json_encode($style_options) . '));'; + + return $row; + } + + /** + * 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 $empty Value for empty points + * + * @return string WKT with the set of parameters passed by the GIS editor + * @access public + */ + public function generateWkt(array $gis_data, $index, $empty = '') + { + $data_row = $gis_data[$index]['MULTILINESTRING']; + + $no_of_lines = isset($data_row['no_of_lines']) + ? $data_row['no_of_lines'] : 1; + if ($no_of_lines < 1) { + $no_of_lines = 1; + } + + $wkt = 'MULTILINESTRING('; + for ($i = 0; $i < $no_of_lines; $i++) { + $no_of_points = isset($data_row[$i]['no_of_points']) + ? $data_row[$i]['no_of_points'] : 2; + if ($no_of_points < 2) { + $no_of_points = 2; + } + $wkt .= '('; + for ($j = 0; $j < $no_of_points; $j++) { + $wkt .= ((isset($data_row[$i][$j]['x']) + && trim((string) $data_row[$i][$j]['x']) != '') + ? $data_row[$i][$j]['x'] : $empty) + . ' ' . ((isset($data_row[$i][$j]['y']) + && trim((string) $data_row[$i][$j]['y']) != '') + ? $data_row[$i][$j]['y'] : $empty) . ','; + } + $wkt + = mb_substr( + $wkt, + 0, + mb_strlen($wkt) - 1 + ); + $wkt .= '),'; + } + $wkt + = mb_substr( + $wkt, + 0, + mb_strlen($wkt) - 1 + ); + $wkt .= ')'; + + return $wkt; + } + + /** + * Generate the WKT for the data from ESRI shape files. + * + * @param array $row_data GIS data + * + * @return string the WKT for the data from ESRI shape files + * @access public + */ + public function getShape(array $row_data) + { + $wkt = 'MULTILINESTRING('; + for ($i = 0; $i < $row_data['numparts']; $i++) { + $wkt .= '('; + foreach ($row_data['parts'][$i]['points'] as $point) { + $wkt .= $point['x'] . ' ' . $point['y'] . ','; + } + $wkt + = mb_substr( + $wkt, + 0, + mb_strlen($wkt) - 1 + ); + $wkt .= '),'; + } + $wkt + = mb_substr( + $wkt, + 0, + mb_strlen($wkt) - 1 + ); + $wkt .= ')'; + + return $wkt; + } + + /** + * Generate parameters for the GIS data editor from the value of the GIS column. + * + * @param string $value Value of the GIS column + * @param int $index Index of the geometry + * + * @return array params for the GIS data editor from the value of the GIS column + * @access public + */ + 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'] = 'MULTILINESTRING'; + $wkt = $value; + } + + // Trim to remove leading 'MULTILINESTRING((' and trailing '))' + $multilinestirng + = mb_substr( + $wkt, + 17, + mb_strlen($wkt) - 19 + ); + // Separate each linestring + $linestirngs = explode("),(", $multilinestirng); + $params[$index]['MULTILINESTRING']['no_of_lines'] = count($linestirngs); + + $j = 0; + foreach ($linestirngs as $linestring) { + $points_arr = $this->extractPoints($linestring, null); + $no_of_points = count($points_arr); + $params[$index]['MULTILINESTRING'][$j]['no_of_points'] = $no_of_points; + for ($i = 0; $i < $no_of_points; $i++) { + $params[$index]['MULTILINESTRING'][$j][$i]['x'] = $points_arr[$i][0]; + $params[$index]['MULTILINESTRING'][$j][$i]['y'] = $points_arr[$i][1]; + } + $j++; + } + + return $params; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Gis/GisMultiPoint.php b/srcs/phpmyadmin/libraries/classes/Gis/GisMultiPoint.php new file mode 100644 index 0000000..8a6b500 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Gis/GisMultiPoint.php @@ -0,0 +1,416 @@ +setMinMax($multipoint, []); + } + + /** + * 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 $point_color Color for the GIS POLYGON object + * @param array $scale_data Array containing data related to scaling + * @param resource $image Image object + * + * @return resource the modified image object + * @access public + */ + public function prepareRowAsPng( + $spatial, + ?string $label, + $point_color, + array $scale_data, + $image + ) { + // allocate colors + $black = imagecolorallocate($image, 0, 0, 0); + $red = hexdec(mb_substr($point_color, 1, 2)); + $green = hexdec(mb_substr($point_color, 3, 2)); + $blue = hexdec(mb_substr($point_color, 4, 2)); + $color = imagecolorallocate($image, $red, $green, $blue); + + // Trim to remove leading 'MULTIPOINT(' and trailing ')' + $multipoint + = mb_substr( + $spatial, + 11, + mb_strlen($spatial) - 12 + ); + $points_arr = $this->extractPoints($multipoint, $scale_data); + + foreach ($points_arr as $point) { + // draw a small circle to mark the point + if ($point[0] != '' && $point[1] != '') { + imagearc($image, $point[0], $point[1], 7, 7, 0, 360, $color); + } + } + // print label for each point + if ((isset($label) && trim($label) != '') + && ($points_arr[0][0] != '' && $points_arr[0][1] != '') + ) { + imagestring( + $image, + 1, + $points_arr[0][0], + $points_arr[0][1], + trim($label), + $black + ); + } + + return $image; + } + + /** + * Adds to the TCPDF instance, the data related to a row in the GIS dataset. + * + * @param string $spatial GIS MULTIPOINT object + * @param string|null $label Label for the GIS MULTIPOINT object + * @param string $point_color Color for the GIS MULTIPOINT object + * @param array $scale_data Array containing data related to scaling + * @param TCPDF $pdf TCPDF instance + * + * @return TCPDF the modified TCPDF instance + * @access public + */ + public function prepareRowAsPdf( + $spatial, + ?string $label, + $point_color, + array $scale_data, + $pdf + ) { + // allocate colors + $red = hexdec(mb_substr($point_color, 1, 2)); + $green = hexdec(mb_substr($point_color, 3, 2)); + $blue = hexdec(mb_substr($point_color, 4, 2)); + $line = [ + 'width' => 1.25, + 'color' => [ + $red, + $green, + $blue, + ], + ]; + + // Trim to remove leading 'MULTIPOINT(' and trailing ')' + $multipoint + = mb_substr( + $spatial, + 11, + mb_strlen($spatial) - 12 + ); + $points_arr = $this->extractPoints($multipoint, $scale_data); + + foreach ($points_arr as $point) { + // draw a small circle to mark the point + if ($point[0] != '' && $point[1] != '') { + $pdf->Circle($point[0], $point[1], 2, 0, 360, 'D', $line); + } + } + // print label for each point + if ((isset($label) && trim($label) != '') + && ($points_arr[0][0] != '' && $points_arr[0][1] != '') + ) { + $pdf->SetXY($points_arr[0][0], $points_arr[0][1]); + $pdf->SetFontSize(5); + $pdf->Cell(0, 0, trim($label)); + } + + return $pdf; + } + + /** + * Prepares and returns the code related to a row in the GIS dataset as SVG. + * + * @param string $spatial GIS MULTIPOINT object + * @param string $label Label for the GIS MULTIPOINT object + * @param string $point_color Color for the GIS MULTIPOINT object + * @param array $scale_data Array containing data related to scaling + * + * @return string the code related to a row in the GIS dataset + * @access public + */ + public function prepareRowAsSvg($spatial, $label, $point_color, array $scale_data) + { + $point_options = [ + 'name' => $label, + 'class' => 'multipoint vector', + 'fill' => 'white', + 'stroke' => $point_color, + 'stroke-width' => 2, + ]; + + // Trim to remove leading 'MULTIPOINT(' and trailing ')' + $multipoint + = mb_substr( + $spatial, + 11, + mb_strlen($spatial) - 12 + ); + $points_arr = $this->extractPoints($multipoint, $scale_data); + + $row = ''; + foreach ($points_arr as $point) { + if ($point[0] != '' && $point[1] != '') { + $row .= ' $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 MULTIPOINT object + * @param int $srid Spatial reference ID + * @param string $label Label for the GIS MULTIPOINT object + * @param string $point_color Color for the GIS MULTIPOINT object + * @param array $scale_data Array containing data related to scaling + * + * @return string JavaScript related to a row in the GIS dataset + * @access public + */ + public function prepareRowAsOl( + $spatial, + $srid, + $label, + $point_color, + array $scale_data + ) { + $style_options = [ + 'pointRadius' => 3, + 'fillColor' => '#ffffff', + 'strokeColor' => $point_color, + 'strokeWidth' => 2, + 'label' => $label, + 'labelYOffset' => -8, + 'fontSize' => 10, + ]; + if ($srid == 0) { + $srid = 4326; + } + $result = $this->getBoundsForOl($srid, $scale_data); + + // Trim to remove leading 'MULTIPOINT(' and trailing ')' + $multipoint + = mb_substr( + $spatial, + 11, + mb_strlen($spatial) - 12 + ); + $points_arr = $this->extractPoints($multipoint, null); + + $result .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector(' + . 'new OpenLayers.Geometry.MultiPoint(' + . $this->getPointsArrayForOpenLayers($points_arr, $srid) + . '), null, ' . json_encode($style_options) . '));'; + + return $result; + } + + /** + * 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 $empty Multipoint does not adhere to this + * + * @return string WKT with the set of parameters passed by the GIS editor + * @access public + */ + public function generateWkt(array $gis_data, $index, $empty = '') + { + $no_of_points = isset($gis_data[$index]['MULTIPOINT']['no_of_points']) + ? $gis_data[$index]['MULTIPOINT']['no_of_points'] : 1; + if ($no_of_points < 1) { + $no_of_points = 1; + } + $wkt = 'MULTIPOINT('; + for ($i = 0; $i < $no_of_points; $i++) { + $wkt .= ((isset($gis_data[$index]['MULTIPOINT'][$i]['x']) + && trim((string) $gis_data[$index]['MULTIPOINT'][$i]['x']) != '') + ? $gis_data[$index]['MULTIPOINT'][$i]['x'] : '') + . ' ' . ((isset($gis_data[$index]['MULTIPOINT'][$i]['y']) + && trim((string) $gis_data[$index]['MULTIPOINT'][$i]['y']) != '') + ? $gis_data[$index]['MULTIPOINT'][$i]['y'] : '') . ','; + } + + $wkt + = mb_substr( + $wkt, + 0, + mb_strlen($wkt) - 1 + ); + $wkt .= ')'; + + return $wkt; + } + + /** + * Generate the WKT for the data from ESRI shape files. + * + * @param array $row_data GIS data + * + * @return string the WKT for the data from ESRI shape files + * @access public + */ + public function getShape(array $row_data) + { + $wkt = 'MULTIPOINT('; + for ($i = 0; $i < $row_data['numpoints']; $i++) { + $wkt .= $row_data['points'][$i]['x'] . ' ' + . $row_data['points'][$i]['y'] . ','; + } + + $wkt + = mb_substr( + $wkt, + 0, + mb_strlen($wkt) - 1 + ); + $wkt .= ')'; + + return $wkt; + } + + /** + * Generate parameters for the GIS data editor from the value of the GIS column. + * + * @param string $value Value of the GIS column + * @param integer $index Index of the geometry + * + * @return array params for the GIS data editor from the value of the GIS column + * @access public + */ + 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'] = 'MULTIPOINT'; + $wkt = $value; + } + + // Trim to remove leading 'MULTIPOINT(' and trailing ')' + $points + = mb_substr( + $wkt, + 11, + mb_strlen($wkt) - 12 + ); + $points_arr = $this->extractPoints($points, null); + + $no_of_points = count($points_arr); + $params[$index]['MULTIPOINT']['no_of_points'] = $no_of_points; + for ($i = 0; $i < $no_of_points; $i++) { + $params[$index]['MULTIPOINT'][$i]['x'] = $points_arr[$i][0]; + $params[$index]['MULTIPOINT'][$i]['y'] = $points_arr[$i][1]; + } + + return $params; + } + + /** + * Overridden to make sure that only the points having valid values + * for x and y coordinates are added. + * + * @param array $points_arr x and y coordinates for each point + * @param string $srid spatial reference id + * + * @return string JavaScript for adding an array of points to OpenLayers + * @access protected + */ + protected function getPointsArrayForOpenLayers(array $points_arr, $srid) + { + $ol_array = 'new Array('; + foreach ($points_arr as $point) { + if ($point[0] != '' && $point[1] != '') { + $ol_array .= $this->getPointForOpenLayers($point, $srid) . ', '; + } + } + + $olArrayLength = mb_strlen($ol_array); + if (mb_substr($ol_array, $olArrayLength - 2) == ', ') { + $ol_array = mb_substr($ol_array, 0, $olArrayLength - 2); + } + $ol_array .= ')'; + + return $ol_array; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Gis/GisMultiPolygon.php b/srcs/phpmyadmin/libraries/classes/Gis/GisMultiPolygon.php new file mode 100644 index 0000000..3d2449c --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Gis/GisMultiPolygon.php @@ -0,0 +1,617 @@ +setMinMax($ring, $min_max); + } + + return $min_max; + } + + /** + * 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 $fill_color Color for the GIS POLYGON object + * @param array $scale_data Array containing data related to scaling + * @param resource $image Image object + * + * @return resource the modified image object + * @access public + */ + public function prepareRowAsPng( + $spatial, + ?string $label, + $fill_color, + array $scale_data, + $image + ) { + // allocate colors + $black = imagecolorallocate($image, 0, 0, 0); + $red = hexdec(mb_substr($fill_color, 1, 2)); + $green = hexdec(mb_substr($fill_color, 3, 2)); + $blue = hexdec(mb_substr($fill_color, 4, 2)); + $color = imagecolorallocate($image, $red, $green, $blue); + + // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))' + $multipolygon + = mb_substr( + $spatial, + 15, + mb_strlen($spatial) - 18 + ); + // Separate each polygon + $polygons = explode(")),((", $multipolygon); + + $first_poly = true; + $points_arr = []; + foreach ($polygons as $polygon) { + // If the polygon doesn't have an inner polygon + if (mb_strpos($polygon, "),(") === false) { + $points_arr = $this->extractPoints($polygon, $scale_data, true); + } else { + // Separate outer and inner polygons + $parts = explode("),(", $polygon); + $outer = $parts[0]; + $inner = array_slice($parts, 1); + + $points_arr = $this->extractPoints($outer, $scale_data, true); + + foreach ($inner as $inner_poly) { + $points_arr = array_merge( + $points_arr, + $this->extractPoints($inner_poly, $scale_data, true) + ); + } + } + // draw polygon + imagefilledpolygon($image, $points_arr, count($points_arr) / 2, $color); + // mark label point if applicable + if (isset($label) && trim($label) != '' && $first_poly) { + $label_point = [ + $points_arr[2], + $points_arr[3], + ]; + } + $first_poly = false; + } + // print label if applicable + if (isset($label_point)) { + imagestring( + $image, + 1, + $points_arr[2], + $points_arr[3], + trim($label), + $black + ); + } + + return $image; + } + + /** + * Adds to the TCPDF instance, the data related to a row in the GIS dataset. + * + * @param string $spatial GIS MULTIPOLYGON object + * @param string|null $label Label for the GIS MULTIPOLYGON object + * @param string $fill_color Color for the GIS MULTIPOLYGON object + * @param array $scale_data Array containing data related to scaling + * @param TCPDF $pdf TCPDF instance + * + * @return TCPDF the modified TCPDF instance + * @access public + */ + public function prepareRowAsPdf($spatial, ?string $label, $fill_color, array $scale_data, $pdf) + { + // allocate colors + $red = hexdec(mb_substr($fill_color, 1, 2)); + $green = hexdec(mb_substr($fill_color, 3, 2)); + $blue = hexdec(mb_substr($fill_color, 4, 2)); + $color = [ + $red, + $green, + $blue, + ]; + + // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))' + $multipolygon + = mb_substr( + $spatial, + 15, + mb_strlen($spatial) - 18 + ); + // Separate each polygon + $polygons = explode(")),((", $multipolygon); + + $first_poly = true; + foreach ($polygons as $polygon) { + // If the polygon doesn't have an inner polygon + if (mb_strpos($polygon, "),(") === false) { + $points_arr = $this->extractPoints($polygon, $scale_data, true); + } else { + // Separate outer and inner polygons + $parts = explode("),(", $polygon); + $outer = $parts[0]; + $inner = array_slice($parts, 1); + + $points_arr = $this->extractPoints($outer, $scale_data, true); + + foreach ($inner as $inner_poly) { + $points_arr = array_merge( + $points_arr, + $this->extractPoints($inner_poly, $scale_data, true) + ); + } + } + // draw polygon + $pdf->Polygon($points_arr, 'F*', [], $color, true); + // mark label point if applicable + if (isset($label) && trim($label) != '' && $first_poly) { + $label_point = [ + $points_arr[2], + $points_arr[3], + ]; + } + $first_poly = false; + } + + // print label if applicable + if (isset($label_point)) { + $pdf->SetXY($label_point[0], $label_point[1]); + $pdf->SetFontSize(5); + $pdf->Cell(0, 0, trim($label)); + } + + return $pdf; + } + + /** + * Prepares and returns the code related to a row in the GIS dataset as SVG. + * + * @param string $spatial GIS MULTIPOLYGON object + * @param string $label Label for the GIS MULTIPOLYGON object + * @param string $fill_color Color for the GIS MULTIPOLYGON object + * @param array $scale_data Array containing data related to scaling + * + * @return string the code related to a row in the GIS dataset + * @access public + */ + public function prepareRowAsSvg($spatial, $label, $fill_color, array $scale_data) + { + $polygon_options = [ + 'name' => $label, + 'class' => 'multipolygon vector', + 'stroke' => 'black', + 'stroke-width' => 0.5, + 'fill' => $fill_color, + 'fill-rule' => 'evenodd', + 'fill-opacity' => 0.8, + ]; + + $row = ''; + + // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))' + $multipolygon + = mb_substr( + $spatial, + 15, + mb_strlen($spatial) - 18 + ); + // Separate each polygon + $polygons = explode(")),((", $multipolygon); + + foreach ($polygons as $polygon) { + $row .= '_drawPath($polygon, $scale_data); + } else { + // Separate outer and inner polygons + $parts = explode("),(", $polygon); + $outer = $parts[0]; + $inner = array_slice($parts, 1); + + $row .= $this->_drawPath($outer, $scale_data); + + foreach ($inner as $inner_poly) { + $row .= $this->_drawPath($inner_poly, $scale_data); + } + } + $polygon_options['id'] = $label . mt_rand(); + $row .= '"'; + foreach ($polygon_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 MULTIPOLYGON object + * @param int $srid Spatial reference ID + * @param string $label Label for the GIS MULTIPOLYGON object + * @param string $fill_color Color for the GIS MULTIPOLYGON object + * @param array $scale_data Array containing data related to scaling + * + * @return string JavaScript related to a row in the GIS dataset + * @access public + */ + public function prepareRowAsOl($spatial, $srid, $label, $fill_color, array $scale_data) + { + $style_options = [ + 'strokeColor' => '#000000', + 'strokeWidth' => 0.5, + 'fillColor' => $fill_color, + 'fillOpacity' => 0.8, + 'label' => $label, + 'fontSize' => 10, + ]; + if ($srid == 0) { + $srid = 4326; + } + $row = $this->getBoundsForOl($srid, $scale_data); + + // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))' + $multipolygon + = mb_substr( + $spatial, + 15, + mb_strlen($spatial) - 18 + ); + // Separate each polygon + $polygons = explode(")),((", $multipolygon); + + $row .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector(' + . 'new OpenLayers.Geometry.MultiPolygon(' + . $this->getPolygonArrayForOpenLayers($polygons, $srid) + . '), null, ' . json_encode($style_options) . '));'; + + return $row; + } + + /** + * Draws a ring of the polygon using SVG path element. + * + * @param string $polygon The ring + * @param array $scale_data Array containing data related to scaling + * + * @return string the code to draw the ring + * @access private + */ + private function _drawPath($polygon, array $scale_data) + { + $points_arr = $this->extractPoints($polygon, $scale_data); + + $row = ' M ' . $points_arr[0][0] . ', ' . $points_arr[0][1]; + $other_points = array_slice($points_arr, 1, count($points_arr) - 2); + foreach ($other_points as $point) { + $row .= ' L ' . $point[0] . ', ' . $point[1]; + } + $row .= ' Z '; + + return $row; + } + + /** + * 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 $empty Value for empty points + * + * @return string WKT with the set of parameters passed by the GIS editor + * @access public + */ + public function generateWkt(array $gis_data, $index, $empty = '') + { + $data_row = $gis_data[$index]['MULTIPOLYGON']; + + $no_of_polygons = isset($data_row['no_of_polygons']) + ? $data_row['no_of_polygons'] : 1; + if ($no_of_polygons < 1) { + $no_of_polygons = 1; + } + + $wkt = 'MULTIPOLYGON('; + for ($k = 0; $k < $no_of_polygons; $k++) { + $no_of_lines = isset($data_row[$k]['no_of_lines']) + ? $data_row[$k]['no_of_lines'] : 1; + if ($no_of_lines < 1) { + $no_of_lines = 1; + } + $wkt .= '('; + for ($i = 0; $i < $no_of_lines; $i++) { + $no_of_points = isset($data_row[$k][$i]['no_of_points']) + ? $data_row[$k][$i]['no_of_points'] : 4; + if ($no_of_points < 4) { + $no_of_points = 4; + } + $wkt .= '('; + for ($j = 0; $j < $no_of_points; $j++) { + $wkt .= ((isset($data_row[$k][$i][$j]['x']) + && trim((string) $data_row[$k][$i][$j]['x']) != '') + ? $data_row[$k][$i][$j]['x'] : $empty) + . ' ' . ((isset($data_row[$k][$i][$j]['y']) + && trim((string) $data_row[$k][$i][$j]['y']) != '') + ? $data_row[$k][$i][$j]['y'] : $empty) . ','; + } + $wkt + = mb_substr( + $wkt, + 0, + mb_strlen($wkt) - 1 + ); + $wkt .= '),'; + } + $wkt + = mb_substr( + $wkt, + 0, + mb_strlen($wkt) - 1 + ); + $wkt .= '),'; + } + $wkt + = mb_substr( + $wkt, + 0, + mb_strlen($wkt) - 1 + ); + $wkt .= ')'; + + return $wkt; + } + + /** + * Generate the WKT for the data from ESRI shape files. + * + * @param array $row_data GIS data + * + * @return string the WKT for the data from ESRI shape files + * @access public + */ + public function getShape(array $row_data) + { + // Determines whether each line ring is an inner ring or an outer ring. + // If it's an inner ring get a point on the surface which can be used to + // correctly classify inner rings to their respective outer rings. + foreach ($row_data['parts'] as $i => $ring) { + $row_data['parts'][$i]['isOuter'] + = GisPolygon::isOuterRing($ring['points']); + } + + // Find points on surface for inner rings + foreach ($row_data['parts'] as $i => $ring) { + if (! $ring['isOuter']) { + $row_data['parts'][$i]['pointOnSurface'] + = GisPolygon::getPointOnSurface($ring['points']); + } + } + + // Classify inner rings to their respective outer rings. + foreach ($row_data['parts'] as $j => $ring1) { + if ($ring1['isOuter']) { + continue; + } + foreach ($row_data['parts'] as $k => $ring2) { + if (! $ring2['isOuter']) { + continue; + } + + // If the pointOnSurface of the inner ring + // is also inside the outer ring + if (GisPolygon::isPointInsidePolygon( + $ring1['pointOnSurface'], + $ring2['points'] + ) + ) { + if (! isset($ring2['inner'])) { + $row_data['parts'][$k]['inner'] = []; + } + $row_data['parts'][$k]['inner'][] = $j; + } + } + } + + $wkt = 'MULTIPOLYGON('; + // for each polygon + foreach ($row_data['parts'] as $ring) { + if (! $ring['isOuter']) { + continue; + } + + $wkt .= '('; // start of polygon + + $wkt .= '('; // start of outer ring + foreach ($ring['points'] as $point) { + $wkt .= $point['x'] . ' ' . $point['y'] . ','; + } + $wkt + = mb_substr( + $wkt, + 0, + mb_strlen($wkt) - 1 + ); + $wkt .= ')'; // end of outer ring + + // inner rings if any + if (isset($ring['inner'])) { + foreach ($ring['inner'] as $j) { + $wkt .= ',('; // start of inner ring + foreach ($row_data['parts'][$j]['points'] as $innerPoint) { + $wkt .= $innerPoint['x'] . ' ' . $innerPoint['y'] . ','; + } + $wkt + = mb_substr( + $wkt, + 0, + mb_strlen($wkt) - 1 + ); + $wkt .= ')'; // end of inner ring + } + } + + $wkt .= '),'; // end of polygon + } + $wkt + = mb_substr( + $wkt, + 0, + mb_strlen($wkt) - 1 + ); + + $wkt .= ')'; // end of multipolygon + return $wkt; + } + + /** + * Generate parameters for the GIS data editor from the value of the GIS column. + * + * @param string $value Value of the GIS column + * @param int $index Index of the geometry + * + * @return array params for the GIS data editor from the value of the GIS column + * @access public + */ + 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'] = 'MULTIPOLYGON'; + $wkt = $value; + } + + // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))' + $multipolygon + = mb_substr( + $wkt, + 15, + mb_strlen($wkt) - 18 + ); + // Separate each polygon + $polygons = explode(")),((", $multipolygon); + + $param_row =& $params[$index]['MULTIPOLYGON']; + $param_row['no_of_polygons'] = count($polygons); + + $k = 0; + foreach ($polygons as $polygon) { + // If the polygon doesn't have an inner polygon + if (mb_strpos($polygon, "),(") === false) { + $param_row[$k]['no_of_lines'] = 1; + $points_arr = $this->extractPoints($polygon, null); + $no_of_points = count($points_arr); + $param_row[$k][0]['no_of_points'] = $no_of_points; + for ($i = 0; $i < $no_of_points; $i++) { + $param_row[$k][0][$i]['x'] = $points_arr[$i][0]; + $param_row[$k][0][$i]['y'] = $points_arr[$i][1]; + } + } else { + // Separate outer and inner polygons + $parts = explode("),(", $polygon); + $param_row[$k]['no_of_lines'] = count($parts); + $j = 0; + foreach ($parts as $ring) { + $points_arr = $this->extractPoints($ring, null); + $no_of_points = count($points_arr); + $param_row[$k][$j]['no_of_points'] = $no_of_points; + for ($i = 0; $i < $no_of_points; $i++) { + $param_row[$k][$j][$i]['x'] = $points_arr[$i][0]; + $param_row[$k][$j][$i]['y'] = $points_arr[$i][1]; + } + $j++; + } + } + $k++; + } + + return $params; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Gis/GisPoint.php b/srcs/phpmyadmin/libraries/classes/Gis/GisPoint.php new file mode 100644 index 0000000..f11b0c8 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Gis/GisPoint.php @@ -0,0 +1,363 @@ +setMinMax($point, []); + } + + /** + * 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 $point_color Color for the GIS POLYGON object + * @param array $scale_data Array containing data related to scaling + * @param resource $image Image object + * + * @return resource the modified image object + * @access public + */ + public function prepareRowAsPng( + $spatial, + ?string $label, + $point_color, + array $scale_data, + $image + ) { + // allocate colors + $black = imagecolorallocate($image, 0, 0, 0); + $red = hexdec(mb_substr($point_color, 1, 2)); + $green = hexdec(mb_substr($point_color, 3, 2)); + $blue = hexdec(mb_substr($point_color, 4, 2)); + $color = imagecolorallocate($image, $red, $green, $blue); + + // Trim to remove leading 'POINT(' and trailing ')' + $point + = mb_substr( + $spatial, + 6, + mb_strlen($spatial) - 7 + ); + $points_arr = $this->extractPoints($point, $scale_data); + + // draw a small circle to mark the point + if ($points_arr[0][0] != '' && $points_arr[0][1] != '') { + imagearc( + $image, + $points_arr[0][0], + $points_arr[0][1], + 7, + 7, + 0, + 360, + $color + ); + // print label if applicable + if (isset($label) && trim($label) != '') { + imagestring( + $image, + 1, + $points_arr[0][0], + $points_arr[0][1], + trim($label), + $black + ); + } + } + + return $image; + } + + /** + * Adds to the TCPDF instance, the data related to a row in the GIS dataset. + * + * @param string $spatial GIS POINT object + * @param string|null $label Label for the GIS POINT object + * @param string $point_color Color for the GIS POINT object + * @param array $scale_data Array containing data related to scaling + * @param TCPDF $pdf TCPDF instance + * + * @return TCPDF the modified TCPDF instance + * @access public + */ + public function prepareRowAsPdf( + $spatial, + ?string $label, + $point_color, + array $scale_data, + $pdf + ) { + // allocate colors + $red = hexdec(mb_substr($point_color, 1, 2)); + $green = hexdec(mb_substr($point_color, 3, 2)); + $blue = hexdec(mb_substr($point_color, 4, 2)); + $line = [ + 'width' => 1.25, + 'color' => [ + $red, + $green, + $blue, + ], + ]; + + // Trim to remove leading 'POINT(' and trailing ')' + $point + = mb_substr( + $spatial, + 6, + mb_strlen($spatial) - 7 + ); + $points_arr = $this->extractPoints($point, $scale_data); + + // draw a small circle to mark the point + if ($points_arr[0][0] != '' && $points_arr[0][1] != '') { + $pdf->Circle( + $points_arr[0][0], + $points_arr[0][1], + 2, + 0, + 360, + 'D', + $line + ); + // print label if applicable + if (isset($label) && trim($label) != '') { + $pdf->SetXY($points_arr[0][0], $points_arr[0][1]); + $pdf->SetFontSize(5); + $pdf->Cell(0, 0, trim($label)); + } + } + + return $pdf; + } + + /** + * Prepares and returns the code related to a row in the GIS dataset as SVG. + * + * @param string $spatial GIS POINT object + * @param string $label Label for the GIS POINT object + * @param string $point_color Color for the GIS POINT object + * @param array $scale_data Array containing data related to scaling + * + * @return string the code related to a row in the GIS dataset + * @access public + */ + public function prepareRowAsSvg($spatial, $label, $point_color, array $scale_data) + { + $point_options = [ + 'name' => $label, + 'id' => $label . mt_rand(), + 'class' => 'point vector', + 'fill' => 'white', + 'stroke' => $point_color, + 'stroke-width' => 2, + ]; + + // Trim to remove leading 'POINT(' and trailing ')' + $point + = mb_substr( + $spatial, + 6, + mb_strlen($spatial) - 7 + ); + $points_arr = $this->extractPoints($point, $scale_data); + + $row = ''; + if ($points_arr[0][0] != '' && $points_arr[0][1] != '') { + $row .= ' $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 POINT object + * @param int $srid Spatial reference ID + * @param string $label Label for the GIS POINT object + * @param string $point_color Color for the GIS POINT object + * @param array $scale_data Array containing data related to scaling + * + * @return string JavaScript related to a row in the GIS dataset + * @access public + */ + public function prepareRowAsOl( + $spatial, + $srid, + $label, + $point_color, + array $scale_data + ) { + $style_options = [ + 'pointRadius' => 3, + 'fillColor' => '#ffffff', + 'strokeColor' => $point_color, + 'strokeWidth' => 2, + 'label' => $label, + 'labelYOffset' => -8, + 'fontSize' => 10, + ]; + if ($srid == 0) { + $srid = 4326; + } + $result = $this->getBoundsForOl($srid, $scale_data); + + // Trim to remove leading 'POINT(' and trailing ')' + $point + = mb_substr( + $spatial, + 6, + mb_strlen($spatial) - 7 + ); + $points_arr = $this->extractPoints($point, null); + + if ($points_arr[0][0] != '' && $points_arr[0][1] != '') { + $result .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector(' + . $this->getPointForOpenLayers($points_arr[0], $srid) . ', null, ' + . json_encode($style_options) . '));'; + } + + return $result; + } + + /** + * 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 $empty Point does not adhere to this parameter + * + * @return string WKT with the set of parameters passed by the GIS editor + * @access public + */ + public function generateWkt(array $gis_data, $index, $empty = '') + { + return 'POINT(' + . ((isset($gis_data[$index]['POINT']['x']) + && trim((string) $gis_data[$index]['POINT']['x']) != '') + ? $gis_data[$index]['POINT']['x'] : '') + . ' ' + . ((isset($gis_data[$index]['POINT']['y']) + && trim((string) $gis_data[$index]['POINT']['y']) != '') + ? $gis_data[$index]['POINT']['y'] : '') . ')'; + } + + /** + * Generate the WKT for the data from ESRI shape files. + * + * @param array $row_data GIS data + * + * @return string the WKT for the data from ESRI shape files + * @access public + */ + public function getShape(array $row_data) + { + return 'POINT(' . (isset($row_data['x']) ? $row_data['x'] : '') + . ' ' . (isset($row_data['y']) ? $row_data['y'] : '') . ')'; + } + + /** + * 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 + * @access public + */ + 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'] = 'POINT'; + $wkt = $value; + } + + // Trim to remove leading 'POINT(' and trailing ')' + $point + = mb_substr( + $wkt, + 6, + mb_strlen($wkt) - 7 + ); + $points_arr = $this->extractPoints($point, null); + + $params[$index]['POINT']['x'] = $points_arr[0][0]; + $params[$index]['POINT']['y'] = $points_arr[0][1]; + + return $params; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Gis/GisPolygon.php b/srcs/phpmyadmin/libraries/classes/Gis/GisPolygon.php new file mode 100644 index 0000000..b44da07 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Gis/GisPolygon.php @@ -0,0 +1,618 @@ +setMinMax($ring, []); + } + + /** + * 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 $fill_color Color for the GIS POLYGON object + * @param array $scale_data Array containing data related to scaling + * @param resource $image Image object + * + * @return resource the modified image object + * @access public + */ + public function prepareRowAsPng( + $spatial, + ?string $label, + $fill_color, + array $scale_data, + $image + ) { + // allocate colors + $black = imagecolorallocate($image, 0, 0, 0); + $red = hexdec(mb_substr($fill_color, 1, 2)); + $green = hexdec(mb_substr($fill_color, 3, 2)); + $blue = hexdec(mb_substr($fill_color, 4, 2)); + $color = imagecolorallocate($image, $red, $green, $blue); + + // Trim to remove leading 'POLYGON((' and trailing '))' + $polygon = mb_substr( + $spatial, + 9, + mb_strlen($spatial) - 11 + ); + + // If the polygon doesn't have an inner polygon + if (mb_strpos($polygon, "),(") === false) { + $points_arr = $this->extractPoints($polygon, $scale_data, true); + } else { + // Separate outer and inner polygons + $parts = explode("),(", $polygon); + $outer = $parts[0]; + $inner = array_slice($parts, 1); + + $points_arr = $this->extractPoints($outer, $scale_data, true); + + foreach ($inner as $inner_poly) { + $points_arr = array_merge( + $points_arr, + $this->extractPoints($inner_poly, $scale_data, true) + ); + } + } + + // draw polygon + imagefilledpolygon($image, $points_arr, count($points_arr) / 2, $color); + // print label if applicable + if (isset($label) && trim($label) != '') { + imagestring( + $image, + 1, + $points_arr[2], + $points_arr[3], + trim($label), + $black + ); + } + + return $image; + } + + /** + * Adds to the TCPDF instance, 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 $fill_color Color for the GIS POLYGON object + * @param array $scale_data Array containing data related to scaling + * @param TCPDF $pdf TCPDF instance + * + * @return TCPDF the modified TCPDF instance + * @access public + */ + public function prepareRowAsPdf($spatial, ?string $label, $fill_color, array $scale_data, $pdf) + { + // allocate colors + $red = hexdec(mb_substr($fill_color, 1, 2)); + $green = hexdec(mb_substr($fill_color, 3, 2)); + $blue = hexdec(mb_substr($fill_color, 4, 2)); + $color = [ + $red, + $green, + $blue, + ]; + + // Trim to remove leading 'POLYGON((' and trailing '))' + $polygon = mb_substr( + $spatial, + 9, + mb_strlen($spatial) - 11 + ); + + // If the polygon doesn't have an inner polygon + if (mb_strpos($polygon, "),(") === false) { + $points_arr = $this->extractPoints($polygon, $scale_data, true); + } else { + // Separate outer and inner polygons + $parts = explode("),(", $polygon); + $outer = $parts[0]; + $inner = array_slice($parts, 1); + + $points_arr = $this->extractPoints($outer, $scale_data, true); + + foreach ($inner as $inner_poly) { + $points_arr = array_merge( + $points_arr, + $this->extractPoints($inner_poly, $scale_data, true) + ); + } + } + + // draw polygon + $pdf->Polygon($points_arr, 'F*', [], $color, true); + // print label if applicable + if (isset($label) && trim($label) != '') { + $pdf->SetXY($points_arr[2], $points_arr[3]); + $pdf->SetFontSize(5); + $pdf->Cell(0, 0, trim($label)); + } + + return $pdf; + } + + /** + * Prepares and returns the code related to a row in the GIS dataset as SVG. + * + * @param string $spatial GIS POLYGON object + * @param string $label Label for the GIS POLYGON object + * @param string $fill_color Color for the GIS POLYGON object + * @param array $scale_data Array containing data related to scaling + * + * @return string the code related to a row in the GIS dataset + * @access public + */ + public function prepareRowAsSvg($spatial, $label, $fill_color, array $scale_data) + { + $polygon_options = [ + 'name' => $label, + 'id' => $label . mt_rand(), + 'class' => 'polygon vector', + 'stroke' => 'black', + 'stroke-width' => 0.5, + 'fill' => $fill_color, + 'fill-rule' => 'evenodd', + 'fill-opacity' => 0.8, + ]; + + // Trim to remove leading 'POLYGON((' and trailing '))' + $polygon + = mb_substr( + $spatial, + 9, + mb_strlen($spatial) - 11 + ); + + $row = '_drawPath($polygon, $scale_data); + } else { + // Separate outer and inner polygons + $parts = explode("),(", $polygon); + $outer = $parts[0]; + $inner = array_slice($parts, 1); + + $row .= $this->_drawPath($outer, $scale_data); + + foreach ($inner as $inner_poly) { + $row .= $this->_drawPath($inner_poly, $scale_data); + } + } + + $row .= '"'; + foreach ($polygon_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 POLYGON object + * @param int $srid Spatial reference ID + * @param string $label Label for the GIS POLYGON object + * @param string $fill_color Color for the GIS POLYGON object + * @param array $scale_data Array containing data related to scaling + * + * @return string JavaScript related to a row in the GIS dataset + * @access public + */ + public function prepareRowAsOl($spatial, $srid, $label, $fill_color, array $scale_data) + { + $style_options = [ + 'strokeColor' => '#000000', + 'strokeWidth' => 0.5, + 'fillColor' => $fill_color, + 'fillOpacity' => 0.8, + 'label' => $label, + 'fontSize' => 10, + ]; + if ($srid == 0) { + $srid = 4326; + } + $row = $this->getBoundsForOl($srid, $scale_data); + + // Trim to remove leading 'POLYGON((' and trailing '))' + $polygon + = + mb_substr( + $spatial, + 9, + mb_strlen($spatial) - 11 + ); + + // Separate outer and inner polygons + $parts = explode("),(", $polygon); + $row .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector(' + . $this->getPolygonForOpenLayers($parts, $srid) + . ', null, ' . json_encode($style_options) . '));'; + + return $row; + } + + /** + * Draws a ring of the polygon using SVG path element. + * + * @param string $polygon The ring + * @param array $scale_data Array containing data related to scaling + * + * @return string the code to draw the ring + * @access private + */ + private function _drawPath($polygon, array $scale_data) + { + $points_arr = $this->extractPoints($polygon, $scale_data); + + $row = ' M ' . $points_arr[0][0] . ', ' . $points_arr[0][1]; + $other_points = array_slice($points_arr, 1, count($points_arr) - 2); + foreach ($other_points as $point) { + $row .= ' L ' . $point[0] . ', ' . $point[1]; + } + $row .= ' Z '; + + return $row; + } + + /** + * 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 $empty Value for empty points + * + * @return string WKT with the set of parameters passed by the GIS editor + * @access public + */ + public function generateWkt(array $gis_data, $index, $empty = '') + { + $no_of_lines = isset($gis_data[$index]['POLYGON']['no_of_lines']) + ? $gis_data[$index]['POLYGON']['no_of_lines'] : 1; + if ($no_of_lines < 1) { + $no_of_lines = 1; + } + + $wkt = 'POLYGON('; + for ($i = 0; $i < $no_of_lines; $i++) { + $no_of_points = isset($gis_data[$index]['POLYGON'][$i]['no_of_points']) + ? $gis_data[$index]['POLYGON'][$i]['no_of_points'] : 4; + if ($no_of_points < 4) { + $no_of_points = 4; + } + $wkt .= '('; + for ($j = 0; $j < $no_of_points; $j++) { + $wkt .= ((isset($gis_data[$index]['POLYGON'][$i][$j]['x']) + && trim((string) $gis_data[$index]['POLYGON'][$i][$j]['x']) != '') + ? $gis_data[$index]['POLYGON'][$i][$j]['x'] : $empty) + . ' ' . ((isset($gis_data[$index]['POLYGON'][$i][$j]['y']) + && trim((string) $gis_data[$index]['POLYGON'][$i][$j]['y']) != '') + ? $gis_data[$index]['POLYGON'][$i][$j]['y'] : $empty) . ','; + } + $wkt + = + mb_substr( + $wkt, + 0, + mb_strlen($wkt) - 1 + ); + $wkt .= '),'; + } + $wkt + = + mb_substr( + $wkt, + 0, + mb_strlen($wkt) - 1 + ); + $wkt .= ')'; + + return $wkt; + } + + /** + * Calculates the area of a closed simple polygon. + * + * @param array $ring array of points forming the ring + * + * @return float the area of a closed simple polygon + * @access public + * @static + */ + public static function area(array $ring) + { + + $no_of_points = count($ring); + + // If the last point is same as the first point ignore it + $last = count($ring) - 1; + if (($ring[0]['x'] == $ring[$last]['x']) + && ($ring[0]['y'] == $ring[$last]['y']) + ) { + $no_of_points--; + } + + // _n-1 + // A = _1_ \ (X(i) * Y(i+1)) - (Y(i) * X(i+1)) + // 2 /__ + // i=0 + $area = 0; + for ($i = 0; $i < $no_of_points; $i++) { + $j = ($i + 1) % $no_of_points; + $area += $ring[$i]['x'] * $ring[$j]['y']; + $area -= $ring[$i]['y'] * $ring[$j]['x']; + } + $area /= 2.0; + + return $area; + } + + /** + * Determines whether a set of points represents an outer ring. + * If points are in clockwise orientation then, they form an outer ring. + * + * @param array $ring array of points forming the ring + * + * @return bool whether a set of points represents an outer ring + * @access public + * @static + */ + public static function isOuterRing(array $ring) + { + // If area is negative then it's in clockwise orientation, + // i.e. it's an outer ring + return GisPolygon::area($ring) < 0; + } + + /** + * Determines whether a given point is inside a given polygon. + * + * @param array $point x, y coordinates of the point + * @param array $polygon array of points forming the ring + * + * @return bool whether a given point is inside a given polygon + * @access public + * @static + */ + public static function isPointInsidePolygon(array $point, array $polygon) + { + // If first point is repeated at the end remove it + $last = count($polygon) - 1; + if (($polygon[0]['x'] == $polygon[$last]['x']) + && ($polygon[0]['y'] == $polygon[$last]['y']) + ) { + $polygon = array_slice($polygon, 0, $last); + } + + $no_of_points = count($polygon); + $counter = 0; + + // Use ray casting algorithm + $p1 = $polygon[0]; + for ($i = 1; $i <= $no_of_points; $i++) { + $p2 = $polygon[$i % $no_of_points]; + if ($point['y'] <= min([$p1['y'], $p2['y']])) { + $p1 = $p2; + continue; + } + + if ($point['y'] > max([$p1['y'], $p2['y']])) { + $p1 = $p2; + continue; + } + + if ($point['x'] > max([$p1['x'], $p2['x']])) { + $p1 = $p2; + continue; + } + + if ($p1['y'] != $p2['y']) { + $xinters = ($point['y'] - $p1['y']) + * ($p2['x'] - $p1['x']) + / ($p2['y'] - $p1['y']) + $p1['x']; + if ($p1['x'] == $p2['x'] || $point['x'] <= $xinters) { + $counter++; + } + } + + $p1 = $p2; + } + + return $counter % 2 != 0; + } + + /** + * Returns a point that is guaranteed to be on the surface of the ring. + * (for simple closed rings) + * + * @param array $ring array of points forming the ring + * + * @return array|bool a point on the surface of the ring + * @access public + * @static + */ + public static function getPointOnSurface(array $ring) + { + $x0 = null; + $x1 = null; + $y0 = null; + $y1 = null; + // Find two consecutive distinct points. + for ($i = 0, $nb = count($ring) - 1; $i < $nb; $i++) { + if ($ring[$i]['y'] != $ring[$i + 1]['y']) { + $x0 = $ring[$i]['x']; + $x1 = $ring[$i + 1]['x']; + $y0 = $ring[$i]['y']; + $y1 = $ring[$i + 1]['y']; + break; + } + } + + if (! isset($x0)) { + return false; + } + + // Find the mid point + $x2 = ($x0 + $x1) / 2; + $y2 = ($y0 + $y1) / 2; + + // Always keep $epsilon < 1 to go with the reduction logic down here + $epsilon = 0.1; + $denominator = sqrt(pow($y1 - $y0, 2) + pow($x0 - $x1, 2)); + $pointA = []; + $pointB = []; + + while (true) { + // Get the points on either sides of the line + // with a distance of epsilon to the mid point + $pointA['x'] = $x2 + ($epsilon * ($y1 - $y0)) / $denominator; + $pointA['y'] = $y2 + ($pointA['x'] - $x2) * ($x0 - $x1) / ($y1 - $y0); + + $pointB['x'] = $x2 + ($epsilon * ($y1 - $y0)) / (0 - $denominator); + $pointB['y'] = $y2 + ($pointB['x'] - $x2) * ($x0 - $x1) / ($y1 - $y0); + + // One of the points should be inside the polygon, + // unless epsilon chosen is too large + if (GisPolygon::isPointInsidePolygon($pointA, $ring)) { + return $pointA; + } + + if (GisPolygon::isPointInsidePolygon($pointB, $ring)) { + return $pointB; + } + + //If both are outside the polygon reduce the epsilon and + //recalculate the points(reduce exponentially for faster convergence) + $epsilon = pow($epsilon, 2); + if ($epsilon == 0) { + return false; + } + } + } + + /** Generate parameters for the GIS data editor from the value of the GIS column. + * + * @param string $value Value of the GIS column + * @param int $index Index of the geometry + * + * @return array params for the GIS data editor from the value of the GIS column + * @access public + */ + 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'] = 'POLYGON'; + $wkt = $value; + } + + // Trim to remove leading 'POLYGON((' and trailing '))' + $polygon + = + mb_substr( + $wkt, + 9, + mb_strlen($wkt) - 11 + ); + // Separate each linestring + $linerings = explode("),(", $polygon); + $params[$index]['POLYGON']['no_of_lines'] = count($linerings); + + $j = 0; + foreach ($linerings as $linering) { + $points_arr = $this->extractPoints($linering, null); + $no_of_points = count($points_arr); + $params[$index]['POLYGON'][$j]['no_of_points'] = $no_of_points; + for ($i = 0; $i < $no_of_points; $i++) { + $params[$index]['POLYGON'][$j][$i]['x'] = $points_arr[$i][0]; + $params[$index]['POLYGON'][$j][$i]['y'] = $points_arr[$i][1]; + } + $j++; + } + + return $params; + } +} diff --git a/srcs/phpmyadmin/libraries/classes/Gis/GisVisualization.php b/srcs/phpmyadmin/libraries/classes/Gis/GisVisualization.php new file mode 100644 index 0000000..19f5ca8 --- /dev/null +++ b/srcs/phpmyadmin/libraries/classes/Gis/GisVisualization.php @@ -0,0 +1,726 @@ + [ + '#B02EE0', + '#E0642E', + '#E0D62E', + '#2E97E0', + '#BCE02E', + '#E02E75', + '#5CE02E', + '#E0B02E', + '#0022E0', + '#726CB1', + '#481A36', + '#BAC658', + '#127224', + '#825119', + '#238C74', + '#4C489B', + '#87C9BF', + ], + // The width of the GIS visualization. + 'width' => 600, + // The height of the GIS visualization. + 'height' => 450, + ]; + /** + * @var array Options that the user has specified. + */ + private $_userSpecifiedSettings = null; + + /** + * Returns the settings array + * + * @return array the settings array + * @access public + */ + public function getSettings() + { + return $this->_settings; + } + + /** + * Factory + * + * @param string $sql_query SQL to fetch raw data for visualization + * @param array $options Users specified options + * @param integer $row number of rows + * @param integer $pos start position + * + * @return GisVisualization + * + * @access public + */ + public static function get($sql_query, array $options, $row, $pos) + { + return new GisVisualization($sql_query, $options, $row, $pos); + } + + /** + * Get visualization + * + * @param array $data Raw data, if set, parameters other than $options will be + * ignored + * @param array $options Users specified options + * + * @return GisVisualization + */ + public static function getByData(array $data, array $options) + { + return new GisVisualization(null, $options, null, null, $data); + } + + /** + * Check if data has SRID + * + * @return bool + */ + public function hasSrid() + { + foreach ($this->_data as $row) { + if ($row['srid'] != 0) { + return true; + } + } + + return false; + } + + /** + * Constructor. Stores user specified options. + * + * @param string $sql_query SQL to fetch raw data for visualization + * @param array $options Users specified options + * @param integer $row number of rows + * @param integer $pos start position + * @param array|null $data raw data. If set, parameters other than $options + * will be ignored + * + * @access public + */ + private function __construct($sql_query, array $options, $row, $pos, $data = null) + { + $this->_userSpecifiedSettings = $options; + if (isset($data)) { + $this->_data = $data; + } else { + $this->_modified_sql = $this->_modifySqlQuery($sql_query, $row, $pos); + $this->_data = $this->_fetchRawData(); + } + } + + /** + * All the variable initialization, options handling has to be done here. + * + * @return void + * @access protected + */ + protected function init() + { + $this->_handleOptions(); + } + + /** + * Returns sql for fetching raw data + * + * @param string $sql_query The SQL to modify. + * @param integer $rows Number of rows. + * @param integer $pos Start position. + * + * @return string the modified sql query. + */ + private function _modifySqlQuery($sql_query, $rows, $pos) + { + $modified_query = 'SELECT '; + $spatialAsText = 'ASTEXT'; + $spatialSrid = 'SRID'; + + if ($this->_userSpecifiedSettings['mysqlVersion'] >= 50600) { + $spatialAsText = 'ST_ASTEXT'; + $spatialSrid = 'ST_SRID'; + } + + // If label column is chosen add it to the query + if (! empty($this->_userSpecifiedSettings['labelColumn'])) { + $modified_query .= Util::backquote( + $this->_userSpecifiedSettings['labelColumn'] + ) + . ', '; + } + // Wrap the spatial column with 'ST_ASTEXT()' function and add it + $modified_query .= $spatialAsText . '(' + . Util::backquote($this->_userSpecifiedSettings['spatialColumn']) + . ') AS ' . Util::backquote( + $this->_userSpecifiedSettings['spatialColumn'] + ) + . ', '; + + // Get the SRID + $modified_query .= $spatialSrid . '(' + . Util::backquote($this->_userSpecifiedSettings['spatialColumn']) + . ') AS ' . Util::backquote('srid') . ' '; + + // Append the original query as the inner query + $modified_query .= 'FROM (' . $sql_query . ') AS ' + . Util::backquote('temp_gis'); + + // LIMIT clause + if (is_numeric($rows) && $rows > 0) { + $modified_query .= ' LIMIT '; + if (is_numeric($pos) && $pos >= 0) { + $modified_query .= $pos . ', ' . $rows; + } else { + $modified_query .= $rows; + } + } + + return $modified_query; + } + + /** + * Returns raw data for GIS visualization. + * + * @return array the raw data. + */ + private function _fetchRawData() + { + $modified_result = $GLOBALS['dbi']->tryQuery($this->_modified_sql); + + if ($modified_result === false) { + return []; + } + + $data = []; + while ($row = $GLOBALS['dbi']->fetchAssoc($modified_result)) { + $data[] = $row; + } + + return $data; + } + + /** + * A function which handles passed parameters. Useful if desired + * chart needs to be a little bit different from the default one. + * + * @return void + * @access private + */ + private function _handleOptions() + { + if ($this->_userSpecifiedSettings !== null) { + $this->_settings = array_merge( + $this->_settings, + $this->_userSpecifiedSettings + ); + } + } + + /** + * Sanitizes the file name. + * + * @param string $file_name file name + * @param string $ext extension of the file + * + * @return string the sanitized file name + * @access private + */ + private function _sanitizeName($file_name, $ext) + { + $file_name = Sanitize::sanitizeFilename($file_name); + + // Check if the user already added extension; + // get the substring where the extension would be if it was included + $extension_start_pos = mb_strlen($file_name) - mb_strlen($ext) - 1; + $user_extension + = mb_substr( + $file_name, + $extension_start_pos, + mb_strlen($file_name) + ); + $required_extension = "." . $ext; + if (mb_strtolower($user_extension) != $required_extension) { + $file_name .= $required_extension; + } + + return $file_name; + } + + /** + * Handles common tasks of writing the visualization to file for various formats. + * + * @param string $file_name file name + * @param string $type mime type + * @param string $ext extension of the file + * + * @return void + * @access private + */ + private function _toFile($file_name, $type, $ext) + { + $file_name = $this->_sanitizeName($file_name, $ext); + Core::downloadHeader($file_name, $type); + } + + /** + * Generate the visualization in SVG format. + * + * @return string the generated image resource + * @access private + */ + private function _svg() + { + $this->init(); + + $output = '' + . "\n" + . '' + . ''; + + $scale_data = $this->_scaleDataSet($this->_data); + $output .= $this->_prepareDataSet($this->_data, $scale_data, 'svg', ''); + + $output .= ''; + + return $output; + } + + /** + * Get the visualization as a SVG. + * + * @return string the visualization as a SVG + * @access public + */ + public function asSVG() + { + return $this->_svg(); + } + + /** + * Saves as a SVG image to a file. + * + * @param string $file_name File name + * + * @return void + * @access public + */ + public function toFileAsSvg($file_name) + { + $img = $this->_svg(); + $this->_toFile($file_name, 'image/svg+xml', 'svg'); + echo($img); + } + + /** + * Generate the visualization in PNG format. + * + * @return resource the generated image resource + * @access private + */ + private function _png() + { + $this->init(); + + // create image + $image = imagecreatetruecolor( + $this->_settings['width'], + $this->_settings['height'] + ); + + // fill the background + $bg = imagecolorallocate($image, 229, 229, 229); + imagefilledrectangle( + $image, + 0, + 0, + $this->_settings['width'] - 1, + $this->_settings['height'] - 1, + $bg + ); + + $scale_data = $this->_scaleDataSet($this->_data); + $image = $this->_prepareDataSet($this->_data, $scale_data, 'png', $image); + + return $image; + } + + /** + * Get the visualization as a PNG. + * + * @return string the visualization as a PNG + * @access public + */ + public function asPng() + { + $img = $this->_png(); + + // render and save it to variable + ob_start(); + imagepng($img, null, 9, PNG_ALL_FILTERS); + imagedestroy($img); + $output = ob_get_contents(); + ob_end_clean(); + + // base64 encode + $encoded = base64_encode($output); + + return ''; + } + + /** + * Saves as a PNG image to a file. + * + * @param string $file_name File name + * + * @return void + * @access public + */ + public function toFileAsPng($file_name) + { + $img = $this->_png(); + $this->_toFile($file_name, 'image/png', 'png'); + imagepng($img, null, 9, PNG_ALL_FILTERS); + imagedestroy($img); + } + + /** + * Get the code for visualization with OpenLayers. + * + * @todo Should return JSON to avoid eval() in gis_data_editor.js + * + * @return string the code for visualization with OpenLayers + * @access public + */ + public function asOl() + { + $this->init(); + $scale_data = $this->_scaleDataSet($this->_data); + $output + = 'if (typeof OpenLayers !== "undefined") {' + . 'var options = {' + . 'projection: new OpenLayers.Projection("EPSG:900913"),' + . 'displayProjection: new OpenLayers.Projection("EPSG:4326"),' + . 'units: "m",' + . 'numZoomLevels: 18,' + . 'maxResolution: 156543.0339,' + . 'maxExtent: new OpenLayers.Bounds(' + . '-20037508, -20037508, 20037508, 20037508),' + . 'restrictedExtent: new OpenLayers.Bounds(' + . '-20037508, -20037508, 20037508, 20037508)' + . '};' + . 'var map = new OpenLayers.Map("openlayersmap", options);' + . 'var layerNone = new OpenLayers.Layer.Boxes(' + . '"None", {isBaseLayer: true});' + . 'var layerOSM = new OpenLayers.Layer.OSM("OSM",' + . '[' + . '"https://a.tile.openstreetmap.org/${z}/${x}/${y}.png",' + . '"https://b.tile.openstreetmap.org/${z}/${x}/${y}.png",' + . '"https://c.tile.openstreetmap.org/${z}/${x}/${y}.png"' + . ']);' + . 'map.addLayers([layerOSM,layerNone]);' + . 'var vectorLayer = new OpenLayers.Layer.Vector("Data");' + . 'var bound;'; + $output .= $this->_prepareDataSet($this->_data, $scale_data, 'ol', ''); + $output .= 'map.addLayer(vectorLayer);' + . 'map.zoomToExtent(bound);' + . 'if (map.getZoom() < 2) {' + . 'map.zoomTo(2);' + . '}' + . 'map.addControl(new OpenLayers.Control.LayerSwitcher());' + . 'map.addControl(new OpenLayers.Control.MousePosition());' + . '}'; + + return $output; + } + + /** + * Saves as a PDF to a file. + * + * @param string $file_name File name + * + * @return void + * @access public + */ + public function toFileAsPdf($file_name) + { + $this->init(); + + // create pdf + $pdf = new TCPDF( + '', + 'pt', + $GLOBALS['cfg']['PDFDefaultPageSize'], + true, + 'UTF-8', + false + ); + + // disable header and footer + $pdf->setPrintHeader(false); + $pdf->setPrintFooter(false); + + //set auto page breaks + $pdf->SetAutoPageBreak(false); + + // add a page + $pdf->AddPage(); + + $scale_data = $this->_scaleDataSet($this->_data); + $pdf = $this->_prepareDataSet($this->_data, $scale_data, 'pdf', $pdf); + + // sanitize file name + $file_name = $this->_sanitizeName($file_name, 'pdf'); + $pdf->Output($file_name, 'D'); + } + + /** + * Convert file to image + * + * @param string $format Output format + * + * @return string File + */ + public function toImage($format) + { + if ($format == 'svg') { + return $this->asSVG(); + } elseif ($format == 'png') { + return $this->asPng(); + } elseif ($format == 'ol') { + return $this->asOl(); + } + } + + /** + * Convert file to given format + * + * @param string $filename Filename + * @param string $format Output format + * + * @return void + */ + public function toFile($filename, $format) + { + if ($format == 'svg') { + $this->toFileAsSvg($filename); + } elseif ($format == 'png') { + $this->toFileAsPng($filename); + } elseif ($format == 'pdf') { + $this->toFileAsPdf($filename); + } + } + + /** + * Calculates the scale, horizontal and vertical offset that should be used. + * + * @param array $data Row data + * + * @return array an array containing the scale, x and y offsets + * @access private + */ + private function _scaleDataSet(array $data) + { + $min_max = [ + 'maxX' => 0.0, + 'maxY' => 0.0, + 'minX' => 0.0, + 'minY' => 0.0, + ]; + $border = 15; + // effective width and height of the plot + $plot_width = $this->_settings['width'] - 2 * $border; + $plot_height = $this->_settings['height'] - 2 * $border; + + foreach ($data as $row) { + // Figure out the data type + $ref_data = $row[$this->_settings['spatialColumn']]; + $type_pos = mb_strpos($ref_data, '('); + if ($type_pos === false) { + continue; + } + $type = mb_substr($ref_data, 0, $type_pos); + + $gis_obj = GisFactory::factory($type); + if (! $gis_obj) { + continue; + } + $scale_data = $gis_obj->scaleRow( + $row[$this->_settings['spatialColumn']] + ); + + // Update minimum/maximum values for x and y coordinates. + $c_maxX = (float) $scale_data['maxX']; + if ($min_max['maxX'] === 0.0 || $c_maxX > $min_max['maxX']) { + $min_max['maxX'] = $c_maxX; + } + + $c_minX = (float) $scale_data['minX']; + if ($min_max['minX'] === 0.0 || $c_minX < $min_max['minX']) { + $min_max['minX'] = $c_minX; + } + + $c_maxY = (float) $scale_data['maxY']; + if ($min_max['maxY'] === 0.0 || $c_maxY > $min_max['maxY']) { + $min_max['maxY'] = $c_maxY; + } + + $c_minY = (float) $scale_data['minY']; + if ($min_max['minY'] === 0.0 || $c_minY < $min_max['minY']) { + $min_max['minY'] = $c_minY; + } + } + + // scale the visualization + $x_ratio = ($min_max['maxX'] - $min_max['minX']) / $plot_width; + $y_ratio = ($min_max['maxY'] - $min_max['minY']) / $plot_height; + $ratio = ($x_ratio > $y_ratio) ? $x_ratio : $y_ratio; + + $scale = ($ratio != 0) ? (1 / $ratio) : 1; + + if ($x_ratio < $y_ratio) { + // center horizontally + $x = ($min_max['maxX'] + $min_max['minX'] - $plot_width / $scale) / 2; + // fit vertically + $y = $min_max['minY'] - ($border / $scale); + } else { + // fit horizontally + $x = $min_max['minX'] - ($border / $scale); + // center vertically + $y = ($min_max['maxY'] + $min_max['minY'] - $plot_height / $scale) / 2; + } + + return [ + 'scale' => $scale, + 'x' => $x, + 'y' => $y, + 'minX' => $min_max['minX'], + 'maxX' => $min_max['maxX'], + 'minY' => $min_max['minY'], + 'maxY' => $min_max['maxY'], + 'height' => $this->_settings['height'], + ]; + } + + /** + * Prepares and return the dataset as needed by the visualization. + * + * @param array $data Raw data + * @param array $scale_data Data related to scaling + * @param string $format Format of the visualization + * @param object $results Image object in the case of png + * TCPDF object in the case of pdf + * + * @return mixed the formatted array of data + * @access private + */ + private function _prepareDataSet(array $data, array $scale_data, $format, $results) + { + $color_number = 0; + + // loop through the rows + foreach ($data as $row) { + $index = $color_number % count($this->_settings['colors']); + + // Figure out the data type + $ref_data = $row[$this->_settings['spatialColumn']]; + $type_pos = mb_strpos($ref_data, '('); + if ($type_pos === false) { + continue; + } + $type = mb_substr($ref_data, 0, $type_pos); + + $gis_obj = GisFactory::factory($type); + if (! $gis_obj) { + continue; + } + $label = ''; + if (isset($this->_settings['labelColumn']) + && isset($row[$this->_settings['labelColumn']]) + ) { + $label = $row[$this->_settings['labelColumn']]; + } + + if ($format == 'svg') { + $results .= $gis_obj->prepareRowAsSvg( + $row[$this->_settings['spatialColumn']], + $label, + $this->_settings['colors'][$index], + $scale_data + ); + } elseif ($format == 'png') { + $results = $gis_obj->prepareRowAsPng( + $row[$this->_settings['spatialColumn']], + $label, + $this->_settings['colors'][$index], + $scale_data, + $results + ); + } elseif ($format == 'pdf') { + $results = $gis_obj->prepareRowAsPdf( + $row[$this->_settings['spatialColumn']], + $label, + $this->_settings['colors'][$index], + $scale_data, + $results + ); + } elseif ($format == 'ol') { + $results .= $gis_obj->prepareRowAsOl( + $row[$this->_settings['spatialColumn']], + $row['srid'], + $label, + $this->_settings['colors'][$index], + $scale_data + ); + } + $color_number++; + } + + return $results; + } + + /** + * Set user specified settings + * + * @param array $userSpecifiedSettings User specified settings + * + * @return void + */ + public function setUserSpecifiedSettings(array $userSpecifiedSettings) + { + $this->_userSpecifiedSettings = $userSpecifiedSettings; + } +} -- cgit