[Schmitzm-commits] r508 - in trunk/src: org/geotools/gui/swing schmitzm/geotools
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Tue Nov 3 15:38:30 CET 2009
Author: alfonx
Date: 2009-11-03 15:38:26 +0100 (Tue, 03 Nov 2009)
New Revision: 508
Modified:
trunk/src/org/geotools/gui/swing/JMapPane.java
trunk/src/schmitzm/geotools/JTSUtil.java
Log:
* Zooming out is now restricted to context.getLayerBounds. Backprted from 1.3
Modified: trunk/src/org/geotools/gui/swing/JMapPane.java
===================================================================
--- trunk/src/org/geotools/gui/swing/JMapPane.java 2009-11-02 16:37:03 UTC (rev 507)
+++ trunk/src/org/geotools/gui/swing/JMapPane.java 2009-11-03 14:38:26 UTC (rev 508)
@@ -87,6 +87,7 @@
import org.opengis.filter.FilterFactory2;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
+import schmitzm.geotools.JTSUtil;
import schmitzm.swing.SwingUtil;
import com.vividsolutions.jts.geom.Coordinate;
@@ -1276,19 +1277,24 @@
if (getWidth() == 0)
return env;
if (env == null)
- return env;
+ return null;
+ Envelope newArea = null;
+
/**
* Correct the aspect Ratio before we check the rest. Otherwise we might
- * easily fail.
+ * easily fail. We allow to grow here, because we don't check against
+ * the maxExtend
*/
- env = fixAspectRatio(this.getBounds(), env);
+ Rectangle curPaintArea = getVisibleRect();
+ env = JTSUtil.fixAspectRatio(curPaintArea, env, true);
+
final double scale = env.getWidth() / getWidth();
final double centerX = env.getMinX() + env.getWidth() / 2.;
final double centerY = env.getMinY() + env.getHeight() / 2.;
- double newWidth2;
- double newHeight2;
+ double newWidth2 = 0;
+ double newHeight2 = 0;
if (scale < getMaxZoomScale()) {
// ****************************************************************************
// Wir zoomen weiter rein als erlaubt => Anpassen des envelope
@@ -1305,15 +1311,136 @@
// ****************************************************************************
// Die mapArea / der Envelope ist ist gueltig! Keine Aenderungen
// ****************************************************************************
- return env;
+ newArea = env;
}
- final Coordinate ll = new Coordinate(centerX - newWidth2, centerY
- - newHeight2);
- final Coordinate ur = new Coordinate(centerX + newWidth2, centerY
- + newHeight2);
+ if (newArea == null) {
- return new Envelope(ll, ur);
+ final Coordinate ll = new Coordinate(centerX - newWidth2, centerY
+ - newHeight2);
+ final Coordinate ur = new Coordinate(centerX + newWidth2, centerY
+ + newHeight2);
+
+ newArea = new Envelope(ll, ur);
+ }
+
+ Envelope maxAllowedExtend;
+ try {
+ maxAllowedExtend = getContext().getLayerBounds();
+ } catch (IOException e) {
+ LOGGER.error("Retriving layer bounds failes"+e);
+ return newArea;
+ }
+ while (maxAllowedExtend != null && !maxAllowedExtend.contains(newArea)
+ && newArea != null && !newArea.isNull()
+ && !Double.isNaN(newArea.getMinX())
+ && !Double.isNaN(newArea.getMaxX())
+ && !Double.isNaN(newArea.getMinY())
+ && !Double.isNaN(newArea.getMaxY())) {
+ /*
+ * If a maxExtend is set, we have to honour that...
+ */
+
+ // Exceeds top? Move down and maybe cut
+ if (newArea.getMaxY() > maxAllowedExtend.getMaxY()) {
+ double divY = newArea.getMaxY() - maxAllowedExtend.getMaxY();
+ // LOGGER.debug("Moving area down by " + divY);
+
+ newArea = new Envelope(new Coordinate(newArea.getMinX(),
+ newArea.getMinY() - divY), new Coordinate(newArea
+ .getMaxX(), newArea.getMaxY() - divY));
+
+ if (newArea.getMinY() < maxAllowedExtend.getMinY()) {
+ // LOGGER.debug("Now it exeeds the bottom border.. cut!");
+ // And cut the bottom if it moved out of the area
+ newArea = new Envelope(new Coordinate(newArea.getMinX(),
+ maxAllowedExtend.getMinY()), new Coordinate(newArea
+ .getMaxX(), newArea.getMaxY()));
+
+ // LOGGER.debug("and fix aspect ratio");
+
+ newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea,
+ false);
+ }
+ }
+
+ // Exceeds bottom? Move up and maybe cut
+ if (newArea.getMinY() < maxAllowedExtend.getMinY()) {
+ double divY = newArea.getMinY() - maxAllowedExtend.getMinY();
+ // LOGGER.debug("Moving area up by " + divY);
+
+ newArea = new Envelope(new Coordinate(newArea.getMinX(),
+ newArea.getMinY() - divY), new Coordinate(newArea
+ .getMaxX(), newArea.getMaxY() - divY));
+
+ if (newArea.getMaxY() > maxAllowedExtend.getMaxY()) {
+ // LOGGER.debug("Now it exeeds the top border.. cut!");
+ // And cut the bottom if it moved out of the area
+ newArea = new Envelope(new Coordinate(newArea.getMinX(),
+ newArea.getMinY()), new Coordinate(newArea
+ .getMaxX(), maxAllowedExtend.getMaxY()));
+
+ // LOGGER.debug("and fix aspect ratio");
+
+ newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea,
+ false);
+ }
+ }
+
+ // Exceeds to the right? move and maybe cut
+ if (newArea.getMaxX() > maxAllowedExtend.getMaxX()) {
+
+ // Move left..
+ double divX = newArea.getMaxX() - maxAllowedExtend.getMaxX();
+ // LOGGER.debug("Moving area left by " + divX);
+
+ newArea = new Envelope(new Coordinate(newArea.getMinX() - divX,
+ newArea.getMinY()), new Coordinate(newArea.getMaxX()
+ - divX, newArea.getMaxY()));
+
+ if (newArea.getMinX() < maxAllowedExtend.getMinX()) {
+ // LOGGER.debug("Now it exeeds the left border.. cut!");
+ // And cut the left if it moved out of the area
+ newArea = new Envelope(new Coordinate(maxAllowedExtend
+ .getMinX(), newArea.getMinY()), new Coordinate(
+ newArea.getMaxX(), newArea.getMaxY()));
+
+ // LOGGER.debug("and fix aspect ratio");
+
+ newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea,
+ false);
+ }
+ }
+
+ // Exceeds to the left? move and maybe cut
+ if (newArea.getMinX() < maxAllowedExtend.getMinX()) {
+
+ // Move right..
+ double divX = newArea.getMinX() - maxAllowedExtend.getMinX();
+ // LOGGER.debug("Moving area right by " + divX);
+
+ newArea = new Envelope(new Coordinate(newArea.getMinX() - divX,
+ newArea.getMinY()), new Coordinate(newArea.getMaxX()
+ - divX, newArea.getMaxY()));
+
+ if (newArea.getMaxX() > maxAllowedExtend.getMaxX()) {
+ // LOGGER.debug("Now it exeeds the right border.. cut!");
+ // And cut the left if it moved out of the area
+ newArea = new Envelope(new Coordinate(newArea.getMinX(),
+ newArea.getMinY()), new Coordinate(maxAllowedExtend
+ .getMaxX(), newArea.getMaxY()));
+
+ // LOGGER.debug("and fix aspect ratio");
+
+ newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea,
+ false);
+ }
+ }
+
+ }
+
+ return newArea;
+
}
/**
Modified: trunk/src/schmitzm/geotools/JTSUtil.java
===================================================================
--- trunk/src/schmitzm/geotools/JTSUtil.java 2009-11-02 16:37:03 UTC (rev 507)
+++ trunk/src/schmitzm/geotools/JTSUtil.java 2009-11-03 14:38:26 UTC (rev 508)
@@ -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;
@@ -49,7 +51,71 @@
public class JTSUtil {
private static Logger LOGGER = Logger.getLogger( JTSUtil.class.getName() );
+
+
+
/**
+ * Returns an {@link Envelope} that has the same aspect ratio as the given rectangle
+ * @param rect
+ * 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 rect, 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 ( rect == null || rect.width == 0 || rect.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 = rect.getWidth() / mapArea.getWidth(); /*
+ * calculate the
+ * new scale
+ */
+
+ final double scaleY = rect.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 */((rect.getWidth() / scale) - mapWidth);
+ final double deltaY = /* Math.abs */((rect.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);
+ }
+
+
+ /**
* Created an (CRS-less) JTS-Envelope from an OpenGIS-Envelope.
* @param envelope an OpenGIS-Envelope
* @return an JTS-Envelope
More information about the Schmitzm-commits
mailing list