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