Geotools解析SLD

Geotools解析SLD

geotools解析SLD文件时,默认解析SLD的版本为1.0.0,当解析1.1.0版本的SLD文件时,会无法解析样式,需要手动进行版本选择。

public Style createStyleFromSld(String uri) throws XPathExpressionException, IOException, SAXException, ParserConfigurationException {
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  DocumentBuilder db = dbf.newDocumentBuilder();
  Document xmlDocument = db.parse(uri);	// sld文件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;
}

参考文章:https://www.coder.work/article/5666888

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
GeoTools是一个开源的Java工具包,可用于处理地理空间数据。GeoTools提供了对GeoJSON格式的支持,可以使用GeoTools解析GeoJSON数据。 以下是一个简单的示例代码,展示了如何使用GeoTools解析GeoJSON数据: ```java File geojsonFile = new File("path/to/geojson/file"); // GeoJSON文件路径 // 创建GeoJSON文件读取器 GeoJSONDataStoreFactory dataStoreFactory = new GeoJSONDataStoreFactory(); Map<String, Object> params = new HashMap<>(); params.put(GeoJSONDataStoreFactory.URLP.key, geojsonFile.toURI().toURL()); GeoJSONDataStore dataStore = (GeoJSONDataStore) dataStoreFactory.createDataStore(params); // 获取GeoJSON数据源中的FeatureCollection SimpleFeatureSource featureSource = dataStore.getFeatureSource(dataStore.getTypeNames()[0]); SimpleFeatureCollection featureCollection = featureSource.getFeatures(); // 遍历FeatureCollection try (SimpleFeatureIterator featureIterator = featureCollection.features()) { while (featureIterator.hasNext()) { SimpleFeature feature = featureIterator.next(); Geometry geometry = (Geometry) feature.getDefaultGeometry(); // 获取几何对象 // 对几何对象进行操作 // ... } } ``` 上述代码首先创建了一个GeoJSON文件读取器,然后使用该读取器获取GeoJSON数据源中的FeatureCollection。接着遍历FeatureCollection,对其中的每个Feature进行操作。 需要注意的是,使用GeoTools解析GeoJSON数据时,需要先将GeoJSON文件转换为GeoJSON数据源,然后再从数据源中获取FeatureCollection。GeoTools支持的数据源类型包括文件、URL、InputStream等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值