001: package org.geotools.demo;
002:
003: import java.awt.BorderLayout;
004: import java.awt.Color;
005: import java.awt.event.ActionEvent;
006: import java.awt.event.ActionListener;
007: import java.io.File;
008: import java.io.FileNotFoundException;
009: import java.io.IOException;
010: import java.net.MalformedURLException;
011:
012: import javax.swing.JButton;
013: import javax.swing.JFileChooser;
014: import javax.swing.JFrame;
015: import javax.swing.JOptionPane;
016: import javax.swing.JPanel;
017: import javax.swing.WindowConstants;
018: import javax.swing.filechooser.FileFilter;
019:
020: import org.geotools.data.FeatureSource;
021: import org.geotools.data.shapefile.ShapefileDataStore;
022: import org.geotools.factory.CommonFactoryFinder;
023: import org.geotools.feature.FeatureType;
024: import org.geotools.gui.swing.JMapPane;
025: import org.geotools.map.DefaultMapContext;
026: import org.geotools.map.MapContext;
027: import org.geotools.renderer.lite.StreamingRenderer;
028: import org.geotools.styling.FeatureTypeStyle;
029: import org.geotools.styling.Fill;
030: import org.geotools.styling.LineSymbolizer;
031: import org.geotools.styling.PointSymbolizer;
032: import org.geotools.styling.PolygonSymbolizer;
033: import org.geotools.styling.Rule;
034: import org.geotools.styling.SLD;
035: import org.geotools.styling.SLDParser;
036: import org.geotools.styling.Style;
037: import org.geotools.styling.StyleFactory;
038: import org.geotools.styling.Symbolizer;
039: import org.opengis.filter.FilterFactory;
040: import org.opengis.referencing.crs.CoordinateReferenceSystem;
041:
042: import com.vividsolutions.jts.geom.LineString;
043: import com.vividsolutions.jts.geom.MultiLineString;
044: import com.vividsolutions.jts.geom.MultiPolygon;
045: import com.vividsolutions.jts.geom.Polygon;
046:
047: public class ShapeLab {
048:
049: static StyleFactory styleFactory = CommonFactoryFinder
050: .getStyleFactory(null);
051: static FilterFactory filterFactory = CommonFactoryFinder
052: .getFilterFactory(null);
053:
054: /**
055: * Prompt the user for a file and open up ImageLab.
056: *
057: * @param args
058: * filename of image
059: */
060: public static void main(String[] args) throws Exception {
061: File file = getShapeFile(args);
062:
063: ShapefileDataStore shapefile = new ShapefileDataStore(file
064: .toURL());
065: String typeName = shapefile.getTypeNames()[0];
066: FeatureSource featureSource = shapefile.getFeatureSource();
067: FeatureType schema = featureSource.getSchema();
068: CoordinateReferenceSystem crs = schema.getDefaultGeometry()
069: .getCoordinateSystem();
070:
071: MapContext map = new DefaultMapContext(crs);
072: Style style = createStyle(file, schema);
073: map.addLayer(featureSource, style);
074:
075: showMap(map);
076: }
077:
078: private static Style createStyle(File file, FeatureType schema) {
079: File sld = toSLDFile(file);
080: if (sld.exists()) {
081: return createFromSLD(sld);
082: }
083: Class type = schema.getDefaultGeometry().getBinding();
084: if (type.isAssignableFrom(Polygon.class)
085: || type.isAssignableFrom(MultiPolygon.class)) {
086: return createPolygonStyle();
087: } else if (type.isAssignableFrom(LineString.class)
088: || type.isAssignableFrom(MultiLineString.class)) {
089: return createLineStyle();
090: } else {
091: return createPointStyle();
092: }
093: }
094:
095: private static Style createFromSLD(File sld) {
096: SLDParser stylereader;
097: try {
098: stylereader = new SLDParser(styleFactory, sld.toURL());
099: Style[] style = stylereader.readXML();
100: return style[0];
101: } catch (Exception e) {
102: JOptionPane.showMessageDialog(null, e.getMessage());
103: System.exit(0);
104: }
105: return null;
106: }
107:
108: private static Style createPointStyle() {
109: Style style;
110: PointSymbolizer symbolizer = styleFactory
111: .createPointSymbolizer();
112: symbolizer.getGraphic().setSize(filterFactory.literal(1));
113: Rule rule = styleFactory.createRule();
114: rule.setSymbolizers(new Symbolizer[] { symbolizer });
115: FeatureTypeStyle fts = styleFactory.createFeatureTypeStyle();
116: fts.setRules(new Rule[] { rule });
117: style = styleFactory.createStyle();
118: style.addFeatureTypeStyle(fts);
119: return style;
120: }
121:
122: private static Style createLineStyle() {
123: Style style;
124:
125: LineSymbolizer symbolizer = styleFactory.createLineSymbolizer();
126: SLD.setLineColour(symbolizer, Color.BLUE);
127: symbolizer.getStroke().setWidth(filterFactory.literal(1));
128: symbolizer.getStroke().setColor(
129: filterFactory.literal(Color.BLUE));
130:
131: Rule rule = styleFactory.createRule();
132: rule.setSymbolizers(new Symbolizer[] { symbolizer });
133: FeatureTypeStyle fts = styleFactory.createFeatureTypeStyle();
134: fts.setRules(new Rule[] { rule });
135: style = styleFactory.createStyle();
136: style.addFeatureTypeStyle(fts);
137: return style;
138: }
139:
140: private static Style createPolygonStyle() {
141: Style style;
142: PolygonSymbolizer symbolizer = styleFactory
143: .createPolygonSymbolizer();
144: Fill fill = styleFactory.createFill(filterFactory
145: .literal("#FFAA00"), filterFactory.literal(0.5));
146: symbolizer.setFill(fill);
147: Rule rule = styleFactory.createRule();
148: rule.setSymbolizers(new Symbolizer[] { symbolizer });
149: FeatureTypeStyle fts = styleFactory.createFeatureTypeStyle();
150: fts.setRules(new Rule[] { rule });
151: style = styleFactory.createStyle();
152: style.addFeatureTypeStyle(fts);
153: return style;
154: }
155:
156: private static void showMap(MapContext map) throws IOException {
157: final JMapPane mapPane = new JMapPane(new StreamingRenderer(),
158: map);
159: mapPane.setMapArea(map.getLayerBounds());
160: JFrame frame = new JFrame("ImageLab2");
161:
162: frame.setLayout(new BorderLayout());
163: frame.add(mapPane, BorderLayout.CENTER);
164: JPanel buttons = new JPanel();
165: JButton zoomInButton = new JButton("Zoom In");
166: zoomInButton.addActionListener(new ActionListener() {
167: public void actionPerformed(ActionEvent e) {
168: mapPane.setState(JMapPane.ZoomIn);
169: }
170: });
171: buttons.add(zoomInButton);
172:
173: JButton zoomOutButton = new JButton("Zoom Out");
174: zoomOutButton.addActionListener(new ActionListener() {
175: public void actionPerformed(ActionEvent e) {
176: mapPane.setState(JMapPane.ZoomOut);
177: }
178: });
179: buttons.add(zoomOutButton);
180:
181: JButton pamButton = new JButton("Move");
182: pamButton.addActionListener(new ActionListener() {
183: public void actionPerformed(ActionEvent e) {
184: mapPane.setState(JMapPane.Pan);
185: }
186: });
187: buttons.add(pamButton);
188:
189: frame.add(buttons, BorderLayout.NORTH);
190:
191: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
192: frame.setSize(600, 400);
193: frame.setVisible(true);
194: }
195:
196: private static File getShapeFile(String[] args)
197: throws FileNotFoundException {
198: File file;
199: if (args.length == 0) {
200: JFileChooser chooser = new JFileChooser();
201: chooser.setDialogTitle("Open Shapefile for Reprojection");
202: chooser.setFileFilter(new FileFilter() {
203: public boolean accept(File f) {
204: return f.isDirectory()
205: || f.getPath().endsWith("shp")
206: || f.getPath().endsWith("SHP");
207: }
208:
209: public String getDescription() {
210: return "Shapefiles";
211: }
212: });
213: int returnVal = chooser.showOpenDialog(null);
214:
215: if (returnVal != JFileChooser.APPROVE_OPTION) {
216: System.exit(0);
217: }
218: file = chooser.getSelectedFile();
219:
220: System.out.println("You chose to open this file: "
221: + file.getName());
222: } else {
223: file = new File(args[0]);
224: }
225: if (!file.exists()) {
226: throw new FileNotFoundException(file.getAbsolutePath());
227: }
228: return file;
229: }
230:
231: /** Figure out the URL for the "sld" file */
232: public static File toSLDFile(File file) {
233: String filename = file.getAbsolutePath();
234: if (filename.endsWith(".shp") || filename.endsWith(".dbf")
235: || filename.endsWith(".shx")) {
236: filename = filename.substring(0, filename.length() - 4);
237: filename += ".sld";
238: } else if (filename.endsWith(".SLD")
239: || filename.endsWith(".SLD")
240: || filename.endsWith(".SLD")) {
241: filename = filename.substring(0, filename.length() - 4);
242: filename += ".SLD";
243: }
244: return new File(filename);
245: }
246: }
|