cvs: pear /Image_GIS GIS.php Parser.php Renderer.php /Image_GIS/GIS Parser.php Renderer.php /Image_GIS/GIS/Parser E00.php /Image_GIS/GIS/Renderer
GD.php SVG.php /Image_GIS/Parser E00.php /Image_GIS/Renderer GD.php SVG.php

From: Date: Mon, 30 Dec 2002 11:36:52 +0000
Subject: cvs: pear /Image_GIS GIS.php Parser.php Renderer.php /Image_GIS/GIS Parser.php Renderer.php /Image_GIS/GIS/Parser E00.php /Image_GIS/GIS/Renderer
GD.php SVG.php /Image_GIS/Parser E00.php /Image_GIS/Renderer GD.php SVG.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-7994@lists.php.net to get a copy of this message
sebastian Mon Dec 30 06:36:52 2002 EDT Added files: /pear/Image_GIS/GIS Parser.php Renderer.php /pear/Image_GIS/GIS/Parser E00.php /pear/Image_GIS/GIS/Renderer GD.php SVG.php Removed files: /pear/Image_GIS Parser.php Renderer.php /pear/Image_GIS/Parser E00.php /pear/Image_GIS/Renderer GD.php SVG.php Modified files: /pear/Image_GIS GIS.php Log: Some refactoring. Either required to match the directory layout described in package.xml or to allow for automatic range detection. The latter is not yet implemented.

Index: pear/Image_GIS/GIS.php diff -u pear/Image_GIS/GIS.php:1.1 pear/Image_GIS/GIS.php:1.2 --- pear/Image_GIS/GIS.php:1.1 Mon Dec 30 05:04:44 2002 +++ pear/Image_GIS/GIS.php Mon Dec 30 06:36:50 2002 @@ -13,7 +13,7 @@ // | license@php.net so we can mail you a copy immediately. | // +------------------------------------------------------------------------+ // -// $Id: GIS.php,v 1.1 2002/12/30 10:04:44 sebastian Exp $ +// $Id: GIS.php,v 1.2 2002/12/30 11:36:50 sebastian Exp $ // require_once 'Image/Color.php'; @@ -32,15 +32,15 @@ * $map = new Image_GIS(960, 1280); * $map->setRange(9.7, 10.5, 54.2, 54.7); * -* $map->draw('germany_rdline.e00', 'gray'); -* $map->draw('germany_pppoly.e00', 'green'); -* $map->draw('germany_dnnet.e00', 'blue'); -* $map->draw('germany_ponet.e00', 'black'); +* $map->addDataFile('germany_rdline.e00', 'gray'); +* $map->addDataFile('germany_pppoly.e00', 'green'); +* $map->addDataFile('germany_dnnet.e00', 'blue'); +* $map->addDataFile('germany_ponet.e00', 'black'); * * $map->saveImage('kiel.png'); * ?> * -* @version $Revision: 1.1 $ +* @version $Revision: 1.2 $ * @since Image_GIS 1.0.0 */ class Image_GIS { @@ -70,29 +70,37 @@ * * @param mixed $width * @param integer $height - * @param string $parser * @param string $renderer + * @param string $parser * @param boolean $debug * @access public */ - function Image_GIS($width, $height = -1, $parser = 'E00', $renderer = 'GD', $debug = false) { + function Image_GIS($width, $height = -1, $renderer = 'GD', $parser = 'E00', $debug = false) { $this->debug = $debug; - $this->setRenderer($renderer, $width, $height); $this->setParser($parser); - $this->setRange(9, 11, 55, 54); + $this->setRenderer($renderer, $width, $height); } /** - * Draws a datafile. + * Adds a datafile to the map. * * @param string $dataFile * @param mixed $color * @return boolean * @access public */ - function draw($dataFile, $color) { - return $this->parser->draw($dataFile, $color); + function addDataFile($dataFile, $color) { + return $this->parser->addDataFile($dataFile, $color); + } + + /** + * Renders the image. + * + * @access public + */ + function render() { + $this->renderer->render($this->parser->parse()); } /** @@ -103,6 +111,8 @@ * @access public */ function saveImage($filename) { + $this->render(); + return $this->renderer->saveImage($filename); } @@ -114,7 +124,7 @@ * @access public */ function setParser($parser) { - $this->parser = &Image_GIS_Parser::factory($parser, $this->renderer, $this->debug); + $this->parser = &Image_GIS_Parser::factory($parser, $this->debug); } /** @@ -147,6 +157,8 @@ * @access public */ function showImage() { + $this->render(); + $this->renderer->showImage(); } } Index: pear/Image_GIS/GIS/Parser.php +++ pear/Image_GIS/GIS/Parser.php <?php // // +------------------------------------------------------------------------+ // | PEAR :: Image :: GIS :: Parser Base Class | // +------------------------------------------------------------------------+ // | Copyright (c) 2002-2003 Jan Kneschke <jan@kneschke.de> and | // | Sebastian Bergmann <sb@sebastian-bergmann.de>. | // +------------------------------------------------------------------------+ // | This source file is subject to version 3.00 of the PHP License, | // | that is available at http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +------------------------------------------------------------------------+ // // $Id: Parser.php,v 1.1 2002/12/30 11:36:50 sebastian Exp $ // /** * Parser Base Class. * * @version $Revision: 1.1 $ * @since Image_GIS 1.0.0 */ class Image_GIS_Parser { /** * Data Files. * * @var array $dataFiles */ var $dataFiles = array(); /** * Set to TRUE to enable debugging. * * @var boolean $debug */ var $debug; /** * Lines. * * @var array $lines */ var $lines = array(); /** * Constructor. * * @param boolean $debug * @access public */ function Image_GIS_Parser($debug) { $this->debug = $debug; } /** * Factory. * * @param string $parser * @param boolean $debug * @return object * @access public */ function &factory($parser, $debug) { include_once 'Image/GIS/Parser/' . $parser . '.php'; $class = 'Image_GIS_Parser_' . $parser; return new $class($debug); } /** * Adds a datafile to the map. * * @param string $dataFile * @param mixed $color * @access public */ function addDataFile($dataFile, $color) { $this->dataFiles[$dataFile] = $color; } /** * Parses the data files of the map. * * @access public * @return array */ function parse() { foreach ($this->dataFiles as $dataFile => $color) { $this->parseFile($dataFile, $color); } return $this->lines; } /** * Parses a data file. * * @param string $dataFile * @param mixed $color * @access public * @abstract */ function parseFile($dataFile, $color) { /* abstract */ } } ?> Index: pear/Image_GIS/GIS/Renderer.php +++ pear/Image_GIS/GIS/Renderer.php <?php // // +------------------------------------------------------------------------+ // | PEAR :: Image :: GIS :: Renderer Base Class | // +------------------------------------------------------------------------+ // | Copyright (c) 2002-2003 Jan Kneschke <jan@kneschke.de> and | // | Sebastian Bergmann <sb@sebastian-bergmann.de>. | // +------------------------------------------------------------------------+ // | This source file is subject to version 3.00 of the PHP License, | // | that is available at http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +------------------------------------------------------------------------+ // // $Id: Renderer.php,v 1.1 2002/12/30 11:36:50 sebastian Exp $ // require_once 'Image/Color.php'; /** * Renderer Base Class. * * @version $Revision: 1.1 $ * @since Image_GIS 1.0.0 */ class Image_GIS_Renderer { /** * Set to TRUE to enable debugging. * * @var boolean $debug */ var $debug; /** * @var array $min */ var $min; /** * @var array $max */ var $max; /** * Width of the image. * * @var integer $width */ var $width; /** * Height of the image. * * @var integer $height */ var $height; /** * Constructor. * * @param mixed $width * @param integer $height * @param boolean $debug * @access public */ function Image_GIS_Renderer($width, $height, $debug) { $this->debug = $debug; if ($width < 0 || $width > 2048) { $width = 640; } if ($height < 0 || $height > 2048) { $height = 480; } $this->width = $width; $this->height = $height; } /** * Factory. * * @param string $renderer * @param mixed $width * @param integer $height * @param boolean $debug * @return object * @access public */ function &factory($renderer, $width, $height, $debug) { if (@include_once('Image/GIS/Renderer/' . $renderer . '.php')) { $class = 'Image_GIS_Renderer_' . $renderer; return new $class($width, $height, $debug); } } /** * Draws a clipped line from ($x1, $y1) to ($x2, $y2) * using $color. * * @param float $x1 * @param float $y1 * @param float $x2 * @param float $y2 * @param mixed $color * @access public */ function drawClippedLine($x1, $y1, $x2, $y2, $color) { if (($x1 > $this->max['x'] || $x1 < $this->min['x'] || $y1 > $this->max['y'] || $y1 < $this->min['y']) && ($x2 > $this->max['x'] || $x2 < $this->min['x'] || $y2 > $this->max['y'] || $y2 < $this->min['y'])) { if ($this->debug) { printf('clipped x1: %d %d %d<br />', $x1, $this->min['x'], $this->max['x']); printf('clipped y1: %d %d %d<br />', $y1, $this->min['y'], $this->max['y']); printf('clipped x2: %d %d %d<br />', $x2, $this->min['x'], $this->max['x']); printf('clipped y2: %d %d %d<br />', $y2, $this->min['y'], $this->max['y']); } } else { if (!is_array($color)) { $color = Image_Color::namedColor2RGB($color); } $x1 = $this->polar2image($x1, 'x'); $y1 = $this->polar2image($y1, 'y'); $x2 = $this->polar2image($x2, 'x'); $y2 = $this->polar2image($y2, 'y'); if ($this->debug) { printf('Drawing line (%s, %s) -> (%s, %s)<br />', $x1, $y1, $x2, $y2); } $this->drawLine($x1, $y1, $x2, $y2, $color[0], $color[1], $color[2]); } } /** * Converts a polar coordinate to an image coordinate. * * @param float $polarCoordinate * @param string $direction * @access public */ function polar2image($polarCoordinate, $direction) { switch ($direction) { case 'x': { return ($polarCoordinate - $this->min[$direction]) * ($this->width / ($this->max[$direction] - $this->min[$direction])); } break; case 'y': { return ($polarCoordinate - $this->max[$direction]) * ($this->height / ($this->min[$direction] - $this->max[$direction])); } break; } } /** * Renders the image. * * @param array $lines * @access public */ function render($lines) { foreach ($lines as $line) { $this->drawClippedLine($line[0], $line[1], $line[2], $line[3], $line[4]); } } /** * Sets the range of the data to be rendered. * * @param float $x1 * @param float $x2 * @param float $y1 * @param float $y2 * @access public */ function setRange($x1, $x2, $y1, $y2) { $this->min = array('x' => $x1, 'y' => $y1); $this->max = array('x' => $x2, 'y' => $y2); } /** * Draws a line from ($x1, $y1) to ($x2, $y2) * using the color rgb($r, $g, $b). * * @param float $x1 * @param float $y1 * @param float $x2 * @param float $y2 * @param float $r * @param float $g * @param float $b * @access public * @abstract */ function drawLine($x1, $y1, $x2, $y2, $r, $g, $b) { /* abstract */ } /** * Saves the rendered image to a given file. * * @param string $filename * @return boolean * @access public * @abstract */ function saveImage($filename) { /* abstract */ } /** * Shows the rendered image. * * @access public * @abstract */ function showImage() { /* abstract */ } } ?> Index: pear/Image_GIS/GIS/Parser/E00.php +++ pear/Image_GIS/GIS/Parser/E00.php <?php // // +------------------------------------------------------------------------+ // | PEAR :: Image :: GIS :: E00 Parser | // +------------------------------------------------------------------------+ // | Copyright (c) 2002-2003 Jan Kneschke <jan@kneschke.de> and | // | Sebastian Bergmann <sb@sebastian-bergmann.de>. | // +------------------------------------------------------------------------+ // | This source file is subject to version 3.00 of the PHP License, | // | that is available at http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +------------------------------------------------------------------------+ // // $Id: E00.php,v 1.1 2002/12/30 11:36:50 sebastian Exp $ // require_once 'Image/GIS/Parser.php'; /** * E00 Parser. * * @version $Revision: 1.1 $ * @since Image_GIS 1.0.0 */ class Image_GIS_Parser_E00 extends Image_GIS_Parser { /** * Constructor. * * @param boolean $debug * @access public */ function Image_GIS_Parser_E00($debug) { $this->Image_GIS_Parser($debug); } /** * Parses a data file. * * @param string $dataFile * @param mixed $color * @return boolean * @access public */ function parseFile($dataFile, $color) { if ($fp = @fopen($dataFile, 'r')) { $numRecords = 0; $ln = 0; while(0 || $line = fgets($fp, 1024)) { $ln ++; if ($numRecords == 0 && preg_match("#^\s+([0-9]+)\s+([-0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)\s+([0-9]+)#", $line, $a)) { $numRecords = $a[7]; $pl['x'] = -1; $pl['y'] = -1; } else if ($numRecords && preg_match("#^ ([0-9]\.[0-9]{7}E[-+][0-9]{2}) ([0-9]\.[0-9]{7}E[-+][0-9]{2}) ([0-9]\.[0-9]{7}E[-+][0-9]{2}) ([0-9]\.[0-9]{7}E[-+][0-9]{2})#", $line, $a)) { if ($this->debug) { echo $a[0] . '<br />'; } if ($pl['x'] != -1 && $pl['y'] != -1) { $this->lines[] = array($pl['x'], $pl['y'], $a[1], $a[2], $color); } $numRecords--; $this->lines[] = array($a[1], $a[2], $a[3], $a[4], $color); $pl['x'] = $a[3]; $pl['y'] = $a[4]; $numRecords--; } else if ($numRecords && preg_match("#^ ([0-9]\.[0-9]{7}E[-+][0-9]{2}) ([0-9]\.[0-9]{7}E[-+][0-9]{2})#", $line, $a)) { if ($pl['x'] != -1 && $pl['y'] != -1) { $this->lines[] = array($pl['x'], $pl['y'], $a[1], $a[2], $color); $pl['x'] = $a[1]; $pl['y'] = $a[2]; } $numRecords--; } else if ($this->debug && $ln > 2) { sprintf( 'Died at: %s<br />', $ln ); break; } else if ($this->debug) { echo $line . '<br />'; } } @fclose($fp); return true; } return false; } } ?> Index: pear/Image_GIS/GIS/Renderer/GD.php +++ pear/Image_GIS/GIS/Renderer/GD.php <?php // // +------------------------------------------------------------------------+ // | PEAR :: Image :: GIS :: GD Renderer | // +------------------------------------------------------------------------+ // | Copyright (c) 2002-2003 Jan Kneschke <jan@kneschke.de> and | // | Sebastian Bergmann <sb@sebastian-bergmann.de>. | // +------------------------------------------------------------------------+ // | This source file is subject to version 3.00 of the PHP License, | // | that is available at http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +------------------------------------------------------------------------+ // // $Id: GD.php,v 1.1 2002/12/30 11:36:50 sebastian Exp $ // require_once 'Image/GIS/Renderer.php'; /** * GD Renderer. * * @version $Revision: 1.1 $ * @since Image_GIS 1.0.0 */ class Image_GIS_Renderer_GD extends Image_GIS_Renderer { /** * GD Image Palette. * * @var array $palette */ var $palette = array(); /** * GD Image Ressource. * * @var ressource $img */ var $img; /** * Constructor. * * @param mixed $width * @param integer $sizyY * @param boolean $debug * @access public */ function Image_GIS_Renderer_GD($width, $height, $debug) { if (is_file($width)) { $this->img = imagecreatefrompng($width); $width = imagesx($this->img); $height = imagesy($this->img); $this->Image_GIS_Renderer($width, $height, $debug); } else { $this->Image_GIS_Renderer($width, $height, $debug); $this->img = imagecreate($this->width, $this->height); imagecolorallocate($this->img, 255, 255, 255); } } /** * Draws a line from ($x1, $y1) to ($x2, $y2) * using the color rgb($r, $g, $b). * * @param float $x1 * @param float $y1 * @param float $x2 * @param float $y2 * @param float $r * @param float $g * @param float $b * @access public */ function drawLine($x1, $y1, $x2, $y2, $r, $g, $b) { if (!isset($this->palette[$r][$g][$b])) { $this->palette[$r][$g][$b] = imagecolorallocate($this->img, $r, $g, $b); } imageline( $this->img, $x1, $y1, $x2, $y2, $this->palette[$r][$g][$b] ); } /** * Saves the rendered image to a given file. * * @param string $filename * @return boolean * @access public */ function saveImage($filename) { return imagepng($this->img, $filename); } /** * Shows the rendered image. * * @access public */ function showImage() { header('Content-Type: image/png'); imagepng($this->img); } } ?> Index: pear/Image_GIS/GIS/Renderer/SVG.php +++ pear/Image_GIS/GIS/Renderer/SVG.php <?php // // +------------------------------------------------------------------------+ // | PEAR :: Image :: GIS :: SVG Renderer | // +------------------------------------------------------------------------+ // | Copyright (c) 2002-2003 Jan Kneschke <jan@kneschke.de> and | // | Sebastian Bergmann <sb@sebastian-bergmann.de>. | // +------------------------------------------------------------------------+ // | This source file is subject to version 3.00 of the PHP License, | // | that is available at http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +------------------------------------------------------------------------+ // // $Id: SVG.php,v 1.1 2002/12/30 11:36:50 sebastian Exp $ // require_once 'Image/GIS/Renderer.php'; require_once 'XML/Tree.php'; /** * SVG Renderer. * * @version $Revision: 1.1 $ * @since Image_GIS 1.0.0 */ class Image_GIS_Renderer_SVG extends Image_GIS_Renderer { /** * SVG Document. * * @var XML_Tree $svg */ var $svg; /** * SVG Document Root. * * @var XML_Tree_Node $root */ var $root; /** * Constructor. * * @param mixed $width * @param integer $sizyY * @param boolean $debug * @access public */ function Image_GIS_Renderer_SVG($width, $height, $debug) { $this->Image_GIS_Renderer($width, $height, $debug); $this->svg = new XML_Tree; $this->root = &$this->svg->addRoot( 'svg', '', array( 'width' => $this->width, 'height' => $this->height ) ); } /** * Draws a line from ($x1, $y1) to ($x2, $y2) * using the color rgb($r, $g, $b). * * @param float $x1 * @param float $y1 * @param float $x2 * @param float $y2 * @param float $r * @param float $g * @param float $b * @access public */ function drawLine($x1, $y1, $x2, $y2, $r, $g, $b) { $line = &$this->root->addChild( 'line', '', array( 'x1' => $x1, 'y1' => $y1, 'x2' => $x2, 'y2' => $y2, 'style' => sprintf( 'stroke:rgb(%s, %s, %s)', $r, $g, $b ) ) ); } /** * Saves the rendered image to a given file. * * @param string $filename * @return boolean * @access public */ function saveImage($filename) { if ($fp = @fopen($filename, 'w')) { @fputs($fp, $this->root->get()); @fclose($fp); return true; } return false; } /** * Shows the rendered image. * * @access public */ function showImage() { header('Content-Type: image/svg+xml'); echo $this->root->get(); } } ?>
« previous php.pear.cvs (#7994) next »