根据geotools自己封装的工具类,主要有shp文件的内容读取,转geojson,要素的增删改,新shp生成
需要的依赖
这里主要用的是geotools的依赖,版本是<geotools.version>23.2</geotools.version>
还用到了fastjson,版本1.2.57
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.57</version>
</dependency>
<!-- geotools -->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geojson</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-cql</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-ysld</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-epsg-hsql</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-epsg-extension</artifactId>
<version>${geotools.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.geotools/gt-main -->
<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>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-xml</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-referencing</artifactId>
<version>${geotools.version}</version>
</dependency>
如果不好下需要指定一下repository
<repository>
<id>GeoSolutions</id>
<url>http://maven.geo-solutions.it/</url>
</repository>
读取工具
public class ShapeFileReaderUtils {
/**
* 校验shp文件
*
* @param fileUrl 文件地址 shp或shp文件夹
* @return File
* @throws Exception e
*/
public static File checkShapeFile(String fileUrl) throws Exception {
File file = new File(fileUrl);
if (file.isDirectory()) {
File[] fa = file.listFiles();
if (fa == null || fa.length < 1) {
throw new Exception("找不到shp文件");
}
boolean flag = true;
for (File f : fa) {
if (new ShpFiles(file).exists(ShpFileType.SHP)) {
file = f;
flag = false;
break;
}
}
if (flag) {
throw new Exception("找不到shp文件");
}
} else {
if (!new ShpFiles(file).exists(ShpFileType.SHP)) {
throw new Exception("找不到shp文件");
}
}
return file;
}
/**
* shp文件要素源
*
* @param fileUrl 文件地址
* @return ContentFeatureSource
* @throws Exception e
*/
public static ContentFeatureSource getFeatureSourceFromShapeFile(String fileUrl) throws Exception {
ShapefileDataStore sds = null;
try {
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
File file = checkShapeFile(fileUrl);
sds = (ShapefileDataStore) dataStoreFactory.createDataStore(file.toURI().toURL());
String type = sds.getTypeNames()[0];
sds.setCharset(getShapeFileCharsetName(fileUrl));
return sds.getFeatureSource(type);
} catch (Exception e) {
e.printStackTrace();
return

最低0.47元/天 解锁文章
3606





