gpt4 book ai didi

java - Geotools:将不同样式的点添加到同一 map 图层

转载 作者:行者123 更新时间:2023-11-30 07:19:03 24 4
gpt4 key购买 nike

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

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

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

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

}
}

}

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

最佳答案

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

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 .

关于java - Geotools:将不同样式的点添加到同一 map 图层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37902478/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com