001: package org.geotools.demo.example;
002:
003: import java.util.HashMap;
004: import java.util.Map;
005: import java.util.logging.Level;
006: import java.util.logging.Logger;
007:
008: import javax.swing.JFrame;
009: import javax.swing.WindowConstants;
010:
011: import org.geotools.data.DataStore;
012: import org.geotools.data.DataStoreFinder;
013: import org.geotools.data.DefaultQuery;
014: import org.geotools.data.FeatureSource;
015: import org.geotools.factory.CommonFactoryFinder;
016: import org.geotools.factory.GeoTools;
017: import org.geotools.gui.swing.JMapPane;
018: import org.geotools.map.DefaultMapContext;
019: import org.geotools.map.MapContext;
020: import org.geotools.referencing.crs.DefaultGeographicCRS;
021: import org.geotools.renderer.GTRenderer;
022: import org.geotools.renderer.lite.StreamingRenderer;
023: import org.geotools.styling.FeatureTypeStyle;
024: import org.geotools.styling.LineSymbolizer;
025: import org.geotools.styling.Rule;
026: import org.geotools.styling.Stroke;
027: import org.geotools.styling.Style;
028: import org.geotools.styling.StyleFactory;
029: import org.geotools.styling.Symbolizer;
030: import org.opengis.filter.Filter;
031: import org.opengis.filter.FilterFactory2;
032:
033: /**
034: * This example also works against a local geoserver.
035: *
036: * @author Jody Garnett
037: */
038: public class SLDExample {
039:
040: public static void main(String args[]) {
041: try {
042: //supressInfo();
043: localSLD();
044: } catch (Exception ignore) {
045: ignore.printStackTrace();
046: }
047: }
048:
049: public static void supressInfo() {
050: org.geotools.util.logging.Logging.getLogger("org.geotools.gml")
051: .setLevel(Level.SEVERE);
052: org.geotools.util.logging.Logging.getLogger(
053: "net.refractions.xml").setLevel(Level.SEVERE);
054: }
055:
056: public static void localSLD() throws Exception {
057: FeatureSource source = demoFeatureSource();
058: Style style = demoStyle(source.getSchema().getTypeName());
059:
060: show(source, style);
061: }
062:
063: static FeatureSource demoFeatureSource() throws Exception {
064: String getCapabilities = "http://localhost:8080/geoserver/wfs?service=WFS&request=GetCapabilities";
065:
066: Map connectionParameters = new HashMap();
067: connectionParameters.put(
068: "WFSDataStoreFactory:GET_CAPABILITIES_URL",
069: getCapabilities);
070:
071: DataStore data = DataStoreFinder
072: .getDataStore(connectionParameters);
073: String typeName = data.getTypeNames()[0];
074: FeatureSource fs = data.getFeatureSource(typeName);
075: System.out.println(fs.getFeatures().size());
076: return fs;
077: }
078:
079: static Style demoStyle(String typeName) throws Exception {
080: StyleFactory sf = CommonFactoryFinder.getStyleFactory(GeoTools
081: .getDefaultHints());
082: FilterFactory2 ff = CommonFactoryFinder
083: .getFilterFactory2(GeoTools.getDefaultHints());
084:
085: Stroke stroke = sf.createStroke(ff.literal("#FF0000"), ff
086: .literal(2));
087:
088: LineSymbolizer lineSymbolizer = sf.createLineSymbolizer();
089: lineSymbolizer.setStroke(stroke);
090:
091: Rule rule = sf.createRule();
092: rule.setFilter(Filter.INCLUDE);
093: rule.setSymbolizers(new Symbolizer[] { lineSymbolizer });
094:
095: FeatureTypeStyle type = sf.createFeatureTypeStyle();
096: type.setFeatureTypeName("Feature");
097: type.addRule(rule);
098:
099: Style style = sf.createStyle();
100: style.addFeatureTypeStyle(type);
101:
102: return style;
103: }
104:
105: public static void show(FeatureSource source, Style style)
106: throws Exception {
107: JFrame frame = new JFrame("FOSS4G");
108: frame.setBounds(20, 20, 450, 200);
109: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
110:
111: JMapPane mp = new JMapPane();
112: frame.getContentPane().add(mp);
113: mp.setMapArea(source.getBounds());
114:
115: MapContext context = new DefaultMapContext(
116: DefaultGeographicCRS.WGS84);
117: context.setAreaOfInterest(source.getBounds(),
118: DefaultGeographicCRS.WGS84);
119: context.addLayer(source, style);
120: //context.getLayerBounds();
121:
122: GTRenderer renderer = new StreamingRenderer();
123: // HashMap hints = new HashMap();
124: // hints.put("memoryPreloadingEnabled", Boolean.TRUE);
125: // renderer.setRendererHints( hints );
126:
127: mp.setRenderer(renderer);
128: mp.setContext(context);
129:
130: frame.setVisible(true);
131: }
132: }
|