【问题标题】:Geotools: Add points with different styles to same map layerGeotools:将不同样式的点添加到同一地图图层
【发布时间】:2016-06-18 23:14:04
【问题描述】:

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

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

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

点的大小在下面的代码sn-p中动态计算:

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);
        }

    }
}

}

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

【问题讨论】:

    标签: java geolocation geocoding geotools


    【解决方案1】:

    它可以更有效地完成:-) 我认为最简单的方法是从entitys 构建您的SimpleFeatures,以便它们包含所需的颜色、大小和不透明度。比如:

    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 分解为具有自己样式的一组单独的点,除非你想写一个 @ 987654321@.

    【讨论】:

    • 链接失效。我们可以在不重新加载地图的情况下将样式应用于地图吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-14
    • 2022-01-08
    • 2020-01-04
    • 2012-08-12
    • 1970-01-01
    • 2015-01-09
    • 2012-04-19
    相关资源
    最近更新 更多