[Schmitzm-commits] r540 - branches/1.0-gt2-2.6/src/skrueger/geotools

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Nov 20 20:34:52 CET 2009


Author: alfonx
Date: 2009-11-20 20:34:50 +0100 (Fri, 20 Nov 2009)
New Revision: 540

Modified:
   branches/1.0-gt2-2.6/src/skrueger/geotools/SelectXMapPaneMouseListener.java
   branches/1.0-gt2-2.6/src/skrueger/geotools/XMapPane.java
Log:
* Fixed the "jump left" problem in XMapPane

Modified: branches/1.0-gt2-2.6/src/skrueger/geotools/SelectXMapPaneMouseListener.java
===================================================================
--- branches/1.0-gt2-2.6/src/skrueger/geotools/SelectXMapPaneMouseListener.java	2009-11-20 19:10:05 UTC (rev 539)
+++ branches/1.0-gt2-2.6/src/skrueger/geotools/SelectXMapPaneMouseListener.java	2009-11-20 19:34:50 UTC (rev 540)
@@ -4,18 +4,30 @@
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseWheelEvent;
+import java.awt.geom.AffineTransform;
 import java.awt.geom.Point2D;
+import java.lang.reflect.Constructor;
 import java.util.Enumeration;
 import java.util.Hashtable;
 import java.util.Iterator;
+import java.util.List;
+import java.util.WeakHashMap;
 
 import org.apache.log4j.Logger;
 import org.geotools.data.memory.MemoryFeatureCollection;
 import org.geotools.feature.FeatureCollection;
+import org.geotools.feature.FeatureIterator;
+import org.geotools.geometry.DirectPosition2D;
+import org.geotools.geometry.jts.ReferencedEnvelope;
 import org.geotools.map.MapLayer;
+import org.geotools.swing.event.MapMouseEvent;
+import org.geotools.swing.tool.InfoToolHelper;
 import org.geotools.swing.tool.VectorLayerHelper;
+import org.geotools.swing.utils.MapLayerUtils;
+import org.opengis.feature.Feature;
 import org.opengis.feature.simple.SimpleFeature;
 import org.opengis.feature.simple.SimpleFeatureType;
+import org.opengis.feature.type.GeometryDescriptor;
 
 import schmitzm.geotools.gui.SelectableXMapPane;
 import schmitzm.geotools.map.event.FeatureSelectedEvent;
@@ -44,6 +56,139 @@
 	}
 
 	/**
+	 * Default distance fraction used with line and point features. When the
+	 * user clicks on the map, this tool searches for features within a
+	 * rectangle of width w centred on the mouse location, where w is the
+	 * average map side length multiplied by the value of this constant.
+	 */
+	public static final double DEFAULT_DISTANCE_FRACTION = 0.01d;
+	
+
+    private WeakHashMap<MapLayer, InfoToolHelper> helperTable= new WeakHashMap<MapLayer, InfoToolHelper>();
+//
+//	@Override
+//	public void mouseClicked2(MouseEvent event) {
+//		AffineTransform tr = xMapPane.getScreenToWorld();
+//		DirectPosition2D pos = new DirectPosition2D(event.getX(), event
+//				.getY());
+//		tr.transform(pos, pos);
+//		pos.setCoordinateReferenceSystem(xMapPane.getContext()
+//				.getCoordinateReferenceSystem());
+//		FeatureIterator<? extends Feature> iter = null;
+//
+//		for (MapLayer layer : xMapPane.getContext().getLayers()) {
+//			if (layer.isSelected()) {
+//				InfoToolHelper helper = null;
+//
+//				String layerName = layer.getTitle();
+//				if (layerName == null || layerName.length() == 0) {
+//					layerName = layer.getFeatureSource().getName()
+//							.getLocalPart();
+//				}
+//				if (layerName == null || layerName.length() == 0) {
+//					layerName = layer.getFeatureSource().getSchema().getName()
+//							.getLocalPart();
+//				}
+//
+//				helper = helperTable.get(layer);
+//				if (helper == null) {
+//					if (MapLayerUtils.isGridLayer(layer)) {
+//						String gridAttrName = MapLayerUtils
+//								.getGridAttributeName(layer);
+//						try {
+//							iter = layer.getFeatureSource().getFeatures()
+//									.features();
+//							Object rasterSource = iter.next().getProperty(
+//									gridAttrName).getValue();
+//							Class<?> clazz = Class
+//									.forName("org.geotools.swing.tool.GridLayerHelper");
+//							Constructor<?> ctor = clazz
+//									.getConstructor(Object.class);
+//							helper = (InfoToolHelper) ctor
+//									.newInstance(rasterSource);
+//							helperTable.put(layer, helper);
+//
+//						} catch (Exception ex) {
+//							throw new IllegalStateException(
+//									"Failed to create InfoToolHelper for grid layer",
+//									ex);
+//
+//						} finally {
+//							iter.close();
+//						}
+//
+//					} else {
+//						GeometryDescriptor geomDesc = layer.getFeatureSource()
+//								.getSchema().getGeometryDescriptor();
+//						String attrName = geomDesc.getLocalName();
+//						Class<?> geomClass = geomDesc.getType().getBinding();
+//
+//						try {
+//							Class<?> clazz = Class
+//									.forName("org.geotools.swing.tool.VectorLayerHelper");
+//							Constructor<?> ctor = clazz.getConstructor(
+//									MapLayer.class, String.class, Class.class);
+//							helper = (InfoToolHelper) ctor.newInstance(layer,
+//									attrName, geomClass);
+//							helperTable.put(layer, helper);
+//
+//						} catch (Exception ex) {
+//							throw new IllegalStateException(
+//									"Failed to create InfoToolHelper for vector layer",
+//									ex);
+//						}
+//					}
+//				}
+//
+//				Object info = null;
+//
+//				if (helper.getType() == InfoToolHelper.Type.VECTOR_HELPER) {
+//					ReferencedEnvelope mapEnv = xMapPane.getMapArea();
+//					double searchWidth = DEFAULT_DISTANCE_FRACTION
+//							* (mapEnv.getWidth() + mapEnv.getHeight()) / 2;
+//					try {
+//						info = helper.getInfo(pos, Double.valueOf(searchWidth));
+//					} catch (Exception ex) {
+//						throw new IllegalStateException(ex);
+//					}
+//
+//					if (info != null) {
+//						FeatureCollection selectedFeatures = (FeatureCollection) info;
+//						try {
+//							iter = selectedFeatures.features();
+//							while (iter.hasNext()) {
+//								System.out.println(layerName +": "+ iter.next());
+//							}
+//
+//						} catch (Exception ex) {
+//							throw new IllegalStateException(ex);
+//
+//						} finally {
+//							if (iter != null) {
+//								iter.close();
+//							}
+//						}
+//					}
+//
+//				} else {
+//					try {
+//						info = helper.getInfo(pos);
+//					} catch (Exception ex) {
+//						throw new IllegalStateException(ex);
+//					}
+//
+//					if (info != null) {
+//						List<Number> bandValues = (List<Number>) info;
+//						if (!bandValues.isEmpty()) {
+//							System.out.println(layerName +": "+ iter.next());
+//						}
+//					}
+//				}
+//			}
+//		}
+//	}
+
+	/**
 	 * Reagiert auf Linksklick mit der ueber {@link #setState(int)}eingestellten
 	 * Aktion und auf Rechtsklick mit Zoom-Out (sofern {@link #ZOOM_IN}-State
 	 * fuer Linksklick eingestellt). Alle anderen Klicks werden ignoriert.
@@ -56,7 +201,7 @@
 	public void mouseClicked(MouseEvent e) {
 		if (!isEnabled())
 			return;
-		
+
 		xMapPane.setCursor(XMapPane.WAIT_CURSOR);
 		//		
 		// int clickX = e.getX();
@@ -76,10 +221,9 @@
 		// Fenster-Koordinate in Geo-Koordinate umwandelt
 		Point2D geoCoord = xMapPane.getScreenToWorld().transform(e.getPoint(),
 				null);
-		
-		
-//		new VectorLayerHelper(layer, geomAttributeName, geomClass)
-		
+
+		// new VectorLayerHelper(layer, geomAttributeName, geomClass)
+
 		com.vividsolutions.jts.geom.Point mousePoint = new GeometryFactory()
 				.createPoint(new Coordinate(geoCoord.getX(), geoCoord.getY()));
 
@@ -91,8 +235,8 @@
 		final Double dist = Math.abs(geoCoordAtDistance.getX()
 				- geoCoord.getX()) / 2;
 
-//		final Envelope envelope = new Envelope(geoCoord.getX(),
-//				geoCoord.getY(), geoCoord.getX(), geoCoord.getY());
+		// final Envelope envelope = new Envelope(geoCoord.getX(),
+		// geoCoord.getY(), geoCoord.getX(), geoCoord.getY());
 
 		//
 		// This is a tweak. The NearPointFilterGenerator doesn't work
@@ -108,15 +252,15 @@
 		// new NearPointFilterGenerator(geoCoord, dist,
 		// getContext().getCoordinateReferenceSystem()),
 		// state, envelope);
-		
-//		envelope.expandBy(dist);
-//
+
+		// envelope.expandBy(dist);
+		//
 		Envelope envelope = new Envelope(geoCoord.getX() - dist, geoCoord
 				.getY()
 				- dist, geoCoord.getX() + dist, geoCoord.getY() + dist);
 
 		Hashtable<MapLayer, FeatureCollection<SimpleFeatureType, SimpleFeature>> result = xMapPane
-				.findVisibleFeatures(new BoundingBoxFilterGenerator(  envelope,
+				.findVisibleFeatures(new BoundingBoxFilterGenerator(envelope,
 						xMapPane.getContext().getCoordinateReferenceSystem()),
 						state, envelope);
 
@@ -179,7 +323,7 @@
 						layer, envelope, fcOne));
 			}
 		}
-		
+
 		xMapPane.updateCursor();
 
 	}

Modified: branches/1.0-gt2-2.6/src/skrueger/geotools/XMapPane.java
===================================================================
--- branches/1.0-gt2-2.6/src/skrueger/geotools/XMapPane.java	2009-11-20 19:10:05 UTC (rev 539)
+++ branches/1.0-gt2-2.6/src/skrueger/geotools/XMapPane.java	2009-11-20 19:34:50 UTC (rev 540)
@@ -794,7 +794,7 @@
 
 					// LOGGER.debug("and fix aspect ratio");
 
-					newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea,
+					newArea = JTSUtil.fixAspectRatio(getVisibleRect(), newArea,
 							false);
 				}
 			}
@@ -818,7 +818,7 @@
 
 					// LOGGER.debug("and fix aspect ratio");
 
-					newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea,
+					newArea = JTSUtil.fixAspectRatio(getVisibleRect(), newArea,
 							false);
 				}
 			}
@@ -844,7 +844,7 @@
 
 					// LOGGER.debug("and fix aspect ratio");
 
-					newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea,
+					newArea = JTSUtil.fixAspectRatio(getVisibleRect(), newArea,
 							false);
 				}
 			}
@@ -870,7 +870,7 @@
 
 					// LOGGER.debug("and fix aspect ratio");
 
-					newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea,
+					newArea = JTSUtil.fixAspectRatio(getVisibleRect(), newArea,
 							false);
 				}
 			}
@@ -979,7 +979,7 @@
 	 */
 	protected boolean drawScaledPreviewImage_Zoom(final Graphics2D graphics) {
 
-		if (1 == 1)return false;
+//		if (1 == 1)return false;
 		
 		if (quickPreviewHint == 0)
 			return false;
@@ -1055,8 +1055,8 @@
 		//
 		if (finalImage == null) {
 			// Rectangle curPaintArea = getVisibleRect();
-			finalImage = new BufferedImage(getBounds().width,
-					getBounds().height, IMAGETYPE);
+			finalImage = new BufferedImage(getVisibleRect().width,
+					getVisibleRect().height, IMAGETYPE);
 
 			requestStartRendering();
 		}
@@ -1073,8 +1073,8 @@
 	private BufferedImage getLocalImage() {
 
 		if (localImage == null) {
-			localImage = new BufferedImage(getBounds().width,
-					getBounds().height, IMAGETYPE_withAlpha);
+			localImage = new BufferedImage(getVisibleRect().width,
+					getVisibleRect().height, IMAGETYPE_withAlpha);
 		}
 
 		return localImage;
@@ -1155,7 +1155,7 @@
 			}
 
 			// Kartenbereich um 10% vergroessern
-			return JTSUtil.fixAspectRatio(this.getBounds(), JTSUtil
+			return JTSUtil.fixAspectRatio(getVisibleRect(), JTSUtil
 					.expandEnvelope(layerBounds, 0.1), true);
 		}
 		return maxExtend;
@@ -1519,7 +1519,8 @@
 	 */
 	public void performPan() {
 
-		final Rectangle winBounds = getBounds();
+		Rectangle winBounds = getVisibleRect();
+		
 		winBounds.translate(-imageOrigin.x, -imageOrigin.y);
 		final Envelope newMapArea = tranformWindowToGeo(winBounds.x,
 				winBounds.y, winBounds.x + winBounds.width, winBounds.y
@@ -1642,12 +1643,9 @@
 	private void resetTransforms() {
 		ReferencedEnvelope refMapEnv = new ReferencedEnvelope(mapArea,
 				getContext().getCoordinateReferenceSystem());
-		//
-		Rectangle paintArea = getBounds();
+
+		Rectangle paintArea = getVisibleRect(); // NOT USE GET BOUNDS!
 		
-		if (!getBounds().equals(getVisibleRect())) {
-			System.out.println("did we expect that?");
-		}
 		//        
 		// double xscale = paintArea.getWidth() / refEnv.getWidth();
 		// double yscale = paintArea.getHeight() / refEnv.getHeight();
@@ -2170,8 +2168,9 @@
 
 		// Paint a logo to the bottom right if available
 		if (mapImage != null) {
-			graphics.drawImage(mapImage, getBounds().width
-					- mapImage.getWidth() - 10, getBounds().height
+			Rectangle visibleRect = getVisibleRect();
+			graphics.drawImage(mapImage, visibleRect.width
+					- mapImage.getWidth() - 10, getVisibleRect().height
 					- mapImage.getHeight() - 10, null);
 		}
 



More information about the Schmitzm-commits mailing list