Hi Brian,

It is possible to have different types of Geometry objects in
SimpleFeatures and have them display in a single layer.  The code
below demonstrates drawing a few points and lines this way.  Not all
data sources will support mixed Geometry types in a single attribute
though. I don't think shapefiles do (though I might be wrong about
that).

Whether you choose to work with a single MapLayer or one layer per
Geometry type depends on your own requirements.

Michael

PS. the code below was quickly put together with GeoTools 2.7 (trunk
sources).  To get it to work with GeoTools 2.6.x just replace
SimpleFeeatureCollection with FeatureCollection<SimpleFeatureType,
SimpleFeature>


package org.geotools.demo;

import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.io.WKTReader;
import java.awt.Color;
import org.geotools.data.DataUtilities;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.feature.FeatureCollections;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.map.DefaultMapContext;
import org.geotools.map.MapContext;
import org.geotools.styling.FeatureTypeStyle;
import org.geotools.styling.Graphic;
import org.geotools.styling.Rule;
import org.geotools.styling.Stroke;
import org.geotools.styling.Style;
import org.geotools.styling.StyleFactory;
import org.geotools.styling.Symbolizer;
import org.geotools.swing.JMapFrame;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.Filter;
import org.opengis.filter.FilterFactory2;
import org.opengis.filter.expression.Function;

/*
 * Creates some features having either Point or LineString as the
Geometry and displays them
 * in a single MapLayer
 */
public class LinesAndPoints {

    public static void main(String[] args) throws Exception {
        new LinesAndPoints().displayFeatures();
    }

    private void displayFeatures() throws Exception {
        SimpleFeatureCollection features = createFeatureCollection();
        Style style = createStyle("shape");
        MapContext map = new DefaultMapContext();
        map.addLayer(features, style);
        JMapFrame.showMap(map);
    }

    private SimpleFeatureCollection createFeatureCollection() throws Exception {
        SimpleFeatureCollection fc = FeatureCollections.newCollection();
        SimpleFeatureType TYPE = DataUtilities.createType("testtype",
"shape:Geometry, name:String");
        SimpleFeatureBuilder fb = new SimpleFeatureBuilder(TYPE);

        Geometry g;
        WKTReader reader = new WKTReader();

        g = reader.read("POINT(1 1)");
        fc.add(fb.buildFeature(null, new Object[]{g, "p1"}));

        g = reader.read("POINT(2 2)");
        fc.add(fb.buildFeature(null, new Object[]{g, "p2"}));

        g = reader.read("POINT(3 3)");
        fc.add(fb.buildFeature(null, new Object[]{g, "p3"}));

        g = reader.read("POINT(4 4)");
        fc.add(fb.buildFeature(null, new Object[]{g, "p4"}));

        g = reader.read("LINESTRING(1 2, 4 5)");
        fc.add(fb.buildFeature(null, new Object[]{g, "line1"}));

        g = reader.read("LINESTRING(1 0, 4 3)");
        fc.add(fb.buildFeature(null, new Object[]{g, "line1"}));

        return fc;
    }

    private Style createStyle(String geomPropertyName) {
        StyleFactory sf = CommonFactoryFinder.getStyleFactory(null);
        FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(null);

        Rule pointRule = sf.createRule();
        Function getGeomFn = ff.function("geometryType",
ff.property(geomPropertyName));
        Filter pointFilter = ff.equals(getGeomFn, ff.literal("Point"));
        pointRule.setFilter(pointFilter);
        pointRule.setElseFilter(false);

        Graphic graphic = sf.getDefaultGraphic();
        graphic.graphicalSymbols().clear();
        graphic.graphicalSymbols().add(sf.getCircleMark());
        Symbolizer sym = sf.createPointSymbolizer(graphic, geomPropertyName);
        pointRule.symbolizers().add(sym);

        Rule lineRule = sf.createRule();
        lineRule.setIsElseFilter(true);
        Stroke stroke = sf.createStroke(ff.literal(Color.BLUE),
ff.literal(1.0f));
        Symbolizer lineSym = sf.createLineSymbolizer(stroke, geomPropertyName);
        lineRule.symbolizers().add(lineSym);

        FeatureTypeStyle fts = sf.createFeatureTypeStyle(new
Rule[]{pointRule, lineRule});
        Style style = sf.createStyle();
        style.featureTypeStyles().add(fts);
        return style;
    }
}

------------------------------------------------------------------------------

_______________________________________________
Geotools-gt2-users mailing list
Geotools-gt2-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to