【问题标题】:How to parse SLD 1.0.0 or 1.1.0 with geotools?如何使用 geotools 解析 SLD 1.0.0 或 1.1.0?
【发布时间】:2019-09-26 12:47:17
【问题描述】:

是否有内置方法可以使用 geotools 解析 SLD 文件,适用于 SLD 1.0.0 和 SLD 1.1.0?

【问题讨论】:

    标签: java geotools sld


    【解决方案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;
    }
    

    【讨论】:

    • 在理想情况下,您可以查看文件的 mime 类型并使用它来区分它们,但如果您的方法失败,或者尝试其中一种方法是最好的计划
    猜你喜欢
    • 2013-02-05
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多