[Mapbender-commits] r1628 - trunk/mapbender/http/classes

svn_mapbender at osgeo.org svn_mapbender at osgeo.org
Wed Aug 8 05:29:55 EDT 2007


Author: christoph
Date: 2007-08-08 05:29:55 -0400 (Wed, 08 Aug 2007)
New Revision: 1628

Added:
   trunk/mapbender/http/classes/class_bbox.php
Log:
new classes representing a coordinate and a bounding box w/ epsg transformation routine

Added: trunk/mapbender/http/classes/class_bbox.php
===================================================================
--- trunk/mapbender/http/classes/class_bbox.php	                        (rev 0)
+++ trunk/mapbender/http/classes/class_bbox.php	2007-08-08 09:29:55 UTC (rev 1628)
@@ -0,0 +1,210 @@
+<?php
+# $Id$
+# http://www.mapbender.org/index.php/Mod_treefolder2.php
+# Copyright (C) 2007 Melchior Moos
+# 
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2, or (at your option)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+require_once(dirname(__FILE__)."/../../conf/mapbender.conf");
+require_once(dirname(__FILE__)."/../classes/class_mb_exception.php");
+$con = db_connect(DBSERVER,OWNER,PW);
+db_select_db(DB,$con);
+
+/**
+ * A Mapbender_point is a 2-dimensional point with an EPSG. 
+ */
+class Mapbender_point {
+	var $x;
+	var $y;
+	var $epsg;
+	
+	/**
+	 * @constructor
+	 */
+	function Mapbender_point($x, $y, $epsg) {
+		if ($x == null || $y == null || $epsg == null) {
+			$e = new mb_exception("Mapbender_point: constructor: some parameters are not set!");
+		}
+		$this->x = $x;
+		$this->y = $y;
+		$this->epsg = $epsg;
+	}
+	
+	/**
+	 * computes a new point with the minimal coordinates of this point and $point
+	 */
+	function min ($point) {
+		if ($this->epsg == $point->epsg) {
+			if ($this->isWestOf($point)) {
+				$minx = $this->x;
+			}
+			else {
+				$minx = $point->x;
+			}
+			if ($this->isSouthOf($point)) {
+				$miny = $this->y;
+			}
+			else {
+				$miny = $point->y;
+			}
+			return new Mapbender_point($minx, $miny, $this->epsg);
+		}
+		else {
+			$e = new mb_exception("Mapbender_point: cannot process min with different EPSG codes");
+		}
+	}
+	
+	/**
+	 * computes a new point with the maximal coordinates of this point and $point
+	 */
+	function max ($point) {
+		if ($this->epsg == $point->epsg) {
+			if ($this->isWestOf($point)) {
+				$maxx = $point->x;
+			}
+			else {
+				$maxx = $this->x;
+			}
+			if ($this->isSouthOf($point)) {
+				$maxy = $point->y;
+			}
+			else {
+				$maxy = $this->y;
+			}
+			return new Mapbender_point($maxx, $maxy, $this->epsg);
+		}
+		else {
+			$e = new mb_exception("Mapbender_point: cannot process min with different EPSG codes");
+		}
+	}
+	
+	function isWestOf($point) {
+		if ($this->x < $point->x) {
+			return true;
+		}
+	}
+
+	function isSouthOf($point) {
+		if ($this->y < $point->y) {
+			return true;
+		}
+	}
+
+	/**
+	 * transforms this point to another EPSG
+	 */
+	function transform($toEpsg) {
+		if(SYS_DBTYPE=='pgsql'){
+			$currentEpsg = preg_replace("/EPSG:/", "", $this->epsg);
+			$targetEpsg = preg_replace("/EPSG:/", "", $toEpsg);
+			$sql = "SELECT X(transform(GeometryFromText('POINT(".$this->x." ".$this->y.")',".$currentEpsg."),".$targetEpsg.")) as x, ";
+			$sql .= "Y(transform(GeometryFromText('POINT(".$this->x." ".$this->y.")',".$currentEpsg."),".$targetEpsg.")) as y";
+			$res = db_query($sql);
+			$point = new Mapbender_point(db_result($res,0,"x"), db_result($res,0,"y"), $toEpsg);
+			$this->x = $point->x;
+			$this->y = $point->y;
+			$this->epsg = $point->epsg;
+		}
+		else {
+			$e = new mb_exception("transformCoordinates needs PostgreSQL");
+		}
+	}
+	
+	function toString() {
+		return "(" . $this->x . "," . $this->y . "," . $this->epsg . ")";
+	}
+}
+
+/**
+ * A bounding box consisting of an lower left and an upper right point, and an EPSG.
+ */
+class Mapbender_bbox {
+	var $min;
+	var $max;
+	var $epsg;
+	
+	/**
+	 * @constructor
+	 */
+	function Mapbender_bbox($min, $max, $epsg) {
+		if ($min->isWestOf($max) && $min->isSouthOf($max)) {
+			if ($min->epsg == $max->epsg && $min->epsg == $epsg) {
+				$this->min = $min;
+				$this->max = $max;
+				$this->epsg = $epsg;
+			}
+			else {
+				$e = new mb_exception("Mapbender_bbox: EPSG mismatch!");
+			}
+		}
+		else {
+			$e = new mb_exception("Mapbender_bbox: min is not southwest of max!");
+		}
+	}
+	
+	/**
+	 * Computes a new bounding box, this bbox UNION $bbox
+	 */
+	function union ($bbox) {
+		if ($this->isValid()) {
+			if ($bbox != null && $bbox->isValid()) {
+				if ($this->epsg == $bbox->epsg) {
+					return new Mapbender_bbox($this->min->min($bbox->min), $this->max->max($bbox->max), $this->epsg);
+				}
+				else {
+					$e = new mb_exception("mapbender_bbox: cannot process union with different EPSG codes");
+				}
+			}
+			else {
+				$e = new mb_notice("mapbender_bbox: union: second parameter not a bbox, returning first bbox");
+				return new Mapbender_bbox($this->min, $this->max, $this->epsg);
+			}
+		}
+		if ($bbox != null && $bbox->isValid()) {
+			$e = new mb_notice("mapbender_bbox: union: first parameter not a bbox, returning second bbox");
+			return $bbox;
+		}
+		return null;
+	}
+	
+	/**
+	 * transforms this bbox in another EPSG
+	 */
+	function transform($toEpsg) {
+		if ($this->isValid()) {
+			$this->epsg = $toEpsg;
+			$this->min->transform($toEpsg);
+			$this->max->transform($toEpsg);
+			return true;
+		}
+		return false;
+	}
+
+	/**
+	 * checks if lower left and upper right coordinate are set, as well as EPSG
+	 */
+	function isValid() {
+		if ($this->min != null && $this->max != null && $this->epsg != null) {
+			return true;
+		}
+		$e = new mb_exception("Mapbender_bbox: this is not a valid bbox!");
+		return false;
+	}
+	
+	function toString() {
+		return "[" . $this->min->toString() . $this->max->toString() . " " . $this->epsg . "]"; 
+	}
+}
+?>
\ No newline at end of file


Property changes on: trunk/mapbender/http/classes/class_bbox.php
___________________________________________________________________
Name: svn:keywords
   + Id



More information about the Mapbender_commits mailing list