[Thuban-commits] r2805 - in trunk/thuban: . libraries/thuban

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Wed Jan 9 01:43:58 CET 2008


Author: bernhard
Date: 2008-01-09 01:43:45 +0100 (Wed, 09 Jan 2008)
New Revision: 2805

Modified:
   trunk/thuban/ChangeLog
   trunk/thuban/libraries/thuban/wxproj.cpp
Log:
Making Thuban robust against shapefiles which contain empty shapes.

* libraries/thuban/wxproj.cpp(project_points): setting an exception
now if called with no points at all. This is necessary, because 
the NULL pointer already indicates an error condition, thus it
cannot be used to indicate that there was no point in the result.
(draw_polygon_shape, (point_in_polygon_shape): stop if there 
are no points in the shape.
Propagate an error condition on failure of (project_points()).
(point_in_polygon_shape): shifted some lines around to allow
a cleanup jump point.

* Thuban/Model/layer.py, Thuban/UI/viewport.py: removed unused import
of  point_in_polygon_shape.


Modified: trunk/thuban/ChangeLog
===================================================================
--- trunk/thuban/ChangeLog	2008-01-09 00:34:37 UTC (rev 2804)
+++ trunk/thuban/ChangeLog	2008-01-09 00:43:45 UTC (rev 2805)
@@ -1,5 +1,17 @@
 2008-01-09   Bernhard Reiter <bernhard at intevation.de>
 
+	Making Thuban robust against shapefiles which contain empty shapes.
+
+	* libraries/thuban/wxproj.cpp(project_points): setting an exception
+	now if called with no points at all. This is necessary, because 
+	the NULL pointer already indicates an error condition, thus it
+	cannot be used to indicate that there was no point in the result.
+	(draw_polygon_shape, (point_in_polygon_shape): stop if there 
+	are no points in the shape.
+	Propagate an error condition on failure of (project_points()).
+	(point_in_polygon_shape): shifted some lines around to allow
+	a cleanup jump point.
+
 	* Thuban/Model/layer.py, Thuban/UI/viewport.py: removed unused import
 	of  point_in_polygon_shape.
 

Modified: trunk/thuban/libraries/thuban/wxproj.cpp
===================================================================
--- trunk/thuban/libraries/thuban/wxproj.cpp	2008-01-09 00:34:37 UTC (rev 2804)
+++ trunk/thuban/libraries/thuban/wxproj.cpp	2008-01-09 00:43:45 UTC (rev 2805)
@@ -185,7 +185,12 @@
 {
     int i;
     int num_points = num_vertices + num_parts - 1;
-    if (num_points <= 0) return NULL;
+    if (num_points <= 0) 
+    {
+        PyErr_SetString(PyExc_ValueError, 
+                "project_points() called without points");
+        return NULL;
+    }
 
     wxPoint* points = (wxPoint*)malloc(num_points * sizeof(wxPoint));
     if (!points)
@@ -373,6 +378,12 @@
 			    "Can't get shape %d from shapefile", shape_index);
     
     num_points = shape->nVertices + shape->nParts - 1;
+    if (num_points <= 0 )
+    {
+        /* printf("empty shape\n"); TODO: empty shape, should we log this? */
+        goto draw_polygon_shape_cleanup2;
+    }
+
     points = project_points(shape->nVertices, shape->nParts,
 			    shape->padfX, shape->padfY, shape->panPartStart,
 			    draw_info->forward, 
@@ -381,6 +392,9 @@
                             draw_info->scaley, 
                             draw_info->offx, 
                             draw_info->offy);
+    /* propagating the error if there is an exception pending */
+    if (!points) return NULL;
+
     dc = draw_info->dc;
 
     // If the shape is a polygon and a non-transparent brush was given,
@@ -421,6 +435,7 @@
     
     free(points);
 
+  draw_polygon_shape_cleanup2: 
     pyshapelib_api->SHPDestroyObject(shape);
 
     Py_INCREF(Py_None);
@@ -518,12 +533,22 @@
 	return PyErr_Format(PyExc_ValueError,
 			    "Can't get shape %d from shapefile", shape_index);
 
+    long scaled_px = (px << PREC_BITS) + 1, scaled_py = (py << PREC_BITS) + 1;
+
     num_points = shape->nVertices + shape->nParts - 1;
+    if (num_points <= 0 )
+    {
+        /* TODO: empty shape, should we log this somehow? */
+        result = -1;
+        goto point_in_polygon_shape_cleanup2;
+    }
+
     points = project_points(shape->nVertices, shape->nParts,
 			    shape->padfX, shape->padfY, shape->panPartStart,
 			    forward, inverse, scalex, scaley, offx, offy);
+    /* propagating the error if there is an exception pending */
+    if (!points) return NULL;
 
-    long scaled_px = (px << PREC_BITS) + 1, scaled_py = (py << PREC_BITS) + 1;
 
     cross_count = 0; linehit = 0;
     for (int part = 0; part < shape->nParts; part++)
@@ -565,8 +590,6 @@
 
     free(points);
 
-    pyshapelib_api->SHPDestroyObject(shape);
-
     if (filled)
     {
 	if (stroked && linehit)
@@ -585,6 +608,8 @@
 	result = 0;
 		
 
+  point_in_polygon_shape_cleanup2:
+    pyshapelib_api->SHPDestroyObject(shape);
     return PyInt_FromLong(result);
 }
 



More information about the Thuban-commits mailing list