Geotools:将具有不同样式的点添加到同一个地图图层

时间:2016-06-18 23:14:05

标签: java geolocation geocoding geotools

我在我的java项目中使用GeoTools将不同大小,颜色和不透明度的点绘制到地图中。 由于他们有很多不同的风格,我在地图中最终得到了数千层。目前每个添加的点都有自己的图层,因为我没有设法将它们添加到一个单独的图层。

我从我的Entity对象中获取积分。来自同一实体的每个点都具有相同的颜色。有更多具有相同名称/颜色的实体。

这是在地图上绘制一个实体的方式:

enter image description here

点的大小是在以下代码段中动态计算的:

public class Stackoverflow {

private final MapContent mapContent;

public Stackoverflow() {
    this.mapContent = new MapContent();
}

public void addPoints(final HashMap<String, Color> colorsForEntities, final List<Entity> toDraw){
    final GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
    // Create SimpleFeatureType for Point
    final SimpleFeatureTypeBuilder pointTb = new SimpleFeatureTypeBuilder();
    pointTb.setName("points");
    pointTb.setCRS(DefaultGeographicCRS.WGS84);
    pointTb.add("point", Point.class);
    final SimpleFeatureBuilder pointFeatureBuilder = new SimpleFeatureBuilder(pointTb.buildFeatureType());

    for (final Entity entity : toDraw) {

        final List<Point2D> pathPoints = entity.getPoints(); 

        Color color = colorsForEntities.get(entity.getName());

        Layer layerPoints;
        Style pointStyle;


        final float maxOpacity = MyProperties.getFloat(Constants.POINT_STYLE_OPACITY);
        final float maxSize = MyProperties.getFloat(Constants.POINT_STYLE_SIZE);

        final int NumPoints = pathPoints.size();
        float opacity = maxOpacity;
        float size = maxSize;

        final float deltaOpacity = maxOpacity / NumPoints;
        final float deltaSize = maxSize / NumPoints;

        for (final Point2D point2D : pathPoints) {
            final Point point = geometryFactory.createPoint(new Coordinate(point2D.getX(), point2D.getY()));
            pointFeatureBuilder.add(point);

            final SimpleFeature feature = pointFeatureBuilder.buildFeature(null);
            final DefaultFeatureCollection pointCollection = new DefaultFeatureCollection();
            pointCollection.add(feature);

            pointStyle = SLD.createPointStyle("Circle", color, color, opacity, size);

            opacity = opacity - deltaOpacity;
            size = size - deltaSize;
            layerPoints = new FeatureLayer(pointCollection, pointStyle);
            mapContent.addLayer(layerPoints);
        }

    }
}

}

是否可以将点添加到单个图层或至少获得更少量的图层?

1 个答案:

答案 0 :(得分:1)

它可以更有效地完成:-)我认为最简单的方法是从SimpleFeature构建entity s,以便它们包含所需的颜色,大小和不透明度。类似的东西:

final SimpleFeatureTypeBuilder pointTb = new SimpleFeatureTypeBuilder();
pointTb.setName("points");
pointTb.setCRS(DefaultGeographicCRS.WGS84);
pointTb.add("point", Point.class);
pointTb.add("color", String.class);
pointTb.add("size", Integer.class);
pointTb.add("opacity", Float.class);
final SimpleFeatureBuilder pointFeatureBuilder = new SimpleFeatureBuilder(pointTb.buildFeatureType());

然后,您可以创建一个Style来获取这些属性并使用它们来设置每个点的样式。像(未经测试)的东西:

StyleBuilder sb = new StyleBuilder();
FilterFactory2 ff = sb.getFilterFactory();

Mark testMark = sb.createMark(sb.literalExpression("Circle"), 
    sb.createFill(sb.attributeExpression("color"),sb.attributeExpression("opacity")),
        null);
Graphic graph = sb.createGraphic(null, // An external graphics if needed
        new Mark[] { testMark }, // a Mark if not an external graphics
        null, // aSymbol
        ff.literal(sb.attributeExpression("opacity")), // opacity
        ff.property("size"), // read from feature "size" attribute
        ff.literal(0)); // rotation, here read into the feature
PointSymbolizer aPointSymbolizer = sb.createPointSymbolizer(graph);

// creation of the style
org.geotools.styling.Style style = sb.createStyle(aPointSymbolizer);

由于SLD不提供对循环的任何支持,我认为没有办法避免必须将每个entity分解为一组具有自己样式的单个点,除非你想写一个custom mark factory