首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用geotools解析SLD 1.0.0或1.1.0?

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

Stack Overflow用户
提问于 2019-09-26 20:47:17
回答 1查看 110关注 0票数 0

对于SLD 1.0.0和SLD 1.1.0,有没有内置的解析SLD文件的geotools方法?

EN

回答 1

Stack Overflow用户

发布于 2019-09-26 20:47:17

我还没有找到一种内置的方法,但一种可能的解决方案是从XML文件中检索SLD版本。根据版本的不同,可以使用合适的Configuration类对其进行解析。

代码语言:javascript
复制
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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58117159

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档