GIS visualization to PDF

This commit is contained in:
Madhura Jayaratne 2011-06-03 22:28:56 +05:30
parent 17c222f83e
commit 0e52941faa
8 changed files with 318 additions and 10 deletions

View File

@ -84,13 +84,13 @@ class PMA_GIS_Geometrycollection extends PMA_GIS_Geometry
*
* @param string $spatial GIS GEOMETRYCOLLECTION object
* @param string $label Label for the GIS GEOMETRYCOLLECTION object
* @param string $line_color Color 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 image $image Image object
*
* @return the code related to a row in the GIS dataset
*/
public function prepareRowAsPng($spatial, $label, $line_color, $scale_data, $image)
public function prepareRowAsPng($spatial, $label, $color, $scale_data, $image)
{
// Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
$goem_col = substr($spatial, 19, (strlen($spatial) - 20));
@ -102,22 +102,50 @@ class PMA_GIS_Geometrycollection extends PMA_GIS_Geometry
$type = substr($sub_part, 0, $type_pos);
$gis_obj = PMA_GIS_Factory::factory($type);
$image = $gis_obj->prepareRowAsPng($sub_part, $label, $line_color, $scale_data, $image);
$image = $gis_obj->prepareRowAsPng($sub_part, $label, $color, $scale_data, $image);
}
return $image;
}
/**
* Adds to the PDF 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 image $pdf Pdf object
*
* @return the code related to a row in the GIS dataset
*/
public function prepareRowAsPdf($spatial, $label, $color, $scale_data, $pdf)
{
// Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
$goem_col = substr($spatial, 19, (strlen($spatial) - 20));
// Split the geometry collection object to get its constituents.
$sub_parts = $this->_explodeGeomCol($goem_col);
foreach ($sub_parts as $sub_part) {
$type_pos = stripos($sub_part, '(');
$type = substr($sub_part, 0, $type_pos);
$gis_obj = PMA_GIS_Factory::factory($type);
$image = $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 $line_color Color 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 the code related to a row in the GIS dataset
*/
public function prepareRowAsSvg($spatial, $label, $line_color, $scale_data)
public function prepareRowAsSvg($spatial, $label, $color, $scale_data)
{
$row = '';
@ -131,7 +159,7 @@ class PMA_GIS_Geometrycollection extends PMA_GIS_Geometry
$type = substr($sub_part, 0, $type_pos);
$gis_obj = PMA_GIS_Factory::factory($type);
$row .= $gis_obj->prepareRowAsSvg($sub_part, $label, $line_color, $scale_data);
$row .= $gis_obj->prepareRowAsSvg($sub_part, $label, $color, $scale_data);
}
return $row;
}

View File

@ -80,6 +80,41 @@ class PMA_GIS_Linestring extends PMA_GIS_Geometry
return $image;
}
/**
* Adds to the PDF 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 image $pdf Pdf object
*
* @return the code related to a row in the GIS dataset
*/
public function prepareRowAsPdf($spatial, $label, $line_color, $scale_data, $pdf)
{
// allocate colors
$r = hexdec(substr($line_color, 1, 2));
$g = hexdec(substr($line_color, 3, 2));
$b = hexdec(substr($line_color, 4, 2));
$line = array('width' => 1.5, 'color' => array($r, $g, $b));
// Trim to remove leading 'LINESTRING(' and trailing ')'
$linesrting = substr($spatial, 11, (strlen($spatial) - 12));
$points_arr = $this->extractPoints($linesrting, $scale_data);
foreach ($points_arr as $point) {
if (! isset($temp_point)) {
$temp_point = $point;
} else {
// draw line section
$pdf->Line($temp_point[0], $temp_point[1], $point[0], $point[1], $line);
$temp_point = $point;
}
}
return $pdf;
}
/**
* Prepares and returns the code related to a row in the GIS dataset as SVG.
*

View File

@ -94,6 +94,46 @@ class PMA_GIS_Multilinestring extends PMA_GIS_Geometry
return $image;
}
/**
* Adds to the PDF 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 image $pdf Pdf object
*
* @return the code related to a row in the GIS dataset
*/
public function prepareRowAsPdf($spatial, $label, $line_color, $scale_data, $pdf)
{
// allocate colors
$r = hexdec(substr($line_color, 1, 2));
$g = hexdec(substr($line_color, 3, 2));
$b = hexdec(substr($line_color, 4, 2));
$line = array('width' => 1.5, 'color' => array($r, $g, $b));
// Trim to remove leading 'MULTILINESTRING((' and trailing '))'
$multilinestirng = substr($spatial, 17, (strlen($spatial) - 19));
// Seperate each linestring
$linestirngs = explode("),(", $multilinestirng);
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);
}
return $pdf;
}
/**
* Prepares and returns the code related to a row in the GIS dataset as SVG.
*

View File

@ -75,6 +75,36 @@ class PMA_GIS_Multipoint extends PMA_GIS_Geometry
return $image;
}
/**
* Adds to the PDF 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 $line_color Color for the GIS MULTIPOINT object
* @param array $scale_data Array containing data related to scaling
* @param image $pdf Pdf object
*
* @return the code related to a row in the GIS dataset
*/
public function prepareRowAsPdf($spatial, $label, $line_color, $scale_data, $pdf)
{
// allocate colors
$r = hexdec(substr($line_color, 1, 2));
$g = hexdec(substr($line_color, 3, 2));
$b = hexdec(substr($line_color, 4, 2));
$line = array('width' => 1.25, 'color' => array($r, $g, $b));
// Trim to remove leading 'MULTIPOINT(' and trailing ')'
$multipoint = substr($spatial, 11, (strlen($spatial) - 12));
$points_arr = $this->extractPoints($multipoint, $scale_data);
foreach ($points_arr as $point) {
// draw a small circle to mark the point
$pdf->Circle($point[0], $point[1], 2, 0, 360, 'D', $line);
}
return $pdf;
}
/**
* Prepares and returns the code related to a row in the GIS dataset as SVG.
*

View File

@ -116,6 +116,54 @@ class PMA_GIS_Multipolygon extends PMA_GIS_Geometry
return $image;
}
/**
* Adds to the PDF 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 image $pdf Pdf object
*
* @return the code related to a row in the GIS dataset
*/
public function prepareRowAsPdf($spatial, $label, $fill_color, $scale_data, $pdf)
{
// allocate colors
$r = hexdec(substr($fill_color, 1, 2));
$g = hexdec(substr($fill_color, 3, 2));
$b = hexdec(substr($fill_color, 4, 2));
$color = array($r, $g, $b);
// Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
$multipolygon = substr($spatial, 15, (strlen($spatial) - 18));
// Seperate each polygon
$polygons = explode(")),((", $multipolygon);
foreach ($polygons as $polygon) {
// If the polygon doesnt have an inner polygon
if (strpos($polygon, "),(") === false) {
$points_arr = $this->extractPoints($polygon, $scale_data, true);
} else {
// Seperate outer and inner polygons
$parts = explode("),(", $polygon);
$outer = $parts[0];
$inner = array_slice($parts, 1);
$points_arr = $this->extractPoints($outer, $scale_data, true);
foreach ($inner as $inner_poly) {
$points_arr = array_merge(
$points_arr, $this->extractPoints($inner_poly, $scale_data, true)
);
}
}
// draw polygon
$pdf->Polygon($points_arr, 'F*', array(), $color, true);
}
return $pdf;
}
/**
* Prepares and returns the code related to a row in the GIS dataset as SVG.
*

View File

@ -73,6 +73,34 @@ class PMA_GIS_Point extends PMA_GIS_Geometry
return $image;
}
/**
* Adds to the PDF 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 $line_color Color for the GIS POINT object
* @param array $scale_data Array containing data related to scaling
* @param image $pdf Pdf object
*
* @return the code related to a row in the GIS dataset
*/
public function prepareRowAsPdf($spatial, $label, $line_color, $scale_data, $pdf)
{
// allocate colors
$r = hexdec(substr($line_color, 1, 2));
$g = hexdec(substr($line_color, 3, 2));
$b = hexdec(substr($line_color, 4, 2));
$line = array('width' => 1.25, 'color' => array($r, $g, $b));
// Trim to remove leading 'POINT(' and trailing ')'
$point = substr($spatial, 6, (strlen($spatial) - 7));
$points_arr = $this->extractPoints($point, $scale_data);
// draw a small circle to mark the point
$pdf->Circle($points_arr[0][0], $points_arr[0][1], 2, 0, 360, 'D', $line);
return $pdf;
}
/**
* Prepares and returns the code related to a row in the GIS dataset as SVG.
*

View File

@ -108,6 +108,51 @@ class PMA_GIS_Polygon extends PMA_GIS_Geometry
return $image;
}
/**
* Adds to the PDF 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 image $pdf Pdf object
*
* @return the code related to a row in the GIS dataset
*/
public function prepareRowAsPdf($spatial, $label, $fill_color, $scale_data, $pdf)
{
// allocate colors
$r = hexdec(substr($fill_color, 1, 2));
$g = hexdec(substr($fill_color, 3, 2));
$b = hexdec(substr($fill_color, 4, 2));
$color = array($r, $g, $b);
// Trim to remove leading 'POLYGON((' and trailing '))'
$polygon = substr($spatial, 9, (strlen($spatial) - 11));
// If the polygon doesnt have an inner polygon
if (strpos($polygon, "),(") === false) {
$points_arr = $this->extractPoints($polygon, $scale_data, true);
} else {
// Seperate outer and inner polygons
$parts = explode("),(", $polygon);
$outer = $parts[0];
$inner = array_slice($parts, 1);
$points_arr = $this->extractPoints($outer, $scale_data, true);
foreach ($inner as $inner_poly) {
$points_arr = array_merge(
$points_arr, $this->extractPoints($inner_poly, $scale_data, true)
);
}
}
// draw polygon
$pdf->Polygon($points_arr, 'F*', array(), $color, true);
return $pdf;
}
/**
* Prepares and returns the code related to a row in the GIS dataset as SVG.
*

View File

@ -92,16 +92,17 @@ class PMA_GIS_Visualization
}
/**
* Handles common tasks of writing the visualization to file for various formats.
* Sanitizes the file name.
*
* @param string $file_name file name
* @param string $type mime type
* @param string $ext extension of the file
*
* @return the sanitized file name
*/
private function _toFile($file_name, $type, $ext)
private function _sanitizeName($file_name, $ext)
{
// convert filename to iso-8859-1, it is safer
$file_name = PMA_convert_string('', 'iso-8859-1', $file_name);
$file_name = PMA_convert_string('utf-8', 'iso-8859-1', $file_name);
// Check if the user already added extension;
// get the substring where the extension would be if it was included
@ -111,6 +112,19 @@ class PMA_GIS_Visualization
if (strtolower($user_extension) != $required_extension) {
$file_name .= $required_extension;
}
return $file_name;
}
/**
* Handles common tasks of writing the visualization to file for various formats.
*
* @param string $file_name file name
* @param string $type mime type
* @param string $ext extension of the file
*/
private function _toFile($file_name, $type, $ext)
{
$file_name = $this->_sanitizeName($file_name);
ob_clean();
@ -229,6 +243,40 @@ class PMA_GIS_Visualization
imagedestroy($img);
}
/**
* Saves as a PDF to a file.
*
* @param string $file_name File name
*/
public function toFileAsPdf($file_name)
{
$this->init();
include_once './libraries/tcpdf/tcpdf.php';
// create pdf
$pdf = new TCPDF('', 'pt', 'A4', 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, 0, $scale_data, 'pdf', $pdf);
// sanitize file name
$file_name = $this->_sanitizeName($file_name, 'pdf');
ob_clean();
$pdf->Output($file_name, 'D');
}
/**
* Calculates the scale, horizontal and vertical offset that should be used.
*
@ -340,6 +388,11 @@ class PMA_GIS_Visualization
$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
);
}
$color_number++;
}
@ -347,3 +400,4 @@ class PMA_GIS_Visualization
}
}
?>