Commits

Frank Wynants committed 63688d0

Added random WFS style example

Comments (0)

Files changed (3)

gwt-openlayers-showcase/src/main/resources/org/gwtopenmaps/demo/openlayers/public/examples/cql/CqlExample.txt

+package org.gwtopenmaps.demo.openlayers.client.examples.cql;
+
+import javax.inject.Inject;
+
+import org.gwtopenmaps.demo.openlayers.client.basic.AbstractExample;
+import org.gwtopenmaps.demo.openlayers.client.components.store.ShowcaseExampleStore;
+import org.gwtopenmaps.openlayers.client.LonLat;
+import org.gwtopenmaps.openlayers.client.Map;
+import org.gwtopenmaps.openlayers.client.MapOptions;
+import org.gwtopenmaps.openlayers.client.MapWidget;
+import org.gwtopenmaps.openlayers.client.OpenLayers;
+import org.gwtopenmaps.openlayers.client.control.LayerSwitcher;
+import org.gwtopenmaps.openlayers.client.control.OverviewMap;
+import org.gwtopenmaps.openlayers.client.control.ScaleLine;
+import org.gwtopenmaps.openlayers.client.layer.TransitionEffect;
+import org.gwtopenmaps.openlayers.client.layer.WMS;
+import org.gwtopenmaps.openlayers.client.layer.WMSOptions;
+import org.gwtopenmaps.openlayers.client.layer.WMSParams;
+
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.event.dom.client.ClickEvent;
+import com.google.gwt.event.dom.client.ClickHandler;
+import com.google.gwt.user.client.ui.Button;
+import com.google.gwt.user.client.ui.HTML;
+import com.google.gwt.user.client.ui.HorizontalPanel;
+import com.google.gwt.user.client.ui.TextBox;
+
+public class CqlExample extends AbstractExample {
+
+    @Inject
+    public CqlExample(ShowcaseExampleStore store) {
+        super("WMS CQL filtering",
+              "Demonstrates the use of CQL to do filtering.",
+              new String[]{"WMS", "CQL", "filter"}, store);
+    }
+
+    @Override
+    public void buildPanel() {
+        OpenLayers.setProxyHost("olproxy?targetURL=");
+
+        //create some MapOptions
+        MapOptions defaultMapOptions = new MapOptions();
+        defaultMapOptions.setNumZoomLevels(16);
+
+        //Create a MapWidget
+        MapWidget mapWidget = new MapWidget("800px", "500px", defaultMapOptions);
+        //Create a WMS layer as base layer
+        final WMSParams wmsParams = new WMSParams();
+        wmsParams.setFormat("image/png");
+        wmsParams.setLayers("topp:states");
+        wmsParams.setStyles("");
+
+        WMSOptions wmsLayerParams = new WMSOptions();
+        wmsLayerParams.setUntiled();
+        wmsLayerParams.setTransitionEffect(TransitionEffect.RESIZE);
+
+        String wmsUrl = "http://demo.opengeo.org/geoserver/wms";
+
+        final WMS wmsLayer = new WMS("Basic WMS", wmsUrl, wmsParams, wmsLayerParams);
+
+        //Add the WMS to the map
+        Map map = mapWidget.getMap();
+        map.addLayer(wmsLayer);
+
+        //Lets add some default controls to the map
+        map.addControl(new LayerSwitcher()); //+ sign in the upperright corner to display the layer switcher
+        map.addControl(new OverviewMap()); //+ sign in the lowerright to display the overviewmap
+        map.addControl(new ScaleLine()); //Display the scaleline
+
+        //Center and zoom to a location
+        map.setCenter(new LonLat(-100, 40), 4);
+
+        //Create a textbox and a button for CQL
+        final TextBox txtCql = new TextBox();
+        txtCql.setText("STATE_ABBR = 'TX'");
+        Button butCql = new Button("Do CQL", new ClickHandler()
+        {
+
+            public void onClick(ClickEvent event)
+            {
+                wmsParams.setCQLFilter(txtCql.getText()); //set the CQL filter
+                wmsLayer.mergeNewParams(wmsParams); //and force the layer to reread its WMSParams
+            }
+        });
+
+        HorizontalPanel hpCql = new HorizontalPanel();
+        hpCql.setSpacing(3);
+        hpCql.add(txtCql);
+        hpCql.add(butCql);
+
+        contentPanel.add(
+                new HTML(
+                "<p>This example shows how to use a CQL filter on a WMS layer.</p><p>Enter the CQL filter in the textbox, and click the <b>'Do CQL'</b> button to execute the CQL. An example CQL you can execute is allready given in the textbox.</p>"));
+        contentPanel.add(hpCql);
+        contentPanel.add(mapWidget);
+
+        initWidget(contentPanel);
+
+        mapWidget.getElement().getFirstChildElement().getStyle().setZIndex(0); //force the map to fall behind popups
+    }
+
+    @Override
+    public String getSourceCodeURL() {
+        return GWT.getModuleBaseURL() + "examples/wmswfs/"
+                + "WmsWfsExample.txt";
+    }
+}

gwt-openlayers-showcase/src/main/resources/org/gwtopenmaps/demo/openlayers/public/examples/variablewfsstyle/VariableWfsStyle.txt

+package org.gwtopenmaps.demo.openlayers.client.examples.variablewfsstyle;
+
+import javax.inject.Inject;
+
+import org.gwtopenmaps.demo.openlayers.client.InfoPanel;
+import org.gwtopenmaps.demo.openlayers.client.basic.AbstractExample;
+import org.gwtopenmaps.demo.openlayers.client.components.store.ShowcaseExampleStore;
+import org.gwtopenmaps.openlayers.client.LonLat;
+import org.gwtopenmaps.openlayers.client.Map;
+import org.gwtopenmaps.openlayers.client.MapOptions;
+import org.gwtopenmaps.openlayers.client.MapWidget;
+import org.gwtopenmaps.openlayers.client.OpenLayers;
+import org.gwtopenmaps.openlayers.client.Style;
+import org.gwtopenmaps.openlayers.client.StyleMap;
+import org.gwtopenmaps.openlayers.client.control.LayerSwitcher;
+import org.gwtopenmaps.openlayers.client.control.OverviewMap;
+import org.gwtopenmaps.openlayers.client.control.ScaleLine;
+import org.gwtopenmaps.openlayers.client.event.LayerLoadEndListener;
+import org.gwtopenmaps.openlayers.client.feature.VectorFeature;
+import org.gwtopenmaps.openlayers.client.layer.TransitionEffect;
+import org.gwtopenmaps.openlayers.client.layer.Vector;
+import org.gwtopenmaps.openlayers.client.layer.VectorOptions;
+import org.gwtopenmaps.openlayers.client.layer.WMS;
+import org.gwtopenmaps.openlayers.client.layer.WMSOptions;
+import org.gwtopenmaps.openlayers.client.layer.WMSParams;
+import org.gwtopenmaps.openlayers.client.protocol.WFSProtocol;
+import org.gwtopenmaps.openlayers.client.protocol.WFSProtocolOptions;
+import org.gwtopenmaps.openlayers.client.strategy.BBoxStrategy;
+import org.gwtopenmaps.openlayers.client.strategy.Strategy;
+import org.gwtopenmaps.openlayers.client.util.Attributes;
+
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.event.dom.client.ClickEvent;
+import com.google.gwt.event.dom.client.ClickHandler;
+import com.google.gwt.user.client.Random;
+import com.google.gwt.user.client.ui.Button;
+import com.google.gwt.user.client.ui.HTML;
+
+public class VariableWfsStyle extends AbstractExample {
+
+    @Inject
+    public VariableWfsStyle(ShowcaseExampleStore store) {
+        super("Variable style WFS using attributes.",
+              "Demonstrates how the style of a WFS can be made variable using attributes.",
+              new String[]{"WFS", "style", "attribute", "variable"}, store);
+    }
+
+    @Override
+    public void buildPanel() {
+        OpenLayers.setProxyHost("olproxy?targetURL=");
+
+        //create some MapOptions
+        MapOptions defaultMapOptions = new MapOptions();
+        defaultMapOptions.setNumZoomLevels(16);
+
+        //Create a MapWidget
+        MapWidget mapWidget = new MapWidget("500px", "500px", defaultMapOptions);
+        //Create a WMS layer as base layer
+        WMSParams wmsParams = new WMSParams();
+        wmsParams.setFormat("image/png");
+        wmsParams.setLayers("topp:tasmania_state_boundaries");
+        wmsParams.setStyles("");
+
+        WMSOptions wmsLayerParams = new WMSOptions();
+        wmsLayerParams.setUntiled();
+        wmsLayerParams.setTransitionEffect(TransitionEffect.RESIZE);
+
+        String wmsUrl = "http://demo.opengeo.org/geoserver/wms";
+
+        WMS wmsLayer = new WMS("Basic WMS", wmsUrl, wmsParams, wmsLayerParams);
+
+        //Add the WMS to the map
+        Map map = mapWidget.getMap();
+        map.addLayer(wmsLayer);
+
+        //Create a WFS layer
+        WFSProtocolOptions wfsProtocolOptions = new WFSProtocolOptions();
+        wfsProtocolOptions.setUrl("http://demo.opengeo.org/geoserver/wfs");
+        wfsProtocolOptions.setFeatureType("tasmania_roads");
+        wfsProtocolOptions.setFeatureNameSpace("http://www.openplans.org/topp");
+        //if your wms is in a different projection use wfsProtocolOptions.setSrsName(LAMBERT72);
+
+        WFSProtocol wfsProtocol = new WFSProtocol(wfsProtocolOptions);
+
+        VectorOptions vectorOptions = new VectorOptions();
+        vectorOptions.setProtocol(wfsProtocol);
+        vectorOptions.setStrategies(new Strategy[]{new BBoxStrategy()});
+        //if your wms is in a different projection use vectorOptions.setProjection(LAMBERT72);
+
+        final Vector wfsLayer = new Vector("wfsExample", vectorOptions);
+        //give a style to the WFS layer
+        final String[] colors = new String[]{"blue", "red", "green"}; //just some possible colors
+        final double[] opacitys = new double[] {0.3, 0.6, 0.9}; //some possible opacity values
+
+
+        final Style style = new Style();
+        style.setStrokeWidth(3);
+        //set some variable styles using ${attributename}. Later we set an attribute on each Feature with the name defined here (see method randomizeStyles).
+        style.getJSObject().setProperty("strokeOpacity", "${variableopacity}"); // we can't use style.setStrokeOpacity(double d) cause this expects a double and "${variableopacity}" isn't a double
+        style.setStrokeColor("${variablecolor}");
+
+        wfsLayer.setStyleMap(new StyleMap(style));
+
+        wfsLayer.addLayerLoadEndListener(new LayerLoadEndListener()
+        {
+            public void onLoadEnd(LoadEndEvent eventObject)
+            {
+                randomizeStyles(wfsLayer, opacitys, colors); //initialize
+            }
+        });
+
+        final Button butGo = new Button("Randomize style !", new ClickHandler()
+        {
+            public void onClick(ClickEvent event)
+            {
+                randomizeStyles(wfsLayer, opacitys, colors);
+            }
+        });
+
+        map.addLayer(wfsLayer);
+
+        //Lets add some default controls to the map
+        map.addControl(new LayerSwitcher()); //+ sign in the upperright corner to display the layer switcher
+        map.addControl(new OverviewMap()); //+ sign in the lowerright to display the overviewmap
+        map.addControl(new ScaleLine()); //Display the scaleline
+
+        //Center and zoom to a location
+        map.setCenter(new LonLat(146.7, -41.8), 6);
+
+        contentPanel.add(
+                new HTML(
+                "<p>This example shows how to add a WMS layer and a WFS overlay to a map.</p>"));
+        contentPanel.add(
+                new InfoPanel(
+                "For WFS it is adviced to use a proxy to avoid cross reference problems. See the gwt-openlayers-server code for more info."));
+        contentPanel.add(butGo);
+        contentPanel.add(mapWidget);
+
+        initWidget(contentPanel);
+
+        mapWidget.getElement().getFirstChildElement().getStyle().setZIndex(0); //force the map to fall behind popups
+    }
+
+    /**
+     * Modify the attributes of the features to modify the styles (because the styles are defined with the same names as the attributes).
+     *
+     * @param wfsLayer The layer which we want to randomize
+     * @param opacitys Some opacity values we choose a random one from
+     * @param colors Some color value whe choose a random one from
+     */
+    private void randomizeStyles(Vector wfsLayer, double[] opacitys, String[] colors)
+    {
+        final VectorFeature[] features = wfsLayer.getFeatures();
+        for (VectorFeature feature : features)
+        {
+            final Attributes attr = feature.getAttributes();
+            String newOpacity = "" + opacitys[Random.nextInt(3)];
+            attr.setAttribute("variableopacity", "" + newOpacity);
+            attr.setAttribute("variablecolor", colors[Random.nextInt(3)]);
+        }
+        wfsLayer.redraw();
+
+    }
+
+    @Override
+    public String getSourceCodeURL() {
+        return GWT.getModuleBaseURL() + "examples/wmswfs/"
+                + "WmsWfsExample.txt";
+    }
+}

gwt-openlayers-showcase/src/main/resources/org/gwtopenmaps/demo/openlayers/public/examples/wmsfeatureinfo/WmsFeatureInfoExample.txt

 
     @Inject
     public WmsFeatureInfoExample(ShowcaseExampleStore store) {
-        super("WMS get featuretype example",
+        super("WMS get featureinfo example",
               "Demonstrates how to get feature information from a WMS layer.",
-              new String[]{"WMS", "feature", "featuretype", "get"}, store);
+              new String[]{"WMS", "feature", "featureinfo", "get", "info"}, store);
     }
 
     @Override