[geos-commits] r3477 - in branches/3.2: . capi source/headers/geos/linearref source/linearref

svn_geos at osgeo.org svn_geos at osgeo.org
Wed Sep 21 04:56:30 EDT 2011


Author: strk
Date: 2011-09-21 01:56:30 -0700 (Wed, 21 Sep 2011)
New Revision: 3477

Modified:
   branches/3.2/NEWS
   branches/3.2/capi/geos_ts_c.cpp
   branches/3.2/source/headers/geos/linearref/LengthIndexOfPoint.h
   branches/3.2/source/headers/geos/linearref/LinearIterator.h
   branches/3.2/source/linearref/LinearIterator.cpp
   branches/3.2/source/linearref/LinearLocation.cpp
Log:
Fix LinearReferencing functions segfault (#353)


Modified: branches/3.2/NEWS
===================================================================
--- branches/3.2/NEWS	2011-09-21 08:39:40 UTC (rev 3476)
+++ branches/3.2/NEWS	2011-09-21 08:56:30 UTC (rev 3477)
@@ -6,6 +6,7 @@
   - DistanceOp segfaults on MULTIPOLYGON with EMPTY elements (#367)
   - RobustDeterminant is not robust (#450)
   - Fix out-of-place build for python binding (#332)
+  - Fix LinearReferencing functions segfault (#353)
 
 Changes in 3.2.2
  

Modified: branches/3.2/capi/geos_ts_c.cpp
===================================================================
--- branches/3.2/capi/geos_ts_c.cpp	2011-09-21 08:39:40 UTC (rev 3476)
+++ branches/3.2/capi/geos_ts_c.cpp	2011-09-21 08:56:30 UTC (rev 3477)
@@ -4403,36 +4403,52 @@
               const Geometry *g,
               const Geometry *p)
 {
+    if ( 0 == extHandle ) return -1.0;
+    GEOSContextHandleInternal_t *handle = 
+        reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
+    if ( handle->initialized == 0 ) return -1.0;
 
     const geos::geom::Point* point = dynamic_cast<const geos::geom::Point*>(p);
     if (!point) {
-        if ( 0 == extHandle )
-        {
-            return -1.0;
-        }
-        GEOSContextHandleInternal_t *handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
-        if ( 0 == handle->initialized )
-        {
-            return -1.0;
-        }
-
         handle->ERROR_MESSAGE("third argument of GEOSProject_r must be Point*");
         return -1.0;
     }
+
     const geos::geom::Coordinate* inputPt = p->getCoordinate();
-    return geos::linearref::LengthIndexedLine(g).project(*inputPt);
+
+    try {
+        return geos::linearref::LengthIndexedLine(g).project(*inputPt);
+    } catch (const std::exception &e) {
+        handle->ERROR_MESSAGE("%s", e.what());
+        return -1.0;
+    } catch (...) {
+        handle->ERROR_MESSAGE("Unknown exception thrown");
+        return -1.0;
+    }
 }
 
 
 Geometry*
 GEOSInterpolate_r(GEOSContextHandle_t extHandle, const Geometry *g, double d)
 {
-    geos::linearref::LengthIndexedLine lil(g);
-    geos::geom::Coordinate coord = lil.extractPoint(d);
-    GEOSContextHandleInternal_t *handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
-    const GeometryFactory *gf = handle->geomFactory;
-    Geometry* point = gf->createPoint(coord);
-    return point;
+    if ( 0 == extHandle ) return 0;
+    GEOSContextHandleInternal_t *handle = 
+        reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
+    if ( handle->initialized == 0 ) return 0;
+
+    try {
+    	geos::linearref::LengthIndexedLine lil(g);
+    	geos::geom::Coordinate coord = lil.extractPoint(d);
+    	const GeometryFactory *gf = handle->geomFactory;
+    	Geometry* point = gf->createPoint(coord);
+    	return point;
+    } catch (const std::exception &e) {
+        handle->ERROR_MESSAGE("%s", e.what());
+        return 0;
+    } catch (...) {
+        handle->ERROR_MESSAGE("Unknown exception thrown");
+        return 0;
+    }
 }
 
 

Modified: branches/3.2/source/headers/geos/linearref/LengthIndexOfPoint.h
===================================================================
--- branches/3.2/source/headers/geos/linearref/LengthIndexOfPoint.h	2011-09-21 08:39:40 UTC (rev 3476)
+++ branches/3.2/source/headers/geos/linearref/LengthIndexOfPoint.h	2011-09-21 08:56:30 UTC (rev 3477)
@@ -34,6 +34,15 @@
 namespace linearref   // geos::linearref
 {
 
+/**
+ * \brief
+ * Computes the length index of the point
+ * on a linear Geometry nearest a given Coordinate.
+ *
+ * The nearest point is not necessarily unique; this class
+ * always computes the nearest point closest to
+ * the start of the geometry.
+ */
 class LengthIndexOfPoint
 {
 
@@ -53,17 +62,17 @@
 	LengthIndexOfPoint(const geom::Geometry *linearGeom);
 
 	/**
-	 * Find the nearest location along a linear {@link Geometry} to a given point.
+	 * Find the nearest location along a linear Geometry to a given point.
 	 *
 	 * @param inputPt the coordinate to locate
 	 * @return the location of the nearest point
 	 */
 	double indexOf(const geom::Coordinate& inputPt) const;
 
-	/**
-	 * Finds the nearest index along the linear {@link Geometry}
-	 * to a given {@link Coordinate}
-	 * after the specified minimum index.
+	/** \brief
+	 * Finds the nearest index along the linear Geometry
+	 * to a given Coordinate after the specified minimum index.
+	 *
 	 * If possible the location returned will be strictly greater than the
 	 * <code>minLocation</code>.
 	 * If this is not possible, the

Modified: branches/3.2/source/headers/geos/linearref/LinearIterator.h
===================================================================
--- branches/3.2/source/headers/geos/linearref/LinearIterator.h	2011-09-21 08:39:40 UTC (rev 3476)
+++ branches/3.2/source/headers/geos/linearref/LinearIterator.h	2011-09-21 08:56:30 UTC (rev 3477)
@@ -31,11 +31,11 @@
 namespace geos { namespace linearref
 {
 
-/**
+/** \brief
  * An iterator over the components and coordinates of a linear geometry
- * ({@link LineString}s and {@link MultiLineString}s.
+ * (LineString or MultiLineString).
  *
- * The standard usage pattern for a {@link LinearIterator} is:
+ * The standard usage pattern for a LinearIterator is:
  *
  * <pre>
  * for (LinearIterator it = new LinearIterator(...); it.hasNext(); it.next()) {
@@ -52,7 +52,7 @@
 {
 public:
 	/**
-	 * Creates an iterator initialized to the start of a linear {@link Geometry}
+	 * Creates an iterator initialized to the start of a linear Geometry
 	 *
 	 * @param linear the linear geometry to iterate over
 	 */
@@ -142,7 +142,8 @@
 	const unsigned int numLines;
 
 	/**
-	 * Invariant: currentLine <> null if the iterator is pointing at a valid coordinate
+	 * Invariant: currentLine <> null if the iterator is pointing
+	 *            at a valid coordinate
 	 */
 	void loadCurrentLine();
 

Modified: branches/3.2/source/linearref/LinearIterator.cpp
===================================================================
--- branches/3.2/source/linearref/LinearIterator.cpp	2011-09-21 08:39:40 UTC (rev 3476)
+++ branches/3.2/source/linearref/LinearIterator.cpp	2011-09-21 08:56:30 UTC (rev 3477)
@@ -23,6 +23,7 @@
 #include <geos/linearref/LinearIterator.h>
 #include <geos/linearref/LinearLocation.h>
 #include <geos/linearref/LengthLocationMap.h>
+#include <geos/util/IllegalArgumentException.h>
 
 using namespace geos::geom;
 
@@ -74,6 +75,9 @@
 		return;
 	}
 	currentLine = dynamic_cast<const LineString *> (linear->getGeometryN(componentIndex));
+	if ( ! currentLine ) {
+		throw util::IllegalArgumentException("LinearIterator only supports lineal geometry components");
+	}
 }
 
 bool LinearIterator::hasNext() const

Modified: branches/3.2/source/linearref/LinearLocation.cpp
===================================================================
--- branches/3.2/source/linearref/LinearLocation.cpp	2011-09-21 08:39:40 UTC (rev 3476)
+++ branches/3.2/source/linearref/LinearLocation.cpp	2011-09-21 08:56:30 UTC (rev 3477)
@@ -22,6 +22,7 @@
 #include <geos/linearref/LengthIndexedLine.h>
 #include <geos/linearref/LinearLocation.h>
 #include <geos/linearref/LengthLocationMap.h>
+#include <geos/util/IllegalArgumentException.h>
 
 using namespace std;
 
@@ -201,6 +202,9 @@
 LinearLocation::getCoordinate(const Geometry* linearGeom) const
 {
 	const LineString* lineComp = dynamic_cast<const LineString *> (linearGeom->getGeometryN(componentIndex));
+	if ( ! lineComp ) {
+		throw util::IllegalArgumentException("LinearLocation::getCoordinate only works with LineString geometries");
+	}  
 	Coordinate p0 = lineComp->getCoordinateN(segmentIndex);
 	if (segmentIndex >= lineComp->getNumPoints() - 1)
 		return p0;



More information about the geos-commits mailing list