1 |
mojays |
2 |
/* |
2 |
|
|
* GeoTools - OpenSource mapping toolkit |
3 |
|
|
* http://geotools.org |
4 |
|
|
* (C) 2002-2006, GeoTools Project Managment Committee (PMC) |
5 |
|
|
* |
6 |
|
|
* This library is free software; you can redistribute it and/or |
7 |
|
|
* modify it under the terms of the GNU Lesser General Public |
8 |
|
|
* License as published by the Free Software Foundation; |
9 |
|
|
* version 2.1 of the License. |
10 |
|
|
* |
11 |
|
|
* This library is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
|
|
* Lesser General Public License for more details. |
15 |
|
|
*/ |
16 |
|
|
package org.geotools.gui.swing; |
17 |
|
|
/** |
18 |
|
|
* @author Ian Turton |
19 |
|
|
* |
20 |
|
|
*/ |
21 |
|
|
import java.awt.Color; |
22 |
|
|
import java.awt.Graphics; |
23 |
|
|
import java.awt.Graphics2D; |
24 |
|
|
import java.awt.LayoutManager; |
25 |
|
|
import java.awt.Rectangle; |
26 |
|
|
import java.awt.event.MouseEvent; |
27 |
|
|
import java.awt.event.MouseListener; |
28 |
|
|
import java.awt.image.BufferedImage; |
29 |
|
|
import java.beans.PropertyChangeEvent; |
30 |
|
|
import java.beans.PropertyChangeListener; |
31 |
|
|
import java.io.IOException; |
32 |
|
|
|
33 |
|
|
import javax.swing.JPanel; |
34 |
|
|
|
35 |
|
|
import org.geotools.feature.FeatureCollection; |
36 |
|
|
import org.geotools.filter.Filter; |
37 |
|
|
import org.geotools.filter.FilterFactory; |
38 |
|
|
import org.geotools.filter.FilterFactoryFinder; |
39 |
|
|
import org.geotools.filter.GeometryFilter; |
40 |
|
|
import org.geotools.filter.IllegalFilterException; |
41 |
|
|
import org.geotools.gui.swing.event.HighlightChangeListener; |
42 |
|
|
import org.geotools.gui.swing.event.HighlightChangedEvent; |
43 |
|
|
import org.geotools.map.DefaultMapContext; |
44 |
|
|
import org.geotools.map.MapContext; |
45 |
|
|
import org.geotools.map.MapLayer; |
46 |
|
|
import org.geotools.renderer.GTRenderer; |
47 |
|
|
import org.geotools.styling.Graphic; |
48 |
|
|
import org.geotools.styling.LineSymbolizer; |
49 |
|
|
import org.geotools.styling.Mark; |
50 |
|
|
import org.geotools.styling.PointSymbolizer; |
51 |
|
|
import org.geotools.styling.PolygonSymbolizer; |
52 |
|
|
import org.geotools.styling.Style; |
53 |
|
|
import org.geotools.styling.StyleBuilder; |
54 |
|
|
import org.geotools.styling.StyleFactory; |
55 |
|
|
import org.geotools.styling.StyleFactoryFinder; |
56 |
|
|
import org.opengis.referencing.crs.CoordinateReferenceSystem; |
57 |
|
|
|
58 |
|
|
import com.vividsolutions.jts.geom.Coordinate; |
59 |
|
|
import com.vividsolutions.jts.geom.Envelope; |
60 |
|
|
import com.vividsolutions.jts.geom.Geometry; |
61 |
|
|
import com.vividsolutions.jts.geom.GeometryFactory; |
62 |
|
|
|
63 |
|
|
/** |
64 |
|
|
* <b>Xulu:<br> |
65 |
|
|
* Code taken from gt-2.3.4 to suppress the {@code System.out}-messages |
66 |
|
|
* in {@link #setState(int)} and {@link #paintComponent(Graphics)}.</b><br><br> |
67 |
|
|
* A simple map container that is a JPanel with a map in. |
68 |
|
|
* provides simple pan,zoom, highlight and selection |
69 |
|
|
* The mappane stores an image of the map (drawn from the context) and |
70 |
|
|
* an image of the slected feature(s) to speed up |
71 |
|
|
* rendering of the highlights. Thus the whole map is only redrawn |
72 |
|
|
* when the bbox changes, selection is only redrawn when the |
73 |
|
|
* selected feature changes. |
74 |
|
|
* |
75 |
|
|
* If you intend to use this in production code you'll |
76 |
|
|
* need to make selection and highlighting work in the same way. |
77 |
|
|
* @author Ian Turton |
78 |
|
|
* |
79 |
|
|
*/ |
80 |
|
|
/** |
81 |
|
|
* @author ijt1 |
82 |
|
|
* |
83 |
|
|
*/ |
84 |
|
|
/** |
85 |
|
|
* @author ijt1 |
86 |
|
|
* |
87 |
|
|
*/ |
88 |
|
|
public class JMapPane extends JPanel implements MouseListener, |
89 |
|
|
HighlightChangeListener, PropertyChangeListener { |
90 |
|
|
/** |
91 |
|
|
* what renders the map |
92 |
|
|
*/ |
93 |
|
|
GTRenderer renderer; |
94 |
|
|
|
95 |
|
|
/** |
96 |
|
|
* the map context to render |
97 |
|
|
*/ |
98 |
|
|
MapContext context; |
99 |
|
|
|
100 |
|
|
private MapContext selectionContext; |
101 |
|
|
|
102 |
|
|
/** |
103 |
|
|
* the area of the map to draw |
104 |
|
|
*/ |
105 |
|
|
protected Envelope mapArea; |
106 |
|
|
|
107 |
|
|
/** |
108 |
|
|
* the size of the pane last time we drew |
109 |
|
|
*/ |
110 |
|
|
protected Rectangle oldRect = null; |
111 |
|
|
|
112 |
|
|
/** |
113 |
|
|
* the last map area drawn. |
114 |
|
|
*/ |
115 |
|
|
protected Envelope oldMapArea = null; |
116 |
|
|
|
117 |
|
|
/** |
118 |
|
|
* the base image of the map |
119 |
|
|
*/ |
120 |
|
|
private BufferedImage baseImage; |
121 |
|
|
/** |
122 |
|
|
* image of selection |
123 |
|
|
*/ |
124 |
|
|
private BufferedImage selectImage; |
125 |
|
|
/** |
126 |
|
|
* style for selected items |
127 |
|
|
*/ |
128 |
|
|
private Style selectionStyle; |
129 |
|
|
/** |
130 |
|
|
* layer that selection works on |
131 |
|
|
*/ |
132 |
|
|
private int selectionLayer = -1; |
133 |
|
|
/** |
134 |
|
|
* layer that highlight works on |
135 |
|
|
*/ |
136 |
|
|
private MapLayer highlightLayer; |
137 |
|
|
/** |
138 |
|
|
* the object which manages highlighting |
139 |
|
|
*/ |
140 |
|
|
private HighlightManager highlightManager; |
141 |
|
|
/** |
142 |
|
|
* is highlighting on or off |
143 |
|
|
*/ |
144 |
|
|
private boolean highlight = true; |
145 |
|
|
/** |
146 |
|
|
* a factory for filters |
147 |
|
|
*/ |
148 |
|
|
FilterFactory ff = FilterFactoryFinder.createFilterFactory(); |
149 |
|
|
/** |
150 |
|
|
* a factory for geometries |
151 |
|
|
*/ |
152 |
|
|
GeometryFactory gf = new GeometryFactory(); |
153 |
|
|
/** |
154 |
|
|
* the collections of features to be selected or highlighted |
155 |
|
|
*/ |
156 |
|
|
FeatureCollection selection, highlightFeature; |
157 |
|
|
|
158 |
|
|
public static final int Reset = 0; |
159 |
|
|
|
160 |
|
|
public static final int ZoomIn = 1; |
161 |
|
|
|
162 |
|
|
public static final int ZoomOut = 2; |
163 |
|
|
|
164 |
|
|
public static final int Pan = 3; |
165 |
|
|
|
166 |
|
|
public static final int Select = 4; |
167 |
|
|
|
168 |
|
|
private int state = ZoomIn; |
169 |
|
|
|
170 |
|
|
/** |
171 |
|
|
* how far to zoom in or out |
172 |
|
|
*/ |
173 |
|
|
private double zoomFactor = 2.0; |
174 |
|
|
|
175 |
|
|
Style lineHighlightStyle; |
176 |
|
|
|
177 |
|
|
Style pointHighlightStyle; |
178 |
|
|
|
179 |
|
|
Style polygonHighlightStyle; |
180 |
|
|
|
181 |
|
|
Style polygonSelectionStyle; |
182 |
|
|
|
183 |
|
|
Style pointSelectionStyle; |
184 |
|
|
|
185 |
|
|
Style lineSelectionStyle; |
186 |
|
|
|
187 |
|
|
public JMapPane() { |
188 |
|
|
this(null, true, null, null); |
189 |
|
|
} |
190 |
|
|
/** |
191 |
|
|
* create a basic JMapPane |
192 |
|
|
* @param render - how to draw the map |
193 |
|
|
* @param context - the map context to display |
194 |
|
|
*/ |
195 |
|
|
public JMapPane(GTRenderer render, MapContext context) { |
196 |
|
|
this(null, true, render, context); |
197 |
|
|
} |
198 |
|
|
/** |
199 |
|
|
* full constructor extending JPanel |
200 |
|
|
* @param layout - layout (probably shouldn't be set) |
201 |
|
|
* @param isDoubleBuffered - a Swing thing I don't really understand |
202 |
|
|
* @param render - what to draw the map with |
203 |
|
|
* @param context - what to draw |
204 |
|
|
*/ |
205 |
|
|
public JMapPane(LayoutManager layout, boolean isDoubleBuffered, |
206 |
|
|
GTRenderer render, MapContext context) { |
207 |
|
|
super(layout, isDoubleBuffered); |
208 |
|
|
setRenderer(render); |
209 |
|
|
|
210 |
|
|
setContext(context); |
211 |
|
|
|
212 |
|
|
this.addMouseListener(this); |
213 |
|
|
setHighlightManager(new HighlightManager(highlightLayer)); |
214 |
|
|
|
215 |
|
|
lineHighlightStyle = setupStyle(LINE, Color.red); |
216 |
|
|
|
217 |
|
|
pointHighlightStyle = setupStyle(POINT, Color.red); |
218 |
|
|
|
219 |
|
|
polygonHighlightStyle = setupStyle(POLYGON, Color.red); |
220 |
|
|
|
221 |
|
|
polygonSelectionStyle = setupStyle(POLYGON, Color.cyan); |
222 |
|
|
|
223 |
|
|
pointSelectionStyle = setupStyle(POINT, Color.cyan); |
224 |
|
|
|
225 |
|
|
lineSelectionStyle = setupStyle(LINE, Color.cyan); |
226 |
|
|
|
227 |
|
|
} |
228 |
|
|
/** |
229 |
|
|
* get the renderer |
230 |
|
|
*/ |
231 |
|
|
|
232 |
|
|
public GTRenderer getRenderer() { |
233 |
|
|
return renderer; |
234 |
|
|
} |
235 |
|
|
|
236 |
|
|
public void setRenderer(GTRenderer renderer) { |
237 |
|
|
this.renderer = renderer; |
238 |
|
|
|
239 |
|
|
if (this.context != null) { |
240 |
|
|
this.renderer.setContext(this.context); |
241 |
|
|
|
242 |
|
|
} |
243 |
|
|
} |
244 |
|
|
|
245 |
|
|
public MapContext getContext() { |
246 |
|
|
return context; |
247 |
|
|
} |
248 |
|
|
|
249 |
|
|
public void setContext(MapContext context) { |
250 |
|
|
|
251 |
|
|
this.context = context; |
252 |
|
|
|
253 |
|
|
if (renderer != null) { |
254 |
|
|
renderer.setContext(this.context); |
255 |
|
|
|
256 |
|
|
} |
257 |
|
|
} |
258 |
|
|
|
259 |
|
|
public final Envelope getMapArea() { |
260 |
|
|
return mapArea; |
261 |
|
|
} |
262 |
|
|
|
263 |
|
|
public void setMapArea(Envelope mapArea) { |
264 |
|
|
this.mapArea = mapArea; |
265 |
|
|
} |
266 |
|
|
|
267 |
|
|
public int getState() { |
268 |
|
|
return state; |
269 |
|
|
} |
270 |
|
|
|
271 |
|
|
public void setState(int state) { |
272 |
|
|
this.state = state; |
273 |
|
|
//-- LINED OUT FOR XULU -- |
274 |
|
|
// System.out.println("State: " + state); |
275 |
|
|
} |
276 |
|
|
|
277 |
|
|
public double getZoomFactor() { |
278 |
|
|
return zoomFactor; |
279 |
|
|
} |
280 |
|
|
|
281 |
|
|
public void setZoomFactor(double zoomFactor) { |
282 |
|
|
this.zoomFactor = zoomFactor; |
283 |
|
|
} |
284 |
|
|
|
285 |
|
|
public int getSelectionLayer() { |
286 |
|
|
return selectionLayer; |
287 |
|
|
} |
288 |
|
|
|
289 |
|
|
public void setSelectionLayer(int selectionLayer) { |
290 |
|
|
this.selectionLayer = selectionLayer; |
291 |
|
|
} |
292 |
|
|
|
293 |
|
|
public boolean isHighlight() { |
294 |
|
|
return highlight; |
295 |
|
|
} |
296 |
|
|
|
297 |
|
|
public void setHighlight(boolean highlight) { |
298 |
|
|
this.highlight = highlight; |
299 |
|
|
} |
300 |
|
|
|
301 |
|
|
public MapLayer getHighlightLayer() { |
302 |
|
|
return highlightLayer; |
303 |
|
|
} |
304 |
|
|
|
305 |
|
|
public void setHighlightLayer(MapLayer highlightLayer) { |
306 |
|
|
this.highlightLayer = highlightLayer; |
307 |
|
|
if (highlightManager != null) { |
308 |
|
|
highlightManager.setHighlightLayer(highlightLayer); |
309 |
|
|
} |
310 |
|
|
} |
311 |
|
|
|
312 |
|
|
public HighlightManager getHighlightManager() { |
313 |
|
|
return highlightManager; |
314 |
|
|
} |
315 |
|
|
|
316 |
|
|
public void setHighlightManager(HighlightManager highlightManager) { |
317 |
|
|
this.highlightManager = highlightManager; |
318 |
|
|
this.highlightManager.addHighlightChangeListener(this); |
319 |
|
|
this.addMouseMotionListener(this.highlightManager); |
320 |
|
|
} |
321 |
|
|
|
322 |
|
|
public Style getLineHighlightStyle() { |
323 |
|
|
return lineHighlightStyle; |
324 |
|
|
} |
325 |
|
|
|
326 |
|
|
public void setLineHighlightStyle(Style lineHighlightStyle) { |
327 |
|
|
this.lineHighlightStyle = lineHighlightStyle; |
328 |
|
|
} |
329 |
|
|
|
330 |
|
|
public Style getLineSelectionStyle() { |
331 |
|
|
return lineSelectionStyle; |
332 |
|
|
} |
333 |
|
|
|
334 |
|
|
public void setLineSelectionStyle(Style lineSelectionStyle) { |
335 |
|
|
this.lineSelectionStyle = lineSelectionStyle; |
336 |
|
|
} |
337 |
|
|
|
338 |
|
|
public Style getPointHighlightStyle() { |
339 |
|
|
return pointHighlightStyle; |
340 |
|
|
} |
341 |
|
|
|
342 |
|
|
public void setPointHighlightStyle(Style pointHighlightStyle) { |
343 |
|
|
this.pointHighlightStyle = pointHighlightStyle; |
344 |
|
|
} |
345 |
|
|
|
346 |
|
|
public Style getPointSelectionStyle() { |
347 |
|
|
return pointSelectionStyle; |
348 |
|
|
} |
349 |
|
|
|
350 |
|
|
public void setPointSelectionStyle(Style pointSelectionStyle) { |
351 |
|
|
this.pointSelectionStyle = pointSelectionStyle; |
352 |
|
|
} |
353 |
|
|
|
354 |
|
|
public Style getPolygonHighlightStyle() { |
355 |
|
|
return polygonHighlightStyle; |
356 |
|
|
} |
357 |
|
|
|
358 |
|
|
public void setPolygonHighlightStyle(Style polygonHighlightStyle) { |
359 |
|
|
this.polygonHighlightStyle = polygonHighlightStyle; |
360 |
|
|
} |
361 |
|
|
|
362 |
|
|
public Style getPolygonSelectionStyle() { |
363 |
|
|
return polygonSelectionStyle; |
364 |
|
|
} |
365 |
|
|
|
366 |
|
|
public void setPolygonSelectionStyle(Style polygonSelectionStyle) { |
367 |
|
|
this.polygonSelectionStyle = polygonSelectionStyle; |
368 |
|
|
} |
369 |
|
|
|
370 |
|
|
protected boolean reset = false; |
371 |
|
|
|
372 |
|
|
private Double maxZoomScale = Double.MIN_VALUE; |
373 |
|
|
private Double minZoomScale = Double.MAX_VALUE; |
374 |
|
|
|
375 |
|
|
/** |
376 |
|
|
* SK 27.9.2007: Auch hier final bei den Variablen einfegfuegt, da die Routine so oft aufgerufen wird. |
377 |
|
|
*/ |
378 |
|
|
protected void paintComponent(final Graphics g) { |
379 |
|
|
boolean changed = false; |
380 |
|
|
super.paintComponent(g); |
381 |
|
|
if (renderer == null || mapArea == null) { |
382 |
|
|
return; |
383 |
|
|
} |
384 |
|
|
final Rectangle r = getBounds(); |
385 |
|
|
final Rectangle dr = new Rectangle(r.width, r.height); |
386 |
|
|
if (!r.equals(oldRect) || reset) { |
387 |
|
|
/*either the viewer size has changed or we've done a reset*/ |
388 |
|
|
changed = true; /* note we need to redraw */ |
389 |
|
|
reset = false; /* forget about the reset */ |
390 |
|
|
oldRect = r; /* store what the current size is */ |
391 |
|
|
final double mapWidth = mapArea.getWidth(); /* get the extent of the map*/ |
392 |
|
|
final double mapHeight = mapArea.getHeight(); |
393 |
|
|
final double scaleX = r.getWidth() / mapArea.getWidth(); /* calculate the new scale*/ |
394 |
|
|
final double scaleY = r.getHeight() / mapArea.getHeight(); |
395 |
|
|
double scale = 1.0; // stupid compiler! |
396 |
|
|
if (scaleX < scaleY) {/*pick the smaller scale */ |
397 |
|
|
scale = scaleX; |
398 |
|
|
} else { |
399 |
|
|
scale = scaleY; |
400 |
|
|
} |
401 |
|
|
/* calculate the difference in width and height of the new extent*/ |
402 |
|
|
final double deltaX = /*Math.abs*/((r.getWidth() / scale) - mapWidth); |
403 |
|
|
final double deltaY = /*Math.abs*/((r.getHeight() / scale) - mapHeight); |
404 |
|
|
// -- LINED OUT FOR XULU -- |
405 |
|
|
// System.out.println("delta x "+deltaX); System.out.println("delta y "+deltaY); |
406 |
|
|
/* create the new extent */ |
407 |
|
|
final Coordinate ll = new Coordinate(mapArea.getMinX() - (deltaX / 2.0), |
408 |
|
|
mapArea.getMinY() - (deltaY / 2.0)); |
409 |
|
|
final Coordinate ur = new Coordinate(mapArea.getMaxX() + (deltaX / 2.0), |
410 |
|
|
mapArea.getMaxY() + (deltaY / 2.0)); |
411 |
|
|
mapArea = new Envelope(ll, ur); |
412 |
|
|
} |
413 |
|
|
if (!mapArea.equals(oldMapArea)) {/* did the map extent change?*/ |
414 |
|
|
changed = true; |
415 |
|
|
oldMapArea = mapArea; |
416 |
|
|
} |
417 |
|
|
if (changed) {/* if the map changed then redraw*/ |
418 |
|
|
|
419 |
|
|
baseImage = new BufferedImage(dr.width, dr.height, |
420 |
|
|
BufferedImage.TYPE_INT_ARGB); |
421 |
|
|
final Graphics2D ig = baseImage.createGraphics(); |
422 |
|
|
/* System.out.println("rendering"); */ |
423 |
|
|
renderer.setContext(context); |
424 |
|
|
renderer.paint((Graphics2D) ig, dr, mapArea); |
425 |
|
|
} |
426 |
|
|
((Graphics2D) g).drawImage(baseImage, 0, 0, this); |
427 |
|
|
if (selection != null && selection.size() > 0) { |
428 |
|
|
// paint selection |
429 |
|
|
/* |
430 |
|
|
* String type = selection.getDefaultGeometry().getGeometryType(); |
431 |
|
|
* System.out.println(type); if(type==null) type="polygon"; |
432 |
|
|
*/ |
433 |
|
|
final String type = "polygon"; |
434 |
|
|
if (type.equalsIgnoreCase("polygon")) { |
435 |
|
|
|
436 |
|
|
selectionStyle = polygonSelectionStyle; |
437 |
|
|
} else if (type.equalsIgnoreCase("point")) { |
438 |
|
|
|
439 |
|
|
selectionStyle = pointSelectionStyle; |
440 |
|
|
} else if (type.equalsIgnoreCase("line")) { |
441 |
|
|
|
442 |
|
|
selectionStyle = lineSelectionStyle; |
443 |
|
|
} |
444 |
|
|
|
445 |
|
|
selectionContext = new DefaultMapContext(); |
446 |
|
|
|
447 |
|
|
selectionContext.addLayer(selection, selectionStyle); |
448 |
|
|
renderer.setContext(selectionContext); |
449 |
|
|
|
450 |
|
|
selectImage = new BufferedImage(dr.width, dr.height, |
451 |
|
|
BufferedImage.TYPE_INT_ARGB); |
452 |
|
|
final Graphics2D ig = selectImage.createGraphics(); |
453 |
|
|
/* System.out.println("rendering selection"); */ |
454 |
|
|
renderer.paint((Graphics2D) ig, dr, mapArea); |
455 |
|
|
|
456 |
|
|
((Graphics2D) g).drawImage(selectImage, 0, 0, this); |
457 |
|
|
} |
458 |
|
|
if (highlight && highlightFeature != null |
459 |
|
|
&& highlightFeature.size() > 0) { |
460 |
|
|
/* |
461 |
|
|
* String type = selection.getDefaultGeometry().getGeometryType(); |
462 |
|
|
* System.out.println(type); if(type==null) type="polygon"; |
463 |
|
|
*/ |
464 |
|
|
final String type = "polygon"; |
465 |
|
|
Style highlightStyle = null; |
466 |
|
|
if (type.equalsIgnoreCase("polygon")) { |
467 |
|
|
|
468 |
|
|
highlightStyle = polygonHighlightStyle; |
469 |
|
|
} else if (type.equalsIgnoreCase("point")) { |
470 |
|
|
|
471 |
|
|
highlightStyle = pointHighlightStyle; |
472 |
|
|
} else if (type.equalsIgnoreCase("line")) { |
473 |
|
|
|
474 |
|
|
highlightStyle = lineHighlightStyle; |
475 |
|
|
} |
476 |
|
|
|
477 |
|
|
final MapContext highlightContext = new DefaultMapContext(); |
478 |
|
|
|
479 |
|
|
highlightContext.addLayer(highlightFeature, highlightStyle); |
480 |
|
|
renderer.setContext(highlightContext); |
481 |
|
|
|
482 |
|
|
/* System.out.println("rendering highlight"); */ |
483 |
|
|
renderer.paint((Graphics2D) g, dr, mapArea); |
484 |
|
|
|
485 |
|
|
} |
486 |
|
|
} |
487 |
|
|
|
488 |
|
|
public FeatureCollection doSelection(double x, double y, int layer) { |
489 |
|
|
GeometryFilter f = null; |
490 |
|
|
FeatureCollection select = null; |
491 |
|
|
Geometry geometry = gf.createPoint(new Coordinate(x, y)); |
492 |
|
|
try { |
493 |
|
|
f = ff.createGeometryFilter(GeometryFilter.GEOMETRY_CONTAINS); |
494 |
|
|
f.addRightGeometry(ff.createLiteralExpression(geometry)); |
495 |
|
|
} catch (IllegalFilterException e) { |
496 |
|
|
// TODO Auto-generated catch block |
497 |
|
|
e.printStackTrace(); |
498 |
|
|
} |
499 |
|
|
|
500 |
|
|
if (layer == -1) { |
501 |
|
|
for (int i = 0; i < context.getLayers().length; i++) { |
502 |
|
|
FeatureCollection fx = findFeature(f, i); |
503 |
|
|
if (select != null) { |
504 |
|
|
select.addAll(fx); |
505 |
|
|
} else { |
506 |
|
|
select = fx; |
507 |
|
|
} |
508 |
|
|
} |
509 |
|
|
} else { |
510 |
|
|
select = findFeature(f, layer); |
511 |
|
|
} |
512 |
|
|
return select; |
513 |
|
|
} |
514 |
|
|
|
515 |
|
|
/** |
516 |
|
|
* @param f - |
517 |
|
|
* a partial geometry filter. The geom name will be added |
518 |
|
|
* @param i - |
519 |
|
|
* the index of the layer to search |
520 |
|
|
* @throws IndexOutOfBoundsException |
521 |
|
|
*/ |
522 |
|
|
private FeatureCollection findFeature(GeometryFilter f, int i) |
523 |
|
|
throws IndexOutOfBoundsException { |
524 |
|
|
FeatureCollection fcol = null; |
525 |
|
|
if (context != null && i > context.getLayers().length) { |
526 |
|
|
return fcol; |
527 |
|
|
} |
528 |
|
|
MapLayer layer = context.getLayer(i); |
529 |
|
|
|
530 |
|
|
try { |
531 |
|
|
String name = layer.getFeatureSource().getSchema() |
532 |
|
|
.getDefaultGeometry().getName(); |
533 |
|
|
if (name == "") |
534 |
|
|
name = "the_geom"; |
535 |
|
|
f.addLeftGeometry(ff.createAttributeExpression(name)); |
536 |
|
|
// System.out.println("looking with " + f); |
537 |
|
|
FeatureCollection fc = layer.getFeatureSource().getFeatures(f); |
538 |
|
|
if (fc.size() > 0) { |
539 |
|
|
// selectionStyle.getFeatureTypeStyles()[0].getRules()[0].setFilter(f); |
540 |
|
|
selectionLayer = i; |
541 |
|
|
} |
542 |
|
|
if (fcol == null) { |
543 |
|
|
fcol = fc; |
544 |
|
|
// here we should set the defaultgeom type |
545 |
|
|
} else { |
546 |
|
|
fcol.addAll(fc); |
547 |
|
|
} |
548 |
|
|
/* |
549 |
|
|
* GeometryAttributeType gat = |
550 |
|
|
* layer.getFeatureSource().getSchema().getDefaultGeometry(); |
551 |
|
|
* fcol.setDefaultGeometry((Geometry)gat.createDefaultValue()); |
552 |
|
|
*/ |
553 |
|
|
|
554 |
|
|
/* |
555 |
|
|
* Iterator fi = fc.iterator(); while (fi.hasNext()) { Feature feat = |
556 |
|
|
* (Feature) fi.next(); System.out.println("selected " + |
557 |
|
|
* feat.getAttribute("STATE_NAME")); } |
558 |
|
|
*/ |
559 |
|
|
} catch (IOException e) { |
560 |
|
|
// TODO Auto-generated catch block |
561 |
|
|
e.printStackTrace(); |
562 |
|
|
} catch (IllegalFilterException e) { |
563 |
|
|
// TODO Auto-generated catch block |
564 |
|
|
e.printStackTrace(); |
565 |
|
|
}/* |
566 |
|
|
* catch (IllegalAttributeException e) { // TODO Auto-generated |
567 |
|
|
* catch block System.err.println(e.getMessage()); // |
568 |
|
|
* e.printStackTrace(); } |
569 |
|
|
*/ |
570 |
|
|
return fcol; |
571 |
|
|
} |
572 |
|
|
|
573 |
|
|
/* |
574 |
|
|
* (non-Javadoc) |
575 |
|
|
* @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent) |
576 |
|
|
*/ |
577 |
|
|
public void mouseClicked(MouseEvent e) { |
578 |
|
|
// System.out.println("before area "+mapArea+"\nw:"+mapArea.getWidth()+" |
579 |
|
|
// h:"+mapArea.getHeight()); |
580 |
|
|
Rectangle bounds = this.getBounds(); |
581 |
|
|
double x = (double) (e.getX()); |
582 |
|
|
double y = (double) (e.getY()); |
583 |
|
|
double width = mapArea.getWidth(); |
584 |
|
|
double height = mapArea.getHeight(); |
585 |
|
|
|
586 |
|
|
double width2 = width / 2.0; |
587 |
|
|
double height2 = height / 2.0; |
588 |
|
|
|
589 |
|
|
double mapX = (x * width / (double) bounds.width) + mapArea.getMinX(); |
590 |
|
|
double mapY = ((bounds.getHeight() - y) * height / (double) bounds.height) |
591 |
|
|
+ mapArea.getMinY(); |
592 |
|
|
/* |
593 |
|
|
* System.out.println(""+x+"->"+mapX); |
594 |
|
|
* System.out.println(""+y+"->"+mapY); |
595 |
|
|
*/ |
596 |
|
|
/* |
597 |
|
|
* Coordinate ll = new Coordinate(mapArea.getMinX(), mapArea.getMinY()); |
598 |
|
|
* Coordinate ur = new Coordinate(mapArea.getMaxX(), mapArea.getMaxY()); |
599 |
|
|
*/ |
600 |
|
|
|
601 |
|
|
double zlevel = 1.0; |
602 |
|
|
switch (state) { |
603 |
|
|
case Pan: |
604 |
|
|
zlevel = 1.0; |
605 |
|
|
|
606 |
|
|
// Changed by SK: return here.. a mouselistener is amanaging the PANNING |
607 |
|
|
return; |
608 |
|
|
case ZoomIn: |
609 |
|
|
zlevel = zoomFactor; |
610 |
|
|
break; |
611 |
|
|
case ZoomOut: |
612 |
|
|
zlevel = 1.0 / zoomFactor; |
613 |
|
|
break; |
614 |
|
|
case Select: |
615 |
|
|
selection = doSelection(mapX, mapY, selectionLayer); |
616 |
|
|
repaint(); |
617 |
|
|
return; |
618 |
|
|
default: |
619 |
|
|
return; |
620 |
|
|
} |
621 |
|
|
|
622 |
|
|
//**************************************************************************** |
623 |
|
|
// Changed by SK: |
624 |
|
|
// performing the zoom and/or pan by recalculating the mapArea |
625 |
|
|
// important and new here: don't zoom in/out more that the min/max scale! |
626 |
|
|
// n.b.: zoom is not only performed here, but also and with the mouse wheel and setMapArea |
627 |
|
|
//**************************************************************************** |
628 |
|
|
|
629 |
|
|
Coordinate ll = new Coordinate(mapX - (width2 / zlevel), mapY |
630 |
|
|
- (height2 / zlevel)); |
631 |
|
|
Coordinate ur = new Coordinate(mapX + (width2 / zlevel), mapY |
632 |
|
|
+ (height2 / zlevel)); |
633 |
|
|
|
634 |
|
|
final Envelope newMapArea = new Envelope(ll, ur); |
635 |
|
|
|
636 |
|
|
// Hier passiert die Ueberpruefung und ggf Anpassung der Scale |
637 |
|
|
mapArea = bestAllowedMapArea( newMapArea ); |
638 |
|
|
|
639 |
|
|
// System.out.println("after area "+mapArea+"\nw:"+mapArea.getWidth()+" |
640 |
|
|
// h:"+mapArea.getHeight()); |
641 |
|
|
repaint(); |
642 |
|
|
} |
643 |
|
|
|
644 |
|
|
|
645 |
|
|
/** |
646 |
|
|
* Korrigiert den {@link Envelope} aka mapArea auf die beste erlaubte Flaeche damit |
647 |
|
|
* die Massstabsbeschaenkungen noch eingehalten werden, FALLS der uebergeben Envelope |
648 |
|
|
* nicht schon gueltig sein sollte. |
649 |
|
|
* |
650 |
|
|
* @author <a href="mailto:[email protected]">Stefan Alfons Krüger</a> |
651 |
|
|
*/ |
652 |
|
|
public Envelope bestAllowedMapArea(Envelope env) { |
653 |
|
|
if (getWidth() == 0) return env; |
654 |
|
|
|
655 |
|
|
if (env == null) return env; |
656 |
|
|
|
657 |
|
|
double scale = env.getWidth() / getWidth(); |
658 |
|
|
|
659 |
|
|
double centerX = env.getMinX() + env.getWidth() / 2.; |
660 |
|
|
double centerY = env.getMinY() + env.getHeight() / 2.; |
661 |
|
|
|
662 |
|
|
double newWidth2; |
663 |
|
|
double newHeight2; |
664 |
|
|
if (scale < getMaxZoomScale()){ |
665 |
|
|
|
666 |
|
|
//**************************************************************************** |
667 |
|
|
// Wir zoomen weiter rein als erlaubt => Anpassen des envelope |
668 |
|
|
//**************************************************************************** |
669 |
|
|
newWidth2 = getMaxZoomScale() * getWidth() / 2.; |
670 |
|
|
newHeight2 = getMaxZoomScale() * getHeight() / 2.; |
671 |
|
|
} else |
672 |
|
|
if (scale > getMinZoomScale()) |
673 |
|
|
{ |
674 |
|
|
|
675 |
|
|
//**************************************************************************** |
676 |
|
|
// Wir zoomen weiter raus als erlaubt => Anpassen des envelope |
677 |
|
|
//**************************************************************************** |
678 |
|
|
newWidth2 = getMinZoomScale() * getWidth() / 2.; |
679 |
|
|
newHeight2 = getMinZoomScale() * getHeight() / 2.; |
680 |
|
|
} else { |
681 |
|
|
|
682 |
|
|
//**************************************************************************** |
683 |
|
|
// Die mapArea / der Envelope ist ist gueltig! Keine Aenderungen |
684 |
|
|
//**************************************************************************** |
685 |
|
|
return env; |
686 |
|
|
} |
687 |
|
|
|
688 |
|
|
Coordinate ll = new Coordinate(centerX - newWidth2, centerY |
689 |
|
|
- newHeight2); |
690 |
|
|
Coordinate ur = new Coordinate(centerX + newWidth2, centerY |
691 |
|
|
+ newHeight2); |
692 |
|
|
|
693 |
|
|
return new Envelope(ll, ur); |
694 |
|
|
} |
695 |
|
|
|
696 |
|
|
public void mouseEntered(MouseEvent e) { |
697 |
|
|
} |
698 |
|
|
|
699 |
|
|
public void mouseExited(MouseEvent e) { |
700 |
|
|
} |
701 |
|
|
|
702 |
|
|
public void mousePressed(MouseEvent e) { |
703 |
|
|
} |
704 |
|
|
|
705 |
|
|
public void mouseReleased(MouseEvent e) { |
706 |
|
|
} |
707 |
|
|
|
708 |
|
|
private static final int POLYGON = 0; |
709 |
|
|
|
710 |
|
|
private static final int LINE = 1; |
711 |
|
|
|
712 |
|
|
private static final int POINT = 2; |
713 |
|
|
|
714 |
|
|
private org.geotools.styling.Style setupStyle(int type, Color color) { |
715 |
|
|
StyleFactory sf = StyleFactoryFinder.createStyleFactory(); |
716 |
|
|
StyleBuilder sb = new StyleBuilder(sf, ff); |
717 |
|
|
|
718 |
|
|
org.geotools.styling.Style s = sf.createStyle(); |
719 |
|
|
s.setTitle("selection"); |
720 |
|
|
|
721 |
|
|
// TODO parameterise the color |
722 |
|
|
PolygonSymbolizer ps = sb.createPolygonSymbolizer(color); |
723 |
|
|
ps.setStroke(sb.createStroke(color)); |
724 |
|
|
LineSymbolizer ls = sb.createLineSymbolizer(color); |
725 |
|
|
Graphic h = sb.createGraphic(); |
726 |
|
|
h.setMarks(new Mark[] { sb.createMark("square", color) }); |
727 |
|
|
PointSymbolizer pts = sb.createPointSymbolizer(h); |
728 |
|
|
|
729 |
|
|
// Rule r = sb.createRule(new Symbolizer[]{ps,ls,pts}); |
730 |
|
|
switch (type) { |
731 |
|
|
case POLYGON: |
732 |
|
|
s = sb.createStyle(ps); |
733 |
|
|
break; |
734 |
|
|
case POINT: |
735 |
|
|
s = sb.createStyle(pts); |
736 |
|
|
break; |
737 |
|
|
case LINE: |
738 |
|
|
s = sb.createStyle(ls); |
739 |
|
|
} |
740 |
|
|
|
741 |
|
|
return s; |
742 |
|
|
|
743 |
|
|
} |
744 |
|
|
|
745 |
|
|
public void mouseDragged(MouseEvent e) { |
746 |
|
|
// TODO Auto-generated method stub |
747 |
|
|
|
748 |
|
|
} |
749 |
|
|
|
750 |
|
|
public void highlightChanged(HighlightChangedEvent e) { |
751 |
|
|
// TODO Auto-generated method stub |
752 |
|
|
|
753 |
|
|
Filter f = e.getFilter(); |
754 |
|
|
try { |
755 |
|
|
highlightFeature = highlightLayer.getFeatureSource().getFeatures(f); |
756 |
|
|
} catch (IOException e1) { |
757 |
|
|
// TODO Auto-generated catch block |
758 |
|
|
e1.printStackTrace(); |
759 |
|
|
} |
760 |
|
|
repaint(); |
761 |
|
|
|
762 |
|
|
} |
763 |
|
|
|
764 |
|
|
public void propertyChange(PropertyChangeEvent evt) { |
765 |
|
|
// TODO Auto-generated method stub |
766 |
|
|
String prop = evt.getPropertyName(); |
767 |
|
|
if (prop.equalsIgnoreCase("crs")) { |
768 |
|
|
context.setAreaOfInterest(context.getAreaOfInterest(), |
769 |
|
|
(CoordinateReferenceSystem) evt.getNewValue()); |
770 |
|
|
} |
771 |
|
|
} |
772 |
|
|
|
773 |
|
|
public boolean isReset() { |
774 |
|
|
return reset; |
775 |
|
|
} |
776 |
|
|
|
777 |
|
|
public void setReset(boolean reset) { |
778 |
|
|
this.reset = reset; |
779 |
|
|
} |
780 |
|
|
|
781 |
|
|
|
782 |
|
|
|
783 |
|
|
/** |
784 |
|
|
* Retuns the minimum allowed zoom scale. This is the bigger number value of the two. |
785 |
|
|
* Defaults to {@link Double}.MAX_VALUE |
786 |
|
|
* |
787 |
|
|
* @author <a href="mailto:[email protected]">Stefan Alfons Krüger</a> |
788 |
|
|
*/ |
789 |
|
|
public Double getMinZoomScale() { |
790 |
|
|
return minZoomScale; |
791 |
|
|
} |
792 |
|
|
|
793 |
|
|
|
794 |
|
|
/** |
795 |
|
|
* Retuns the maximum allowed zoom scale. This is the smaller number value of the two. |
796 |
|
|
* Defaults to {@link Double}.MIN_VALUE |
797 |
|
|
* |
798 |
|
|
* @author <a href="mailto:[email protected]">Stefan Alfons Krüger</a> |
799 |
|
|
*/ |
800 |
|
|
public Double getMaxZoomScale() { |
801 |
|
|
return maxZoomScale; |
802 |
|
|
} |
803 |
|
|
|
804 |
|
|
/** |
805 |
|
|
* Set the maximum allowed zoom scale. This is the smaller number value of the two. |
806 |
|
|
* |
807 |
|
|
* @author <a href="mailto:[email protected]">Stefan Alfons Krüger</a> |
808 |
|
|
*/ |
809 |
|
|
public void setMaxZoomScale(Double maxZoomScale) { |
810 |
|
|
// System.out.println("setting max scale to "+maxZoomScale); |
811 |
|
|
this.maxZoomScale = maxZoomScale; |
812 |
|
|
} |
813 |
|
|
/** |
814 |
|
|
* Set the minimum (nearest) allowed zoom scale. This is the bigger number value of the two. |
815 |
|
|
* |
816 |
|
|
* @author <a href="mailto:[email protected]">Stefan Alfons Krüger</a> |
817 |
|
|
*/ |
818 |
|
|
public void setMinZoomScale(Double minZoomScale) { |
819 |
|
|
this.minZoomScale = minZoomScale; |
820 |
|
|
} |
821 |
|
|
} |