各个GPT大神们。嘎嘎CV 嘎嘎报错,网上十篇里面九篇是GPT ,复制嘎嘎报错。上代码!
maven 依赖
<!-- https://mvnrepository.com/artifact/org.geotools/gt-geojson -->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geojson</artifactId>
<version>27.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.geotools/gt-shapefile -->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>27.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.geotools/gt-geojsondatastore -->
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geojsondatastore</artifactId>
<version>26.5.01</version>
</dependency>
依赖下载不下来可以配置maven
<repositories>
<repository>
<id>osgeo</id>
<name>OSGeo Release Repository</name>
<url>https://repo.osgeo.org/repository/release/</url>
<snapshots><enabled>false</enabled></snapshots>
<releases><enabled>true</enabled></releases>
</repository>
<repository>
<id>osgeo-snapshot</id>
<name>OSGeo Snapshot Repository</name>
<url>https://repo.osgeo.org/repository/snapshot/</url>
<snapshots><enabled>true</enabled></snapshots>
<releases><enabled>false</enabled></releases>
</repository>
<!--GeoServer-->
<repository>
<id>GeoSolutions</id>
<url>http://maven.geo-solutions.it/</url>
</repository>
</repositories>
java 代码 工具类
package com.xadc.jmrfb3des.controller.wuhan;
import java.io.*;
import com.xadc.jmrfb3des.exception.CustomException;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.geojson.feature.FeatureJSON;
public class GeojsonUtil {
public static String transformShpToGeoJson(String shpPath) throws IOException {
String geojson = null;
ShapefileDataStore store = new ShapefileDataStore(new File(shpPath).toURI().toURL());
SimpleFeatureSource source = store.getFeatureSource();
SimpleFeatureCollection featureCollection = source.getFeatures();
FeatureJSON fjson = new FeatureJSON();
try (StringWriter writer = new StringWriter()) {
fjson.writeFeatureCollection(featureCollection, writer);
geojson = writer.toString();
}catch (Exception e){
throw new CustomException("shp -> 转化错误");
}
return geojson;
}
}
具体逻辑代码,
@Override
public String upload(MultipartFile file) throws Exception {
//获取拷贝后的文件位置
String filePath = this.copyFile(file, uploadUrl + this.getFileNameWithoutExtension(file) + this.generateUUID());
//解压文件 读取 .shp 这个文件
String shpPath = this.unzipAndFindShapeFile(filePath, uploadUrl + ShiroConstant.SHP + this.generateUUID());
return GeojsonUtil.transformShpToGeoJson(shpPath);
}
说明一下:这里的copyFile 就是一个拷贝文件的操作,file 是个压缩包,拷贝到临时目录下,然后解压,这都是基础操作,然后直接调用工具类方法,返回的就是转好之后的json,解压文件这里封装了一下,代码如下:
private String unzipAndFindShapeFile(String zipFilePath, String destFolderPath) throws Exception {
File destFolder = new File(destFolderPath);
if (!destFolder.exists()) {
destFolder.mkdirs();
}
String shapeFilePath = null;
int shpFileCount = 0;
try (ZipFile zipFile = new ZipFile(zipFilePath)) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
File destFile = new File(destFolderPath, entry.getName());
if (!entry.isDirectory()) {
try (InputStream in = zipFile.getInputStream(entry);
OutputStream out = Files.newOutputStream(destFile.toPath())) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
}
if (destFile.getName().toLowerCase().endsWith(ShiroConstant.SHP)) {
shapeFilePath = destFile.getAbsolutePath();
shpFileCount++;
}
}
}
}
if (shpFileCount == 1) {
return shapeFilePath;
} else if (shpFileCount == 0) {
throw new CustomException("上传的文件有误,请核实文件后重新上传");
} else {
throw new CustomException("在zip文件中找到多个.shp文件");
}
}