java - 如何使用 geotools 解析 SLD 1.0.0 或 1.1.0?
问题描述
是否有一种内置方法可以使用 geotools 解析 SLD 文件,适用于 SLD 1.0.0 和 SLD 1.1.0?
解决方案
我还没有找到内置方法,但一种可能的解决方案是从 XML 文件中检索 SLD 版本。根据版本,可以使用合适的Configuration
类对其进行解析。
public Style createStyleFromSld(String uri) throws XPathExpressionException, IOException, SAXException, ParserConfigurationException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document xmlDocument = db.parse(uri);
XPath xPath = XPathFactory.newInstance().newXPath();
String version = xPath.compile("/StyledLayerDescriptor/@version").evaluate(xmlDocument);
Configuration sldConf;
if (version != null && version.startsWith("1.1")) {
sldConf = new org.geotools.sld.v1_1.SLDConfiguration();
} else {
sldConf = new org.geotools.sld.SLDConfiguration();
}
StyledLayerDescriptor sld = (StyledLayerDescriptor) new DOMParser(sldConf, xmlDocument).parse();
NamedLayer l = (NamedLayer) sld.getStyledLayers()[0];
Style style = l.getStyles()[0];
return style;
}
推荐阅读
- python-3.x - 从对象列表中删除一个类的一个对象
- .net - 端点返回动态统计数据的最佳实践
- node.js - 如何将 12 小时日期和时间转换为 24 小时日期和时间
- amazon-s3 - 我试图将我的 django-blog 存储与 IBM 存储桶而不是 AWS s3 合并。但我不能。有没有人可以帮助我
- python - Incorrect input shape after loading Keras model
- sql - 联接表而不丢失数据
- reactjs - react pdf multi lines 将所有行放在一行中
- tracking - Edge (chromium) 浏览器跟踪预防:如何在非 html 类内容(即 PDF)下正确强制加载网站图标?(再次访问时)
- java - 设置 logback 配置以按时间戳创建单独的文件夹,并为文件夹中的每个测试用例创建单独的日志文件
- javascript - 合并对象 Ramda