001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.demo.mappane;
017:
018: import java.awt.BorderLayout;
019: import java.awt.Container;
020: import java.awt.RenderingHints;
021: import java.awt.event.ActionEvent;
022: import java.awt.event.ActionListener;
023: import java.util.HashMap;
024:
025: import javax.swing.Action;
026: import javax.swing.JButton;
027: import javax.swing.JFileChooser;
028: import javax.swing.JFrame;
029: import javax.swing.JLabel;
030: import javax.swing.JOptionPane;
031: import javax.swing.JToolBar;
032: import javax.swing.WindowConstants;
033:
034: import org.geotools.data.ows.Layer;
035: import org.geotools.data.wms.WebMapServer;
036: import org.geotools.geometry.Envelope2D;
037: import org.geotools.gui.swing.JMapPane;
038: import org.geotools.gui.swing.PanAction;
039: import org.geotools.gui.swing.ResetAction;
040: import org.geotools.gui.swing.ZoomInAction;
041: import org.geotools.gui.swing.ZoomOutAction;
042: import org.geotools.map.DefaultMapContext;
043: import org.geotools.map.MapContext;
044: import org.geotools.referencing.CRS;
045: import org.geotools.referencing.crs.DefaultGeographicCRS;
046: import org.geotools.renderer.GTRenderer;
047: import org.geotools.renderer.lite.StreamingRenderer;
048: import org.opengis.referencing.crs.CoordinateReferenceSystem;
049:
050: import com.vividsolutions.jts.geom.Envelope;
051:
052: /**
053: * Sample application that may be used to try JMapPane
054: * to view WMS Layers
055: *
056: * @author Ian Turton
057: */
058: public class WMSViewer implements ActionListener {
059: JFrame frame;
060:
061: JMapPane mp;
062:
063: JToolBar jtb;
064:
065: JLabel text;
066:
067: GTRenderer renderer;
068:
069: MapContext context;
070:
071: final JFileChooser jfc = new JFileChooser();
072:
073: public WMSViewer() {
074: frame = new JFrame("My WMS Viewer");
075: frame.setBounds(20, 20, 450, 200);
076: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
077: Container content = frame.getContentPane();
078: mp = new JMapPane();
079: // mp.addZoomChangeListener(this);
080: content.setLayout(new BorderLayout());
081: jtb = new JToolBar();
082:
083: JButton load = new JButton("Load file");
084: load.addActionListener(this );
085: jtb.add(load);
086: Action zoomIn = new ZoomInAction(mp);
087: Action zoomOut = new ZoomOutAction(mp);
088: Action pan = new PanAction(mp);
089: // Action select = new SelectAction(mp);
090: Action reset = new ResetAction(mp);
091: jtb.add(zoomIn);
092: jtb.add(zoomOut);
093: jtb.add(pan);
094: jtb.addSeparator();
095: jtb.add(reset);
096: jtb.addSeparator();
097: // jtb.add(select);
098: final JButton button = new JButton();
099: button.setText("CRS");
100: button.setToolTipText("Change map prjection");
101: button.addActionListener(new ActionListener() {
102: public void actionPerformed(ActionEvent e) {
103:
104: String code = JOptionPane.showInputDialog(button,
105: "Coordinate Reference System:", "EPSG:4326");
106: if (code == null)
107: return;
108: try {
109: CoordinateReferenceSystem crs = CRS.decode(code);
110: setCRS(crs);
111: } catch (Exception fe) {
112: fe.printStackTrace();
113: JOptionPane.showMessageDialog(button, fe
114: .getMessage(), fe.getClass().toString(),
115: JOptionPane.ERROR_MESSAGE);
116: return;
117: }
118: }
119: });
120: jtb.add(button);
121:
122: content.add(jtb, BorderLayout.NORTH);
123:
124: // JComponent sp = mp.createScrollPane();
125: mp.setSize(400, 200);
126: content.add(mp, BorderLayout.CENTER);
127:
128: content.doLayout();
129: frame.setVisible(true);
130:
131: }
132:
133: /**
134: * Method used to set the current map projection.
135: *
136: * @param crs
137: * A new CRS for the mappnae.
138: */
139: public void setCRS(CoordinateReferenceSystem crs) {
140: mp.getContext().setAreaOfInterest(
141: mp.getContext().getAreaOfInterest(), crs);
142: mp.setReset(true);
143: mp.repaint();
144: }
145:
146: public void load(WMSMapLayer layer) throws Exception {
147:
148: Envelope2D env = layer.getGrid().getEnvelope2D();
149: Envelope en = new Envelope(env.x, env.y, env.width, env.height);
150: mp.setMapArea(en);
151:
152: CoordinateReferenceSystem crs = layer.getGrid()
153: .getCoordinateReferenceSystem();
154: if (crs == null)
155: crs = DefaultGeographicCRS.WGS84;
156: if (context == null) {
157: context = new DefaultMapContext(crs);
158: mp.setContext(context);
159: }
160:
161: //this allows us to listen for resize events and ask for the right size image
162: mp.addComponentListener(layer);
163: context.addLayer(layer);
164: context.addMapBoundsListener(layer);
165: // System.out.println(context.getLayerBounds());
166: // mp.setHighlightLayer(context.getLayer(0));
167:
168: if (renderer == null) {
169: if (false) {
170: renderer = new StreamingRenderer();
171: HashMap hints = new HashMap();
172: hints.put("memoryPreloadingEnabled", Boolean.TRUE);
173: renderer.setRendererHints(hints);
174: } else {
175: renderer = new StreamingRenderer();
176: HashMap hints = new HashMap();
177: hints.put("memoryPreloadingEnabled", Boolean.FALSE);
178: renderer.setRendererHints(hints);
179: RenderingHints rhints = new RenderingHints(
180: RenderingHints.KEY_ANTIALIASING,
181: RenderingHints.VALUE_ANTIALIAS_ON);
182: ((StreamingRenderer) renderer).setJava2DHints(rhints);
183: }
184: mp.setRenderer(renderer);
185:
186: }
187: // mp.getRenderer().addLayer(new RenderedMapScale());
188: frame.repaint();
189: frame.doLayout();
190: }
191:
192: public void actionPerformed(ActionEvent e) {
193: WMSChooser wmc = new WMSChooser(frame, "Select WMS", true);
194: //wmc.setServer("http://www.geovista.psu.edu/geoserver/wms");
195: wmc.setVisible(true);
196: int returnVal = wmc.getLayer();
197: if (returnVal == -1) {
198: return;
199: }
200: WebMapServer wms = wmc.getWms();
201: Layer l = (Layer) wmc.getLayers().get(returnVal);
202: WMSMapLayer layer = new WMSMapLayer(wms, l);
203: try {
204: load(layer);
205: } catch (Exception e1) {
206: // TODO Auto-generated catch block
207: e1.printStackTrace();
208: }
209: }
210:
211: /**
212: * @param args
213: */
214: public static void main(String[] args) throws Exception {
215: WMSViewer mapV = new WMSViewer();
216:
217: }
218: }
|