Hi jv,

Below is some demo code that illustrates:

- creating a common feature type with point geometry for location and
an attribute for species status

- creating features and adding them to a feature collection

- creating a rendering style to use different symbols depending on
status value of a feature

- creating a map and displaying it with the JMapFrame.showMap static method

To run this code right now you will need to be using the bleeding edge
GeoTools: 2.6-SNAPSHOT.

Give us some feedback and we can turn more of this into examples and
docs for the GeoTools site.

Michael

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
import java.awt.Color;
import java.util.HashMap;
import java.util.Map;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureCollections;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.map.DefaultMapContext;
import org.geotools.map.DefaultMapLayer;
import org.geotools.map.MapContext;
import org.geotools.map.MapLayer;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.geotools.styling.FeatureTypeStyle;
import org.geotools.styling.Graphic;
import org.geotools.styling.Mark;
import org.geotools.styling.PointSymbolizer;
import org.geotools.styling.Rule;
import org.geotools.styling.Style;
import org.geotools.styling.StyleBuilder;
import org.geotools.swing.JMapFrame;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.FilterFactory;
import org.opengis.referencing.crs.CoordinateReferenceSystem;


/**
 * Demonstrates creating a feature type with a field that will be used to
 * control point symbols for rendering the features
 */

public class SpeciesStatus {

    public enum Status {
        DISAPPEARED(1, Color.BLACK),
        SURE(2, Color.GREEN),
        UNSURE(3, Color.CYAN),
        UNKNOWN(4, Color.GRAY);

        /* a map for reverse lookup */
        private static Map<Integer, Status> lookup;
        static {
            lookup = new HashMap<Integer, Status>();
            for (Status status : Status.values()) {
                lookup.put(status.getCode(), status);
            }
        }

        private int code;
        private Color color;

        private Status(int code, Color color) {
            this.code = code;
            this.color = color;
        }

        public int getCode() {
            return code;
        }

        public Color getColor() {
            return color;
        }

        public static Status get(int code) {
            return lookup.get(code);
        }
    }

    private CoordinateReferenceSystem crs;
    private SimpleFeatureType featureType;
    private SimpleFeatureBuilder featureBuilder;
    private GeometryFactory geomFactory;

    private FeatureCollection<SimpleFeatureType, SimpleFeature> featureColl;
    private Style style;


    public SpeciesStatus(CoordinateReferenceSystem crs) {
        this.crs = crs;
        this.featureColl = FeatureCollections.newCollection();

        createFeatureType();
        createStyle();
    }

    private void createFeatureType() {
        SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
        builder.setName("species_rec");
        builder.setCRS(crs);
        builder.add("the_geom", Point.class);  // geometry field
        builder.add("speciesId", String.class);  // alpha-numeric ID for species
        builder.add("status", Integer.class);  // integer status code

        featureType =  builder.buildFeatureType();
    }

    /**
     * Create a Style with a Rule for each of the Status values
     * that we want to render as different symbols
     */
    public void createStyle() {
        FilterFactory ff = CommonFactoryFinder.getFilterFactory2(null);
        StyleBuilder sb = new StyleBuilder();

        Rule[] rules = new Rule[Status.values().length];

        int k = 0;
        for (Status status : Status.values()) {

            Graphic graphic = sb.createGraphic();

            // remove the default symbol (a bit clunky)
            graphic.graphicalSymbols().clear();

            // create a new symbol
            Mark mark = sb.createMark(StyleBuilder.MARK_CIRCLE,
status.getColor());
            graphic.graphicalSymbols().add(mark);

            // our point symbolizer to use this symbol
            PointSymbolizer sym = sb.createPointSymbolizer(graphic);

            // a rule that will select features to render with this status value
            Rule rule = sb.createRule(sym);
            rule.setFilter(ff.equals(ff.property("status"),
ff.literal(status.getCode())));
            rules[k++] = rule;
        }

        // put all our rules together in a FeatureTypeStyle
        FeatureTypeStyle fts =
sb.createFeatureTypeStyle(featureType.getTypeName(), rules);

        this.style = sb.createStyle();
        style.featureTypeStyles().add(fts);
    }

    /**
     * Add a new feature to out collection
     * @param speciesId alpha-numeric species Id
     * @param x location X coord (world)
     * @param y location Y coord (world)
     * @param status the species status category
     */
    public void addFeature(String speciesId, double x, double y,
Status status) {
        if (geomFactory == null) {
            geomFactory = new GeometryFactory();
        }

        if (featureBuilder == null) {
            featureBuilder = new SimpleFeatureBuilder(featureType);
        }

        Geometry pos = geomFactory.createPoint(new Coordinate(x, y));

        featureBuilder.add(pos);
        featureBuilder.add(speciesId);
        featureBuilder.add(status.getCode());

        SimpleFeature feature = featureBuilder.buildFeature(null);
        featureColl.add(feature);
    }

    /**
     * Run the demo (args are ignored)
     */
    public static void main(String[] args) {
        SpeciesStatus me = new SpeciesStatus(DefaultGeographicCRS.WGS84);
        me.demo();
    }

    /**
     * Demo: creates a few features with different Status values
     * and adds them to the FeaureCollection. Then displays them
     * as a map.
     */
    private void demo() {
        addFeature("lynx", 2.0, 41.0, Status.UNKNOWN);
        addFeature("bear", 3.0, 42.0, Status.SURE);
        addFeature("bear", 3.5, 41.5, Status.DISAPPEARED);
        addFeature("yeti", 2.5, 42.5, Status.UNSURE);

        MapContext map = new DefaultMapContext(crs);
        map.setTitle("Species status");

        MapLayer layer = new DefaultMapLayer(featureColl, style);
        map.addLayer(layer);

        JMapFrame.showMap(map);
    }

}

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to