]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.gis/src/main/java/org/argeo/slc/geotools/SimpleGisFieldViewer.java
29832cff4335258be5e69307f685faa4ebbf85ed
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.gis / src / main / java / org / argeo / slc / geotools / SimpleGisFieldViewer.java
1 /*
2 * GeoTools - The Open Source Java GIS Tookit
3 * http://geotools.org
4 *
5 * (C) 2006-2008, Open Source Geospatial Foundation (OSGeo)
6 *
7 * This file is hereby placed into the Public Domain. This means anyone is
8 * free to do whatever they wish with this file. Use it well and enjoy!
9 */
10 package org.argeo.slc.geotools;
11
12 import java.awt.Color;
13 import java.io.File;
14 import java.io.IOException;
15 import java.util.Iterator;
16
17 import javax.media.jai.JAI;
18 import javax.swing.SwingUtilities;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.argeo.slc.SlcException;
23 import org.argeo.slc.geotools.swing.VersatileZoomTool;
24 import org.argeo.slc.jts.PositionProvider;
25 import org.geotools.data.FileDataStoreFactorySpi;
26 import org.geotools.data.FileDataStoreFinder;
27 import org.geotools.data.WorldFileReader;
28 import org.geotools.feature.DefaultFeatureCollection;
29 import org.geotools.feature.FeatureCollection;
30 import org.geotools.feature.simple.SimpleFeatureBuilder;
31 import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
32 import org.geotools.gce.image.WorldImageFormat;
33 import org.geotools.map.DefaultMapContext;
34 import org.geotools.map.DefaultMapLayer;
35 import org.geotools.map.MapContext;
36 import org.geotools.map.MapLayer;
37 import org.geotools.referencing.crs.DefaultGeographicCRS;
38 import org.geotools.styling.RasterSymbolizer;
39 import org.geotools.styling.SLD;
40 import org.geotools.styling.Style;
41 import org.geotools.styling.StyleBuilder;
42 import org.geotools.swing.JMapFrame;
43 import org.geotools.swing.JMapPane;
44 import org.opengis.feature.simple.SimpleFeature;
45 import org.opengis.feature.simple.SimpleFeatureType;
46
47 import com.vividsolutions.jts.geom.Point;
48
49 /**
50 * GeoTools Quickstart demo application. Prompts the user for a shapefile and
51 * displays its contents on the screen in a map frame
52 *
53 * @source $URL:
54 * http://svn.osgeo.org/geotools/trunk/demo/example/src/main/java/org
55 * /geotools/demo/Quickstart.java $
56 */
57 public class SimpleGisFieldViewer implements Runnable {
58 private final static Log log = LogFactory
59 .getLog(SimpleGisFieldViewer.class);
60
61 private PositionProvider positionProvider;
62
63 private ClassLoader jaiImageIoClassLoader;
64
65 public static void main(String[] args) throws Exception {
66 new SimpleGisFieldViewer().run();
67 }
68
69 public void run() {
70 Iterator<FileDataStoreFactorySpi> ps = FileDataStoreFinder
71 .getAvailableDataStores();
72 log.debug("Available datastores:");
73 while (ps.hasNext()) {
74 log.debug(ps.next());
75 }
76
77 // display a data store file chooser dialog for shapefiles
78 // File file = JFileDataStoreChooser.showOpenFile("shp", null);
79 // if (file == null) {
80 // return;
81 // }
82 File dir = new File(
83 "/home/mbaudier/gis/projects/100122-EasternBalkans2010/data");
84
85 // FeatureSource<SimpleFeatureType, SimpleFeature> featureSource =
86 // FileDataStoreFinder
87 // .getDataStore(new File(dir, "countries-EuroMed-NEarth.shp"))
88 // .getFeatureSource();
89
90 // Create a map context and add our shapefile to it
91 MapContext mapContext = new DefaultMapContext();
92 mapContext.setTitle("Quickstart");
93
94 // Now display the map
95 // UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
96
97 final JMapFrame frame = new JMapFrame(mapContext);
98 frame.enableStatusBar(true);
99 frame.enableToolBar(false);
100 frame.enableLayerTable(false);
101 frame.initComponents();
102
103 frame.setSize(800, 600);
104
105 final JMapPane mapPane = frame.getMapPane();
106 mapPane.setCursorTool(new VersatileZoomTool());
107
108 SwingUtilities.invokeLater(new Runnable() {
109 public void run() {
110 frame.setVisible(true);
111 }
112 });
113
114 // Create position type
115 SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
116 builder.setName("Position");
117 builder.setNamespaceURI("http://localhost/");
118 builder.setCRS(DefaultGeographicCRS.WGS84);
119
120 // add attributes in order
121 builder.add("Location", Point.class);
122 builder.add("ID", Integer.class);
123 builder.add("Name", String.class);
124
125 // build the type
126 final SimpleFeatureType POSITION = builder.buildFeatureType();
127
128 // PositionProvider positionProvider = new GpsBabelPositionProvider();
129
130 try {
131 // FeatureSource<SimpleFeatureType, SimpleFeature> fs =
132 // FileDataStoreFinder
133 // .getDataStore(new File(dir, "countries-EuroMed-NEarth.shp"))
134 // .getFeatureSource();
135
136 // mapContext.addLayer(FileDataStoreFinder.getDataStore(
137 // new File(dir, "cities-EuroMed-NEarth.shp")).getFeatureSource(),
138 // null);
139
140 // Raster
141 if (jaiImageIoClassLoader != null) {
142 Thread.currentThread().setContextClassLoader(
143 jaiImageIoClassLoader);
144 JAI.getDefaultInstance().getOperationRegistry()
145 .registerServices(
146 WorldFileReader.class.getClassLoader());
147 // OperationDescriptor odesc = (OperationDescriptor) JAI
148 // .getDefaultInstance().getOperationRegistry()
149 // .getDescriptor("rendered", "ImageRead");
150 }
151
152 // Raster style
153 StyleBuilder styleBuilder = new StyleBuilder();
154 RasterSymbolizer rasterSymbolizer = styleBuilder
155 .createRasterSymbolizer();
156 rasterSymbolizer.setGeometryPropertyName("geom");
157 Style rasterStyle = styleBuilder.createStyle(rasterSymbolizer);
158 WorldImageFormat worldImageFormat = new WorldImageFormat();
159
160 File rasterDir = new File("/home/mbaudier/gis/data/100501-Poehali");
161 mapContext.addLayer(worldImageFormat.getReader(
162 new File(rasterDir, "500k--l36-1--(1984).gif")).read(null),
163 rasterStyle);
164 mapContext.addLayer(worldImageFormat.getReader(
165 new File(rasterDir, "500k--l35-4--(1978).gif")).read(null),
166 rasterStyle);
167 mapContext.addLayer(worldImageFormat.getReader(
168 new File(rasterDir, "500k--l35-2--(1980).gif")).read(null),
169 rasterStyle);
170 mapContext.addLayer(worldImageFormat.getReader(
171 new File(rasterDir, "100k--l36-050--(1982).gif")).read(null),
172 rasterStyle);
173
174 mapContext.addLayer(FileDataStoreFinder.getDataStore(
175 new File(dir, "countries-EuroMed-NEarth.shp"))
176 .getFeatureSource(), null);
177 // mapContext.addLayer(FileDataStoreFinder.getDataStore(
178 // new File(dir, "highways-EastBalkan-OSM.shp"))
179 // .getFeatureSource(), null);
180 } catch (IOException e1) {
181 throw new SlcException("Cannot load sta stores", e1);
182 }
183
184 MapLayer mapLayer = null;
185 while (true) {
186 SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(
187 POSITION);
188
189 // add the attributes
190 featureBuilder.add(positionProvider.currentPosition());
191 featureBuilder.add(12);
192 featureBuilder.add("My Name");
193
194 // build the feature
195 SimpleFeature feature = featureBuilder.buildFeature("Flag.12");
196 FeatureCollection<SimpleFeatureType, SimpleFeature> collection = new DefaultFeatureCollection(
197 "testCollection", POSITION);
198 collection.add(feature);
199 if (mapLayer != null)
200 mapContext.removeLayer(mapLayer);
201 Style style = SLD.createSimpleStyle(POSITION, Color.RED);
202 mapLayer = new DefaultMapLayer(collection, style, "");
203 mapContext.addLayer(mapLayer);
204 // mapContext.addLayer(collection,null);
205
206 try {
207 Thread.sleep(1000);
208 } catch (InterruptedException e) {
209 e.printStackTrace();
210 }
211 }
212 }
213
214 public void setPositionProvider(PositionProvider positionProvider) {
215 this.positionProvider = positionProvider;
216 }
217
218 public void setJaiImageIoClassLoader(ClassLoader classLoader) {
219 this.jaiImageIoClassLoader = classLoader;
220 }
221
222 }