package org.springblade.common.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.locationtech.jts.geom.Geometry; import org.locationtech.jts.geom.Point; import org.locationtech.jts.io.ParseException; 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 * @throws IOException */ 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 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); Map shapeFileParams = new HashMap(); shapeFileParams.put("url", shapeFile.toURI().toURL()); // 设置编码 shapeFileParams.put("charset", "utf-8"); 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 source = dataStore.getFeatureSource(typeName); Filter filter = Filter.INCLUDE; FeatureCollection collection = source.getFeatures(filter); return collection; } catch (Exception e) { throw e; } } /** * 解析shp文件返回数据 * @param zipFile: * @return JSONObject * @description 通过shp压缩文件,将其转换为GeoJson格式 */ //将解压后的文件转换成GeoJson格式 public static List> shpToGeoJson(File zipFile) throws IOException{ List> dtoList = new ArrayList<>(); try { FeatureJSON featureJSON = new FeatureJSON(); JSONObject geoJsonObject = new JSONObject(); geoJsonObject.put("type", "FeatureCollection"); // 解压zip文件,获取FeatureCollection FeatureCollection collection = getFeatureCollectionByShpFile(zipFile); // 获取迭代对象 FeatureIterator iterator = collection.features(); // 遍历feature转为json对象 while (iterator.hasNext()) { Map shpDTO = new HashMap<>(); SimpleFeature feature = (SimpleFeature) iterator.next(); StringWriter writer = new StringWriter(); featureJSON.writeFeature(feature, writer); String temp = writer.toString(); JSONObject json = JSONObject.parseObject(temp); try { // 空间坐标 shpDTO.put("geometry",json.getJSONObject("geometry").getString("coordinates")); // 空间类型 if (json.getJSONObject("geometry").get("type") != null) { shpDTO.put("geoType",json.getJSONObject("geometry").getString("type")); } dtoList.add(shpDTO); } catch (NumberFormatException e) { System.err.println("处理单个特征时发生错误:" + e.getMessage()); } catch (Exception e) { System.err.println("处理单个特征时发生错误:" + e.getMessage()); } } iterator.close(); } catch (IOException e) { System.err.println("读取或解析文件时发生错误:" + e.getMessage()); throw e; } return dtoList; } public static void main(String[] args) throws IOException, ParseException { File file = new File("F:\\test4jdata\\shp\\zrq.zip"); List> list = shpToGeoJson(file); System.out.println("list = " + list); } public static double[] getLongitudeLatitude(Geometry wktPoint) throws ParseException { Geometry geometry = wktPoint; if (geometry instanceof Point) { Point point = (Point) geometry; double longitude = point.getX(); double latitude = point.getY(); return new double[]{longitude, latitude}; } else { throw new IllegalArgumentException("The geometry is not a point."); } } }