Geotools 外部图形不显示 SVG 内的异物
加载样式的函数
static final StyleFactory STYLE_FACTORY = CommonFactoryFinder.getStyleFactory();
protected Style loadStyleDynamic(String baseUrl) throws MalformedURLException, UnsupportedEncodingException {
UriBuilder uriBuilder = UriBuilder.fromUri(baseUrl);
URL url = new URL(uriBuilder.toString());
PointSymbolizer symb = STYLE_FACTORY.createPointSymbolizer();
ExternalGraphic eg = STYLE_FACTORY.createExternalGraphic(url, "image/svg+xml");
symb.getGraphic().graphicalSymbols().add(eg);
Literal size = FILTER_FACTORY.literal(scaleFactor);
symb.getGraphic().setSize(size);
Rule rule = STYLE_FACTORY.createRule();
rule.symbolizers().add(symb);
FeatureTypeStyle fts = STYLE_FACTORY.createFeatureTypeStyle(rule);
Style style = STYLE_FACTORY.createStyle();
style.featureTypeStyles().add(fts);
return style;
}
然后我将此样式添加到要加载标记的点的特征集合对象中。
SVG 标记
<svg xmlns='http://www.w3.org/2000/svg' width='122' height='28' viewBox='0 0 122 28' fill='none'><rect x='1' y='1' width='120' height='26' rx='13' fill='#27A6A4'/><rect x='1' y='1' width='120' height='26' rx='13' stroke='#022D42' stroke-width='1.5'/><foreignObject width='122' height='28'><body xmlns='http://www.w3.org/1999/xhtml' style='margin:0;padding:0'><p style='margin: 0;display: block;text-overflow: ellipsis;overflow: hidden;white-space: pre;padding: 7px;text-align: center;color: rgb(2, 45, 66);font-family: Arial, Helvetica, sans-serif;font-size: 12px;font-weight: 600;'>Manhattan Midtown, NY</p></body></foreignObject></svg><svg xmlns='http://www.w3.org/2000/svg' width='122' height='28' viewBox='0 0 122 28' fill='none'><rect x='1' y='1' width='120' height='26' rx='13' fill='#27A6A4'/><rect x='1' y='1' width='120' height='26' rx='13' stroke='#022D42' stroke-width='1.5'/><foreignObject width='122' height='28'><body xmlns='http://www.w3.org/1999/xhtml' style='margin:0;padding:0'><p style='margin: 0;display: block;text-overflow: ellipsis;overflow: hidden;white-space: pre;padding: 7px;text-align: center;color: rgb(2, 45, 66);font-family: Arial, Helvetica, sans-serif;font-size: 12px;font-weight: 600;'>Som Long Text For Ellipses</p></body></foreignObject></svg>
SVG 在最终图像上渲染:
SVG 内的文本未渲染。我认为 geotools 用异物渲染 SVG 存在一些问题。
Function to load style
static final StyleFactory STYLE_FACTORY = CommonFactoryFinder.getStyleFactory();
protected Style loadStyleDynamic(String baseUrl) throws MalformedURLException, UnsupportedEncodingException {
UriBuilder uriBuilder = UriBuilder.fromUri(baseUrl);
URL url = new URL(uriBuilder.toString());
PointSymbolizer symb = STYLE_FACTORY.createPointSymbolizer();
ExternalGraphic eg = STYLE_FACTORY.createExternalGraphic(url, "image/svg+xml");
symb.getGraphic().graphicalSymbols().add(eg);
Literal size = FILTER_FACTORY.literal(scaleFactor);
symb.getGraphic().setSize(size);
Rule rule = STYLE_FACTORY.createRule();
rule.symbolizers().add(symb);
FeatureTypeStyle fts = STYLE_FACTORY.createFeatureTypeStyle(rule);
Style style = STYLE_FACTORY.createStyle();
style.featureTypeStyles().add(fts);
return style;
}
Then I am adding this style to the feature collection object of the point at which I want to load the marker.
SVG Marker
<svg xmlns='http://www.w3.org/2000/svg' width='122' height='28' viewBox='0 0 122 28' fill='none'><rect x='1' y='1' width='120' height='26' rx='13' fill='#27A6A4'/><rect x='1' y='1' width='120' height='26' rx='13' stroke='#022D42' stroke-width='1.5'/><foreignObject width='122' height='28'><body xmlns='http://www.w3.org/1999/xhtml' style='margin:0;padding:0'><p style='margin: 0;display: block;text-overflow: ellipsis;overflow: hidden;white-space: pre;padding: 7px;text-align: center;color: rgb(2, 45, 66);font-family: Arial, Helvetica, sans-serif;font-size: 12px;font-weight: 600;'>Manhattan Midtown, NY</p></body></foreignObject></svg><svg xmlns='http://www.w3.org/2000/svg' width='122' height='28' viewBox='0 0 122 28' fill='none'><rect x='1' y='1' width='120' height='26' rx='13' fill='#27A6A4'/><rect x='1' y='1' width='120' height='26' rx='13' stroke='#022D42' stroke-width='1.5'/><foreignObject width='122' height='28'><body xmlns='http://www.w3.org/1999/xhtml' style='margin:0;padding:0'><p style='margin: 0;display: block;text-overflow: ellipsis;overflow: hidden;white-space: pre;padding: 7px;text-align: center;color: rgb(2, 45, 66);font-family: Arial, Helvetica, sans-serif;font-size: 12px;font-weight: 600;'>Som Long Text For Ellipses</p></body></foreignObject></svg>
SVG rendered on final image:
The text inside the SVG is not rendered. I think there is some issue with geotools to render SVGs with foreign objects.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论