package com.dji.sample.patches.utils;
|
|
import cn.hutool.core.io.FileUtil;
|
import com.alibaba.fastjson.JSONObject;
|
import org.geotools.data.DataStore;
|
import org.geotools.data.DataStoreFinder;
|
import org.geotools.data.FeatureSource;
|
import org.geotools.feature.FeatureCollection;
|
import org.geotools.feature.FeatureIterator;
|
import org.geotools.geojson.feature.FeatureJSON;
|
import org.opengis.feature.simple.SimpleFeature;
|
import org.opengis.feature.simple.SimpleFeatureType;
|
import org.opengis.filter.Filter;
|
import java.io.File;
|
import java.io.IOException;
|
import java.io.StringWriter;
|
import java.util.*;
|
|
public class ShapeFileUtil {
|
/*
|
* @param zipFile: 压缩包文件地址
|
* @return FeatureCollection
|
* @author pangshicheng
|
* @description 解析shp压缩包,并返回解析出的 FeatureCollection
|
* @date 2023/7/18 16:02
|
*/
|
|
public static FeatureCollection getFeatureCollectionByShpFile(File zipFile) throws IOException {
|
try {
|
String tempDir = FileUtil.getTmpDirPath();
|
File shapeDir = new File(tempDir + File.separator + new Date().getTime());
|
shapeDir.mkdir();
|
List<String> files = ZipUtil.unZipFiles(zipFile, shapeDir.getPath() + File.separator);
|
String shapFileName = "";
|
for (String fileName : files) {
|
String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
|
if ("shp".equals(suffix)) {
|
shapFileName = fileName;
|
}
|
}
|
File shapeFile = new File(shapFileName);
|
List<SimpleFeature> list = new ArrayList<>();
|
Map<String, Object> shapeFileParams = new HashMap();
|
shapeFileParams.put("url", shapeFile.toURI().toURL());
|
// 设置编码
|
shapeFileParams.put("charset", "GBK");
|
DataStore dataStore = DataStoreFinder.getDataStore(shapeFileParams);
|
if (dataStore == null) {
|
throw new RuntimeException("couldn't load the damn data store: " + shapeFileParams);
|
}
|
String typeName = dataStore.getTypeNames()[0];
|
FeatureSource<SimpleFeatureType, SimpleFeature> source = dataStore.getFeatureSource(typeName);
|
Filter filter = Filter.INCLUDE;
|
FeatureCollection<SimpleFeatureType, SimpleFeature> collection = source.getFeatures(filter);
|
return collection;
|
} catch (Exception e) {
|
throw e;
|
}
|
}
|
|
/**
|
* @param zipFile:
|
* @return JSONObject
|
* @author pangshicheng
|
* @description 通过shp压缩文件,将其转换为GeoJson格式
|
* @date 2023/7/18 16:04
|
*/
|
public static List<String> shpToGeoJson(File zipFile) throws IOException {
|
FeatureJSON fjson = new FeatureJSON();
|
JSONObject geoJsonObject = new JSONObject();
|
geoJsonObject.put("type", "FeatureCollection");
|
List<String> strings = new ArrayList<>();
|
try {
|
// 获取FeatureCollection
|
FeatureCollection collection = getFeatureCollectionByShpFile(zipFile);
|
|
FeatureIterator iterator = collection.features();
|
List<JSONObject> array = new ArrayList<JSONObject>();
|
//遍历feature转为json对象
|
while (iterator.hasNext()) {
|
SimpleFeature feature = (SimpleFeature) iterator.next();
|
StringWriter writer = new StringWriter();
|
fjson.writeFeature(feature, writer);
|
String temp = writer.toString();
|
byte[] b = temp.getBytes("iso8859-1");
|
temp = new String(b, "gbk");
|
JSONObject json = JSONObject.parseObject(temp);
|
String str2 = json.getJSONObject("geometry").get("coordinates").toString();
|
strings.add(str2);
|
array.add(json);
|
}
|
iterator.close();
|
//添加到geojsonObject
|
geoJsonObject.put("features", array);
|
iterator.close();
|
|
} catch (Exception e) {
|
throw e;
|
}
|
return strings;
|
}
|
|
}
|