Geotools 外部图形不显示 SVG 内的异物

发布于 2025-01-16 20:11:44 字数 2623 浏览 4 评论 0原文

加载样式的函数

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:

enter image description here

The text inside the SVG is not rendered. I think there is some issue with geotools to render SVGs with foreign objects.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文