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

Java Geotools:将具有不同样式的点添加到同一地图图层,java,geolocation,geocoding,geotools,Java,Geolocation,Geocoding,Geotools,在我的java项目中,我使用它将不同大小、颜色和不透明度的点绘制到地图中。 由于它们有很多不同的样式,我的地图中有数千层。目前,每个添加的点都有自己的层,因为我没有将它们添加到一个层中 我从实体对象中获取点。同一实体中的每个点都具有相同的颜色。有更多具有相同名称/颜色的实体 这是在地图上绘制一个实体的方式: 在以下代码段中动态计算点的大小: public class Stackoverflow { private final MapContent mapContent; public St

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

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

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

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

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

    }
}

}
公共类堆栈溢出{
私有最终MapContent MapContent;
公共堆栈溢出(){
this.mapContent=新的mapContent();
}
public void addPoints(最终HashMap colorsforenties,最终列表toDraw){
最终GeometryFactory GeometryFactory=JTSFactoryFinder.getGeometryFactory();
//为点创建SimpleFeatureType
最终SimpleFeatureTypeBuilder点TB=新SimpleFeatureTypeBuilder();
pointTb.setName(“点”);
pointTb.setCRS(默认地理位置CCRS.WGS84);
pointTb.add(“点”,point.class);
最终SimpleFeatureBuilder pointFeatureBuilder=新SimpleFeatureBuilder(pointTb.buildFeatureType());
for(最终实体:toDraw){
最终列表路径点=entity.getPoints();
Color Color=colorsforenties.get(entity.getName());
图层点;
风格点风格;
final float maxOpacity=MyProperties.getFloat(Constants.POINT\u STYLE\u OPACITY);
最终浮点maxSize=MyProperties.getFloat(常量、点、样式、大小);
final int NumPoints=pathPoints.size();
浮动不透明度=最大不透明度;
浮动大小=最大大小;
最终浮点deltaOpacity=maxOpacity/NumPoints;
最终浮点deltaSize=maxSize/NumPoints;
用于(最终点2D点2D:pathPoints){
最终点=geometryFactory.createPoint(新坐标(point2D.getX(),point2D.getY());
pointFeatureBuilder.add(点);
最终SimpleFeature功能=pointFeatureBuilder.buildFeature(null);
final DefaultFeatureCollection pointCollection=新的DefaultFeatureCollection();
添加(特性);
pointStyle=SLD.createPointStyle(“圆”、颜色、颜色、不透明度、大小);
不透明度=不透明度-增量容量;
大小=大小-deltaSize;
layerPoints=新功能层(点集合、点样式);
mapContent.addLayer(layerPoints);
}
}
}
}

是否可以将点添加到一个图层上,或者至少获得更少的图层数量

可以更有效地完成:-)我认为最简单的方法是从
实体
s构建
SimpleFeature
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());
然后可以创建一个
样式
,该样式接受这些特性并使用它们设置每个点的样式。类似于(未经测试):


由于SLD不提供任何循环支持,我认为没有任何方法可以避免将每个
实体
分解为一组具有自己样式的独立点,除非您想编写一个链接。

链接不起作用。我们可以在不重新加载地图的情况下将样式应用于地图吗?
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);