【发布时间】:2016-01-29 10:21:47
【问题描述】:
有人可以提供一些关于如何在 Geotools 的 JMapFrame 中显示 shapefile 的图例的提示吗?我已经为 shapefile 创建了样式,我需要一种方法来告诉用户样式是如何定义的,这就需要图例。
有一个包 org.geotools.legend。但是不知道怎么用。
谢谢!
【问题讨论】:
有人可以提供一些关于如何在 Geotools 的 JMapFrame 中显示 shapefile 的图例的提示吗?我已经为 shapefile 创建了样式,我需要一种方法来告诉用户样式是如何定义的,这就需要图例。
有一个包 org.geotools.legend。但是不知道怎么用。
谢谢!
【问题讨论】:
您需要遍历Styles FeatureTypeStyless Rules Symbolizers 并为每一个绘制一个代表特征。比如:
private void drawLegend(BufferedImage img, Rule r) {
for (Symbolizer sym : r.symbolizers()) {
SimpleFeature feature = null;
if (sym instanceof LineSymbolizer) {
LineString line = drawer.line(new int[] { 1, 1, 10, 20, 20, 20 });
feature = drawer.feature(line);
} else if(sym instanceof PolygonSymbolizer) {
Polygon p = drawer.polygon(new int[] { 1, 1, 1, 18, 18, 18, 18, 1, 1,1 });
feature = drawer.feature(p);
} else if(sym instanceof PointSymbolizer || sym instanceof TextSymbolizer) {
Point p = drawer.point(10, 10);
feature = drawer.feature(p);
}
if(feature == null)
continue;
drawer.drawDirect(img, feature, r);
Graphics2D gr = img.createGraphics();
gr.setColor(Color.BLACK);
if (r.getDescription() != null && r.getDescription().getTitle() != null) {
gr.drawString(r.getDescription().getTitle().toString(), 20, 18);
}
}
}
然后您可以将这些图像绘制到 JPanel 或地图上。
有关完整示例,请参阅 GeoServer 如何创建对 getLegendGraphic request 的响应。
【讨论】: