view flys-client/src/main/java/de/intevation/flys/client/client/ui/map/FloodMap.java @ 882:d3b4010d2c89

I293: Set the style of digitized features in the map. flys-client/trunk@2713 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 13 Sep 2011 07:18:14 +0000
parents 4af1ee86b0b1
children 89a47098bcbd
line wrap: on
line source
package de.intevation.flys.client.client.ui.map;

import org.gwtopenmaps.openlayers.client.Bounds;
import org.gwtopenmaps.openlayers.client.Map;
import org.gwtopenmaps.openlayers.client.MapOptions;
import org.gwtopenmaps.openlayers.client.MapWidget;
import org.gwtopenmaps.openlayers.client.Style;
import org.gwtopenmaps.openlayers.client.event.VectorFeatureAddedListener;
import org.gwtopenmaps.openlayers.client.event.VectorFeatureAddedListener.FeatureAddedEvent;
import org.gwtopenmaps.openlayers.client.feature.VectorFeature;
import org.gwtopenmaps.openlayers.client.format.GeoJSON;
import org.gwtopenmaps.openlayers.client.layer.Vector;
import org.gwtopenmaps.openlayers.client.layer.VectorOptions;
import org.gwtopenmaps.openlayers.client.util.Attributes;
import org.gwtopenmaps.openlayers.client.util.JObjectArray;
import org.gwtopenmaps.openlayers.client.util.JSObject;


public class FloodMap implements VectorFeatureAddedListener {

    public static final String LAYER_BARRIERS = "vector_layer_barriers";

    protected MapWidget mapWidget;
    protected Map       map;
    protected Vector    barrierLayer;
    protected String    srid;
    protected Bounds    maxExtent;


    public FloodMap(String srid, Bounds maxExtent) {
        this.srid      = srid;
        this.maxExtent = maxExtent;

        MapOptions opts = new MapOptions();
        opts.setControls(new JObjectArray(new JSObject[] {}));
        opts.setNumZoomLevels(16);
        opts.setProjection(getRiverProjection());
        opts.setMaxExtent(maxExtent);
        opts.setUnits("m");
        opts.setMaxResolution(500); // TODO DO THIS ON THE FLY

        mapWidget = new MapWidget("510px", "635px", opts);
        map       = mapWidget.getMap();

        getBarrierLayer();
    }


    @Override
    public void onFeatureAdded(FeatureAddedEvent evt) {
        VectorFeature feature = evt.getVectorFeature();

        Attributes attrs = feature.getAttributes();
        String     type  = attrs.getAttributeAsString("typ");

        if (type == null || type.length() == 0) {
            return;
        }

        feature.setStyle(getStyle(type));

        // necessary, otherwise the setStyle() has no effect
        barrierLayer.redraw();
    }


    /**
     * Returns an OpenLayers.Style based on a given type.
     *
     * @param type Type can be one of "pipe1", "pipe2", "ditch", "dam",
     * "ringdike".
     *
     * @return an OpenLayers.Style object.
     */
    public static Style getStyle(String type) {
        Style style = new Style();

        if (type.equals(DrawControl.BARRIER_PIPE1)
            || type.equals(DrawControl.BARRIER_PIPE1_VALUE)
        ) {
            style.setFillColor("#800080");
            style.setStrokeColor("#800080");
        }
        else if (type.equals(DrawControl.BARRIER_PIPE2)
              || type.equals(DrawControl.BARRIER_PIPE2_VALUE)
        ) {
            style.setFillColor("#808080");
            style.setStrokeColor("#808080");
        }
        else if (type.equals(DrawControl.BARRIER_DAM)
              || type.equals(DrawControl.BARRIER_DAM_VALUE)
        ) {
            style.setFillColor("#008000");
            style.setStrokeColor("#008000");
        }
        else if (type.equals(DrawControl.BARRIER_DITCH)
              || type.equals(DrawControl.BARRIER_DITCH_VALUE)
        ) {
            style.setFillColor("#800000");
            style.setStrokeColor("#800000");
        }
        else if (type.equals(DrawControl.BARRIER_RINGDIKE)
              || type.equals(DrawControl.BARRIER_RINGDIKE_VALUE)
        ) {
            style.setFill(false);
            style.setStrokeColor("#FF8000");
        }

        return style;
    }


    public MapWidget getMapWidget() {
        return mapWidget;
    }


    public Map getMap() {
        return map;
    }


    public String getRiverProjection() {
        return "EPSG:" + srid;
    }


    public Vector getBarrierLayer() {
        if (barrierLayer == null) {
            VectorOptions opts = new VectorOptions();
            opts.setProjection(getRiverProjection());
            opts.setMaxExtent(maxExtent);

            barrierLayer = new Vector(LAYER_BARRIERS, opts);
            barrierLayer.setIsBaseLayer(true);

            map.addLayer(barrierLayer);

            barrierLayer.addVectorFeatureAddedListener(this);
        }

        return barrierLayer;
    }


    public String getFeaturesAsGeoJSON() {
        VectorFeature[] features = barrierLayer.getFeatures();

        if (features == null || features.length == 0) {
            return null;
        }

        return new GeoJSON().write(features);
    }


    public void setSize(String width, String height) {
        mapWidget.setWidth(width);
        mapWidget.setHeight(height);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org