如何使用geotools解析SLD 1.0.0或1.1.0?

问题描述 投票:0回答:1

是否存在使用geotools解析SLD文件的内置方法,该方法适用于SLD 1.0.0和SLD 1.1.0?

java geotools sld
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;
}
© www.soinside.com 2019 - 2024. All rights reserved.