[Schmitzm-commits] r494 - in branches/1.0-gt2-2.6/src: gtmig/org/geotools/swing schmitzm/geotools

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Oct 23 16:16:18 CEST 2009


Author: mojays
Date: 2009-10-23 16:16:17 +0200 (Fri, 23 Oct 2009)
New Revision: 494

Modified:
   branches/1.0-gt2-2.6/src/gtmig/org/geotools/swing/JMapPane.java
   branches/1.0-gt2-2.6/src/schmitzm/geotools/GTUtil.java
   branches/1.0-gt2-2.6/src/schmitzm/geotools/JTSUtil.java
Log:
JMapPane#fixAspectRatio(.) moved to JTSUtil

Modified: branches/1.0-gt2-2.6/src/gtmig/org/geotools/swing/JMapPane.java
===================================================================
--- branches/1.0-gt2-2.6/src/gtmig/org/geotools/swing/JMapPane.java	2009-10-23 14:09:27 UTC (rev 493)
+++ branches/1.0-gt2-2.6/src/gtmig/org/geotools/swing/JMapPane.java	2009-10-23 14:16:17 UTC (rev 494)
@@ -324,7 +324,7 @@
 				reset = false; /* forget about the reset */
 				oldRect = r; /* store what the current size is */
 
-				mapArea = fixAspectRatio(r, mapArea, false);
+				mapArea = JTSUtil.fixAspectRatio(r, mapArea, false);
 			}
 		}
 
@@ -360,60 +360,6 @@
 		((Graphics2D) g).drawImage(baseImage, 0, 0, this);
 	}
 
-	/**
-	 * Returns an {@link Envelope} that has the same aspect ratio as the given rectangle
-	 * @param grow
-	 *            If <code>true</code>, than the area will be enlarged to match
-	 *            the aspect ratio. If <code>false</code>, it will only shrink.
-	 */
-	private Envelope fixAspectRatio(final Rectangle r, final Envelope mapArea,
-			boolean grow) {
-		
-		if (mapArea == null) {
-			LOGGER.warn("mapArea has been null in method fixAspectRatio, returning also NULL");
-			return null;
-		}
-		if ( r == null || r.width == 0 || r.height == 0 ) {
-          LOGGER.warn("Empty rectangle in method fixAspectRatio, returning an unmodified mapArea!");
-          return mapArea;
-		}
-
-		final double mapWidth = mapArea.getWidth(); /* get the extent of the map */
-		final double mapHeight = mapArea.getHeight();
-		final double scaleX = r.getWidth() / mapArea.getWidth(); /*
-																 * calculate the
-																 * new scale
-																 */
-
-		final double scaleY = r.getHeight() / mapArea.getHeight();
-		double scale = 1.0; // stupid compiler!
-
-		if ((grow && scaleX < scaleY) || (!grow && scaleX > scaleY)) {
-			scale = scaleX;
-		} else {
-			scale = scaleY;
-		}
-
-		/* calculate the difference in width and height of the new extent */
-		final double deltaX = /* Math.abs */((r.getWidth() / scale) - mapWidth);
-		final double deltaY = /* Math.abs */((r.getHeight() / scale) - mapHeight);
-
-		/*
-		 * System.out.println("delta x " + deltaX);
-		 * System.out.println("delta y " + deltaY);
-		 */
-
-		/* create the new extent */
-		final Coordinate ll = new Coordinate(
-				mapArea.getMinX() - (deltaX / 2.0), mapArea.getMinY()
-						- (deltaY / 2.0));
-		final Coordinate ur = new Coordinate(
-				mapArea.getMaxX() + (deltaX / 2.0), mapArea.getMaxY()
-						+ (deltaY / 2.0));
-
-		return new Envelope(ll, ur);
-	}
-
 	public void mouseClicked(final MouseEvent e) {
 		if (mapArea == null)
 			return;
@@ -922,7 +868,7 @@
 		 * Correct the aspect Ratio before we check the rest. Otherwise we might
 		 * easily fail.
 		 */
-		env = fixAspectRatio(this.getBounds(), env, false);
+		env = JTSUtil.fixAspectRatio(this.getBounds(), env, false);
 
 		final double scale = env.getWidth() / getWidth();
 		final double centerX = env.getMinX() + env.getWidth() / 2.;
@@ -982,7 +928,7 @@
 
 //					LOGGER.debug("and fix aspect ratio");
 
-					newArea = fixAspectRatio(this.getBounds(), newArea, false);
+					newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea, false);
 				}
 			}
 
@@ -1004,7 +950,7 @@
 
 //					LOGGER.debug("and fix aspect ratio");
 
-					newArea = fixAspectRatio(this.getBounds(), newArea, false);
+					newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea, false);
 				}
 			}
 
@@ -1028,7 +974,7 @@
 
 //					LOGGER.debug("and fix aspect ratio");
 
-					newArea = fixAspectRatio(this.getBounds(), newArea, false);
+					newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea, false);
 				}
 			}
 
@@ -1052,7 +998,7 @@
 
 //					LOGGER.debug("and fix aspect ratio");
 
-					newArea = fixAspectRatio(this.getBounds(), newArea, false);
+					newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea, false);
 				}
 			}
 

Modified: branches/1.0-gt2-2.6/src/schmitzm/geotools/GTUtil.java
===================================================================
--- branches/1.0-gt2-2.6/src/schmitzm/geotools/GTUtil.java	2009-10-23 14:09:27 UTC (rev 493)
+++ branches/1.0-gt2-2.6/src/schmitzm/geotools/GTUtil.java	2009-10-23 14:16:17 UTC (rev 494)
@@ -29,6 +29,7 @@
  ******************************************************************************/
 package schmitzm.geotools;
 
+import java.awt.Rectangle;
 import java.awt.geom.Rectangle2D;
 import java.text.DecimalFormat;
 import java.util.HashMap;
@@ -47,6 +48,8 @@
 import org.opengis.geometry.Envelope;
 import org.opengis.referencing.crs.CoordinateReferenceSystem;
 
+import com.vividsolutions.jts.geom.Coordinate;
+
 /**
  * Diese Klasse enthaelt allgemeine Funktionen fuer die Arbeit mit Geotools.
  * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a> (University of Bonn/Germany)
@@ -71,7 +74,7 @@
   /** Konstante fuer das CRS "WGS84" (erzeugt als "EPSG:4326")*/
   public static CoordinateReferenceSystem WGS84 = null;
 
-private static HashMap<Object, Object> defaultRendererHints;
+  private static HashMap<Object, Object> defaultRendererHints;
 
   // Initialisierung der CRS-Konstanten
   static {
@@ -185,7 +188,9 @@
     }
     return map;
   }
+  
 
+
   /**
    * Berechnet den Schnitt zweier {@link Envelope Envelopes}.
    * @param env1 erster Envelope

Modified: branches/1.0-gt2-2.6/src/schmitzm/geotools/JTSUtil.java
===================================================================
--- branches/1.0-gt2-2.6/src/schmitzm/geotools/JTSUtil.java	2009-10-23 14:09:27 UTC (rev 493)
+++ branches/1.0-gt2-2.6/src/schmitzm/geotools/JTSUtil.java	2009-10-23 14:16:17 UTC (rev 494)
@@ -29,6 +29,8 @@
  ******************************************************************************/
 package schmitzm.geotools;
 
+import java.awt.Rectangle;
+
 import org.apache.log4j.Logger;
 import org.geotools.geometry.jts.JTS;
 import org.geotools.referencing.CRS;
@@ -124,4 +126,65 @@
     }
     return destCoord;
   }
+  
+  /**
+   * Returns an {@link Envelope} that has the same aspect ratio as the given rectangle
+   * @param r
+   *             defines the aspect ratio the map area is fixed with (e.g. a
+   *             gui components size)
+   * @param mapArea
+   *             the map area to apply the aspect ratio of "r" to
+   * @param grow
+   *            If <code>true</code>, than the area will be enlarged to match
+   *            the aspect ratio. If <code>false</code>, it will only shrink.
+   */
+  public static Envelope fixAspectRatio(final Rectangle r, final Envelope mapArea,
+          boolean grow) {
+      // no map area to fix the aspect ratio for
+      if (mapArea == null) {
+          LOGGER.warn("mapArea has been null in method fixAspectRatio, returning also NULL");
+          return null;
+      }
+      // no component size to fix the aspect ratio with
+      if ( r == null || r.width == 0 || r.height == 0 ) {
+        LOGGER.warn("Empty rectangle in method fixAspectRatio, returning an unmodified mapArea!");
+        return mapArea;
+      }
+
+      final double mapWidth = mapArea.getWidth(); /* get the extent of the map */
+      final double mapHeight = mapArea.getHeight();
+      final double scaleX = r.getWidth() / mapArea.getWidth(); /*
+                                                               * calculate the
+                                                               * new scale
+                                                               */
+
+      final double scaleY = r.getHeight() / mapArea.getHeight();
+      double scale = 1.0; // stupid compiler!
+
+      if ((grow && scaleX < scaleY) || (!grow && scaleX > scaleY)) {
+          scale = scaleX;
+      } else {
+          scale = scaleY;
+      }
+
+      /* calculate the difference in width and height of the new extent */
+      final double deltaX = /* Math.abs */((r.getWidth() / scale) - mapWidth);
+      final double deltaY = /* Math.abs */((r.getHeight() / scale) - mapHeight);
+
+      /*
+       * System.out.println("delta x " + deltaX);
+       * System.out.println("delta y " + deltaY);
+       */
+
+      /* create the new extent */
+      final Coordinate ll = new Coordinate(
+              mapArea.getMinX() - (deltaX / 2.0), mapArea.getMinY()
+                      - (deltaY / 2.0));
+      final Coordinate ur = new Coordinate(
+              mapArea.getMaxX() + (deltaX / 2.0), mapArea.getMaxY()
+                      + (deltaY / 2.0));
+
+      return new Envelope(ll, ur);
+  }
+
 }



More information about the Schmitzm-commits mailing list