读取shp
pom依赖
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-main</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-opengis</artifactId>
<version>${geotools.version}</version>
</dependency>
读取shp将几何要素转换为wkt
public List<GeoDataVo> readShpTOWkt(String shpPath) throws IOException {
System.out.println("shp解析"+shpPath);
List<GeoDataVo> list=new ArrayList<>();
File file = new File(shpPath);
if (file == null) {
return null;
}
FileDataStore store = FileDataStoreFinder.getDataStore(file);
String geoType=store.getSchema().getGeometryDescriptor().getType().getName().getLocalPart();
SimpleFeatureSource featureSource = store.getFeatureSource();
SimpleFeatureCollection simpleFeatureCollection = featureSource.getFeatures();
SimpleFeatureIterator itertor = simpleFeatureCollection.features();
//遍历featurecollection
while (itertor.hasNext()) {
SimpleFeature feature = itertor.next();
String name= feature.getAttribute("Name").toString();
if(StrUtil.isEmpty(name)){
name=file.getName().substring(0,file.getName().lastIndexOf('.'));
}
String wkt= feature.getDefaultGeometry().toString();
GeoDataVo geoDataVo=new GeoDataVo();
geoDataVo.setType(geoType);
geoDataVo.setWkt(wkt);
geoDataVo.setName(name);
list.add(geoDataVo);
}
return list;
}