[Schmitzm-commits] r451 - branches/1.0-gt2-2.6/src/schmitzm/geotools/gui

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Oct 9 20:45:59 CEST 2009


Author: alfonx
Date: 2009-10-09 20:45:57 +0200 (Fri, 09 Oct 2009)
New Revision: 451

Modified:
   branches/1.0-gt2-2.6/src/schmitzm/geotools/gui/JMapPane.java
Log:
* Made zoomToLayer( MapLayer layer ) more robust against layers without CRS information.

Modified: branches/1.0-gt2-2.6/src/schmitzm/geotools/gui/JMapPane.java
===================================================================
--- branches/1.0-gt2-2.6/src/schmitzm/geotools/gui/JMapPane.java	2009-10-08 21:51:48 UTC (rev 450)
+++ branches/1.0-gt2-2.6/src/schmitzm/geotools/gui/JMapPane.java	2009-10-09 18:45:57 UTC (rev 451)
@@ -79,6 +79,7 @@
 import org.geotools.renderer.GTRenderer;
 import org.geotools.renderer.lite.RendererUtilities;
 import org.geotools.renderer.lite.StreamingRenderer;
+import org.geotools.resources.CRSUtilities;
 import org.geotools.resources.coverage.FeatureUtilities;
 import org.geotools.resources.image.ImageUtilities;
 import org.geotools.styling.Style;
@@ -195,8 +196,7 @@
 			.getPredefinedCursor(Cursor.WAIT_CURSOR);
 
 	/** Logger for debug messages. */
-	protected static final Logger LOGGER = Logger.getLogger(JMapPane.class
-			.getName());
+	protected static final Logger LOGGER = Logger.getLogger(JMapPane.class);
 
 	/** @deprecated ersetzt durch {@link #ZOOM_IN} */
 	public static final int ZoomIn = gtmig.org.geotools.swing.JMapPane.ZoomIn;
@@ -297,7 +297,7 @@
 
 		protected void selectionPerformed(int ox, int oy, int px, int py) {
 			// MS, 20.05.2008:
-		    // In performSelectionEvent(.) wurde das Zoomen wieder
+			// In performSelectionEvent(.) wurde das Zoomen wieder
 			// reingenommen, damit bei Fenster-Auswahl auch gezoomt
 			// wird, wenn der Klick-Zoom (setState(.)) deaktiviert
 			// ist.
@@ -338,7 +338,8 @@
 	private Component mouseWaitCursorComponent;
 
 	/** Cursor wenn kein Mausbutton gedrueckt wird. default oder SwingUtil.PAN **/
-	private Cursor normalCursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
+	private Cursor normalCursor = Cursor
+			.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
 
 	/**
 	 * Manuell gesetzter statischer Cursor, unabhaengig von der aktuellen
@@ -351,9 +352,11 @@
 	/** An (transparent) image to paint over the map in the lower right corner **/
 	private BufferedImage mapImage = null;
 
-	/** Holds a flag for each layer, whether it is regarded or ignored on 
-	 *  {@link #SELECT_TOP}, {@link #SELECT_ALL} and {@link #SELECT_ONE_FROM_TOP}
-	 *  actions. */
+	/**
+	 * Holds a flag for each layer, whether it is regarded or ignored on
+	 * {@link #SELECT_TOP}, {@link #SELECT_ALL} and {@link #SELECT_ONE_FROM_TOP}
+	 * actions.
+	 */
 	final protected HashMap<MapLayer, Boolean> mapLayerSelectable = new HashMap<MapLayer, Boolean>();
 
 	/**
@@ -394,19 +397,23 @@
 	 * @param context
 	 *            Verwaltung der einzelnen Layer (z.B. {@link DefaultMapContext}
 	 *            ).
-	 * @param rendererHints A {@link Map} with hints for the renderer. May be <code>null</code>.
+	 * @param rendererHints
+	 *            A {@link Map} with hints for the renderer. May be
+	 *            <code>null</code>.
 	 */
 	public JMapPane(LayoutManager layout, Boolean isDoubleBuffered,
-			GTRenderer renderer, MapContext context, Map<Object, Object> rendererHints) {
+			GTRenderer renderer, MapContext context,
+			Map<Object, Object> rendererHints) {
 		super(layout != null ? layout : new BorderLayout(),
 				isDoubleBuffered != null ? isDoubleBuffered : true,
 				renderer != null ? renderer : GTUtil.createGTRenderer(),
 				// renderer != null ? renderer : new StreamingRenderer(),
-				context != null ? context : new DefaultMapContext(GeoImportUtil.getDefaultCRS()));
-//
-//		// Having problems with StreamingRendere!
-//		getRenderer().setContext(getContext());
-		
+				context != null ? context : new DefaultMapContext(GeoImportUtil
+						.getDefaultCRS()));
+		//
+		// // Having problems with StreamingRendere!
+		// getRenderer().setContext(getContext());
+
 		// Dieser Hint sorgt wohl dafuer, dass die Rasterpixel nicht
 		// interpoliert werden
 		// Ueber die Methode enableAntiAliasing(boolean) kann das
@@ -418,8 +425,7 @@
 		if (rendererHints != null) {
 			getRenderer().getRendererHints().putAll(rendererHints);
 		}
-		
-		
+
 		// hints.add( new RenderingHints(RenderingHints.KEY_ANTIALIASING,
 		// RenderingHints.VALUE_ANTIALIAS_OFF ) );
 		// hints.add( new RenderingHints(RenderingHints.KEY_INTERPOLATION,
@@ -504,69 +510,66 @@
 		// CRS wird immer vom ersten in die Karte eingefuegten Layer uebernommen
 		// Wenn noch keine MapArea gesetzt wurde, wird diese vom Layer
 		// uebernommen
-		getContext().addMapLayerListListener(
-				new MapLayerListAdapter() {
-					public void layerAdded(MapLayerListEvent e) {
-						if (getContext().getLayerCount() == 1) {
-							CoordinateReferenceSystem crs = null;
-							// CRS aus Layer ermitteln
-							try {
-								crs = e.getLayer().getFeatureSource()
-										.getSchema().getGeometryDescriptor()
-										.getCoordinateReferenceSystem();
-								// wenn noch keine MapArea gesetzt wurde, den
-								// Ausdehnungsbereich des ersten Layers
-								// verwenden, so dass die erste
-								// Karte komplett angezeigt wird
-								if (getMapArea() == null) {
-									Envelope newMapArea = new Envelope(e
-											.getLayer().getFeatureSource()
-											.getBounds());
-									setMapArea( getMaxExtend() );
-									// in layerAdded(.) der Oberklasse wird
-									// mapArea nochmal neu gesetzt, wenn das
-									// erste Layer
-									// eingefuegt wird
-									// >> hier nur die AreaOfInterest setzen
-									getContext().setAreaOfInterest(newMapArea,
-											crs);
-								}
-							} catch (Exception err) {
-								LOGGER
-										.warn("CRS could not be determined from map layer. "+GeoImportUtil.getDefaultCRS().getName()+"  used.");
-								// err.printStackTrace();
-								crs = GeoImportUtil.getDefaultCRS();
-							}
-							// CRS dem MapContext zuweisen
-							try {
-								getContext().setCoordinateReferenceSystem(crs);
-								// LOGGER.debug("MapContext-CRS set to: "+crs);
-							} catch (Exception err) {
-								LOGGER
-										.error(
-												"CRS could not be assigned to map context.",
-												err);
-							}
+		getContext().addMapLayerListListener(new MapLayerListAdapter() {
+			public void layerAdded(MapLayerListEvent e) {
+				if (getContext().getLayerCount() == 1) {
+					CoordinateReferenceSystem crs = null;
+					// CRS aus Layer ermitteln
+					try {
+						crs = e.getLayer().getFeatureSource().getSchema()
+								.getGeometryDescriptor()
+								.getCoordinateReferenceSystem();
+						// wenn noch keine MapArea gesetzt wurde, den
+						// Ausdehnungsbereich des ersten Layers
+						// verwenden, so dass die erste
+						// Karte komplett angezeigt wird
+						if (getMapArea() == null) {
+							Envelope newMapArea = new Envelope(e.getLayer()
+									.getFeatureSource().getBounds());
+							setMapArea(getMaxExtend());
+							// in layerAdded(.) der Oberklasse wird
+							// mapArea nochmal neu gesetzt, wenn das
+							// erste Layer
+							// eingefuegt wird
+							// >> hier nur die AreaOfInterest setzen
+							getContext().setAreaOfInterest(newMapArea, crs);
 						}
+					} catch (Exception err) {
+						LOGGER
+								.warn("CRS could not be determined from map layer. "
+										+ GeoImportUtil.getDefaultCRS()
+												.getName() + "  used.");
+						// err.printStackTrace();
+						crs = GeoImportUtil.getDefaultCRS();
 					}
-				});
+					// CRS dem MapContext zuweisen
+					try {
+						getContext().setCoordinateReferenceSystem(crs);
+						// LOGGER.debug("MapContext-CRS set to: "+crs);
+					} catch (Exception err) {
+						LOGGER.error(
+								"CRS could not be assigned to map context.",
+								err);
+					}
+				}
+			}
+		});
 
-		
-// Solved without just with the access methods
-//		// The default is to regard a layer on selection
-//		// actions
-//        getContext().addMapLayerListListener(
-//            new MapLayerListAdapter() {
-//              @Override
-//              public void layerAdded(MapLayerListEvent e) {
-//                setMapLayerSelectable(e.getLayer(),true);
-//              }
-//              @Override
-//              public void layerRemoved(MapLayerListEvent e) {
-//                // remove reference to layer
-//                mapLayerSelectable.remove(e.getLayer());
-//              }
-//        });
+		// Solved without just with the access methods
+		// // The default is to regard a layer on selection
+		// // actions
+		// getContext().addMapLayerListListener(
+		// new MapLayerListAdapter() {
+		// @Override
+		// public void layerAdded(MapLayerListEvent e) {
+		// setMapLayerSelectable(e.getLayer(),true);
+		// }
+		// @Override
+		// public void layerRemoved(MapLayerListEvent e) {
+		// // remove reference to layer
+		// mapLayerSelectable.remove(e.getLayer());
+		// }
+		// });
 	}
 
 	public JMapPane(LayoutManager layout, boolean isDoubleBuffered,
@@ -690,11 +693,21 @@
 		try {
 
 			// BB umrechnen von Layer-CRS in Map-CRS
-			final Envelope mapAreaNew = JTSUtil.transformEnvelope(layer
-					.getFeatureSource().getBounds(), layer.getFeatureSource()
-					.getSchema().getGeometryDescriptor()
-					.getCoordinateReferenceSystem(), getContext()
-					.getCoordinateReferenceSystem());
+			final CoordinateReferenceSystem targetCRS = getContext()
+					.getCoordinateReferenceSystem();
+			final CoordinateReferenceSystem sourceCRS = layer
+					.getFeatureSource().getSchema()
+					.getCoordinateReferenceSystem();
+
+			Envelope mapAreaNew;
+			if (targetCRS != null && sourceCRS != null
+					&& !CRS.equalsIgnoreMetadata(sourceCRS, targetCRS)) {
+				mapAreaNew = JTSUtil.transformEnvelope(layer.getFeatureSource()
+						.getBounds(), sourceCRS, targetCRS);
+			} else {
+				mapAreaNew = layer.getFeatureSource().getBounds();
+			}
+
 			// Kartenbereich um 10% vergroessern, damit z.B. auch ein
 			// Punkt-Layer,
 			// welches nur aus 2 Punnkten besteht, sichtbar ist (Punkte liegen
@@ -710,7 +723,7 @@
 						.warn("Couldn't transformEnvelope when zooming to the layer");
 			}
 		} catch (Exception err) {
-			LOGGER.warn("Zoom to layer did not terminate correctly", err);
+			LOGGER.error("Zoom to layer did not terminate correctly", err);
 		}
 	}
 
@@ -1146,6 +1159,7 @@
 		// nur Kopie der Transformation zurueckgeben!
 		if (transform != null)
 			return new AffineTransform(transform);
+
 		return null;
 	}
 
@@ -1374,8 +1388,8 @@
 	 * angepasst wird, muss diese Methode ueberschrieben werden.
 	 * <p>
 	 * Neu von SK: Ich habe in der Geotools
-	 * {@link org.gtmig.org.geotools.swing.JMapPane} die noetigen variablen protected
-	 * gemacht, um hier schon festzustellen, ob der aufruf von
+	 * {@link org.gtmig.org.geotools.swing.JMapPane} die noetigen variablen
+	 * protected gemacht, um hier schon festzustellen, ob der aufruf von
 	 * super.paintComponent das eigentliche aussehen der Karte veraendern wird.
 	 * Die Methode paintComponent wird naemlich bei jeder Bewegung der Maus
 	 * aufgerufen, und meistens wird super.paintComponent nur ein gebufferted
@@ -1556,12 +1570,13 @@
 			MapLayer layer = layerList[i];
 			if (!layer.isVisible())
 				continue;
-			if ( !isMapLayerSelectable(layer) ) {
-              LOGGER.debug("Ignoring layer " + layer.getTitle() + " because it is not declared as selectable!");
-			  continue;
+			if (!isMapLayerSelectable(layer)) {
+				LOGGER.debug("Ignoring layer " + layer.getTitle()
+						+ " because it is not declared as selectable!");
+				continue;
 			}
 
-//			LOGGER.debug("mode = " + mode);
+			// LOGGER.debug("mode = " + mode);
 
 			// Bei einem Raster-Layer, das die BB schneidet, abbrechen, wenn nur
 			// im obersten (sichtbaren) Layer gesucht wird.
@@ -2167,7 +2182,8 @@
 	 * wird das 2. Attribut zurueckgegeben, ansonsten die
 	 * {@link FeatureCollection}.
 	 * 
-	 * @see {@link FeatureUtilities#wrapGridCoverage(GridCoverage2D)} and {@link FeatureUtilities#wrapGridCoverageReader(AbstractGridCoverage2DReader, GeneralParameterValue[])} 
+	 * @see {@link FeatureUtilities#wrapGridCoverage(GridCoverage2D)} and
+	 *      {@link FeatureUtilities#wrapGridCoverageReader(AbstractGridCoverage2DReader, GeneralParameterValue[])}
 	 * 
 	 * @param layer
 	 *            ein Layer
@@ -2175,8 +2191,9 @@
 	 *         ein Fehler aufgetreten ist).
 	 */
 	public static Object getLayerSourceObject(MapLayer layer) {
-		return FeatureUtil.getWrappedGeoObject((FeatureSource<SimpleFeatureType, SimpleFeature>) layer
-				.getFeatureSource());
+		return FeatureUtil
+				.getWrappedGeoObject((FeatureSource<SimpleFeatureType, SimpleFeature>) layer
+						.getFeatureSource());
 
 	}
 
@@ -2670,12 +2687,15 @@
 	}
 
 	/**
-	 * Sets whether a layer is regarded or ignored on 
-     * {@link #SELECT_TOP}, {@link #SELECT_ALL} and {@link #SELECT_ONE_FROM_TOP}
-     * actions. 
-	 * @param layer a layer
-	 * @param selectable if {@code false} the layer is ignored during the
-	 *        upper mentioned actions. If <code>null</code>, the default (true) will be used. 
+	 * Sets whether a layer is regarded or ignored on {@link #SELECT_TOP},
+	 * {@link #SELECT_ALL} and {@link #SELECT_ONE_FROM_TOP} actions.
+	 * 
+	 * @param layer
+	 *            a layer
+	 * @param selectable
+	 *            if {@code false} the layer is ignored during the upper
+	 *            mentioned actions. If <code>null</code>, the default (true)
+	 *            will be used.
 	 */
 	public void setMapLayerSelectable(MapLayer layer, Boolean selectable) {
 		if (selectable == null)
@@ -2684,14 +2704,16 @@
 			mapLayerSelectable.put(layer, selectable);
 	}
 
-    /**
-     * Returns whether a layer is regarded or ignored on 
-     * {@link #SELECT_TOP}, {@link #SELECT_ALL} and {@link #SELECT_ONE_FROM_TOP}
-     * actions. Returns <code>true</code> if the selectability has not been defined.
-     * @param layer a layer
-     */
-    public boolean isMapLayerSelectable(MapLayer layer) {
-      Boolean selectable = mapLayerSelectable.get(layer);
-      return selectable == null ? true : selectable;
-    }
+	/**
+	 * Returns whether a layer is regarded or ignored on {@link #SELECT_TOP},
+	 * {@link #SELECT_ALL} and {@link #SELECT_ONE_FROM_TOP} actions. Returns
+	 * <code>true</code> if the selectability has not been defined.
+	 * 
+	 * @param layer
+	 *            a layer
+	 */
+	public boolean isMapLayerSelectable(MapLayer layer) {
+		Boolean selectable = mapLayerSelectable.get(layer);
+		return selectable == null ? true : selectable;
+	}
 }



More information about the Schmitzm-commits mailing list