diff options
author | Christian Foellmann <foellmann@foe-services.de> | 2014-04-05 17:35:38 +0400 |
---|---|---|
committer | Christian Foellmann <foellmann@foe-services.de> | 2014-04-05 17:35:38 +0400 |
commit | 9eb27ef6ebc763ddc6f6278462b8ffeed6de0e36 (patch) | |
tree | d3b4a1c7ae68cafb05b9d60040c9c22d457cee62 /libraries/gis | |
parent | d472216554769ba596795f23a70c1809fa8897a3 (diff) |
INIT phpmyadmin 4.1.12 multilanguage
Diffstat (limited to 'libraries/gis')
-rw-r--r-- | libraries/gis/pma_gis_factory.php | 63 | ||||
-rw-r--r-- | libraries/gis/pma_gis_geometry.php | 361 | ||||
-rw-r--r-- | libraries/gis/pma_gis_geometrycollection.php | 336 | ||||
-rw-r--r-- | libraries/gis/pma_gis_linestring.php | 298 | ||||
-rw-r--r-- | libraries/gis/pma_gis_multilinestring.php | 370 | ||||
-rw-r--r-- | libraries/gis/pma_gis_multipoint.php | 343 | ||||
-rw-r--r-- | libraries/gis/pma_gis_multipolygon.php | 527 | ||||
-rw-r--r-- | libraries/gis/pma_gis_point.php | 294 | ||||
-rw-r--r-- | libraries/gis/pma_gis_polygon.php | 549 | ||||
-rw-r--r-- | libraries/gis/pma_gis_visualization.php | 503 |
10 files changed, 3644 insertions, 0 deletions
diff --git a/libraries/gis/pma_gis_factory.php b/libraries/gis/pma_gis_factory.php new file mode 100644 index 0000000000..a8f8b3777f --- /dev/null +++ b/libraries/gis/pma_gis_factory.php @@ -0,0 +1,63 @@ +<?php +/* vim: set expandtab sw=4 ts=4 sts=4: */ +/** + * Contains the factory class that handles the creation of geometric objects + * + * @package PhpMyAdmin-GIS + */ + +if (! defined('PHPMYADMIN')) { + exit; +} + +/** + * Factory class that handles the creation of geometric objects. + * + * @package PhpMyAdmin-GIS + */ +class PMA_GIS_Factory +{ + /** + * Returns the singleton instance of geometric class of the given type. + * + * @param string $type type of the geometric object + * + * @return PMA_GIS_Geometry the singleton instance of geometric class + * of the given type + * + * @access public + * @static + */ + public static function factory($type) + { + include_once './libraries/gis/pma_gis_geometry.php'; + + $type_lower = strtolower($type); + if (! file_exists('./libraries/gis/pma_gis_' . $type_lower . '.php')) { + return false; + } + if (include_once './libraries/gis/pma_gis_' . $type_lower . '.php') { + switch(strtoupper($type)) { + case 'MULTIPOLYGON' : + return PMA_GIS_Multipolygon::singleton(); + case 'POLYGON' : + return PMA_GIS_Polygon::singleton(); + case 'MULTIPOINT' : + return PMA_GIS_Multipoint::singleton(); + case 'POINT' : + return PMA_GIS_Point::singleton(); + case 'MULTILINESTRING' : + return PMA_GIS_Multilinestring::singleton(); + case 'LINESTRING' : + return PMA_GIS_Linestring::singleton(); + case 'GEOMETRYCOLLECTION' : + return PMA_GIS_Geometrycollection::singleton(); + default : + return false; + } + } else { + return false; + } + } +} +?> diff --git a/libraries/gis/pma_gis_geometry.php b/libraries/gis/pma_gis_geometry.php new file mode 100644 index 0000000000..b457db53cd --- /dev/null +++ b/libraries/gis/pma_gis_geometry.php @@ -0,0 +1,361 @@ +<?php +/* vim: set expandtab sw=4 ts=4 sts=4: */ +/** + * Base class for all GIS data type classes + * + * @package PhpMyAdmin-GIS + */ + +if (! defined('PHPMYADMIN')) { + exit; +} + +/** + * Base class for all GIS data type classes. + * + * @package PhpMyAdmin-GIS + */ +abstract class PMA_GIS_Geometry +{ + /** + * Prepares and returns the code related to a row in the GIS dataset as SVG. + * + * @param string $spatial GIS data object + * @param string $label label for the GIS data object + * @param string $color color for the GIS data object + * @param array $scale_data data related to scaling + * + * @return string the code related to a row in the GIS dataset + * @access public + */ + public abstract function prepareRowAsSvg($spatial, $label, $color, $scale_data); + + /** + * Adds to the PNG image object, the data related to a row in the GIS dataset. + * + * @param string $spatial GIS data object + * @param string $label label for the GIS data object + * @param string $color color for the GIS data object + * @param array $scale_data array containing data related to scaling + * @param object $image image object + * + * @return object the modified image object + * @access public + */ + public abstract function prepareRowAsPng($spatial, $label, $color, + $scale_data, $image + ); + + /** + * Adds to the TCPDF instance, the data related to a row in the GIS dataset. + * + * @param string $spatial GIS data object + * @param string $label label for the GIS data object + * @param string $color color for the GIS data 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 abstract function prepareRowAsPdf($spatial, $label, $color, + $scale_data, $pdf + ); + + /** + * Prepares the JavaScript related to a row in the GIS dataset + * to visualize it with OpenLayers. + * + * @param string $spatial GIS data object + * @param int $srid spatial reference ID + * @param string $label label for the GIS data object + * @param string $color color for the GIS data object + * @param array $scale_data array containing data related to scaling + * + * @return string the JavaScript related to a row in the GIS dataset + * @access public + */ + public abstract function prepareRowAsOl($spatial, $srid, $label, + $color, $scale_data + ); + + /** + * Scales each row. + * + * @param string $spatial spatial data of a row + * + * @return array array containing the min, max values for x and y cordinates + * @access public + */ + public abstract function scaleRow($spatial); + + /** + * 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 abstract function generateWkt($gis_data, $index, $empty = ''); + + /** + * Returns OpenLayers.Bounds object that correspond to the bounds of GIS data. + * + * @param string $srid spatial reference ID + * @param array $scale_data data related to scaling + * + * @return string OpenLayers.Bounds object that + * correspond to the bounds of GIS data + * @access protected + */ + protected function getBoundsForOl($srid, $scale_data) + { + return 'bound = new OpenLayers.Bounds(); ' + . 'bound.extend(new OpenLayers.LonLat(' + . $scale_data['minX'] . ', ' . $scale_data['minY'] + . ').transform(new OpenLayers.Projection("EPSG:' + . $srid . '"), map.getProjectionObject())); ' + . 'bound.extend(new OpenLayers.LonLat(' + . $scale_data['maxX'] . ', ' . $scale_data['maxY'] + . ').transform(new OpenLayers.Projection("EPSG:' + . $srid . '"), map.getProjectionObject()));'; + } + + /** + * Updates the min, max values with the given point set. + * + * @param string $point_set point set + * @param array $min_max existing min, max values + * + * @return array the updated min, max values + * @access protected + */ + protected function setMinMax($point_set, $min_max) + { + // Seperate each point + $points = explode(",", $point_set); + + foreach ($points as $point) { + // Extract cordinates of the point + $cordinates = explode(" ", $point); + + $x = (float) $cordinates[0]; + if (! isset($min_max['maxX']) || $x > $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 = strripos($value, ","); + $srid = trim(substr($value, $last_comma + 1)); + $wkt = trim(substr($value, 1, $last_comma - 2)); + } elseif (preg_match("/^" . $geom_types . "\(.*\)$/i", $value)) { + $wkt = $value; + } + return array('srid' => $srid, 'wkt' => $wkt); + } + + /** + * Extracts points, scales and returns them as an array. + * + * @param string $point_set string of comma sperated points + * @param array $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 = array(); + + // Seperate each point + $points = explode(",", $point_set); + + foreach ($points as $point) { + // Extract cordinates 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 = trim($cordinates[0]); + $y = trim($cordinates[1]); + } + } else { + $x = ''; + $y = ''; + } + + + if (! $linear) { + $points_arr[] = array($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($polygons, $srid) + { + $ol_array = 'new Array('; + foreach ($polygons as $polygon) { + $rings = explode("),(", $polygon); + $ol_array .= $this->getPolygonForOpenLayers($rings, $srid) . ', '; + } + $ol_array = substr($ol_array, 0, 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($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($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 = substr($ol_array, 0, 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($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($points_arr, $srid) + { + $ol_array = 'new Array('; + foreach ($points_arr as $point) { + $ol_array .= $this->getPointForOpenLayers($point, $srid) . ', '; + } + $ol_array = substr($ol_array, 0, 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($point, $srid) + { + return '(new OpenLayers.Geometry.Point(' . $point[0] . ',' . $point[1] . '))' + . '.transform(new OpenLayers.Projection("EPSG:' + . $srid . '"), map.getProjectionObject())'; + } +} +?> diff --git a/libraries/gis/pma_gis_geometrycollection.php b/libraries/gis/pma_gis_geometrycollection.php new file mode 100644 index 0000000000..09b04ed404 --- /dev/null +++ b/libraries/gis/pma_gis_geometrycollection.php @@ -0,0 +1,336 @@ +<?php +/* vim: set expandtab sw=4 ts=4 sts=4: */ +/** + * Handles actions related to GIS GEOMETRYCOLLECTION objects + * + * @package PhpMyAdmin-GIS + */ + +if (! defined('PHPMYADMIN')) { + exit; +} + +/** + * Handles actions related to GIS GEOMETRYCOLLECTION objects + * + * @package PhpMyAdmin-GIS + */ +class PMA_GIS_Geometrycollection extends PMA_GIS_Geometry +{ + // Hold the singleton instance of the class + private static $_instance; + + /** + * A private constructor; prevents direct creation of object. + * + * @access private + */ + private function __construct() + { + } + + /** + * Returns the singleton. + * + * @return PMA_GIS_Geometrycollection the singleton + * @access public + */ + public static function singleton() + { + if (!isset(self::$_instance)) { + $class = __CLASS__; + self::$_instance = new $class; + } + + return self::$_instance; + } + + /** + * Scales each row. + * + * @param string $spatial spatial data of a row + * + * @return array array containing the min, max values for x and y cordinates + * @access public + */ + public function scaleRow($spatial) + { + $min_max = array(); + + // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')' + $goem_col = substr($spatial, 19, (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 = stripos($sub_part, '('); + $type = substr($sub_part, 0, $type_pos); + + $gis_obj = PMA_GIS_Factory::factory($type); + if (! $gis_obj) { + continue; + } + $scale_data = $gis_obj->scaleRow($sub_part); + + // Upadate minimum/maximum values for x and y cordinates. + $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 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 + * @param object $image image object + * + * @return resource the modified image object + * @access public + */ + public function prepareRowAsPng($spatial, $label, $color, $scale_data, $image) + { + // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')' + $goem_col = substr($spatial, 19, (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 = stripos($sub_part, '('); + $type = substr($sub_part, 0, $type_pos); + + $gis_obj = PMA_GIS_Factory::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 $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 pubilc + */ + public function prepareRowAsPdf($spatial, $label, $color, $scale_data, $pdf) + { + // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')' + $goem_col = substr($spatial, 19, (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 = stripos($sub_part, '('); + $type = substr($sub_part, 0, $type_pos); + + $gis_obj = PMA_GIS_Factory::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, $scale_data) + { + $row = ''; + + // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')' + $goem_col = substr($spatial, 19, (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 = stripos($sub_part, '('); + $type = substr($sub_part, 0, $type_pos); + + $gis_obj = PMA_GIS_Factory::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, $scale_data) + { + $row = ''; + + // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')' + $goem_col = substr($spatial, 19, (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 = stripos($sub_part, '('); + $type = substr($sub_part, 0, $type_pos); + + $gis_obj = PMA_GIS_Factory::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 $goem_col geometry collection string + * + * @return array the constituents of the geometry collection object + * @access private + */ + private function _explodeGeomCol($goem_col) + { + $sub_parts = array(); + $br_count = 0; + $start = 0; + $count = 0; + foreach (str_split($goem_col) as $char) { + if ($char == '(') { + $br_count++; + } elseif ($char == ')') { + $br_count--; + if ($br_count == 0) { + $sub_parts[] = substr($goem_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($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 = PMA_GIS_Factory::factory($type); + if (! $gis_obj) { + continue; + } + $wkt .= $gis_obj->generateWkt($gis_data, $i, $empty) . ','; + } + } + if (isset($gis_data[0]['gis_type'])) { + $wkt = substr($wkt, 0, 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 = array(); + $data = PMA_GIS_Geometry::generateParams($value); + $params['srid'] = $data['srid']; + $wkt = $data['wkt']; + + // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')' + $goem_col = substr($wkt, 19, (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 = stripos($sub_part, '('); + $type = substr($sub_part, 0, $type_pos); + $gis_obj = PMA_GIS_Factory::factory($type); + if (! $gis_obj) { + continue; + } + $params = array_merge($params, $gis_obj->generateParams($sub_part, $i)); + $i++; + } + return $params; + } +} +?> diff --git a/libraries/gis/pma_gis_linestring.php b/libraries/gis/pma_gis_linestring.php new file mode 100644 index 0000000000..306e88bca4 --- /dev/null +++ b/libraries/gis/pma_gis_linestring.php @@ -0,0 +1,298 @@ +<?php +/* vim: set expandtab sw=4 ts=4 sts=4: */ +/** + * Handles actions related to GIS LINESTRING objects + * + * @package PhpMyAdmin-GIS + */ + +if (! defined('PHPMYADMIN')) { + exit; +} + +/** + * Handles actions related to GIS LINESTRING objects + * + * @package PhpMyAdmin-GIS + */ +class PMA_GIS_Linestring extends PMA_GIS_Geometry +{ + // Hold the singleton instance of the class + private static $_instance; + + /** + * A private constructor; prevents direct creation of object. + * + * @access private + */ + private function __construct() + { + } + + /** + * Returns the singleton. + * + * @return PMA_GIS_Linestring the singleton + * @access public + */ + public static function singleton() + { + if (!isset(self::$_instance)) { + $class = __CLASS__; + self::$_instance = new $class; + } + + return self::$_instance; + } + + /** + * Scales each row. + * + * @param string $spatial spatial data of a row + * + * @return array an array containing the min, max values for x and y cordinates + * @access public + */ + public function scaleRow($spatial) + { + // Trim to remove leading 'LINESTRING(' and trailing ')' + $linesrting = substr($spatial, 11, (strlen($spatial) - 12)); + return $this->setMinMax($linesrting, array()); + } + + /** + * Adds to the PNG image object, the data related to a row in the GIS dataset. + * + * @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 + * @param object $image Image object + * + * @return resource the modified image object + * @access public + */ + public function prepareRowAsPng($spatial, $label, $line_color, + $scale_data, $image + ) { + // allocate colors + $black = imagecolorallocate($image, 0, 0, 0); + $red = hexdec(substr($line_color, 1, 2)); + $green = hexdec(substr($line_color, 3, 2)); + $blue = hexdec(substr($line_color, 4, 2)); + $color = imagecolorallocate($image, $red, $green, $blue); + + // Trim to remove leading 'LINESTRING(' and trailing ')' + $linesrting = substr($spatial, 11, (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 $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, $label, $line_color, $scale_data, $pdf) + { + // allocate colors + $red = hexdec(substr($line_color, 1, 2)); + $green = hexdec(substr($line_color, 3, 2)); + $blue = hexdec(substr($line_color, 4, 2)); + $line = array('width' => 1.5, 'color' => array($red, $green, $blue)); + + // Trim to remove leading 'LINESTRING(' and trailing ')' + $linesrting = substr($spatial, 11, (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, $scale_data) + { + $line_options = array( + 'name' => $label, + 'id' => $label . rand(), + 'class' => 'linestring vector', + 'fill' => 'none', + 'stroke' => $line_color, + 'stroke-width'=> 2, + ); + + // Trim to remove leading 'LINESTRING(' and trailing ')' + $linesrting = substr($spatial, 11, (strlen($spatial) - 12)); + $points_arr = $this->extractPoints($linesrting, $scale_data); + + $row = '<polyline points="'; + foreach ($points_arr as $point) { + $row .= $point[0] . ',' . $point[1] . ' '; + } + $row .= '"'; + foreach ($line_options as $option => $val) { + $row .= ' ' . $option . '="' . trim($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, $scale_data) + { + $style_options = array( + '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 = substr($spatial, 11, (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($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($gis_data[$index]['LINESTRING'][$i]['x']) != '') + ? $gis_data[$index]['LINESTRING'][$i]['x'] : $empty) + . ' ' . ((isset($gis_data[$index]['LINESTRING'][$i]['y']) + && trim($gis_data[$index]['LINESTRING'][$i]['y']) != '') + ? $gis_data[$index]['LINESTRING'][$i]['y'] : $empty) .','; + } + $wkt = substr($wkt, 0, 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) + { + if ($index == -1) { + $index = 0; + $params = array(); + $data = PMA_GIS_Geometry::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 = substr($wkt, 11, (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/libraries/gis/pma_gis_multilinestring.php b/libraries/gis/pma_gis_multilinestring.php new file mode 100644 index 0000000000..f6e85639c2 --- /dev/null +++ b/libraries/gis/pma_gis_multilinestring.php @@ -0,0 +1,370 @@ +<?php +/* vim: set expandtab sw=4 ts=4 sts=4: */ +/** + * Handles actions related to GIS MULTILINESTRING objects + * + * @package PhpMyAdmin-GIS + */ + +if (! defined('PHPMYADMIN')) { + exit; +} + +/** + * Handles actions related to GIS MULTILINESTRING objects + * + * @package PhpMyAdmin-GIS + */ +class PMA_GIS_Multilinestring extends PMA_GIS_Geometry +{ + // Hold the singleton instance of the class + private static $_instance; + + /** + * A private constructor; prevents direct creation of object. + * + * @access private + */ + private function __construct() + { + } + + /** + * Returns the singleton. + * + * @return PMA_GIS_Multilinestring the singleton + * @access public + */ + public static function singleton() + { + if (!isset(self::$_instance)) { + $class = __CLASS__; + self::$_instance = new $class; + } + + return self::$_instance; + } + + /** + * Scales each row. + * + * @param string $spatial spatial data of a row + * + * @return array an array containing the min, max values for x and y cordinates + * @access public + */ + public function scaleRow($spatial) + { + $min_max = array(); + + // Trim to remove leading 'MULTILINESTRING((' and trailing '))' + $multilinestirng = substr($spatial, 17, (strlen($spatial) - 19)); + // Seperate each linestring + $linestirngs = explode("),(", $multilinestirng); + + foreach ($linestirngs as $linestring) { + $min_max = $this->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 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 + * @param object $image Image object + * + * @return object the modified image object + * @access public + */ + public function prepareRowAsPng($spatial, $label, $line_color, + $scale_data, $image + ) { + // allocate colors + $black = imagecolorallocate($image, 0, 0, 0); + $red = hexdec(substr($line_color, 1, 2)); + $green = hexdec(substr($line_color, 3, 2)); + $blue = hexdec(substr($line_color, 4, 2)); + $color = imagecolorallocate($image, $red, $green, $blue); + + // Trim to remove leading 'MULTILINESTRING((' and trailing '))' + $multilinestirng = substr($spatial, 17, (strlen($spatial) - 19)); + // Seperate 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 $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, $label, $line_color, $scale_data, $pdf) + { + // allocate colors + $red = hexdec(substr($line_color, 1, 2)); + $green = hexdec(substr($line_color, 3, 2)); + $blue = hexdec(substr($line_color, 4, 2)); + $line = array('width' => 1.5, 'color' => array($red, $green, $blue)); + + // Trim to remove leading 'MULTILINESTRING((' and trailing '))' + $multilinestirng = substr($spatial, 17, (strlen($spatial) - 19)); + // Seperate 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, $scale_data) + { + $line_options = array( + 'name' => $label, + 'class' => 'linestring vector', + 'fill' => 'none', + 'stroke' => $line_color, + 'stroke-width'=> 2, + ); + + // Trim to remove leading 'MULTILINESTRING((' and trailing '))' + $multilinestirng = substr($spatial, 17, (strlen($spatial) - 19)); + // Seperate each linestring + $linestirngs = explode("),(", $multilinestirng); + + $row = ''; + foreach ($linestirngs as $linestring) { + $points_arr = $this->extractPoints($linestring, $scale_data); + + $row .= '<polyline points="'; + foreach ($points_arr as $point) { + $row .= $point[0] . ',' . $point[1] . ' '; + } + $row .= '"'; + $line_options['id'] = $label . rand(); + foreach ($line_options as $option => $val) { + $row .= ' ' . $option . '="' . trim($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, $scale_data) + { + $style_options = array( + '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 = substr($spatial, 17, (strlen($spatial) - 19)); + // Seperate 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($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($data_row[$i][$j]['x']) != '') + ? $data_row[$i][$j]['x'] : $empty) + . ' ' . ((isset($data_row[$i][$j]['y']) + && trim($data_row[$i][$j]['y']) != '') + ? $data_row[$i][$j]['y'] : $empty) . ','; + } + $wkt = substr($wkt, 0, strlen($wkt) - 1); + $wkt .= '),'; + } + $wkt = substr($wkt, 0, 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($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 = substr($wkt, 0, strlen($wkt) - 1); + $wkt .= '),'; + } + $wkt = substr($wkt, 0, 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) + { + if ($index == -1) { + $index = 0; + $params = array(); + $data = PMA_GIS_Geometry::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 = substr($wkt, 17, (strlen($wkt) - 19)); + // Seperate 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/libraries/gis/pma_gis_multipoint.php b/libraries/gis/pma_gis_multipoint.php new file mode 100644 index 0000000000..6ef57ddec7 --- /dev/null +++ b/libraries/gis/pma_gis_multipoint.php @@ -0,0 +1,343 @@ +<?php +/* vim: set expandtab sw=4 ts=4 sts=4: */ +/** + * Handles actions related to GIS MULTIPOINT objects + * + * @package PhpMyAdmin-GIS + */ + +if (! defined('PHPMYADMIN')) { + exit; +} + +/** + * Handles actions related to GIS MULTIPOINT objects + * + * @package PhpMyAdmin-GIS + */ +class PMA_GIS_Multipoint extends PMA_GIS_Geometry +{ + // Hold the singleton instance of the class + private static $_instance; + + /** + * A private constructor; prevents direct creation of object. + * + * @access private + */ + private function __construct() + { + } + + /** + * Returns the singleton. + * + * @return PMA_GIS_Multipoint the singleton + * @access public + */ + public static function singleton() + { + if (!isset(self::$_instance)) { + $class = __CLASS__; + self::$_instance = new $class; + } + + return self::$_instance; + } + + /** + * Scales each row. + * + * @param string $spatial spatial data of a row + * + * @return array an array containing the min, max values for x and y cordinates + * @access public + */ + public function scaleRow($spatial) + { + // Trim to remove leading 'MULTIPOINT(' and trailing ')' + $multipoint = substr($spatial, 11, (strlen($spatial) - 12)); + return $this->setMinMax($multipoint, array()); + } + + /** + * Adds to the PNG image object, the data related to a row in the GIS dataset. + * + * @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 + * @param object $image Image object + * + * @return object the modified image object + * @access public + */ + public function prepareRowAsPng($spatial, $label, $point_color, + $scale_data, $image + ) { + // allocate colors + $black = imagecolorallocate($image, 0, 0, 0); + $red = hexdec(substr($point_color, 1, 2)); + $green = hexdec(substr($point_color, 3, 2)); + $blue = hexdec(substr($point_color, 4, 2)); + $color = imagecolorallocate($image, $red, $green, $blue); + + // Trim to remove leading 'MULTIPOINT(' and trailing ')' + $multipoint = substr($spatial, 11, (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 $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, $label, $point_color, + $scale_data, $pdf + ) { + // allocate colors + $red = hexdec(substr($point_color, 1, 2)); + $green = hexdec(substr($point_color, 3, 2)); + $blue = hexdec(substr($point_color, 4, 2)); + $line = array('width' => 1.25, 'color' => array($red, $green, $blue)); + + // Trim to remove leading 'MULTIPOINT(' and trailing ')' + $multipoint = substr($spatial, 11, (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, $scale_data) + { + $point_options = array( + 'name' => $label, + 'class' => 'multipoint vector', + 'fill' => 'white', + 'stroke' => $point_color, + 'stroke-width'=> 2, + ); + + // Trim to remove leading 'MULTIPOINT(' and trailing ')' + $multipoint = substr($spatial, 11, (strlen($spatial) - 12)); + $points_arr = $this->extractPoints($multipoint, $scale_data); + + $row = ''; + foreach ($points_arr as $point) { + if ($point[0] != '' && $point[1] != '') { + $row .= '<circle cx="' . $point[0] . '" cy="' + . $point[1] . '" r="3"'; + $point_options['id'] = $label . rand(); + foreach ($point_options as $option => $val) { + $row .= ' ' . $option . '="' . trim($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, $scale_data + ) { + $style_options = array( + '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 = substr($spatial, 11, (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($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($gis_data[$index]['MULTIPOINT'][$i]['x']) != '') + ? $gis_data[$index]['MULTIPOINT'][$i]['x'] : '') + . ' ' . ((isset($gis_data[$index]['MULTIPOINT'][$i]['y']) + && trim($gis_data[$index]['MULTIPOINT'][$i]['y']) != '') + ? $gis_data[$index]['MULTIPOINT'][$i]['y'] : '') . ','; + } + $wkt = substr($wkt, 0, 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($row_data) + { + $wkt = 'MULTIPOINT('; + for ($i = 0; $i < $row_data['numpoints']; $i++) { + $wkt .= $row_data['points'][$i]['x'] . ' ' + . $row_data['points'][$i]['y'] . ','; + } + $wkt = substr($wkt, 0, 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) + { + if ($index == -1) { + $index = 0; + $params = array(); + $data = PMA_GIS_Geometry::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 = substr($wkt, 11, (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; + } + + /** + * Overidden 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($points_arr, $srid) + { + $ol_array = 'new Array('; + foreach ($points_arr as $point) { + if ($point[0] != '' && $point[1] != '') { + $ol_array .= $this->getPointForOpenLayers($point, $srid) . ', '; + } + } + if (substr($ol_array, strlen($ol_array) - 2) == ', ') { + $ol_array = substr($ol_array, 0, strlen($ol_array) - 2); + } + $ol_array .= ')'; + + return $ol_array; + } +} +?> diff --git a/libraries/gis/pma_gis_multipolygon.php b/libraries/gis/pma_gis_multipolygon.php new file mode 100644 index 0000000000..7afbff386e --- /dev/null +++ b/libraries/gis/pma_gis_multipolygon.php @@ -0,0 +1,527 @@ +<?php +/* vim: set expandtab sw=4 ts=4 sts=4: */ +/** + * Handles actions related to GIS MULTIPOLYGON objects + * + * @package PhpMyAdmin-GIS + */ + +if (! defined('PHPMYADMIN')) { + exit; +} + +/** + * Handles actions related to GIS MULTIPOLYGON objects + * + * @package PhpMyAdmin-GIS + */ +class PMA_GIS_Multipolygon extends PMA_GIS_Geometry +{ + // Hold the singleton instance of the class + private static $_instance; + + /** + * A private constructor; prevents direct creation of object. + * + * @access private + */ + private function __construct() + { + } + + /** + * Returns the singleton. + * + * @return PMA_GIS_Multipolygon the singleton + * @access public + */ + public static function singleton() + { + if (!isset(self::$_instance)) { + $class = __CLASS__; + self::$_instance = new $class; + } + + return self::$_instance; + } + + /** + * Scales each row. + * + * @param string $spatial spatial data of a row + * + * @return array an array containing the min, max values for x and y cordinates + * @access public + */ + public function scaleRow($spatial) + { + $min_max = array(); + + // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))' + $multipolygon = substr($spatial, 15, (strlen($spatial) - 18)); + // Seperate each polygon + $polygons = explode(")),((", $multipolygon); + + foreach ($polygons as $polygon) { + // If the polygon doesn't have an inner ring, use polygon itself + if (strpos($polygon, "),(") === false) { + $ring = $polygon; + } else { + // Seperate outer ring and use it to determin min-max + $parts = explode("),(", $polygon); + $ring = $parts[0]; + } + $min_max = $this->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 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 + * @param object $image Image object + * + * @return object the modified image object + * @access public + */ + public function prepareRowAsPng($spatial, $label, $fill_color, + $scale_data, $image + ) { + // allocate colors + $black = imagecolorallocate($image, 0, 0, 0); + $red = hexdec(substr($fill_color, 1, 2)); + $green = hexdec(substr($fill_color, 3, 2)); + $blue = hexdec(substr($fill_color, 4, 2)); + $color = imagecolorallocate($image, $red, $green, $blue); + + // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))' + $multipolygon = substr($spatial, 15, (strlen($spatial) - 18)); + // Seperate each polygon + $polygons = explode(")),((", $multipolygon); + + $first_poly = true; + foreach ($polygons as $polygon) { + // If the polygon doesnt have an inner polygon + if (strpos($polygon, "),(") === false) { + $points_arr = $this->extractPoints($polygon, $scale_data, true); + } else { + // Seperate 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, sizeof($points_arr) / 2, $color); + // mark label point if applicable + if (isset($label) && trim($label) != '' && $first_poly) { + $label_point = array($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 $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, $label, $fill_color, $scale_data, $pdf) + { + // allocate colors + $red = hexdec(substr($fill_color, 1, 2)); + $green = hexdec(substr($fill_color, 3, 2)); + $blue = hexdec(substr($fill_color, 4, 2)); + $color = array($red, $green, $blue); + + // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))' + $multipolygon = substr($spatial, 15, (strlen($spatial) - 18)); + // Seperate each polygon + $polygons = explode(")),((", $multipolygon); + + $first_poly = true; + foreach ($polygons as $polygon) { + // If the polygon doesnt have an inner polygon + if (strpos($polygon, "),(") === false) { + $points_arr = $this->extractPoints($polygon, $scale_data, true); + } else { + // Seperate 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*', array(), $color, true); + // mark label point if applicable + if (isset($label) && trim($label) != '' && $first_poly) { + $label_point = array($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, $scale_data) + { + $polygon_options = array( + '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 = substr($spatial, 15, (strlen($spatial) - 18)); + // Seperate each polygon + $polygons = explode(")),((", $multipolygon); + + foreach ($polygons as $polygon) { + $row .= '<path d="'; + + // If the polygon doesnt have an inner polygon + if (strpos($polygon, "),(") === false) { + $row .= $this->_drawPath($polygon, $scale_data); + } else { + // Seperate 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 . rand(); + $row .= '"'; + foreach ($polygon_options as $option => $val) { + $row .= ' ' . $option . '="' . trim($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, $scale_data) + { + $style_options = array( + '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 = substr($spatial, 15, (strlen($spatial) - 18)); + // Seperate 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, $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($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($data_row[$k][$i][$j]['x']) != '') + ? $data_row[$k][$i][$j]['x'] : $empty) + . ' ' . ((isset($data_row[$k][$i][$j]['y']) + && trim($data_row[$k][$i][$j]['y']) != '') + ? $data_row[$k][$i][$j]['y'] : $empty) .','; + } + $wkt = substr($wkt, 0, strlen($wkt) - 1); + $wkt .= '),'; + } + $wkt = substr($wkt, 0, strlen($wkt) - 1); + $wkt .= '),'; + } + $wkt = substr($wkt, 0, 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($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. + include_once './libraries/gis/pma_gis_polygon.php'; + foreach ($row_data['parts'] as $i => $ring) { + $row_data['parts'][$i]['isOuter'] + = PMA_GIS_Polygon::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'] + = PMA_GIS_Polygon::getPointOnSurface($ring['points']); + } + } + + // Classify inner rings to their respective outer rings. + foreach ($row_data['parts'] as $j => $ring1) { + if (! $ring1['isOuter']) { + foreach ($row_data['parts'] as $k => $ring2) { + if ($ring2['isOuter']) { + // If the pointOnSurface of the inner ring + // is also inside the outer ring + if (PMA_GIS_Polygon::isPointInsidePolygon( + $ring1['pointOnSurface'], $ring2['points'] + )) { + if (! isset($ring2['inner'])) { + $row_data['parts'][$k]['inner'] = array(); + } + $row_data['parts'][$k]['inner'][] = $j; + } + } + } + } + } + + $wkt = 'MULTIPOLYGON('; + // for each polygon + foreach ($row_data['parts'] as $ring) { + if ($ring['isOuter']) { + $wkt .= '('; // start of polygon + + $wkt .= '('; // start of outer ring + foreach ($ring['points'] as $point) { + $wkt .= $point['x'] . ' ' . $point['y'] . ','; + } + $wkt = substr($wkt, 0, 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 = substr($wkt, 0, strlen($wkt) - 1); + $wkt .= ')'; // end of inner ring + } + } + + $wkt .= '),'; // end of polygon + } + } + $wkt = substr($wkt, 0, 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) + { + if ($index == -1) { + $index = 0; + $params = array(); + $data = PMA_GIS_Geometry::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 = substr($wkt, 15, (strlen($wkt) - 18)); + // Seperate 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 doesnt have an inner polygon + if (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 { + // Seperate 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/libraries/gis/pma_gis_point.php b/libraries/gis/pma_gis_point.php new file mode 100644 index 0000000000..e434e0c26c --- /dev/null +++ b/libraries/gis/pma_gis_point.php @@ -0,0 +1,294 @@ +<?php +/* vim: set expandtab sw=4 ts=4 sts=4: */ +/** + * Handles actions related to GIS POINT objects + * + * @package PhpMyAdmin-GIS + */ + +if (! defined('PHPMYADMIN')) { + exit; +} + +/** + * Handles actions related to GIS POINT objects + * + * @package PhpMyAdmin-GIS + */ +class PMA_GIS_Point extends PMA_GIS_Geometry +{ + // Hold the singleton instance of the class + private static $_instance; + + /** + * A private constructor; prevents direct creation of object. + * + * @access private + */ + private function __construct() + { + } + + /** + * Returns the singleton. + * + * @return PMA_GIS_Point the singleton + * @access public + */ + public static function singleton() + { + if (!isset(self::$_instance)) { + $class = __CLASS__; + self::$_instance = new $class; + } + + return self::$_instance; + } + + /** + * Scales each row. + * + * @param string $spatial spatial data of a row + * + * @return array an array containing the min, max values for x and y cordinates + * @access public + */ + public function scaleRow($spatial) + { + // Trim to remove leading 'POINT(' and trailing ')' + $point = substr($spatial, 6, (strlen($spatial) - 7)); + return $this->setMinMax($point, array()); + } + + /** + * Adds to the PNG image object, the data related to a row in the GIS dataset. + * + * @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 + * @param object $image Image object + * + * @return object the modified image object + * @access public + */ + public function prepareRowAsPng($spatial, $label, $point_color, + $scale_data, $image + ) { + // allocate colors + $black = imagecolorallocate($image, 0, 0, 0); + $red = hexdec(substr($point_color, 1, 2)); + $green = hexdec(substr($point_color, 3, 2)); + $blue = hexdec(substr($point_color, 4, 2)); + $color = imagecolorallocate($image, $red, $green, $blue); + + // Trim to remove leading 'POINT(' and trailing ')' + $point = substr($spatial, 6, (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 $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, $label, $point_color, + $scale_data, $pdf + ) { + // allocate colors + $red = hexdec(substr($point_color, 1, 2)); + $green = hexdec(substr($point_color, 3, 2)); + $blue = hexdec(substr($point_color, 4, 2)); + $line = array('width' => 1.25, 'color' => array($red, $green, $blue)); + + // Trim to remove leading 'POINT(' and trailing ')' + $point = substr($spatial, 6, (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, $scale_data) + { + $point_options = array( + 'name' => $label, + 'id' => $label . rand(), + 'class' => 'point vector', + 'fill' => 'white', + 'stroke' => $point_color, + 'stroke-width'=> 2, + ); + + // Trim to remove leading 'POINT(' and trailing ')' + $point = substr($spatial, 6, (strlen($spatial) - 7)); + $points_arr = $this->extractPoints($point, $scale_data); + + $row = ''; + if ($points_arr[0][0] != '' && $points_arr[0][1] != '') { + $row .= '<circle cx="' . $points_arr[0][0] + . '" cy="' . $points_arr[0][1] . '" r="3"'; + foreach ($point_options as $option => $val) { + $row .= ' ' . $option . '="' . trim($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, $scale_data + ) { + $style_options = array( + '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 = substr($spatial, 6, (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 deos not adhere to this parameter + * + * @return string WKT with the set of parameters passed by the GIS editor + * @access public + */ + public function generateWkt($gis_data, $index, $empty = '') + { + return 'POINT(' + . ((isset($gis_data[$index]['POINT']['x']) + && trim($gis_data[$index]['POINT']['x']) != '') + ? $gis_data[$index]['POINT']['x'] : '') + . ' ' + . ((isset($gis_data[$index]['POINT']['y']) + && trim($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($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) + { + if ($index == -1) { + $index = 0; + $params = array(); + $data = PMA_GIS_Geometry::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 = substr($wkt, 6, (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/libraries/gis/pma_gis_polygon.php b/libraries/gis/pma_gis_polygon.php new file mode 100644 index 0000000000..215625e13e --- /dev/null +++ b/libraries/gis/pma_gis_polygon.php @@ -0,0 +1,549 @@ +<?php +/* vim: set expandtab sw=4 ts=4 sts=4: */ +/** + * Handles actions related to GIS POLYGON objects + * + * @package PhpMyAdmin-GIS + */ + +if (! defined('PHPMYADMIN')) { + exit; +} + +/** + * Handles actions related to GIS POLYGON objects + * + * @package PhpMyAdmin-GIS + */ +class PMA_GIS_Polygon extends PMA_GIS_Geometry +{ + // Hold the singleton instance of the class + private static $_instance; + + /** + * A private constructor; prevents direct creation of object. + * + * @access private + */ + private function __construct() + { + } + + /** + * Returns the singleton. + * + * @return PMA_GIS_Polygon the singleton + * @access public + */ + public static function singleton() + { + if (!isset(self::$_instance)) { + $class = __CLASS__; + self::$_instance = new $class; + } + + return self::$_instance; + } + + /** + * Scales each row. + * + * @param string $spatial spatial data of a row + * + * @return array an array containing the min, max values for x and y cordinates + * @access public + */ + public function scaleRow($spatial) + { + // Trim to remove leading 'POLYGON((' and trailing '))' + $polygon = substr($spatial, 9, (strlen($spatial) - 11)); + + // If the polygon doesn't have an inner ring, use polygon itself + if (strpos($polygon, "),(") === false) { + $ring = $polygon; + } else { + // Seperate outer ring and use it to determin min-max + $parts = explode("),(", $polygon); + $ring = $parts[0]; + } + return $this->setMinMax($ring, array()); + } + + /** + * Adds to the PNG image object, the data related to a row in the GIS dataset. + * + * @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 + * @param object $image Image object + * + * @return object the modified image object + * @access public + */ + public function prepareRowAsPng($spatial, $label, $fill_color, + $scale_data, $image + ) { + // allocate colors + $black = imagecolorallocate($image, 0, 0, 0); + $red = hexdec(substr($fill_color, 1, 2)); + $green = hexdec(substr($fill_color, 3, 2)); + $blue = hexdec(substr($fill_color, 4, 2)); + $color = imagecolorallocate($image, $red, $green, $blue); + + // Trim to remove leading 'POLYGON((' and trailing '))' + $polygon = substr($spatial, 9, (strlen($spatial) - 11)); + + // If the polygon doesnt have an inner polygon + if (strpos($polygon, "),(") === false) { + $points_arr = $this->extractPoints($polygon, $scale_data, true); + } else { + // Seperate 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, sizeof($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 $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, $label, $fill_color, $scale_data, $pdf) + { + // allocate colors + $red = hexdec(substr($fill_color, 1, 2)); + $green = hexdec(substr($fill_color, 3, 2)); + $blue = hexdec(substr($fill_color, 4, 2)); + $color = array($red, $green, $blue); + + // Trim to remove leading 'POLYGON((' and trailing '))' + $polygon = substr($spatial, 9, (strlen($spatial) - 11)); + + // If the polygon doesnt have an inner polygon + if (strpos($polygon, "),(") === false) { + $points_arr = $this->extractPoints($polygon, $scale_data, true); + } else { + // Seperate 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*', array(), $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, $scale_data) + { + $polygon_options = array( + 'name' => $label, + 'id' => $label . 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 = substr($spatial, 9, (strlen($spatial) - 11)); + + $row = '<path d="'; + + // If the polygon doesnt have an inner polygon + if (strpos($polygon, "),(") === false) { + $row .= $this->_drawPath($polygon, $scale_data); + } else { + // Seperate 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($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, $scale_data) + { + $style_options = array( + '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 = substr($spatial, 9, (strlen($spatial) - 11)); + + // Seperate 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, $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($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($gis_data[$index]['POLYGON'][$i][$j]['x']) != '') + ? $gis_data[$index]['POLYGON'][$i][$j]['x'] : $empty) + . ' ' . ((isset($gis_data[$index]['POLYGON'][$i][$j]['y']) + && trim($gis_data[$index]['POLYGON'][$i][$j]['y']) != '') + ? $gis_data[$index]['POLYGON'][$i][$j]['y'] : $empty) .','; + } + $wkt = substr($wkt, 0, strlen($wkt) - 1); + $wkt .= '),'; + } + $wkt = substr($wkt, 0, 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($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($ring) + { + // If area is negative then it's in clockwise orientation, + // i.e. it's an outer ring + if (PMA_GIS_Polygon::area($ring) < 0) { + return true; + } + return false; + } + + /** + * 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($point, $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(array($p1['y'], $p2['y']))) { + if ($point['y'] <= max(array($p1['y'], $p2['y']))) { + if ($point['x'] <= max(array($p1['x'], $p2['x']))) { + 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; + } + + if ($counter % 2 == 0) { + return false; + } else { + return true; + } + } + + /** + * 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|void a point on the surface of the ring + * @access public + * @static + */ + public static function getPointOnSurface($ring) + { + // Find two consecutive distinct points. + for ($i = 0; $i < count($ring) - 1; $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( + PMA_Util::pow(($y1 - $y0), 2) + + PMA_Util::pow(($x0 - $x1), 2) + ); + $pointA = array(); $pointB = array(); + + 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 epcilon chosen is too large + if (PMA_GIS_Polygon::isPointInsidePolygon($pointA, $ring)) { + return $pointA; + } elseif (PMA_GIS_Polygon::isPointInsidePolygon($pointB, $ring)) { + return $pointB; + } else { + //If both are outside the polygon reduce the epsilon and + //recalculate the points(reduce exponentially for faster convergance) + $epsilon = PMA_Util::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) + { + if ($index == -1) { + $index = 0; + $params = array(); + $data = PMA_GIS_Geometry::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 = substr($wkt, 9, (strlen($wkt) - 11)); + // Seperate 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/libraries/gis/pma_gis_visualization.php b/libraries/gis/pma_gis_visualization.php new file mode 100644 index 0000000000..85f0e289bf --- /dev/null +++ b/libraries/gis/pma_gis_visualization.php @@ -0,0 +1,503 @@ +<?php +/* vim: set expandtab sw=4 ts=4 sts=4: */ +/** + * Handles visualization of GIS data + * + * @package PhpMyAdmin-GIS + */ + +if (! defined('PHPMYADMIN')) { + exit; +} + +/** + * Handles visualization of GIS data + * + * @package PhpMyAdmin-GIS + */ +class PMA_GIS_Visualization +{ + /** + * @var array Raw data for the visualization + */ + private $_data; + + /** + * @var array Set of default settigs values are here. + */ + private $_settings = array( + + // Array of colors to be used for GIS visualizations. + 'colors' => array( + '#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; + } + + /** + * Constructor. Stores user specified options. + * + * @param array $data Data for the visualization + * @param array $options Users specified options + * + * @access public + */ + public function __construct($data, $options) + { + $this->_userSpecifiedSettings = $options; + $this->_data = $data; + } + + /** + * All the variable initialization, options handling has to be done here. + * + * @return void + * @access protected + */ + protected function init() + { + $this->_handleOptions(); + } + + /** + * 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 (! is_null($this->_userSpecifiedSettings)) { + $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 = PMA_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 = strlen($file_name) - strlen($ext) - 1; + $user_extension = substr( + $file_name, $extension_start_pos, strlen($file_name) + ); + $required_extension = "." . $ext; + if (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); + PMA_downloadHeader($file_name, $type); + } + + /** + * Generate the visualization in SVG format. + * + * @return string the generated image resource + * @access private + */ + private function _svg() + { + $this->init(); + + $output = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . "\n"; + $output .= '<svg version="1.1" xmlns:svg="http://www.w3.org/2000/svg"' + . ' xmlns="http://www.w3.org/2000/svg"' + . ' width="' . $this->_settings['width'] . '"' + . ' height="' . $this->_settings['height'] . '">'; + $output .= '<g id="groupPanel">'; + + $scale_data = $this->_scaleDataSet($this->_data); + $output .= $this->_prepareDataSet($this->_data, $scale_data, 'svg', ''); + + $output .= '</g>'; + $output .= '</svg>'; + + return $output; + } + + /** + * Get the visualization as a SVG. + * + * @return string the visualization as a SVG + * @access public + */ + public function asSVG() + { + $output = $this->_svg(); + return $output; + } + + /** + * 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 '<img src="data:image/png;base64,'. $encoded .'" />'; + } + + /** + * 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. + * + * @return string the code for visualization with OpenLayers + * @access public + */ + public function asOl() + { + $this->init(); + $scale_data = $this->_scaleDataSet($this->_data); + $output + = '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 layerMapnik = new OpenLayers.Layer.OSM.Mapnik("Mapnik");' + . 'var layerCycleMap = new OpenLayers.Layer.OSM.CycleMap("CycleMap");' + . 'map.addLayers([layerMapnik,layerCycleMap,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(); + + include_once './libraries/tcpdf/tcpdf.php'; + + // 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'); + } + + /** + * 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($data) + { + $min_max = array(); + $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 = stripos($ref_data, '('); + $type = substr($ref_data, 0, $type_pos); + + $gis_obj = PMA_GIS_Factory::factory($type); + if (! $gis_obj) { + continue; + } + $scale_data = $gis_obj->scaleRow( + $row[$this->_settings['spatialColumn']] + ); + + // Upadate minimum/maximum values for x and y cordinates. + $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; + } + } + + // 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 array( + '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 visulaization + * @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($data, $scale_data, $format, $results) + { + $color_number = 0; + + // loop through the rows + foreach ($data as $row) { + $index = $color_number % sizeof($this->_settings['colors']); + + // Figure out the data type + $ref_data = $row[$this->_settings['spatialColumn']]; + $type_pos = stripos($ref_data, '('); + $type = substr($ref_data, 0, $type_pos); + + $gis_obj = PMA_GIS_Factory::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; + } +} +?> |