是否有一种内置方法可以使用 geotools 解析 SLD 文件,适用于 SLD 1.0.0 和 SLD 1.1.0?
问问题
123 次
1 回答
1
我还没有找到内置方法,但一种可能的解决方案是从 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;
}
于 2019-09-26T12:47:17.087 回答