2 * GeoTools - The Open Source Java GIS Tookit
5 * (C) 2006-2008, Open Source Geospatial Foundation (OSGeo)
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!
10 package org
.argeo
.slc
.geotools
;
12 import java
.awt
.Color
;
14 import java
.io
.IOException
;
15 import java
.util
.Iterator
;
17 import javax
.media
.jai
.JAI
;
18 import javax
.swing
.SwingUtilities
;
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
;
47 import com
.vividsolutions
.jts
.geom
.Point
;
50 * GeoTools Quickstart demo application. Prompts the user for a shapefile and
51 * displays its contents on the screen in a map frame
54 * http://svn.osgeo.org/geotools/trunk/demo/example/src/main/java/org
55 * /geotools/demo/Quickstart.java $
57 public class SimpleGisFieldViewer
implements Runnable
{
58 private final static Log log
= LogFactory
59 .getLog(SimpleGisFieldViewer
.class);
61 private PositionProvider positionProvider
;
63 private ClassLoader jaiImageIoClassLoader
;
65 public static void main(String
[] args
) throws Exception
{
66 new SimpleGisFieldViewer().run();
70 Iterator
<FileDataStoreFactorySpi
> ps
= FileDataStoreFinder
71 .getAvailableDataStores();
72 log
.debug("Available datastores:");
73 while (ps
.hasNext()) {
77 // display a data store file chooser dialog for shapefiles
78 // File file = JFileDataStoreChooser.showOpenFile("shp", null);
79 // if (file == null) {
83 "/home/mbaudier/gis/projects/100122-EasternBalkans2010/data");
85 // FeatureSource<SimpleFeatureType, SimpleFeature> featureSource =
86 // FileDataStoreFinder
87 // .getDataStore(new File(dir, "countries-EuroMed-NEarth.shp"))
88 // .getFeatureSource();
90 // Create a map context and add our shapefile to it
91 MapContext mapContext
= new DefaultMapContext();
92 mapContext
.setTitle("Quickstart");
94 // Now display the map
95 // UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
97 final JMapFrame frame
= new JMapFrame(mapContext
);
98 frame
.enableStatusBar(true);
99 frame
.enableToolBar(false);
100 frame
.enableLayerTable(false);
101 frame
.initComponents();
103 frame
.setSize(800, 600);
105 final JMapPane mapPane
= frame
.getMapPane();
106 mapPane
.setCursorTool(new VersatileZoomTool());
108 SwingUtilities
.invokeLater(new Runnable() {
110 frame
.setVisible(true);
114 // Create position type
115 SimpleFeatureTypeBuilder builder
= new SimpleFeatureTypeBuilder();
116 builder
.setName("Position");
117 builder
.setNamespaceURI("http://localhost/");
118 builder
.setCRS(DefaultGeographicCRS
.WGS84
);
120 // add attributes in order
121 builder
.add("Location", Point
.class);
122 builder
.add("ID", Integer
.class);
123 builder
.add("Name", String
.class);
126 final SimpleFeatureType POSITION
= builder
.buildFeatureType();
128 // PositionProvider positionProvider = new GpsBabelPositionProvider();
131 // FeatureSource<SimpleFeatureType, SimpleFeature> fs =
132 // FileDataStoreFinder
133 // .getDataStore(new File(dir, "countries-EuroMed-NEarth.shp"))
134 // .getFeatureSource();
136 // mapContext.addLayer(FileDataStoreFinder.getDataStore(
137 // new File(dir, "cities-EuroMed-NEarth.shp")).getFeatureSource(),
141 if (jaiImageIoClassLoader
!= null) {
142 Thread
.currentThread().setContextClassLoader(
143 jaiImageIoClassLoader
);
144 JAI
.getDefaultInstance().getOperationRegistry()
146 WorldFileReader
.class.getClassLoader());
147 // OperationDescriptor odesc = (OperationDescriptor) JAI
148 // .getDefaultInstance().getOperationRegistry()
149 // .getDescriptor("rendered", "ImageRead");
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();
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),
164 mapContext
.addLayer(worldImageFormat
.getReader(
165 new File(rasterDir
, "500k--l35-4--(1978).gif")).read(null),
167 mapContext
.addLayer(worldImageFormat
.getReader(
168 new File(rasterDir
, "500k--l35-2--(1980).gif")).read(null),
170 mapContext
.addLayer(worldImageFormat
.getReader(
171 new File(rasterDir
, "100k--l36-050--(1982).gif")).read(null),
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
);
184 MapLayer mapLayer
= null;
186 SimpleFeatureBuilder featureBuilder
= new SimpleFeatureBuilder(
189 // add the attributes
190 featureBuilder
.add(positionProvider
.currentPosition());
191 featureBuilder
.add(12);
192 featureBuilder
.add("My Name");
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);
208 } catch (InterruptedException e
) {
214 public void setPositionProvider(PositionProvider positionProvider
) {
215 this.positionProvider
= positionProvider
;
218 public void setJaiImageIoClassLoader(ClassLoader classLoader
) {
219 this.jaiImageIoClassLoader
= classLoader
;