[Schmitzm-commits] r913 - in trunk: src/schmitzm/geotools src/schmitzm/geotools/gui src_junit/schmitzm/geotools/gui

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Sat Jun 26 12:55:50 CEST 2010


Author: alfonx
Date: 2010-06-26 12:55:49 +0200 (Sat, 26 Jun 2010)
New Revision: 913

Added:
   trunk/src_junit/schmitzm/geotools/gui/XMapPaneTest.java
Modified:
   trunk/src/schmitzm/geotools/JTSUtil.java
   trunk/src/schmitzm/geotools/gui/XMapPane.java
Log:
BugFIX: XMapPane did crash for one-point-only layers, beacause their BBOX has width 0 and a Transfomation from Screen To Map could not be calculated.

Modified: trunk/src/schmitzm/geotools/JTSUtil.java
===================================================================
--- trunk/src/schmitzm/geotools/JTSUtil.java	2010-06-26 10:02:49 UTC (rev 912)
+++ trunk/src/schmitzm/geotools/JTSUtil.java	2010-06-26 10:55:49 UTC (rev 913)
@@ -323,23 +323,91 @@
 
 		return re;
 	}
-	
+
 	/**
 	 * TODO doku
+	 * 
 	 * @param wkt
 	 * @param crs
 	 * @return
 	 * @throws ParseException
 	 */
-	public static Geometry parseWKT(String wkt, CoordinateReferenceSystem crs) throws ParseException {
-		Hints hints = new Hints( Hints.CRS, crs != null ? crs : DefaultGeographicCRS.WGS84 );
-		GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( hints );
+	public static Geometry parseWKT(String wkt, CoordinateReferenceSystem crs)
+			throws ParseException {
+		Hints hints = new Hints(Hints.CRS, crs != null ? crs
+				: DefaultGeographicCRS.WGS84);
+		GeometryFactory geometryFactory = JTSFactoryFinder
+				.getGeometryFactory(hints);
 
-		WKTReader reader = new WKTReader( geometryFactory );
+		WKTReader reader = new WKTReader(geometryFactory);
 		Geometry geometry = (Geometry) reader.read(wkt);
-		
+
 		return geometry;
+
+	}
+
+	/**
+	 * Checks that the passed {@link ReferencedEnvelope} doesn't have a width or
+	 * height of 0! If any of them is zero, it's increased by +/-1 in each
+	 * direction.
+	 * 
+	 * TODO check that the new Envelope is still in the valid are of the CRS
+	 * env.getCoordinateReferenceSystem().getDomainOfValidity()
+	 * 
+	 * @return A {@link ReferencedEnvelope} that has an area greater 0!
+	 */
+	public static ReferencedEnvelope checkNotZeroArea(
+			final ReferencedEnvelope env) {
 		
+		ReferencedEnvelope env2 = env;
+		// Check, that the BBOX doesn't have a height of 0. If so, the height is
+		// increased by 2!
+		if (env.getHeight() == 0.) {
+			env2 = new ReferencedEnvelope(env.getMinX(), env.getMaxX(), env
+					.getMinY() - 1., env.getMaxY() + 1., env
+					.getCoordinateReferenceSystem());
+		}
+		// Check, that the BBOX doesn't have a width of 0. If so, the width is
+		// increased by 2!
+		if (env.getWidth() == 0.) {
+			env2 = new ReferencedEnvelope(env.getMinX() - 1.,
+					env.getMaxX() + 1., env.getMinY(), env.getMaxY(), env
+							.getCoordinateReferenceSystem());
+		}
+
+		// env.getCoordinateReferenceSystem().getDomainOfValidity()
+
+		return env2;
 	}
 
+	/**
+	 * Checks that the passed {@link Envelope} doesn't have a width or
+	 * height of 0! If any of them is zero, it's increased by +/-1 in each
+	 * direction.
+	 * 
+	 * TODO check that the new Envelope is still in the valid are of the CRS
+	 * env.getCoordinateReferenceSystem().getDomainOfValidity()
+	 * 
+	 * @return A {@link ReferencedEnvelope} that has an area greater 0!
+	 */
+	public static Envelope checkNotZeroArea(final Envelope env) {
+		Envelope env2 = env;
+		// Check, that the BBOX doesn't have a height of 0. If so, the height is
+		// increased by 2!
+		if (env.getHeight() == 0.) {
+			env2 = new Envelope(env.getMinX(), env.getMaxX(),
+					env.getMinY() - 1., env.getMaxY() + 1.);
+		}
+		// Check, that the BBOX doesn't have a width of 0. If so, the width is
+		// increased by 2!
+		if (env.getWidth() == 0.) {
+			env2 = new Envelope(env.getMinX() - 1., env.getMaxX() + 1., env
+					.getMinY(), env.getMaxY());
+		}
+
+		// env.getCoordinateReferenceSystem().getDomainOfValidity()
+
+		return env2;
+	}
+
 }

Modified: trunk/src/schmitzm/geotools/gui/XMapPane.java
===================================================================
--- trunk/src/schmitzm/geotools/gui/XMapPane.java	2010-06-26 10:02:49 UTC (rev 912)
+++ trunk/src/schmitzm/geotools/gui/XMapPane.java	2010-06-26 10:55:49 UTC (rev 913)
@@ -773,18 +773,26 @@
 	 * werden, FALLS der uebergeben Envelope nicht schon gueltig sein sollte.<br>
 	 * Since 21. April 09: Before thecalculation starts, the aspect ratio is
 	 * corrected. This change implies, that setMapArea() will most of the time
-	 * not allow setting to a wrong aspectRatio.
+	 * not allow setting to a wrong aspectRatio.<br/>
+	 * This method also checks, that the extend of the mapArea is greater 0! (A
+	 * BBOX of 0 width and height can result froma point layer with just one
+	 * point and will later crash the WIndowsToMapTransformation)
 	 * 
 	 * @author <a href="mailto:skpublic at wikisquare.de">Stefan Alfons Tzeggai</a>
 	 */
 	public ReferencedEnvelope bestAllowedMapArea(ReferencedEnvelope env) {
 
-		if (getWidth() == 0)
-			return env;
-
 		if (env == null)
 			return null;
 
+		env = JTSUtil.checkNotZeroArea(env);
+
+		if (getWidth() == 0) {
+			// If the Swing component is not rendered yet, we can't apply aspect
+			// ratio calculations and return unchanged envelope
+			return env;
+		}
+
 		Envelope newArea = null;
 
 		/**
@@ -1115,10 +1123,11 @@
 
 			SwingUtil.clearAround(graphics, painedArea, visibleArea,
 					getMapBackgroundColor());
-			
+
 			addGadgets(graphics, true);
 		} catch (Exception e) {
-			// Annica reported exceptions here, so i assed a try catch to see whats happening.
+			// Annica reported exceptions here, so i assed a try catch to see
+			// whats happening.
 			LOGGER.error("Exception while painting a scaled preview: ", e);
 		}
 
@@ -1280,7 +1289,7 @@
 					addDefaultMargin(layerBounds), true);
 
 		}
-		return maxExtend;
+		return JTSUtil.checkNotZeroArea(maxExtend);
 	}
 
 	public static final String SPECIAL_LINES_LAYER_ID = "SPECIAL_LINES_LAYER_ID";
@@ -1344,7 +1353,7 @@
 			}
 		}
 
-		return result;
+		return JTSUtil.checkNotZeroArea(result);
 
 	}
 
@@ -1801,11 +1810,16 @@
 		final ReferencedEnvelope refMapEnv = new ReferencedEnvelope(mapArea,
 				getMapContext().getCoordinateReferenceSystem());
 
+		// TODO Die
+
 		worldToScreen = RendererUtilities.worldToScreenTransform(refMapEnv,
 				getVisibleRect());
 
 		try {
-			screenToWorld = worldToScreen.createInverse();
+			if (worldToScreen != null)
+				screenToWorld = worldToScreen.createInverse();
+			else
+				screenToWorld = null;
 
 		} catch (final NoninvertibleTransformException ex) {
 			LOGGER

Added: trunk/src_junit/schmitzm/geotools/gui/XMapPaneTest.java
===================================================================
--- trunk/src_junit/schmitzm/geotools/gui/XMapPaneTest.java	2010-06-26 10:02:49 UTC (rev 912)
+++ trunk/src_junit/schmitzm/geotools/gui/XMapPaneTest.java	2010-06-26 10:55:49 UTC (rev 913)
@@ -0,0 +1,27 @@
+package schmitzm.geotools.gui;
+
+import static org.junit.Assert.*;
+
+import org.geotools.geometry.jts.ReferencedEnvelope;
+import org.junit.Test;
+
+import schmitzm.geotools.GTUtil;
+
+public class XMapPaneTest {
+
+	@Test
+	public void testBestAllowedMapArea() {
+		
+		XMapPane xMapPane = new XMapPane();
+		
+		ReferencedEnvelope zeroWidthBBOx = new ReferencedEnvelope(1,1,2,2,GTUtil.WGS84);
+		assertEquals(0., zeroWidthBBOx.getWidth(), 0.);
+		assertEquals(0., zeroWidthBBOx.getHeight(), 0.);
+		ReferencedEnvelope corrected = xMapPane.bestAllowedMapArea(zeroWidthBBOx);
+		
+		assertTrue(corrected.getWidth() > 0.);
+		assertTrue(corrected.getHeight() > 0.);
+		
+	}
+
+}


Property changes on: trunk/src_junit/schmitzm/geotools/gui/XMapPaneTest.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Id URL
Name: svn:eol-style
   + native



More information about the Schmitzm-commits mailing list