Initial commit

This commit is contained in:
2022-11-21 09:47:28 +01:00
commit 76cec83d26
11652 changed files with 1980467 additions and 0 deletions

View File

@ -0,0 +1,52 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Contains the factory class that handles the creation of geometric objects
*
* @package PhpMyAdmin-GIS
*/
namespace PMA\libraries\gis;
use PMA;
/**
* Factory class that handles the creation of geometric objects.
*
* @package PhpMyAdmin-GIS
*/
class GISFactory
{
/**
* Returns the singleton instance of geometric class of the given type.
*
* @param string $type type of the geometric object
*
* @return GISGeometry the singleton instance of geometric class
* of the given type
*
* @access public
* @static
*/
public static function factory($type)
{
switch (strtoupper($type)) {
case 'MULTIPOLYGON' :
return GISMultipolygon::singleton();
case 'POLYGON' :
return GISPolygon::singleton();
case 'MULTIPOINT' :
return GISMultipoint::singleton();
case 'POINT' :
return GISPoint::singleton();
case 'MULTILINESTRING' :
return GISMultilinestring::singleton();
case 'LINESTRING' :
return GISLinestring::singleton();
case 'GEOMETRYCOLLECTION' :
return GISGeometrycollection::singleton();
default :
return false;
}
}
}

View File

@ -0,0 +1,399 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Base class for all GIS data type classes
*
* @package PhpMyAdmin-GIS
*/
namespace PMA\libraries\gis;
use \TCPDF;
/**
* Base class for all GIS data type classes.
*
* @package PhpMyAdmin-GIS
*/
abstract class GISGeometry
{
/**
* 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 coordinates
* @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:'
. intval($srid) . '"), map.getProjectionObject())); '
. 'bound.extend(new OpenLayers.LonLat('
. $scale_data['maxX'] . ', ' . $scale_data['maxY']
. ').transform(new OpenLayers.Projection("EPSG:'
. intval($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)
{
// Separate each point
$points = explode(",", $point_set);
foreach ($points as $point) {
// Extract coordinates 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 = mb_strripos($value, ",");
$srid = trim(mb_substr($value, $last_comma + 1));
$wkt = trim(mb_substr($value, 1, $last_comma - 2));
} elseif (preg_match("/^" . $geom_types . "\(.*\)$/i", $value)) {
$wkt = $value;
}
return array('srid' => $srid, 'wkt' => $wkt);
}
/**
* Extracts points, scales and returns them as an array.
*
* @param string $point_set string of comma separated 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();
// Separate each point
$points = explode(",", $point_set);
foreach ($points as $point) {
// Extract coordinates of the point
$cordinates = explode(" ", $point);
if (!empty($cordinates[0]) && trim($cordinates[0]) != ''
&& isset($cordinates[1])
&& trim($cordinates[1]) != ''
) {
if ($scale_data != null) {
$x = ($cordinates[0] - $scale_data['x']) * $scale_data['scale'];
$y = $scale_data['height']
- ($cordinates[1] - $scale_data['y']) * $scale_data['scale'];
} else {
$x = floatval(trim($cordinates[0]));
$y = floatval(trim($cordinates[1]));
}
} else {
$x = '';
$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
= mb_substr(
$ol_array,
0,
mb_strlen($ol_array) - 2
);
$ol_array .= ')';
return $ol_array;
}
/**
* Generates JavaScript for adding points for OpenLayers polygon.
*
* @param array $polygon x and y coordinates for each line
* @param string $srid spatial reference id
*
* @return string JavaScript for adding points for OpenLayers polygon
* @access protected
*/
protected function getPolygonForOpenLayers($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
= mb_substr(
$ol_array,
0,
mb_strlen($ol_array) - 2
);
$ol_array .= ')';
return $ol_array;
}
/**
* Generates JavaScript for adding a LineString or LineRing to OpenLayers.
*
* @param array $points_arr x and y coordinates for each point
* @param string $srid spatial reference id
* @param bool $is_line_string whether it's a LineString
*
* @return string JavaScript for adding a LineString or LineRing to OpenLayers
* @access protected
*/
protected function getLineForOpenLayers(
$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
= mb_substr(
$ol_array,
0,
mb_strlen($ol_array) - 2
);
$ol_array .= ')';
return $ol_array;
}
/**
* Generates JavaScript for adding a point to OpenLayers.
*
* @param array $point array containing the x and y coordinates of the point
* @param string $srid spatial reference id
*
* @return string JavaScript for adding points to OpenLayers
* @access protected
*/
protected function getPointForOpenLayers($point, $srid)
{
return '(new OpenLayers.Geometry.Point(' . $point[0] . ',' . $point[1] . '))'
. '.transform(new OpenLayers.Projection("EPSG:'
. intval($srid) . '"), map.getProjectionObject())';
}
}

View File

@ -0,0 +1,416 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Handles actions related to GIS GEOMETRYCOLLECTION objects
*
* @package PhpMyAdmin-GIS
*/
namespace PMA\libraries\gis;
use \TCPDF;
/**
* Handles actions related to GIS GEOMETRYCOLLECTION objects
*
* @package PhpMyAdmin-GIS
*/
class GISGeometrycollection extends GISGeometry
{
// 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 GISGeometrycollection 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 coordinates
* @access public
*/
public function scaleRow($spatial)
{
$min_max = array();
// Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
$goem_col
= mb_substr(
$spatial,
19,
mb_strlen($spatial) - 20
);
// Split the geometry collection object to get its constituents.
$sub_parts = $this->_explodeGeomCol($goem_col);
foreach ($sub_parts as $sub_part) {
$type_pos = mb_strpos($sub_part, '(');
if ($type_pos === false) {
continue;
}
$type = mb_substr($sub_part, 0, $type_pos);
$gis_obj = GISFactory::factory($type);
if (!$gis_obj) {
continue;
}
$scale_data = $gis_obj->scaleRow($sub_part);
// Update minimum/maximum values for x and y coordinates.
$c_maxX = (float)$scale_data['maxX'];
if (!isset($min_max['maxX']) || $c_maxX > $min_max['maxX']) {
$min_max['maxX'] = $c_maxX;
}
$c_minX = (float)$scale_data['minX'];
if (!isset($min_max['minX']) || $c_minX < $min_max['minX']) {
$min_max['minX'] = $c_minX;
}
$c_maxY = (float)$scale_data['maxY'];
if (!isset($min_max['maxY']) || $c_maxY > $min_max['maxY']) {
$min_max['maxY'] = $c_maxY;
}
$c_minY = (float)$scale_data['minY'];
if (!isset($min_max['minY']) || $c_minY < $min_max['minY']) {
$min_max['minY'] = $c_minY;
}
}
return $min_max;
}
/**
* Adds to the PNG image object, the data related to a row in the GIS dataset.
*
* @param string $spatial GIS 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
= mb_substr(
$spatial,
19,
mb_strlen($spatial) - 20
);
// Split the geometry collection object to get its constituents.
$sub_parts = $this->_explodeGeomCol($goem_col);
foreach ($sub_parts as $sub_part) {
$type_pos = mb_strpos($sub_part, '(');
if ($type_pos === false) {
continue;
}
$type = mb_substr($sub_part, 0, $type_pos);
$gis_obj = GISFactory::factory($type);
if (!$gis_obj) {
continue;
}
$image = $gis_obj->prepareRowAsPng(
$sub_part,
$label,
$color,
$scale_data,
$image
);
}
return $image;
}
/**
* Adds to the TCPDF instance, the data related to a row in the GIS dataset.
*
* @param string $spatial GIS GEOMETRYCOLLECTION object
* @param string $label label for the GIS GEOMETRYCOLLECTION object
* @param string $color color for the GIS GEOMETRYCOLLECTION object
* @param array $scale_data array containing data related to scaling
* @param TCPDF $pdf TCPDF instance
*
* @return TCPDF the modified TCPDF instance
* @access public
*/
public function prepareRowAsPdf($spatial, $label, $color, $scale_data, $pdf)
{
// Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
$goem_col
= mb_substr(
$spatial,
19,
mb_strlen($spatial) - 20
);
// Split the geometry collection object to get its constituents.
$sub_parts = $this->_explodeGeomCol($goem_col);
foreach ($sub_parts as $sub_part) {
$type_pos = mb_strpos($sub_part, '(');
if ($type_pos === false) {
continue;
}
$type = mb_substr($sub_part, 0, $type_pos);
$gis_obj = GISFactory::factory($type);
if (!$gis_obj) {
continue;
}
$pdf = $gis_obj->prepareRowAsPdf(
$sub_part,
$label,
$color,
$scale_data,
$pdf
);
}
return $pdf;
}
/**
* Prepares and returns the code related to a row in the GIS dataset as SVG.
*
* @param string $spatial GIS GEOMETRYCOLLECTION object
* @param string $label label for the GIS GEOMETRYCOLLECTION object
* @param string $color color for the GIS GEOMETRYCOLLECTION object
* @param array $scale_data array containing data related to scaling
*
* @return string the code related to a row in the GIS dataset
* @access public
*/
public function prepareRowAsSvg($spatial, $label, $color, $scale_data)
{
$row = '';
// Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
$goem_col
= mb_substr(
$spatial,
19,
mb_strlen($spatial) - 20
);
// Split the geometry collection object to get its constituents.
$sub_parts = $this->_explodeGeomCol($goem_col);
foreach ($sub_parts as $sub_part) {
$type_pos = mb_strpos($sub_part, '(');
if ($type_pos === false) {
continue;
}
$type = mb_substr($sub_part, 0, $type_pos);
$gis_obj = GISFactory::factory($type);
if (!$gis_obj) {
continue;
}
$row .= $gis_obj->prepareRowAsSvg(
$sub_part,
$label,
$color,
$scale_data
);
}
return $row;
}
/**
* Prepares JavaScript related to a row in the GIS dataset
* to visualize it with OpenLayers.
*
* @param string $spatial GIS GEOMETRYCOLLECTION object
* @param int $srid spatial reference ID
* @param string $label label for the GIS GEOMETRYCOLLECTION object
* @param string $color color for the GIS GEOMETRYCOLLECTION object
* @param array $scale_data array containing data related to scaling
*
* @return string JavaScript related to a row in the GIS dataset
* @access public
*/
public function prepareRowAsOl($spatial, $srid, $label, $color, $scale_data)
{
$row = '';
// Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
$goem_col
= mb_substr(
$spatial,
19,
mb_strlen($spatial) - 20
);
// Split the geometry collection object to get its constituents.
$sub_parts = $this->_explodeGeomCol($goem_col);
foreach ($sub_parts as $sub_part) {
$type_pos = mb_strpos($sub_part, '(');
if ($type_pos === false) {
continue;
}
$type = mb_substr($sub_part, 0, $type_pos);
$gis_obj = GISFactory::factory($type);
if (!$gis_obj) {
continue;
}
$row .= $gis_obj->prepareRowAsOl(
$sub_part,
$srid,
$label,
$color,
$scale_data
);
}
return $row;
}
/**
* Splits the GEOMETRYCOLLECTION object and get its constituents.
*
* @param string $geom_col geometry collection string
*
* @return array the constituents of the geometry collection object
* @access private
*/
private function _explodeGeomCol($geom_col)
{
$sub_parts = array();
$br_count = 0;
$start = 0;
$count = 0;
foreach (str_split($geom_col) as $char) {
if ($char == '(') {
$br_count++;
} elseif ($char == ')') {
$br_count--;
if ($br_count == 0) {
$sub_parts[]
= mb_substr(
$geom_col,
$start,
($count + 1 - $start)
);
$start = $count + 2;
}
}
$count++;
}
return $sub_parts;
}
/**
* Generates the WKT with the set of parameters passed by the GIS editor.
*
* @param array $gis_data GIS data
* @param int $index index into the parameter object
* @param string $empty value for empty points
*
* @return string WKT with the set of parameters passed by the GIS editor
* @access public
*/
public function generateWkt($gis_data, $index, $empty = '')
{
$geom_count = (isset($gis_data['GEOMETRYCOLLECTION']['geom_count']))
? $gis_data['GEOMETRYCOLLECTION']['geom_count'] : 1;
$wkt = 'GEOMETRYCOLLECTION(';
for ($i = 0; $i < $geom_count; $i++) {
if (isset($gis_data[$i]['gis_type'])) {
$type = $gis_data[$i]['gis_type'];
$gis_obj = GISFactory::factory($type);
if (!$gis_obj) {
continue;
}
$wkt .= $gis_obj->generateWkt($gis_data, $i, $empty) . ',';
}
}
if (isset($gis_data[0]['gis_type'])) {
$wkt
= mb_substr(
$wkt,
0,
mb_strlen($wkt) - 1
);
}
$wkt .= ')';
return $wkt;
}
/**
* Generates parameters for the GIS data editor from the value of the GIS column.
*
* @param string $value of the GIS column
*
* @return array parameters for the GIS editor from the value of the GIS column
* @access public
*/
public function generateParams($value)
{
$params = array();
$data = GISGeometry::generateParams($value);
$params['srid'] = $data['srid'];
$wkt = $data['wkt'];
// Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
$goem_col
= mb_substr(
$wkt,
19,
mb_strlen($wkt) - 20
);
// Split the geometry collection object to get its constituents.
$sub_parts = $this->_explodeGeomCol($goem_col);
$params['GEOMETRYCOLLECTION']['geom_count'] = count($sub_parts);
$i = 0;
foreach ($sub_parts as $sub_part) {
$type_pos = mb_strpos($sub_part, '(');
if ($type_pos === false) {
continue;
}
$type = mb_substr($sub_part, 0, $type_pos);
$gis_obj = GISFactory::factory($type);
if (!$gis_obj) {
continue;
}
$params = array_merge($params, $gis_obj->generateParams($sub_part, $i));
$i++;
}
return $params;
}
}

View File

@ -0,0 +1,353 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Handles actions related to GIS LINESTRING objects
*
* @package PhpMyAdmin-GIS
*/
namespace PMA\libraries\gis;
use \TCPDF;
/**
* Handles actions related to GIS LINESTRING objects
*
* @package PhpMyAdmin-GIS
*/
class GISLinestring extends GISGeometry
{
// 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 GISLinestring 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 coordinates
* @access public
*/
public function scaleRow($spatial)
{
// Trim to remove leading 'LINESTRING(' and trailing ')'
$linestring
= mb_substr(
$spatial,
11,
mb_strlen($spatial) - 12
);
return $this->setMinMax($linestring, 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(mb_substr($line_color, 1, 2));
$green = hexdec(mb_substr($line_color, 3, 2));
$blue = hexdec(mb_substr($line_color, 4, 2));
$color = imagecolorallocate($image, $red, $green, $blue);
// Trim to remove leading 'LINESTRING(' and trailing ')'
$linesrting
= mb_substr(
$spatial,
11,
mb_strlen($spatial) - 12
);
$points_arr = $this->extractPoints($linesrting, $scale_data);
foreach ($points_arr as $point) {
if (!isset($temp_point)) {
$temp_point = $point;
} else {
// draw line section
imageline(
$image,
$temp_point[0],
$temp_point[1],
$point[0],
$point[1],
$color
);
$temp_point = $point;
}
}
// print label if applicable
if (isset($label) && trim($label) != '') {
imagestring(
$image,
1,
$points_arr[1][0],
$points_arr[1][1],
trim($label),
$black
);
}
return $image;
}
/**
* Adds to the TCPDF instance, the data related to a row in the GIS dataset.
*
* @param string $spatial GIS LINESTRING object
* @param string $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(mb_substr($line_color, 1, 2));
$green = hexdec(mb_substr($line_color, 3, 2));
$blue = hexdec(mb_substr($line_color, 4, 2));
$line = array('width' => 1.5, 'color' => array($red, $green, $blue));
// Trim to remove leading 'LINESTRING(' and trailing ')'
$linesrting
= mb_substr(
$spatial,
11,
mb_strlen($spatial) - 12
);
$points_arr = $this->extractPoints($linesrting, $scale_data);
foreach ($points_arr as $point) {
if (!isset($temp_point)) {
$temp_point = $point;
} else {
// draw line section
$pdf->Line(
$temp_point[0],
$temp_point[1],
$point[0],
$point[1],
$line
);
$temp_point = $point;
}
}
// print label
if (isset($label) && trim($label) != '') {
$pdf->SetXY($points_arr[1][0], $points_arr[1][1]);
$pdf->SetFontSize(5);
$pdf->Cell(0, 0, trim($label));
}
return $pdf;
}
/**
* Prepares and returns the code related to a row in the GIS dataset as SVG.
*
* @param string $spatial GIS LINESTRING object
* @param string $label Label for the GIS LINESTRING object
* @param string $line_color Color for the GIS LINESTRING object
* @param array $scale_data Array containing data related to scaling
*
* @return string the code related to a row in the GIS dataset
* @access public
*/
public function prepareRowAsSvg($spatial, $label, $line_color, $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
= mb_substr(
$spatial,
11,
mb_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
= mb_substr(
$spatial,
11,
mb_strlen($spatial) - 12
);
$points_arr = $this->extractPoints($linesrting, null);
$result .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector('
. $this->getLineForOpenLayers($points_arr, $srid)
. ', null, ' . json_encode($style_options) . '));';
return $result;
}
/**
* Generate the WKT with the set of parameters passed by the GIS editor.
*
* @param array $gis_data GIS data
* @param int $index Index into the parameter object
* @param string $empty Value for empty points
*
* @return string WKT with the set of parameters passed by the GIS editor
* @access public
*/
public function generateWkt($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
= mb_substr(
$wkt,
0,
mb_strlen($wkt) - 1
);
$wkt .= ')';
return $wkt;
}
/**
* Generate parameters for the GIS data editor from the value of the GIS column.
*
* @param string $value of the GIS column
* @param int $index of the geometry
*
* @return array params for the GIS data editor from the value of the GIS column
* @access public
*/
public function generateParams($value, $index = -1)
{
$params = array();
if ($index == -1) {
$index = 0;
$data = GISGeometry::generateParams($value);
$params['srid'] = $data['srid'];
$wkt = $data['wkt'];
} else {
$params[$index]['gis_type'] = 'LINESTRING';
$wkt = $value;
}
// Trim to remove leading 'LINESTRING(' and trailing ')'
$linestring
= mb_substr(
$wkt,
11,
mb_strlen($wkt) - 12
);
$points_arr = $this->extractPoints($linestring, null);
$no_of_points = count($points_arr);
$params[$index]['LINESTRING']['no_of_points'] = $no_of_points;
for ($i = 0; $i < $no_of_points; $i++) {
$params[$index]['LINESTRING'][$i]['x'] = $points_arr[$i][0];
$params[$index]['LINESTRING'][$i]['y'] = $points_arr[$i][1];
}
return $params;
}
}

View File

@ -0,0 +1,441 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Handles actions related to GIS MULTILINESTRING objects
*
* @package PhpMyAdmin-GIS
*/
namespace PMA\libraries\gis;
use \TCPDF;
/**
* Handles actions related to GIS MULTILINESTRING objects
*
* @package PhpMyAdmin-GIS
*/
class GISMultilinestring extends GISGeometry
{
// 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 GISMultilinestring 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 coordinates
* @access public
*/
public function scaleRow($spatial)
{
$min_max = array();
// Trim to remove leading 'MULTILINESTRING((' and trailing '))'
$multilinestirng
= mb_substr(
$spatial,
17,
mb_strlen($spatial) - 19
);
// Separate 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(mb_substr($line_color, 1, 2));
$green = hexdec(mb_substr($line_color, 3, 2));
$blue = hexdec(mb_substr($line_color, 4, 2));
$color = imagecolorallocate($image, $red, $green, $blue);
// Trim to remove leading 'MULTILINESTRING((' and trailing '))'
$multilinestirng
= mb_substr(
$spatial,
17,
mb_strlen($spatial) - 19
);
// Separate each linestring
$linestirngs = explode("),(", $multilinestirng);
$first_line = true;
foreach ($linestirngs as $linestring) {
$points_arr = $this->extractPoints($linestring, $scale_data);
foreach ($points_arr as $point) {
if (!isset($temp_point)) {
$temp_point = $point;
} else {
// draw line section
imageline(
$image,
$temp_point[0],
$temp_point[1],
$point[0],
$point[1],
$color
);
$temp_point = $point;
}
}
unset($temp_point);
// print label if applicable
if (isset($label) && trim($label) != '' && $first_line) {
imagestring(
$image,
1,
$points_arr[1][0],
$points_arr[1][1],
trim($label),
$black
);
}
$first_line = false;
}
return $image;
}
/**
* Adds to the TCPDF instance, the data related to a row in the GIS dataset.
*
* @param string $spatial GIS MULTILINESTRING object
* @param string $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(mb_substr($line_color, 1, 2));
$green = hexdec(mb_substr($line_color, 3, 2));
$blue = hexdec(mb_substr($line_color, 4, 2));
$line = array('width' => 1.5, 'color' => array($red, $green, $blue));
// Trim to remove leading 'MULTILINESTRING((' and trailing '))'
$multilinestirng
= mb_substr(
$spatial,
17,
mb_strlen($spatial) - 19
);
// Separate each linestring
$linestirngs = explode("),(", $multilinestirng);
$first_line = true;
foreach ($linestirngs as $linestring) {
$points_arr = $this->extractPoints($linestring, $scale_data);
foreach ($points_arr as $point) {
if (!isset($temp_point)) {
$temp_point = $point;
} else {
// draw line section
$pdf->Line(
$temp_point[0],
$temp_point[1],
$point[0],
$point[1],
$line
);
$temp_point = $point;
}
}
unset($temp_point);
// print label
if (isset($label) && trim($label) != '' && $first_line) {
$pdf->SetXY($points_arr[1][0], $points_arr[1][1]);
$pdf->SetFontSize(5);
$pdf->Cell(0, 0, trim($label));
}
$first_line = false;
}
return $pdf;
}
/**
* Prepares and returns the code related to a row in the GIS dataset as SVG.
*
* @param string $spatial GIS MULTILINESTRING object
* @param string $label Label for the GIS MULTILINESTRING object
* @param string $line_color Color for the GIS MULTILINESTRING object
* @param array $scale_data Array containing data related to scaling
*
* @return string the code related to a row in the GIS dataset
* @access public
*/
public function prepareRowAsSvg($spatial, $label, $line_color, $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
= mb_substr(
$spatial,
17,
mb_strlen($spatial) - 19
);
// Separate each linestring
$linestirngs = explode("),(", $multilinestirng);
$row = '';
foreach ($linestirngs as $linestring) {
$points_arr = $this->extractPoints($linestring, $scale_data);
$row .= '<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
= mb_substr(
$spatial,
17,
mb_strlen($spatial) - 19
);
// Separate each linestring
$linestirngs = explode("),(", $multilinestirng);
$row .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector('
. 'new OpenLayers.Geometry.MultiLineString('
. $this->getLineArrayForOpenLayers($linestirngs, $srid)
. '), null, ' . json_encode($style_options) . '));';
return $row;
}
/**
* Generate the WKT with the set of parameters passed by the GIS editor.
*
* @param array $gis_data GIS data
* @param int $index Index into the parameter object
* @param string $empty Value for empty points
*
* @return string WKT with the set of parameters passed by the GIS editor
* @access public
*/
public function generateWkt($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
= mb_substr(
$wkt,
0, mb_strlen($wkt) - 1
);
$wkt .= '),';
}
$wkt
= mb_substr(
$wkt,
0,
mb_strlen($wkt) - 1
);
$wkt .= ')';
return $wkt;
}
/**
* Generate the WKT for the data from ESRI shape files.
*
* @param array $row_data GIS data
*
* @return string the WKT for the data from ESRI shape files
* @access public
*/
public function getShape($row_data)
{
$wkt = 'MULTILINESTRING(';
for ($i = 0; $i < $row_data['numparts']; $i++) {
$wkt .= '(';
foreach ($row_data['parts'][$i]['points'] as $point) {
$wkt .= $point['x'] . ' ' . $point['y'] . ',';
}
$wkt
= mb_substr(
$wkt,
0,
mb_strlen($wkt) - 1
);
$wkt .= '),';
}
$wkt
= mb_substr(
$wkt,
0,
mb_strlen($wkt) - 1
);
$wkt .= ')';
return $wkt;
}
/**
* Generate parameters for the GIS data editor from the value of the GIS column.
*
* @param string $value Value of the GIS column
* @param int $index Index of the geometry
*
* @return array params for the GIS data editor from the value of the GIS column
* @access public
*/
public function generateParams($value, $index = -1)
{
$params = array();
if ($index == -1) {
$index = 0;
$data = GISGeometry::generateParams($value);
$params['srid'] = $data['srid'];
$wkt = $data['wkt'];
} else {
$params[$index]['gis_type'] = 'MULTILINESTRING';
$wkt = $value;
}
// Trim to remove leading 'MULTILINESTRING((' and trailing '))'
$multilinestirng
= mb_substr(
$wkt,
17,
mb_strlen($wkt) - 19
);
// Separate each linestring
$linestirngs = explode("),(", $multilinestirng);
$params[$index]['MULTILINESTRING']['no_of_lines'] = count($linestirngs);
$j = 0;
foreach ($linestirngs as $linestring) {
$points_arr = $this->extractPoints($linestring, null);
$no_of_points = count($points_arr);
$params[$index]['MULTILINESTRING'][$j]['no_of_points'] = $no_of_points;
for ($i = 0; $i < $no_of_points; $i++) {
$params[$index]['MULTILINESTRING'][$j][$i]['x'] = $points_arr[$i][0];
$params[$index]['MULTILINESTRING'][$j][$i]['y'] = $points_arr[$i][1];
}
$j++;
}
return $params;
}
}

View File

@ -0,0 +1,409 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Handles actions related to GIS MULTIPOINT objects
*
* @package PhpMyAdmin-GIS
*/
namespace PMA\libraries\gis;
use \TCPDF;
/**
* Handles actions related to GIS MULTIPOINT objects
*
* @package PhpMyAdmin-GIS
*/
class GISMultipoint extends GISGeometry
{
// 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 GISMultipoint 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 coordinates
* @access public
*/
public function scaleRow($spatial)
{
// Trim to remove leading 'MULTIPOINT(' and trailing ')'
$multipoint
= mb_substr(
$spatial,
11,
mb_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(mb_substr($point_color, 1, 2));
$green = hexdec(mb_substr($point_color, 3, 2));
$blue = hexdec(mb_substr($point_color, 4, 2));
$color = imagecolorallocate($image, $red, $green, $blue);
// Trim to remove leading 'MULTIPOINT(' and trailing ')'
$multipoint
= mb_substr(
$spatial,
11,
mb_strlen($spatial) - 12
);
$points_arr = $this->extractPoints($multipoint, $scale_data);
foreach ($points_arr as $point) {
// draw a small circle to mark the point
if ($point[0] != '' && $point[1] != '') {
imagearc($image, $point[0], $point[1], 7, 7, 0, 360, $color);
}
}
// print label for each point
if ((isset($label) && trim($label) != '')
&& ($points_arr[0][0] != '' && $points_arr[0][1] != '')
) {
imagestring(
$image,
1,
$points_arr[0][0],
$points_arr[0][1],
trim($label),
$black
);
}
return $image;
}
/**
* Adds to the TCPDF instance, the data related to a row in the GIS dataset.
*
* @param string $spatial GIS MULTIPOINT object
* @param string $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(mb_substr($point_color, 1, 2));
$green = hexdec(mb_substr($point_color, 3, 2));
$blue = hexdec(mb_substr($point_color, 4, 2));
$line = array('width' => 1.25, 'color' => array($red, $green, $blue));
// Trim to remove leading 'MULTIPOINT(' and trailing ')'
$multipoint
= mb_substr(
$spatial,
11,
mb_strlen($spatial) - 12
);
$points_arr = $this->extractPoints($multipoint, $scale_data);
foreach ($points_arr as $point) {
// draw a small circle to mark the point
if ($point[0] != '' && $point[1] != '') {
$pdf->Circle($point[0], $point[1], 2, 0, 360, 'D', $line);
}
}
// print label for each point
if ((isset($label) && trim($label) != '')
&& ($points_arr[0][0] != '' && $points_arr[0][1] != '')
) {
$pdf->SetXY($points_arr[0][0], $points_arr[0][1]);
$pdf->SetFontSize(5);
$pdf->Cell(0, 0, trim($label));
}
return $pdf;
}
/**
* Prepares and returns the code related to a row in the GIS dataset as SVG.
*
* @param string $spatial GIS MULTIPOINT object
* @param string $label Label for the GIS MULTIPOINT object
* @param string $point_color Color for the GIS MULTIPOINT object
* @param array $scale_data Array containing data related to scaling
*
* @return string the code related to a row in the GIS dataset
* @access public
*/
public function prepareRowAsSvg($spatial, $label, $point_color, $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
= mb_substr(
$spatial,
11,
mb_strlen($spatial) - 12
);
$points_arr = $this->extractPoints($multipoint, $scale_data);
$row = '';
foreach ($points_arr as $point) {
if ($point[0] != '' && $point[1] != '') {
$row .= '<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
= mb_substr(
$spatial,
11,
mb_strlen($spatial) - 12
);
$points_arr = $this->extractPoints($multipoint, null);
$result .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector('
. 'new OpenLayers.Geometry.MultiPoint('
. $this->getPointsArrayForOpenLayers($points_arr, $srid)
. '), null, ' . json_encode($style_options) . '));';
return $result;
}
/**
* Generate the WKT with the set of parameters passed by the GIS editor.
*
* @param array $gis_data GIS data
* @param int $index Index into the parameter object
* @param string $empty Multipoint does not adhere to this
*
* @return string WKT with the set of parameters passed by the GIS editor
* @access public
*/
public function generateWkt($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
= mb_substr(
$wkt,
0,
mb_strlen($wkt) - 1
);
$wkt .= ')';
return $wkt;
}
/**
* Generate the WKT for the data from ESRI shape files.
*
* @param array $row_data GIS data
*
* @return string the WKT for the data from ESRI shape files
* @access public
*/
public function getShape($row_data)
{
$wkt = 'MULTIPOINT(';
for ($i = 0; $i < $row_data['numpoints']; $i++) {
$wkt .= $row_data['points'][$i]['x'] . ' '
. $row_data['points'][$i]['y'] . ',';
}
$wkt
= mb_substr(
$wkt,
0,
mb_strlen($wkt) - 1
);
$wkt .= ')';
return $wkt;
}
/**
* Generate parameters for the GIS data editor from the value of the GIS column.
*
* @param string $value Value of the GIS column
* @param integer $index Index of the geometry
*
* @return array params for the GIS data editor from the value of the GIS column
* @access public
*/
public function generateParams($value, $index = -1)
{
$params = array();
if ($index == -1) {
$index = 0;
$data = GISGeometry::generateParams($value);
$params['srid'] = $data['srid'];
$wkt = $data['wkt'];
} else {
$params[$index]['gis_type'] = 'MULTIPOINT';
$wkt = $value;
}
// Trim to remove leading 'MULTIPOINT(' and trailing ')'
$points
= mb_substr(
$wkt,
11,
mb_strlen($wkt) - 12
);
$points_arr = $this->extractPoints($points, null);
$no_of_points = count($points_arr);
$params[$index]['MULTIPOINT']['no_of_points'] = $no_of_points;
for ($i = 0; $i < $no_of_points; $i++) {
$params[$index]['MULTIPOINT'][$i]['x'] = $points_arr[$i][0];
$params[$index]['MULTIPOINT'][$i]['y'] = $points_arr[$i][1];
}
return $params;
}
/**
* Overridden to make sure that only the points having valid values
* for x and y coordinates are added.
*
* @param array $points_arr x and y coordinates for each point
* @param string $srid spatial reference id
*
* @return string JavaScript for adding an array of points to OpenLayers
* @access protected
*/
protected function getPointsArrayForOpenLayers($points_arr, $srid)
{
$ol_array = 'new Array(';
foreach ($points_arr as $point) {
if ($point[0] != '' && $point[1] != '') {
$ol_array .= $this->getPointForOpenLayers($point, $srid) . ', ';
}
}
$olArrayLength = mb_strlen($ol_array);
if (mb_substr($ol_array, $olArrayLength - 2) == ', ') {
$ol_array = mb_substr($ol_array, 0, $olArrayLength - 2);
}
$ol_array .= ')';
return $ol_array;
}
}

View File

@ -0,0 +1,606 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Handles actions related to GIS MULTIPOLYGON objects
*
* @package PhpMyAdmin-GIS
*/
namespace PMA\libraries\gis;
use \TCPDF;
/**
* Handles actions related to GIS MULTIPOLYGON objects
*
* @package PhpMyAdmin-GIS
*/
class GISMultipolygon extends GISGeometry
{
// 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 GISMultipolygon 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 coordinates
* @access public
*/
public function scaleRow($spatial)
{
$min_max = array();
// Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
$multipolygon
= mb_substr(
$spatial,
15,
mb_strlen($spatial) - 18
);
// Separate each polygon
$polygons = explode(")),((", $multipolygon);
foreach ($polygons as $polygon) {
// If the polygon doesn't have an inner ring, use polygon itself
if (mb_strpos($polygon, "),(") === false) {
$ring = $polygon;
} else {
// Separate outer ring and use it to determine 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(mb_substr($fill_color, 1, 2));
$green = hexdec(mb_substr($fill_color, 3, 2));
$blue = hexdec(mb_substr($fill_color, 4, 2));
$color = imagecolorallocate($image, $red, $green, $blue);
// Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
$multipolygon
= mb_substr(
$spatial,
15,
mb_strlen($spatial) - 18
);
// Separate each polygon
$polygons = explode(")),((", $multipolygon);
$first_poly = true;
foreach ($polygons as $polygon) {
// If the polygon doesn't have an inner polygon
if (mb_strpos($polygon, "),(") === false) {
$points_arr = $this->extractPoints($polygon, $scale_data, true);
} else {
// Separate outer and inner polygons
$parts = explode("),(", $polygon);
$outer = $parts[0];
$inner = array_slice($parts, 1);
$points_arr = $this->extractPoints($outer, $scale_data, true);
foreach ($inner as $inner_poly) {
$points_arr = array_merge(
$points_arr,
$this->extractPoints($inner_poly, $scale_data, true)
);
}
}
// draw polygon
imagefilledpolygon($image, $points_arr, 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(mb_substr($fill_color, 1, 2));
$green = hexdec(mb_substr($fill_color, 3, 2));
$blue = hexdec(mb_substr($fill_color, 4, 2));
$color = array($red, $green, $blue);
// Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
$multipolygon
= mb_substr(
$spatial,
15,
mb_strlen($spatial) - 18
);
// Separate each polygon
$polygons = explode(")),((", $multipolygon);
$first_poly = true;
foreach ($polygons as $polygon) {
// If the polygon doesn't have an inner polygon
if (mb_strpos($polygon, "),(") === false) {
$points_arr = $this->extractPoints($polygon, $scale_data, true);
} else {
// Separate outer and inner polygons
$parts = explode("),(", $polygon);
$outer = $parts[0];
$inner = array_slice($parts, 1);
$points_arr = $this->extractPoints($outer, $scale_data, true);
foreach ($inner as $inner_poly) {
$points_arr = array_merge(
$points_arr,
$this->extractPoints($inner_poly, $scale_data, true)
);
}
}
// draw polygon
$pdf->Polygon($points_arr, 'F*', 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
= mb_substr(
$spatial,
15,
mb_strlen($spatial) - 18
);
// Separate each polygon
$polygons = explode(")),((", $multipolygon);
foreach ($polygons as $polygon) {
$row .= '<path d="';
// If the polygon doesn't have an inner polygon
if (mb_strpos($polygon, "),(") === false) {
$row .= $this->_drawPath($polygon, $scale_data);
} else {
// Separate outer and inner polygons
$parts = explode("),(", $polygon);
$outer = $parts[0];
$inner = array_slice($parts, 1);
$row .= $this->_drawPath($outer, $scale_data);
foreach ($inner as $inner_poly) {
$row .= $this->_drawPath($inner_poly, $scale_data);
}
}
$polygon_options['id'] = $label . 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
= mb_substr(
$spatial,
15,
mb_strlen($spatial) - 18
);
// Separate each polygon
$polygons = explode(")),((", $multipolygon);
$row .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector('
. 'new OpenLayers.Geometry.MultiPolygon('
. $this->getPolygonArrayForOpenLayers($polygons, $srid)
. '), null, ' . json_encode($style_options) . '));';
return $row;
}
/**
* Draws a ring of the polygon using SVG path element.
*
* @param string $polygon The ring
* @param array $scale_data Array containing data related to scaling
*
* @return string the code to draw the ring
* @access private
*/
private function _drawPath($polygon, $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
= mb_substr(
$wkt,
0,
mb_strlen($wkt) - 1
);
$wkt .= '),';
}
$wkt
= mb_substr(
$wkt,
0,
mb_strlen($wkt) - 1
);
$wkt .= '),';
}
$wkt
= mb_substr(
$wkt,
0,
mb_strlen($wkt) - 1
);
$wkt .= ')';
return $wkt;
}
/**
* Generate the WKT for the data from ESRI shape files.
*
* @param array $row_data GIS data
*
* @return string the WKT for the data from ESRI shape files
* @access public
*/
public function getShape($row_data)
{
// Determines whether each line ring is an inner ring or an outer ring.
// If it's an inner ring get a point on the surface which can be used to
// correctly classify inner rings to their respective outer rings.
foreach ($row_data['parts'] as $i => $ring) {
$row_data['parts'][$i]['isOuter']
= GISPolygon::isOuterRing($ring['points']);
}
// Find points on surface for inner rings
foreach ($row_data['parts'] as $i => $ring) {
if (!$ring['isOuter']) {
$row_data['parts'][$i]['pointOnSurface']
= GISPolygon::getPointOnSurface($ring['points']);
}
}
// Classify inner rings to their respective outer rings.
foreach ($row_data['parts'] as $j => $ring1) {
if ($ring1['isOuter']) {
continue;
}
foreach ($row_data['parts'] as $k => $ring2) {
if (!$ring2['isOuter']) {
continue;
}
// If the pointOnSurface of the inner ring
// is also inside the outer ring
if (GISPolygon::isPointInsidePolygon(
$ring1['pointOnSurface'],
$ring2['points']
)
) {
if (!isset($ring2['inner'])) {
$row_data['parts'][$k]['inner'] = array();
}
$row_data['parts'][$k]['inner'][] = $j;
}
}
}
$wkt = 'MULTIPOLYGON(';
// for each polygon
foreach ($row_data['parts'] as $ring) {
if (!$ring['isOuter']) {
continue;
}
$wkt .= '('; // start of polygon
$wkt .= '('; // start of outer ring
foreach ($ring['points'] as $point) {
$wkt .= $point['x'] . ' ' . $point['y'] . ',';
}
$wkt
= mb_substr(
$wkt,
0,
mb_strlen($wkt) - 1
);
$wkt .= ')'; // end of outer ring
// inner rings if any
if (isset($ring['inner'])) {
foreach ($ring['inner'] as $j) {
$wkt .= ',('; // start of inner ring
foreach ($row_data['parts'][$j]['points'] as $innerPoint) {
$wkt .= $innerPoint['x'] . ' ' . $innerPoint['y'] . ',';
}
$wkt
= mb_substr(
$wkt,
0,
mb_strlen($wkt) - 1
);
$wkt .= ')'; // end of inner ring
}
}
$wkt .= '),'; // end of polygon
}
$wkt
= mb_substr(
$wkt,
0,
mb_strlen($wkt) - 1
);
$wkt .= ')'; // end of multipolygon
return $wkt;
}
/**
* Generate parameters for the GIS data editor from the value of the GIS column.
*
* @param string $value Value of the GIS column
* @param int $index Index of the geometry
*
* @return array params for the GIS data editor from the value of the GIS column
* @access public
*/
public function generateParams($value, $index = -1)
{
$params = array();
if ($index == -1) {
$index = 0;
$data = GISGeometry::generateParams($value);
$params['srid'] = $data['srid'];
$wkt = $data['wkt'];
} else {
$params[$index]['gis_type'] = 'MULTIPOLYGON';
$wkt = $value;
}
// Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
$multipolygon
= mb_substr(
$wkt,
15,
mb_strlen($wkt) - 18
);
// Separate each polygon
$polygons = explode(")),((", $multipolygon);
$param_row =& $params[$index]['MULTIPOLYGON'];
$param_row['no_of_polygons'] = count($polygons);
$k = 0;
foreach ($polygons as $polygon) {
// If the polygon doesn't have an inner polygon
if (mb_strpos($polygon, "),(") === false) {
$param_row[$k]['no_of_lines'] = 1;
$points_arr = $this->extractPoints($polygon, null);
$no_of_points = count($points_arr);
$param_row[$k][0]['no_of_points'] = $no_of_points;
for ($i = 0; $i < $no_of_points; $i++) {
$param_row[$k][0][$i]['x'] = $points_arr[$i][0];
$param_row[$k][0][$i]['y'] = $points_arr[$i][1];
}
} else {
// Separate outer and inner polygons
$parts = explode("),(", $polygon);
$param_row[$k]['no_of_lines'] = count($parts);
$j = 0;
foreach ($parts as $ring) {
$points_arr = $this->extractPoints($ring, null);
$no_of_points = count($points_arr);
$param_row[$k][$j]['no_of_points'] = $no_of_points;
for ($i = 0; $i < $no_of_points; $i++) {
$param_row[$k][$j][$i]['x'] = $points_arr[$i][0];
$param_row[$k][$j][$i]['y'] = $points_arr[$i][1];
}
$j++;
}
}
$k++;
}
return $params;
}
}

View File

@ -0,0 +1,356 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Handles actions related to GIS POINT objects
*
* @package PhpMyAdmin-GIS
*/
namespace PMA\libraries\gis;
use \TCPDF;
/**
* Handles actions related to GIS POINT objects
*
* @package PhpMyAdmin-GIS
*/
class GISPoint extends GISGeometry
{
// 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 GISPoint 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 coordinates
* @access public
*/
public function scaleRow($spatial)
{
// Trim to remove leading 'POINT(' and trailing ')'
$point
= mb_substr(
$spatial,
6,
mb_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 resource $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(mb_substr($point_color, 1, 2));
$green = hexdec(mb_substr($point_color, 3, 2));
$blue = hexdec(mb_substr($point_color, 4, 2));
$color = imagecolorallocate($image, $red, $green, $blue);
// Trim to remove leading 'POINT(' and trailing ')'
$point
= mb_substr(
$spatial,
6,
mb_strlen($spatial) - 7
);
$points_arr = $this->extractPoints($point, $scale_data);
// draw a small circle to mark the point
if ($points_arr[0][0] != '' && $points_arr[0][1] != '') {
imagearc(
$image,
$points_arr[0][0],
$points_arr[0][1],
7,
7,
0,
360,
$color
);
// print label if applicable
if (isset($label) && trim($label) != '') {
imagestring(
$image,
1,
$points_arr[0][0],
$points_arr[0][1],
trim($label),
$black
);
}
}
return $image;
}
/**
* Adds to the TCPDF instance, the data related to a row in the GIS dataset.
*
* @param string $spatial GIS POINT object
* @param string $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(mb_substr($point_color, 1, 2));
$green = hexdec(mb_substr($point_color, 3, 2));
$blue = hexdec(mb_substr($point_color, 4, 2));
$line = array('width' => 1.25, 'color' => array($red, $green, $blue));
// Trim to remove leading 'POINT(' and trailing ')'
$point
= mb_substr(
$spatial,
6,
mb_strlen($spatial) - 7
);
$points_arr = $this->extractPoints($point, $scale_data);
// draw a small circle to mark the point
if ($points_arr[0][0] != '' && $points_arr[0][1] != '') {
$pdf->Circle(
$points_arr[0][0],
$points_arr[0][1],
2,
0,
360,
'D',
$line
);
// print label if applicable
if (isset($label) && trim($label) != '') {
$pdf->SetXY($points_arr[0][0], $points_arr[0][1]);
$pdf->SetFontSize(5);
$pdf->Cell(0, 0, trim($label));
}
}
return $pdf;
}
/**
* Prepares and returns the code related to a row in the GIS dataset as SVG.
*
* @param string $spatial GIS POINT object
* @param string $label Label for the GIS POINT object
* @param string $point_color Color for the GIS POINT object
* @param array $scale_data Array containing data related to scaling
*
* @return string the code related to a row in the GIS dataset
* @access public
*/
public function prepareRowAsSvg($spatial, $label, $point_color, $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
= mb_substr(
$spatial,
6,
mb_strlen($spatial) - 7
);
$points_arr = $this->extractPoints($point, $scale_data);
$row = '';
if ($points_arr[0][0] != '' && $points_arr[0][1] != '') {
$row .= '<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
= mb_substr(
$spatial,
6,
mb_strlen($spatial) - 7
);
$points_arr = $this->extractPoints($point, null);
if ($points_arr[0][0] != '' && $points_arr[0][1] != '') {
$result .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector('
. $this->getPointForOpenLayers($points_arr[0], $srid) . ', null, '
. json_encode($style_options) . '));';
}
return $result;
}
/**
* Generate the WKT with the set of parameters passed by the GIS editor.
*
* @param array $gis_data GIS data
* @param int $index Index into the parameter object
* @param string $empty Point does not adhere to this parameter
*
* @return string WKT with the set of parameters passed by the GIS editor
* @access public
*/
public function generateWkt($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)
{
$params = array();
if ($index == -1) {
$index = 0;
$data = GISGeometry::generateParams($value);
$params['srid'] = $data['srid'];
$wkt = $data['wkt'];
} else {
$params[$index]['gis_type'] = 'POINT';
$wkt = $value;
}
// Trim to remove leading 'POINT(' and trailing ')'
$point
= mb_substr(
$wkt,
6,
mb_strlen($wkt) - 7
);
$points_arr = $this->extractPoints($point, null);
$params[$index]['POINT']['x'] = $points_arr[0][0];
$params[$index]['POINT']['y'] = $points_arr[0][1];
return $params;
}
}

View File

@ -0,0 +1,623 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Handles actions related to GIS POLYGON objects
*
* @package PhpMyAdmin-GIS
*/
namespace PMA\libraries\gis;
use PMA\libraries\Util;
use \TCPDF;
/**
* Handles actions related to GIS POLYGON objects
*
* @package PhpMyAdmin-GIS
*/
class GISPolygon extends GISGeometry
{
// 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 GISPolygon 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 coordinates
* @access public
*/
public function scaleRow($spatial)
{
// Trim to remove leading 'POLYGON((' and trailing '))'
$polygon = mb_substr(
$spatial,
9,
mb_strlen($spatial) - 11
);
// If the polygon doesn't have an inner ring, use polygon itself
if (mb_strpos($polygon, "),(") === false) {
$ring = $polygon;
} else {
// Separate outer ring and use it to determine 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(mb_substr($fill_color, 1, 2));
$green = hexdec(mb_substr($fill_color, 3, 2));
$blue = hexdec(mb_substr($fill_color, 4, 2));
$color = imagecolorallocate($image, $red, $green, $blue);
// Trim to remove leading 'POLYGON((' and trailing '))'
$polygon = mb_substr(
$spatial,
9,
mb_strlen($spatial) - 11
);
// If the polygon doesn't have an inner polygon
if (mb_strpos($polygon, "),(") === false) {
$points_arr = $this->extractPoints($polygon, $scale_data, true);
} else {
// Separate outer and inner polygons
$parts = explode("),(", $polygon);
$outer = $parts[0];
$inner = array_slice($parts, 1);
$points_arr = $this->extractPoints($outer, $scale_data, true);
foreach ($inner as $inner_poly) {
$points_arr = array_merge(
$points_arr,
$this->extractPoints($inner_poly, $scale_data, true)
);
}
}
// draw polygon
imagefilledpolygon($image, $points_arr, 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(mb_substr($fill_color, 1, 2));
$green = hexdec(mb_substr($fill_color, 3, 2));
$blue = hexdec(mb_substr($fill_color, 4, 2));
$color = array($red, $green, $blue);
// Trim to remove leading 'POLYGON((' and trailing '))'
$polygon = mb_substr(
$spatial,
9,
mb_strlen($spatial) - 11
);
// If the polygon doesn't have an inner polygon
if (mb_strpos($polygon, "),(") === false) {
$points_arr = $this->extractPoints($polygon, $scale_data, true);
} else {
// Separate outer and inner polygons
$parts = explode("),(", $polygon);
$outer = $parts[0];
$inner = array_slice($parts, 1);
$points_arr = $this->extractPoints($outer, $scale_data, true);
foreach ($inner as $inner_poly) {
$points_arr = array_merge(
$points_arr,
$this->extractPoints($inner_poly, $scale_data, true)
);
}
}
// draw polygon
$pdf->Polygon($points_arr, 'F*', 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
= mb_substr(
$spatial,
9,
mb_strlen($spatial) - 11
);
$row = '<path d="';
// If the polygon doesn't have an inner polygon
if (mb_strpos($polygon, "),(") === false) {
$row .= $this->_drawPath($polygon, $scale_data);
} else {
// Separate outer and inner polygons
$parts = explode("),(", $polygon);
$outer = $parts[0];
$inner = array_slice($parts, 1);
$row .= $this->_drawPath($outer, $scale_data);
foreach ($inner as $inner_poly) {
$row .= $this->_drawPath($inner_poly, $scale_data);
}
}
$row .= '"';
foreach ($polygon_options as $option => $val) {
$row .= ' ' . $option . '="' . trim($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
=
mb_substr(
$spatial,
9,
mb_strlen($spatial) - 11
);
// Separate outer and inner polygons
$parts = explode("),(", $polygon);
$row .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector('
. $this->getPolygonForOpenLayers($parts, $srid)
. ', null, ' . json_encode($style_options) . '));';
return $row;
}
/**
* Draws a ring of the polygon using SVG path element.
*
* @param string $polygon The ring
* @param array $scale_data Array containing data related to scaling
*
* @return string the code to draw the ring
* @access private
*/
private function _drawPath($polygon, $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
=
mb_substr(
$wkt,
0,
mb_strlen($wkt) - 1
);
$wkt .= '),';
}
$wkt
=
mb_substr(
$wkt,
0,
mb_strlen($wkt) - 1
);
$wkt .= ')';
return $wkt;
}
/**
* Calculates the area of a closed simple polygon.
*
* @param array $ring array of points forming the ring
*
* @return float the area of a closed simple polygon
* @access public
* @static
*/
public static function area($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 (GISPolygon::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']))) {
$p1 = $p2;
continue;
}
if ($point['y'] > max(array($p1['y'], $p2['y']))) {
$p1 = $p2;
continue;
}
if ($point['x'] > max(array($p1['x'], $p2['x']))) {
$p1 = $p2;
continue;
}
if ($p1['y'] != $p2['y']) {
$xinters = ($point['y'] - $p1['y'])
* ($p2['x'] - $p1['x'])
/ ($p2['y'] - $p1['y']) + $p1['x'];
if ($p1['x'] == $p2['x'] || $point['x'] <= $xinters) {
$counter++;
}
}
$p1 = $p2;
}
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, $nb = count($ring) - 1; $i < $nb; $i++) {
if ($ring[$i]['y'] != $ring[$i + 1]['y']) {
$x0 = $ring[$i]['x'];
$x1 = $ring[$i + 1]['x'];
$y0 = $ring[$i]['y'];
$y1 = $ring[$i + 1]['y'];
break;
}
}
if (!isset($x0)) {
return false;
}
// Find the mid point
$x2 = ($x0 + $x1) / 2;
$y2 = ($y0 + $y1) / 2;
// Always keep $epsilon < 1 to go with the reduction logic down here
$epsilon = 0.1;
$denominator = sqrt(
Util::pow(($y1 - $y0), 2)
+ 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 epsilon chosen is too large
if (GISPolygon::isPointInsidePolygon($pointA, $ring)) {
return $pointA;
}
if (GISPolygon::isPointInsidePolygon($pointB, $ring)) {
return $pointB;
}
//If both are outside the polygon reduce the epsilon and
//recalculate the points(reduce exponentially for faster convergence)
$epsilon = 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)
{
$params = array();
if ($index == -1) {
$index = 0;
$data = GISGeometry::generateParams($value);
$params['srid'] = $data['srid'];
$wkt = $data['wkt'];
} else {
$params[$index]['gis_type'] = 'POLYGON';
$wkt = $value;
}
// Trim to remove leading 'POLYGON((' and trailing '))'
$polygon
=
mb_substr(
$wkt,
9,
mb_strlen($wkt) - 11
);
// Separate each linestring
$linerings = explode("),(", $polygon);
$params[$index]['POLYGON']['no_of_lines'] = count($linerings);
$j = 0;
foreach ($linerings as $linering) {
$points_arr = $this->extractPoints($linering, null);
$no_of_points = count($points_arr);
$params[$index]['POLYGON'][$j]['no_of_points'] = $no_of_points;
for ($i = 0; $i < $no_of_points; $i++) {
$params[$index]['POLYGON'][$j][$i]['x'] = $points_arr[$i][0];
$params[$index]['POLYGON'][$j][$i]['y'] = $points_arr[$i][1];
}
$j++;
}
return $params;
}
}

View File

@ -0,0 +1,708 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Handles visualization of GIS data
*
* @package PhpMyAdmin-GIS
*/
namespace PMA\libraries\gis;
use PMA\libraries\Util;
use \TCPDF;
require_once 'libraries/sql.lib.php';
/**
* Handles visualization of GIS data
*
* @package PhpMyAdmin-GIS
*/
class GISVisualization
{
/**
* @var array Raw data for the visualization
*/
private $_data;
private $_modified_sql;
/**
* @var array Set of default settings 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;
}
/**
* Factory
*
* @param string $sql_query SQL to fetch raw data for visualization
* @param array $options Users specified options
* @param integer $row number of rows
* @param integer $pos start position
*
* @return GISVisualization
*
* @access public
*/
public static function get($sql_query, $options, $row, $pos)
{
return new GISVisualization($sql_query, $options, $row, $pos);
}
/**
* Get visualization
*
* @param array $data Raw data, if set, parameters other than $options will be
* ignored
* @param array $options Users specified options
*
* @return GISVisualization
*/
public static function getByData($data, $options)
{
return new GISVisualization(null, $options, null, null, $data);
}
/**
* Check if data has SRID
*
* @return bool
*/
public function hasSrid()
{
foreach ($this->_data as $row) {
if ($row['srid'] != 0) {
return true;
}
}
return false;
}
/**
* Constructor. Stores user specified options.
*
* @param string $sql_query SQL to fetch raw data for visualization
* @param array $options Users specified options
* @param integer $row number of rows
* @param integer $pos start position
* @param array $data raw data. If set, parameters other than $options
* will be ignored
*
* @access public
*/
private function __construct($sql_query, $options, $row, $pos, $data = null)
{
$this->_userSpecifiedSettings = $options;
if (isset($data)) {
$this->_data = $data;
} else {
$this->_modified_sql = $this->_modifySqlQuery($sql_query, $row, $pos);
$this->_data = $this->_fetchRawData();
}
}
/**
* All the variable initialization, options handling has to be done here.
*
* @return void
* @access protected
*/
protected function init()
{
$this->_handleOptions();
}
/**
* Returns sql for fetching raw data
*
* @param string $sql_query The SQL to modify.
* @param integer $rows Number of rows.
* @param integer $pos Start position.
*
* @return string the modified sql query.
*/
private function _modifySqlQuery($sql_query, $rows, $pos)
{
$modified_query = 'SELECT ';
// If label column is chosen add it to the query
if (!empty($this->_userSpecifiedSettings['labelColumn'])) {
$modified_query .= Util::backquote(
$this->_userSpecifiedSettings['labelColumn']
)
. ', ';
}
// Wrap the spatial column with 'ASTEXT()' function and add it
$modified_query .= 'ASTEXT('
. Util::backquote($this->_userSpecifiedSettings['spatialColumn'])
. ') AS ' . Util::backquote(
$this->_userSpecifiedSettings['spatialColumn']
)
. ', ';
// Get the SRID
$modified_query .= 'SRID('
. Util::backquote($this->_userSpecifiedSettings['spatialColumn'])
. ') AS ' . Util::backquote('srid') . ' ';
// Append the original query as the inner query
$modified_query .= 'FROM (' . $sql_query . ') AS '
. Util::backquote('temp_gis');
// LIMIT clause
if (is_numeric($rows) && $rows > 0) {
$modified_query .= ' LIMIT ';
if (is_numeric($pos) && $pos >= 0) {
$modified_query .= $pos . ', ' . $rows;
} else {
$modified_query .= $rows;
}
}
return $modified_query;
}
/**
* Returns raw data for GIS visualization.
*
* @return string the raw data.
*/
private function _fetchRawData()
{
$modified_result = $GLOBALS['dbi']->tryQuery($this->_modified_sql);
$data = array();
while ($row = $GLOBALS['dbi']->fetchAssoc($modified_result)) {
$data[] = $row;
}
return $data;
}
/**
* A function which handles passed parameters. Useful if desired
* chart needs to be a little bit different from the default one.
*
* @return void
* @access private
*/
private function _handleOptions()
{
if (!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 = mb_strlen($file_name) - mb_strlen($ext) - 1;
$user_extension
= mb_substr(
$file_name,
$extension_start_pos,
mb_strlen($file_name)
);
$required_extension = "." . $ext;
if (mb_strtolower($user_extension) != $required_extension) {
$file_name .= $required_extension;
}
return $file_name;
}
/**
* Handles common tasks of writing the visualization to file for various formats.
*
* @param string $file_name file name
* @param string $type mime type
* @param string $ext extension of the file
*
* @return void
* @access private
*/
private function _toFile($file_name, $type, $ext)
{
$file_name = $this->_sanitizeName($file_name, $ext);
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"
. '<svg version="1.1" xmlns:svg="http://www.w3.org/2000/svg"'
. ' xmlns="http://www.w3.org/2000/svg"'
. ' width="' . intval($this->_settings['width']) . '"'
. ' height="' . intval($this->_settings['height']) . '">'
. '<g id="groupPanel">';
$scale_data = $this->_scaleDataSet($this->_data);
$output .= $this->_prepareDataSet($this->_data, $scale_data, 'svg', '');
$output .= '</g></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.
*
* @todo Should return JSON to avoid eval() in gis_data_editor.js
*
* @return string the code for visualization with OpenLayers
* @access public
*/
public function asOl()
{
$this->init();
$scale_data = $this->_scaleDataSet($this->_data);
$output
= 'if (typeof OpenLayers !== "undefined") {'
. 'var options = {'
. 'projection: new OpenLayers.Projection("EPSG:900913"),'
. 'displayProjection: new OpenLayers.Projection("EPSG:4326"),'
. 'units: "m",'
. 'numZoomLevels: 18,'
. 'maxResolution: 156543.0339,'
. 'maxExtent: new OpenLayers.Bounds('
. '-20037508, -20037508, 20037508, 20037508),'
. 'restrictedExtent: new OpenLayers.Bounds('
. '-20037508, -20037508, 20037508, 20037508)'
. '};'
. 'var map = new OpenLayers.Map("openlayersmap", options);'
. 'var layerNone = new OpenLayers.Layer.Boxes('
. '"None", {isBaseLayer: true});'
. 'var layerOSM = new OpenLayers.Layer.OSM("OSM",'
. '['
. '"https://a.tile.openstreetmap.org/${z}/${x}/${y}.png",'
. '"https://b.tile.openstreetmap.org/${z}/${x}/${y}.png",'
. '"https://c.tile.openstreetmap.org/${z}/${x}/${y}.png"'
. ']);'
. 'map.addLayers([layerOSM,layerNone]);'
. 'var vectorLayer = new OpenLayers.Layer.Vector("Data");'
. 'var bound;';
$output .= $this->_prepareDataSet($this->_data, $scale_data, 'ol', '');
$output .= 'map.addLayer(vectorLayer);'
. 'map.zoomToExtent(bound);'
. 'if (map.getZoom() < 2) {'
. 'map.zoomTo(2);'
. '}'
. 'map.addControl(new OpenLayers.Control.LayerSwitcher());'
. 'map.addControl(new OpenLayers.Control.MousePosition());'
. '}';
return $output;
}
/**
* Saves as a PDF to a file.
*
* @param string $file_name File name
*
* @return void
* @access public
*/
public function toFileAsPdf($file_name)
{
$this->init();
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');
}
/**
* Convert file to image
*
* @param string $format Output format
*
* @return string File
*/
public function toImage($format)
{
if ($format == 'svg') {
return $this->asSvg();
} elseif ($format == 'png') {
return $this->asPng();
} elseif ($format == 'ol') {
return $this->asOl();
}
}
/**
* Convert file to given format
*
* @param string $filename Filename
* @param string $format Output format
*
* @return void
*/
public function toFile($filename, $format)
{
if ($format == 'svg') {
$this->toFileAsSvg($filename);
} elseif ($format == 'png') {
$this->toFileAsPng($filename);
} elseif ($format == 'pdf') {
$this->toFileAsPdf($filename);
}
}
/**
* Calculates the scale, horizontal and vertical offset that should be used.
*
* @param array $data Row data
*
* @return array an array containing the scale, x and y offsets
* @access private
*/
private function _scaleDataSet($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 = mb_strpos($ref_data, '(');
if ($type_pos === false) {
continue;
}
$type = mb_substr($ref_data, 0, $type_pos);
$gis_obj = GISFactory::factory($type);
if (!$gis_obj) {
continue;
}
$scale_data = $gis_obj->scaleRow(
$row[$this->_settings['spatialColumn']]
);
// Update minimum/maximum values for x and y coordinates.
$c_maxX = (float)$scale_data['maxX'];
if (!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 visualization
* @param object $results Image object in the case of png
* TCPDF object in the case of pdf
*
* @return mixed the formatted array of data
* @access private
*/
private function _prepareDataSet($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 = mb_strpos($ref_data, '(');
if ($type_pos === false) {
continue;
}
$type = mb_substr($ref_data, 0, $type_pos);
$gis_obj = GISFactory::factory($type);
if (!$gis_obj) {
continue;
}
$label = '';
if (isset($this->_settings['labelColumn'])
&& isset($row[$this->_settings['labelColumn']])
) {
$label = $row[$this->_settings['labelColumn']];
}
if ($format == 'svg') {
$results .= $gis_obj->prepareRowAsSvg(
$row[$this->_settings['spatialColumn']],
$label,
$this->_settings['colors'][$index],
$scale_data
);
} elseif ($format == 'png') {
$results = $gis_obj->prepareRowAsPng(
$row[$this->_settings['spatialColumn']],
$label,
$this->_settings['colors'][$index],
$scale_data,
$results
);
} elseif ($format == 'pdf') {
$results = $gis_obj->prepareRowAsPdf(
$row[$this->_settings['spatialColumn']],
$label,
$this->_settings['colors'][$index],
$scale_data,
$results
);
} elseif ($format == 'ol') {
$results .= $gis_obj->prepareRowAsOl(
$row[$this->_settings['spatialColumn']],
$row['srid'],
$label,
$this->_settings['colors'][$index],
$scale_data
);
}
$color_number++;
}
return $results;
}
/**
* Set user specified settings
*
* @param array $userSpecifiedSettings User specified settings
*
* @return void
*/
public function setUserSpecifiedSettings($userSpecifiedSettings)
{
$this->_userSpecifiedSettings = $userSpecifiedSettings;
}
}