/[schmitzm]/trunk/src/org/geotools/swing/JMapPane.GT2-2.3.1
ViewVC logotype

Annotation of /trunk/src/org/geotools/swing/JMapPane.GT2-2.3.1

Parent Directory Parent Directory | Revision Log Revision Log


Revision 621 - (hide annotations)
Thu Jan 28 10:06:05 2010 UTC (15 years, 1 month ago) by alfonx
Original Path: branches/2.0-RC2/src/org/geotools/swing/JMapPane.GT2-2.3.1
File size: 22499 byte(s)
2.0-RC2 ist für die weiterentwicklung und soll bald in den trunk mergen
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.1 to suppress the {@code System.out}-messages
66     * in {@link #setState(int)} and {@link #paintComponent(Graphics)}.</b><br><br>
67     *
68     * A simple map container that is a JPanel with a map in.
69     * provides simple pan,zoom, highlight and selection
70     * The mappane stores an image of the map (drawn from the context) and
71     * an image of the slected feature(s) to speed up
72     * rendering of the highlights. Thus the whole map is only redrawn
73     * when the bbox changes, selection is only redrawn when the
74     * selected feature changes.
75     *
76     * If you intend to use this in production code you'll
77     * need to make selection and highlighting work in the same way.
78     * @author Ian Turton
79     *
80     */
81     public class JMapPane extends JPanel implements MouseListener,
82     HighlightChangeListener, PropertyChangeListener {
83     /**
84     * what renders the map
85     */
86     GTRenderer renderer;
87    
88     /**
89     * the map context to render
90     */
91     MapContext context;
92    
93     private MapContext selectionContext;
94    
95     /**
96     * the area of the map to draw
97     */
98     Envelope mapArea;
99    
100     /**
101     * the size of the pane last time we drew
102     */
103     private Rectangle oldRect = null;
104    
105     /**
106     * the last map area drawn.
107     */
108     private Envelope oldMapArea = null;
109    
110     /**
111     * the base image of the map
112     */
113     private BufferedImage baseImage;
114     /**
115     * image of selection
116     */
117     private BufferedImage selectImage;
118     /**
119     * style for selected items
120     */
121     private Style selectionStyle;
122     /**
123     * layer that selection works on
124     */
125     private int selectionLayer = -1;
126     /**
127     * layer that highlight works on
128     */
129     private MapLayer highlightLayer;
130     /**
131     * the object which manages highlighting
132     */
133     private HighlightManager highlightManager;
134     /**
135     * is highlighting on or off
136     */
137     private boolean highlight = true;
138     /**
139     * a factory for filters
140     */
141     FilterFactory ff = FilterFactoryFinder.createFilterFactory();
142     /**
143     * a factory for geometries
144     */
145     GeometryFactory gf = new GeometryFactory();
146     /**
147     * the collections of features to be selected or highlighted
148     */
149     FeatureCollection selection, highlightFeature;
150    
151     public static final int Reset = 0;
152    
153     public static final int ZoomIn = 1;
154    
155     public static final int ZoomOut = 2;
156    
157     public static final int Pan = 3;
158    
159     public static final int Select = 4;
160    
161     private int state = ZoomIn;
162    
163     /**
164     * how far to zoom in or out
165     */
166     private double zoomFactor = 2.0;
167    
168     Style lineHighlightStyle;
169    
170     Style pointHighlightStyle;
171    
172     Style polygonHighlightStyle;
173    
174     Style polygonSelectionStyle;
175    
176     Style pointSelectionStyle;
177    
178     Style lineSelectionStyle;
179    
180     public JMapPane() {
181     this(null, true, null, null);
182     }
183     /**
184     * create a basic JMapPane
185     * @param render - how to draw the map
186     * @param context - the map context to display
187     */
188     public JMapPane(GTRenderer render, MapContext context) {
189     this(null, true, render, context);
190     }
191     /**
192     * full constructor extending JPanel
193     * @param layout - layout (probably shouldn't be set)
194     * @param isDoubleBuffered - a Swing thing I don't really understand
195     * @param render - what to draw the map with
196     * @param context - what to draw
197     */
198     public JMapPane(LayoutManager layout, boolean isDoubleBuffered,
199     GTRenderer render, MapContext context) {
200     super(layout, isDoubleBuffered);
201     setRenderer(render);
202    
203     setContext(context);
204    
205     this.addMouseListener(this);
206     setHighlightManager(new HighlightManager(highlightLayer));
207    
208     lineHighlightStyle = setupStyle(LINE, Color.red);
209    
210     pointHighlightStyle = setupStyle(POINT, Color.red);
211    
212     polygonHighlightStyle = setupStyle(POLYGON, Color.red);
213    
214     polygonSelectionStyle = setupStyle(POLYGON, Color.cyan);
215    
216     pointSelectionStyle = setupStyle(POINT, Color.cyan);
217    
218     lineSelectionStyle = setupStyle(LINE, Color.cyan);
219    
220     }
221     /**
222     * get the renderer
223     */
224    
225     public GTRenderer getRenderer() {
226     return renderer;
227     }
228    
229     public void setRenderer(GTRenderer renderer) {
230     this.renderer = renderer;
231    
232     if (this.context != null) {
233     this.renderer.setContext(this.context);
234    
235     }
236     }
237    
238     public MapContext getContext() {
239     return context;
240     }
241    
242     public void setContext(MapContext context) {
243    
244     this.context = context;
245    
246     if (renderer != null) {
247     renderer.setContext(this.context);
248    
249     }
250     }
251    
252     public Envelope getMapArea() {
253     return mapArea;
254     }
255    
256     public void setMapArea(Envelope mapArea) {
257     this.mapArea = mapArea;
258     }
259    
260     public int getState() {
261     return state;
262     }
263    
264     public void setState(int state) {
265     this.state = state;
266     // System.out.println("State: " + state);
267     }
268    
269     public double getZoomFactor() {
270     return zoomFactor;
271     }
272    
273     public void setZoomFactor(double zoomFactor) {
274     this.zoomFactor = zoomFactor;
275     }
276    
277     public int getSelectionLayer() {
278     return selectionLayer;
279     }
280    
281     public void setSelectionLayer(int selectionLayer) {
282     this.selectionLayer = selectionLayer;
283     }
284    
285     public boolean isHighlight() {
286     return highlight;
287     }
288    
289     public void setHighlight(boolean highlight) {
290     this.highlight = highlight;
291     }
292    
293     public MapLayer getHighlightLayer() {
294     return highlightLayer;
295     }
296    
297     public void setHighlightLayer(MapLayer highlightLayer) {
298     this.highlightLayer = highlightLayer;
299     if (highlightManager != null) {
300     highlightManager.setHighlightLayer(highlightLayer);
301     }
302     }
303    
304     public HighlightManager getHighlightManager() {
305     return highlightManager;
306     }
307    
308     public void setHighlightManager(HighlightManager highlightManager) {
309     this.highlightManager = highlightManager;
310     this.highlightManager.addHighlightChangeListener(this);
311     this.addMouseMotionListener(this.highlightManager);
312     }
313    
314     public Style getLineHighlightStyle() {
315     return lineHighlightStyle;
316     }
317    
318     public void setLineHighlightStyle(Style lineHighlightStyle) {
319     this.lineHighlightStyle = lineHighlightStyle;
320     }
321    
322     public Style getLineSelectionStyle() {
323     return lineSelectionStyle;
324     }
325    
326     public void setLineSelectionStyle(Style lineSelectionStyle) {
327     this.lineSelectionStyle = lineSelectionStyle;
328     }
329    
330     public Style getPointHighlightStyle() {
331     return pointHighlightStyle;
332     }
333    
334     public void setPointHighlightStyle(Style pointHighlightStyle) {
335     this.pointHighlightStyle = pointHighlightStyle;
336     }
337    
338     public Style getPointSelectionStyle() {
339     return pointSelectionStyle;
340     }
341    
342     public void setPointSelectionStyle(Style pointSelectionStyle) {
343     this.pointSelectionStyle = pointSelectionStyle;
344     }
345    
346     public Style getPolygonHighlightStyle() {
347     return polygonHighlightStyle;
348     }
349    
350     public void setPolygonHighlightStyle(Style polygonHighlightStyle) {
351     this.polygonHighlightStyle = polygonHighlightStyle;
352     }
353    
354     public Style getPolygonSelectionStyle() {
355     return polygonSelectionStyle;
356     }
357    
358     public void setPolygonSelectionStyle(Style polygonSelectionStyle) {
359     this.polygonSelectionStyle = polygonSelectionStyle;
360     }
361    
362     private boolean reset = false;
363    
364     protected void paintComponent(Graphics g) {
365     boolean changed = false;
366     super.paintComponent(g);
367     if (renderer == null || mapArea == null) {
368     return;
369     }
370     Rectangle r = getBounds();
371     Rectangle dr = new Rectangle(r.width, r.height);
372     if (!r.equals(oldRect) || reset) {
373     /*either the viewer size has changed or we've done a reset*/
374     changed = true; /* note we need to redraw */
375     reset = false; /* forget about the reset */
376     oldRect = r; /* store what the current size is */
377     double mapWidth = mapArea.getWidth(); /* get the extent of the map*/
378     double mapHeight = mapArea.getHeight();
379     double scaleX = r.getWidth() / mapArea.getWidth(); /* calculate the new scale*/
380     double scaleY = r.getHeight() / mapArea.getHeight();
381     double scale = 1.0; // stupid compiler!
382     if (scaleX < scaleY) {/*pick the smaller scale */
383     scale = scaleX;
384     } else {
385     scale = scaleY;
386     }
387     /* calculate the difference in width and height of the new extent*/
388     double deltaX = /*Math.abs*/((r.getWidth() / scale) - mapWidth);
389     double deltaY = /*Math.abs*/((r.getHeight() / scale) - mapHeight);
390    
391     // System.out.println("delta x "+deltaX); System.out.println("delta y "+deltaY);
392     /* create the new extent */
393     Coordinate ll = new Coordinate(mapArea.getMinX() - (deltaX / 2.0),
394     mapArea.getMinY() - (deltaY / 2.0));
395     Coordinate ur = new Coordinate(mapArea.getMaxX() + (deltaX / 2.0),
396     mapArea.getMaxY() + (deltaY / 2.0));
397     mapArea = new Envelope(ll, ur);
398     }
399     if (!mapArea.equals(oldMapArea)) {/* did the map extent change?*/
400     changed = true;
401     oldMapArea = mapArea;
402     }
403     if (changed) {/* if the map changed then redraw*/
404    
405     baseImage = new BufferedImage(dr.width, dr.height,
406     BufferedImage.TYPE_INT_ARGB);
407     Graphics2D ig = baseImage.createGraphics();
408     /* System.out.println("rendering"); */
409     renderer.setContext(context);
410     renderer.paint((Graphics2D) ig, dr, mapArea);
411     }
412     ((Graphics2D) g).drawImage(baseImage, 0, 0, this);
413     if (selection != null && selection.size() > 0) {
414     // paint selection
415     /*
416     * String type = selection.getDefaultGeometry().getGeometryType();
417     * System.out.println(type); if(type==null) type="polygon";
418     */
419     String type = "polygon";
420     if (type.equalsIgnoreCase("polygon")) {
421    
422     selectionStyle = polygonSelectionStyle;
423     } else if (type.equalsIgnoreCase("point")) {
424    
425     selectionStyle = pointSelectionStyle;
426     } else if (type.equalsIgnoreCase("line")) {
427    
428     selectionStyle = lineSelectionStyle;
429     }
430    
431     selectionContext = new DefaultMapContext();
432    
433     selectionContext.addLayer(selection, selectionStyle);
434     renderer.setContext(selectionContext);
435    
436     selectImage = new BufferedImage(dr.width, dr.height,
437     BufferedImage.TYPE_INT_ARGB);
438     Graphics2D ig = selectImage.createGraphics();
439     /* System.out.println("rendering selection"); */
440     renderer.paint((Graphics2D) ig, dr, mapArea);
441    
442     ((Graphics2D) g).drawImage(selectImage, 0, 0, this);
443     }
444     if (highlight && highlightFeature != null
445     && highlightFeature.size() > 0) {
446     /*
447     * String type = selection.getDefaultGeometry().getGeometryType();
448     * System.out.println(type); if(type==null) type="polygon";
449     */
450     String type = "polygon";
451     Style highlightStyle = null;
452     if (type.equalsIgnoreCase("polygon")) {
453    
454     highlightStyle = polygonHighlightStyle;
455     } else if (type.equalsIgnoreCase("point")) {
456    
457     highlightStyle = pointHighlightStyle;
458     } else if (type.equalsIgnoreCase("line")) {
459    
460     highlightStyle = lineHighlightStyle;
461     }
462    
463     MapContext highlightContext = new DefaultMapContext();
464    
465     highlightContext.addLayer(highlightFeature, highlightStyle);
466     renderer.setContext(highlightContext);
467    
468     /* System.out.println("rendering highlight"); */
469     renderer.paint((Graphics2D) g, dr, mapArea);
470    
471     }
472     }
473    
474     public FeatureCollection doSelection(double x, double y, int layer) {
475     GeometryFilter f = null;
476     FeatureCollection select = null;
477     Geometry geometry = gf.createPoint(new Coordinate(x, y));
478     try {
479     f = ff.createGeometryFilter(GeometryFilter.GEOMETRY_CONTAINS);
480     f.addRightGeometry(ff.createLiteralExpression(geometry));
481     } catch (IllegalFilterException e) {
482     // TODO Auto-generated catch block
483     e.printStackTrace();
484     }
485    
486     if (layer == -1) {
487     for (int i = 0; i < context.getLayers().length; i++) {
488     FeatureCollection fx = findFeature(f, i);
489     if (select != null) {
490     select.addAll(fx);
491     } else {
492     select = fx;
493     }
494     }
495     } else {
496     select = findFeature(f, layer);
497     }
498     return select;
499     }
500    
501     /**
502     * @param f -
503     * a partial geometry filter. The geom name will be added
504     * @param i -
505     * the index of the layer to search
506     * @throws IndexOutOfBoundsException
507     */
508     private FeatureCollection findFeature(GeometryFilter f, int i)
509     throws IndexOutOfBoundsException {
510     FeatureCollection fcol = null;
511     if (context != null && i > context.getLayers().length) {
512     return fcol;
513     }
514     MapLayer layer = context.getLayer(i);
515    
516     try {
517     String name = layer.getFeatureSource().getSchema()
518     .getDefaultGeometry().getName();
519     if (name == "")
520     name = "the_geom";
521     f.addLeftGeometry(ff.createAttributeExpression(name));
522     // System.out.println("looking with " + f);
523     FeatureCollection fc = layer.getFeatureSource().getFeatures(f);
524     if (fc.size() > 0) {
525     // selectionStyle.getFeatureTypeStyles()[0].getRules()[0].setFilter(f);
526     selectionLayer = i;
527     }
528     if (fcol == null) {
529     fcol = fc;
530     // here we should set the defaultgeom type
531     } else {
532     fcol.addAll(fc);
533     }
534     /*
535     * GeometryAttributeType gat =
536     * layer.getFeatureSource().getSchema().getDefaultGeometry();
537     * fcol.setDefaultGeometry((Geometry)gat.createDefaultValue());
538     */
539    
540     /*
541     * Iterator fi = fc.iterator(); while (fi.hasNext()) { Feature feat =
542     * (Feature) fi.next(); System.out.println("selected " +
543     * feat.getAttribute("STATE_NAME")); }
544     */
545     } catch (IOException e) {
546     // TODO Auto-generated catch block
547     e.printStackTrace();
548     } catch (IllegalFilterException e) {
549     // TODO Auto-generated catch block
550     e.printStackTrace();
551     }/*
552     * catch (IllegalAttributeException e) { // TODO Auto-generated
553     * catch block System.err.println(e.getMessage()); //
554     * e.printStackTrace(); }
555     */
556     return fcol;
557     }
558    
559     public void mouseClicked(MouseEvent e) {
560     // TODO Auto-generated method stub
561     // System.out.println("before area "+mapArea+"\nw:"+mapArea.getWidth()+"
562     // h:"+mapArea.getHeight());
563     Rectangle bounds = this.getBounds();
564     double x = (double) (e.getX());
565     double y = (double) (e.getY());
566     double width = mapArea.getWidth();
567     double height = mapArea.getHeight();
568     double width2 = mapArea.getWidth() / 2.0;
569     double height2 = mapArea.getHeight() / 2.0;
570    
571     double mapX = (x * width / (double) bounds.width) + mapArea.getMinX();
572     double mapY = ((bounds.getHeight() - y) * height / (double) bounds.height)
573     + mapArea.getMinY();
574     /*
575     * System.out.println(""+x+"->"+mapX);
576     * System.out.println(""+y+"->"+mapY);
577     */
578     /*
579     * Coordinate ll = new Coordinate(mapArea.getMinX(), mapArea.getMinY());
580     * Coordinate ur = new Coordinate(mapArea.getMaxX(), mapArea.getMaxY());
581     */
582     double zlevel = 1.0;
583     switch (state) {
584     case Pan:
585     zlevel = 1.0;
586     break;
587     case ZoomIn:
588     zlevel = zoomFactor;
589     break;
590     case ZoomOut:
591     zlevel = 1.0 / zoomFactor;
592     break;
593     case Select:
594     selection = doSelection(mapX, mapY, selectionLayer);
595     repaint();
596     return;
597     default:
598     return;
599     }
600     Coordinate ll = new Coordinate(mapX - (width2 / zlevel), mapY
601     - (height2 / zlevel));
602     Coordinate ur = new Coordinate(mapX + (width2 / zlevel), mapY
603     + (height2 / zlevel));
604    
605     mapArea = new Envelope(ll, ur);
606     // System.out.println("after area "+mapArea+"\nw:"+mapArea.getWidth()+"
607     // h:"+mapArea.getHeight());
608     repaint();
609     }
610    
611     public void mouseEntered(MouseEvent e) {
612     // TODO Auto-generated method stub
613    
614     }
615    
616     public void mouseExited(MouseEvent e) {
617     // TODO Auto-generated method stub
618    
619     }
620    
621     public void mousePressed(MouseEvent e) {
622     // TODO Auto-generated method stub
623    
624     }
625    
626     public void mouseReleased(MouseEvent e) {
627     // TODO Auto-generated method stub
628    
629     }
630    
631     private static final int POLYGON = 0;
632    
633     private static final int LINE = 1;
634    
635     private static final int POINT = 2;
636    
637     private org.geotools.styling.Style setupStyle(int type, Color color) {
638     StyleFactory sf = StyleFactoryFinder.createStyleFactory();
639     StyleBuilder sb = new StyleBuilder(sf, ff);
640    
641     org.geotools.styling.Style s = sf.createStyle();
642     s.setTitle("selection");
643    
644     // TODO parameterise the color
645     PolygonSymbolizer ps = sb.createPolygonSymbolizer(color);
646     ps.setStroke(sb.createStroke(color));
647     LineSymbolizer ls = sb.createLineSymbolizer(color);
648     Graphic h = sb.createGraphic();
649     h.setMarks(new Mark[] { sb.createMark("square", color) });
650     PointSymbolizer pts = sb.createPointSymbolizer(h);
651    
652     // Rule r = sb.createRule(new Symbolizer[]{ps,ls,pts});
653     switch (type) {
654     case POLYGON:
655     s = sb.createStyle(ps);
656     break;
657     case POINT:
658     s = sb.createStyle(pts);
659     break;
660     case LINE:
661     s = sb.createStyle(ls);
662     }
663    
664     return s;
665    
666     }
667    
668     public void mouseDragged(MouseEvent e) {
669     // TODO Auto-generated method stub
670    
671     }
672    
673     public void highlightChanged(HighlightChangedEvent e) {
674     // TODO Auto-generated method stub
675    
676     Filter f = e.getFilter();
677     try {
678     highlightFeature = highlightLayer.getFeatureSource().getFeatures(f);
679     } catch (IOException e1) {
680     // TODO Auto-generated catch block
681     e1.printStackTrace();
682     }
683     repaint();
684    
685     }
686    
687     public void propertyChange(PropertyChangeEvent evt) {
688     // TODO Auto-generated method stub
689     String prop = evt.getPropertyName();
690     if (prop.equalsIgnoreCase("crs")) {
691     context.setAreaOfInterest(context.getAreaOfInterest(),
692     (CoordinateReferenceSystem) evt.getNewValue());
693     }
694     }
695    
696     public boolean isReset() {
697     return reset;
698     }
699    
700     public void setReset(boolean reset) {
701     this.reset = reset;
702     }
703     }
704    

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26