[Mapbender-commits] r7812 - trunk/mapbender/http/php

svn_mapbender at osgeo.org svn_mapbender at osgeo.org
Wed May 11 10:18:54 EDT 2011


Author: apour
Date: 2011-05-11 07:18:54 -0700 (Wed, 11 May 2011)
New Revision: 7812

Modified:
   trunk/mapbender/http/php/mod_digitize_splitLine.php
Log:
bugfix

Modified: trunk/mapbender/http/php/mod_digitize_splitLine.php
===================================================================
--- trunk/mapbender/http/php/mod_digitize_splitLine.php	2011-05-11 07:01:19 UTC (rev 7811)
+++ trunk/mapbender/http/php/mod_digitize_splitLine.php	2011-05-11 14:18:54 UTC (rev 7812)
@@ -1,4 +1,5 @@
 <?php
+
 # License:
 # Copyright (c) 2009, Open Source Geospatial Foundation
 # This program is dual licensed under the GNU General Public License 
@@ -10,71 +11,95 @@
 
 $json = new Mapbender_JSON();
 
-$line1Text = $_REQUEST["line1"];
-$line2Text = $_REQUEST["line2"];
 
+function isLinestring($string) {
+	$floatPattern = "-?\d+(\.\d+)?";
+	$pointPattern = $floatPattern . " " . $floatPattern;
+	$linePattern = "LINESTRING \(" . $pointPattern . ",( )*" . $pointPattern . "(,( )*" . $pointPattern . ")*\)";
+	
+	if(preg_match("/" . $linePattern . "/", $string)) {
+		return true;
+	}
+	return false;
+}
 
-$floatPattern = "-?\d+(\.\d+)?";
-$pointPattern = $floatPattern . " " . $floatPattern;
-$linePattern = "LINESTRING \(" . $pointPattern . ",( )*" . $pointPattern . "(,( )*" . $pointPattern . ")*\)";
 
-$pattern = "/" . $linePattern . "/";
-if (!preg_match($pattern, $line1Text)) {
-	echo "not a line.";
-	die();
+if(!isLinestring($_REQUEST["line1"]) OR !isLinestring($_REQUEST["line2"])) {
+	die("not a line.");
 }
 
-if (!preg_match($pattern, $line2Text)) {
-	echo "not a line.";
-	die();
-}
+$line1Text = $_REQUEST["line1"];
+$line2Text = $_REQUEST["line2"];
 
+
 // find intersection points
-
 $intersection_sql =  sprintf("SELECT ST_AsText(multipoint.geom) as point, "
 							." ST_AsText(ST_StartPoint('%s'::geometry)) AS startpoint,"
 							." ST_AsText(ST_EndPoint('%s'::geometry)) AS endpoint "
 							." FROM ST_Dump((SELECT ST_AsText(ST_Intersection('%s'::geometry,'%s'::geometry)))) AS multipoint;",
 							$line1Text,$line1Text,$line1Text,$line2Text);
+
 $res = db_query($intersection_sql);    
 
 
 // add first and last points to the array of intersectionpoints
+$startPoint = null;
+$endPoint = null;
 $points = array();
 $lineArray = array();
 
+
 while ($row = db_fetch_array($res)) {
-	$points[0] = $row['startpoint'];
-	$endpoint = $row['endpoint'];
+	$startPoint = $row['startpoint'];
+	$endPoint = $row['endpoint'];
 	$points[] = $row['point'];
 }
+
+
 // if the two lines don't intersect, we just do nothing, and return the first line
 if(count($points) == 0){
 	$lineArray[] = $line1Text;
 }else{
-	$points[] = $endpoint;
-
+	$points = array_merge(array($startPoint),$points,array($endPoint));
+	
 	// go through the point array in pairs, cut into segment, and add each segment onto resultArray
 	for($i = 0; $i < count($points)-1; $i++){
 
 		$pointStartText = $points[$i];
 		$pointEndText = $points[$i+1];
 		
-		$nthSegment_sql = sprintf("SELECT ST_AsText(ST_multi(geom)) AS substring FROM "
-			." ST_Dump((SELECT ST_AsText(ST_FORCE_COLLECTION(ST_Line_Substring("
-				."'%s'::geometry,"
-				."ST_Line_Locate_Point('%s'::geometry,'%s'::geometry),"
-				."ST_Line_Locate_Point('%s'::geometry,'%s'::geometry)"
-			.")))));",
-			$line1Text,$line1Text,$pointStartText,$line1Text,$pointEndText);
+		if($i == count($points) -2 && $startPoint == $endPoint) {
+			$nthSegment_sql = sprintf("SELECT ST_AsText(ST_multi(geom)) AS substring FROM "
+				." ST_Dump((SELECT ST_AsText(ST_FORCE_COLLECTION(ST_Line_Substring("
+					."'%s'::geometry,"
+					."ST_Line_Locate_Point('%s'::geometry,'%s'::geometry),"
+					."1.0"
+				.")))));",
+				$line1Text,$line1Text,$pointStartText,$line1Text,$pointEndText);		
+		} else {
+			$nthSegment_sql = sprintf("SELECT ST_AsText(ST_multi(geom)) AS substring FROM "
+				." ST_Dump((SELECT ST_AsText(ST_FORCE_COLLECTION(ST_Line_Substring("
+					."'%s'::geometry,"
+					."ST_Line_Locate_Point('%s'::geometry,'%s'::geometry),"
+					."ST_Line_Locate_Point('%s'::geometry,'%s'::geometry)"
+				.")))));",
+				$line1Text,$line1Text,$pointStartText,$line1Text,$pointEndText);
+		}
+		
 		$res = db_query($nthSegment_sql);    
-		if($row = db_fetch_array($res)){
+		
+		if($row = db_fetch_array($res)) {
 			$lineArray[] = $row['substring'];
 		}
 	}
 }
+
+
+
+// OUTPUT
 $data = array("geometries" => $lineArray);
 $output = $json->encode($data);
 header("Content-type:application/x-json; charset=utf-8");
+
 echo $output;
 ?>



More information about the Mapbender_commits mailing list