package com.ard.tianyu.service.impl; import com.ard.tianyu.config.GeoserverConfig; import com.ard.tianyu.domain.dto.wayline.ArdPositionDTO; import com.ard.tianyu.service.IGeoToolsService; import com.ard.tianyu.util.GeoTools; import com.ard.tianyu.util.Vertex; import lombok.extern.slf4j.Slf4j; import org.geotools.data.DefaultTransaction; import org.geotools.data.Transaction; import org.geotools.data.collection.ListFeatureCollection; import org.geotools.data.simple.SimpleFeatureCollection; import org.geotools.data.simple.SimpleFeatureSource; import org.geotools.data.simple.SimpleFeatureStore; import org.geotools.feature.simple.SimpleFeatureBuilder; import org.geotools.feature.simple.SimpleFeatureTypeBuilder; import org.geotools.geometry.jts.JTSFactoryFinder; import org.geotools.referencing.crs.DefaultGeographicCRS; import org.locationtech.jts.geom.*; import org.locationtech.jts.operation.buffer.BufferParameters; import org.opengis.feature.simple.SimpleFeature; import org.opengis.feature.simple.SimpleFeatureType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; import org.springframework.web.multipart.MultipartFile; import java.io.*; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @Service @Slf4j public class GeoToolsServiceImpl implements IGeoToolsService { @Autowired private GeoserverConfig geoserverConfig; private static Double distance = 1500 * 180 / Math.PI / 6371000; private static GeometryFactory geoFactory = new GeometryFactory(); //坐标类型转换 public Vertex gcj02ToTargetCoordinate(Vertex gcjVertex){ Vertex newVertex = new Vertex(0,0); if(geoserverConfig.getDeviceCoordinateType().equals("WGS84")){ newVertex = GeoTools.GCJ02toWGS84(new Vertex(gcjVertex.x, gcjVertex.y)); }else { newVertex = new Vertex(gcjVertex.x, gcjVertex.y); } return newVertex; } public Vertex targetCoordinateToGcj02(Vertex targetVertex){ Vertex newVertex = new Vertex(0,0); if(geoserverConfig.getDeviceCoordinateType().equals("WGS84")){ newVertex = GeoTools.WGS84toGCJ02(new Vertex(targetVertex.x, targetVertex.y)); }else { newVertex = new Vertex(targetVertex.x, targetVertex.y); } return newVertex; } public List calConvexHull(List polygonPoints){ //缓冲距离 List convexHullPoints = new ArrayList<>(); try{ //用户定义区域 Integer pCount = polygonPoints.size(); //首位最后一个点 Coordinate[] uPoints = new Coordinate[pCount+1]; int i = 0; for(i=0;i= 3){ //生成集合要素 Geometry oneGeometry = geoFactory.createPolygon(uPoints); //做凸包 Geometry geoConvexHull = oneGeometry.convexHull(); Coordinate[] points = geoConvexHull.getCoordinates(); for (Coordinate coordinate : points) { convexHullPoints.add(ArdPositionDTO.builder().longitude(coordinate.getX()) .latitude(coordinate.getY()).build()); } } }catch (Exception e){ e.printStackTrace(); } return convexHullPoints; } private double meterToDegree(double threshold){ return threshold * 180 / Math.PI / 6371000; } public void deleteFile(File filePath) { //判断路径是否存在 if(filePath.exists()) { //判断是否是目录 if (filePath.isDirectory()) { //将目录逐层拆分成数组元素 File[] files = filePath.listFiles(); for (File f : files) { //递归调用,由最底层往上逐层删除 deleteFile(f); } } //如果是文件,直接删除 filePath.delete(); } } public void zipShapeFile(String shpPath){ try{ File shpFile = new File(shpPath); String shpRoot = shpFile.getParentFile().getPath(), _shpName = shpFile.getName(), shpName = _shpName.substring(0, _shpName.lastIndexOf(".")); String zipPath = shpRoot+File.separator+shpName+".zip"; File zipFile = new File(zipPath); InputStream input = null; ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile)); // zip的名称为 zipOut.setComment(shpName); String[] shpFiles = new String[]{ shpRoot+File.separator+shpName+".dbf", shpRoot+File.separator+shpName+".prj", shpRoot+File.separator+shpName+".shp", shpRoot+File.separator+shpName+".shx", }; for(int i=0;i