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.event.ActionEvent;
021: import java.awt.event.ActionListener;
022: import java.io.File;
023: import java.net.MalformedURLException;
024: import java.net.URL;
025: import java.util.HashMap;
026:
027: import javax.swing.Action;
028: import javax.swing.JButton;
029: import javax.swing.JFileChooser;
030: import javax.swing.JFrame;
031: import javax.swing.JLabel;
032: import javax.swing.JOptionPane;
033: import javax.swing.JToolBar;
034: import javax.swing.WindowConstants;
035:
036: import org.geotools.data.FeatureSource;
037: import org.geotools.data.shapefile.ShapefileDataStore;
038: import org.geotools.factory.CommonFactoryFinder;
039: import org.geotools.gui.swing.JMapPane;
040: import org.geotools.gui.swing.PanAction;
041: import org.geotools.gui.swing.ResetAction;
042: import org.geotools.gui.swing.SelectAction;
043: import org.geotools.gui.swing.ZoomInAction;
044: import org.geotools.gui.swing.ZoomOutAction;
045: import org.geotools.map.DefaultMapContext;
046: import org.geotools.map.MapContext;
047: import org.geotools.referencing.CRS;
048: import org.geotools.referencing.crs.DefaultGeographicCRS;
049: import org.geotools.renderer.GTRenderer;
050: import org.geotools.renderer.lite.StreamingRenderer;
051: import org.geotools.styling.SLDParser;
052: import org.geotools.styling.StyleFactory;
053: import org.opengis.referencing.crs.CoordinateReferenceSystem;
054:
055: /**
056: * Sample application that may be used to try JMapPane from the command line.
057: *
058: * @author Ian Turton
059: */
060: public class MapViewer implements ActionListener {
061: JFrame frame;
062: JMapPane mp;
063: JToolBar jtb;
064: JLabel text;
065: final JFileChooser jfc = new JFileChooser();
066:
067: public MapViewer() {
068: frame = new JFrame("My Map Viewer");
069: frame.setBounds(20, 20, 450, 200);
070: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
071: Container content = frame.getContentPane();
072: mp = new JMapPane();
073: //mp.addZoomChangeListener(this);
074: content.setLayout(new BorderLayout());
075: jtb = new JToolBar();
076:
077: JButton load = new JButton("Load file");
078: load.addActionListener(this );
079: jtb.add(load);
080: Action zoomIn = new ZoomInAction(mp);
081: Action zoomOut = new ZoomOutAction(mp);
082: Action pan = new PanAction(mp);
083: Action select = new SelectAction(mp);
084: Action reset = new ResetAction(mp);
085: jtb.add(zoomIn);
086: jtb.add(zoomOut);
087: jtb.add(pan);
088: jtb.addSeparator();
089: jtb.add(reset);
090: jtb.addSeparator();
091: jtb.add(select);
092: final JButton button = new JButton();
093: button.setText("CRS");
094: button.setToolTipText("Change map prjection");
095: button.addActionListener(new ActionListener() {
096: public void actionPerformed(ActionEvent e) {
097:
098: String code = JOptionPane.showInputDialog(button,
099: "Coordinate Reference System:", "EPSG:4326");
100: if (code == null)
101: return;
102: try {
103: CoordinateReferenceSystem crs = CRS.decode(code);
104: setCRS(crs);
105: } catch (Exception fe) {
106: fe.printStackTrace();
107: JOptionPane.showMessageDialog(button, fe
108: .getMessage(), fe.getClass().toString(),
109: JOptionPane.ERROR_MESSAGE);
110: return;
111: }
112: }
113: });
114: jtb.add(button);
115:
116: content.add(jtb, BorderLayout.NORTH);
117:
118: //JComponent sp = mp.createScrollPane();
119: mp.setSize(400, 200);
120: content.add(mp, BorderLayout.CENTER);
121:
122: content.doLayout();
123: frame.setVisible(true);
124:
125: }
126:
127: /**
128: * Method used to set the current map projection.
129: *
130: * @param crs A new CRS for the mappnae.
131: */
132: public void setCRS(CoordinateReferenceSystem crs) {
133: mp.getContext().setAreaOfInterest(
134: mp.getContext().getAreaOfInterest(), crs);
135: mp.setReset(true);
136: mp.repaint();
137: }
138:
139: public void load(URL shape, URL sld) throws Exception {
140: ShapefileDataStore ds = new ShapefileDataStore(shape);
141:
142: FeatureSource fs = ds.getFeatureSource();
143: com.vividsolutions.jts.geom.Envelope env = fs.getBounds();
144: mp.setMapArea(env);
145: StyleFactory factory = CommonFactoryFinder
146: .getStyleFactory(null);
147:
148: SLDParser stylereader = new SLDParser(factory, sld);
149: org.geotools.styling.Style[] style = stylereader.readXML();
150:
151: CoordinateReferenceSystem crs = fs.getSchema()
152: .getDefaultGeometry().getCoordinateSystem();
153: if (crs == null)
154: crs = DefaultGeographicCRS.WGS84;
155: MapContext context = new DefaultMapContext(crs);
156: context.addLayer(fs, style[0]);
157: context.getLayerBounds();
158: mp.setHighlightLayer(context.getLayer(0));
159: mp.setSelectionLayer(context.getLayer(0));
160:
161: GTRenderer renderer;
162: if (true) {
163: renderer = new StreamingRenderer();
164: HashMap hints = new HashMap();
165: hints.put("memoryPreloadingEnabled", Boolean.TRUE);
166: renderer.setRendererHints(hints);
167: } else {
168: renderer = new StreamingRenderer();
169: HashMap hints = new HashMap();
170: hints.put("memoryPreloadingEnabled", Boolean.FALSE);
171: renderer.setRendererHints(hints);
172: }
173: mp.setRenderer(renderer);
174: mp.setContext(context);
175:
176: // mp.getRenderer().addLayer(new RenderedMapScale());
177: frame.repaint();
178: frame.doLayout();
179: }
180:
181: public static URL aquireURL(String target) {
182: if (new File(target).exists()) {
183: try {
184: return new File(target).toURL();
185: } catch (MalformedURLException e) {
186: }
187: }
188: try {
189: return new URL(target);
190: } catch (MalformedURLException e) {
191: return null;
192: }
193: }
194:
195: public void actionPerformed(ActionEvent e) {
196: int returnVal = jfc.showOpenDialog(frame);
197: if (returnVal == JFileChooser.APPROVE_OPTION) {
198: String pathname = jfc.getSelectedFile().getAbsolutePath();
199: URL shape = aquireURL(pathname);
200: if (shape == null) {
201: JOptionPane.showMessageDialog(frame,
202: "could not find file \"" + pathname + "\"",
203: "Could not find file",
204: JOptionPane.ERROR_MESSAGE);
205: System.err.println("Could not find shapefile: "
206: + pathname);
207: return;
208: }
209: String filepart = pathname.substring(0, pathname
210: .lastIndexOf("."));
211: URL sld = aquireURL(filepart + ".sld");
212: if (sld == null) {
213: JOptionPane.showMessageDialog(frame,
214: "could not find SLD file \"" + filepart
215: + ".sld\"", "Could not find SLD file",
216: JOptionPane.ERROR_MESSAGE);
217: System.err.println("Could not find sld file: "
218: + filepart + ".sld");
219: return;
220: }
221: try {
222: this .load(shape, sld);
223: } catch (Exception e1) {
224: // TODO Auto-generated catch block
225: e1.printStackTrace();
226: }
227: }
228:
229: }
230:
231: /**
232: * @param args
233: */
234: public static void main(String[] args) throws Exception {
235: MapViewer mapV = new MapViewer();
236: if (args.length == 0 || !args[0].toLowerCase().endsWith(".shp")) {
237: /*System.out.println("java org.geotools.gui.swing.MapViewer shapefile.shp");
238: System.out.println("Notes:");
239: System.out.println(" Any provided shapefile.prj file or shapefile.sld will be used");
240: System.exit(0);*/
241: } else {
242: String pathname = args[0];
243: URL shape = aquireURL(pathname);
244: if (shape == null) {
245: System.err.println("Could not find shapefile: "
246: + pathname);
247: System.exit(1);
248: }
249: String filepart = pathname.substring(0, pathname
250: .lastIndexOf("."));
251: URL sld = aquireURL(filepart + ".sld");
252: if (sld == null) {
253: System.err.println("Could not find sld file: "
254: + filepart + ".sld");
255: System.exit(1);
256: }
257: mapV.load(shape, sld);
258: }
259: }
260: }
|