如何使用 GeoTools 库来读取 SHP,再将 SHP 文件转换为 JTS 的 Geometry 对象
时间: 2023-04-01 20:04:58 浏览: 337
您好,可以使用以下代码来使用 GeoTools 库来读取 SHP 文件并将其转换为 JTS 的 Geometry 对象:
```
File file = new File("path/to/shapefile.shp");
Map<String, Serializable> params = new HashMap<>();
params.put("url", file.toURI().toURL());
DataStore dataStore = DataStoreFinder.getDataStore(params);
String typeName = dataStore.getTypeNames()[];
SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
SimpleFeatureCollection collection = featureSource.getFeatures();
SimpleFeatureIterator iterator = collection.features();
try {
while (iterator.hasNext()) {
SimpleFeature feature = iterator.next();
Geometry geometry = (Geometry) feature.getDefaultGeometry();
// do something with the geometry object
}
} finally {
iterator.close();
}
```
希望能对您有所帮助。
阅读全文