package com.ard.work.service.impl;
|
|
|
import com.ard.work.config.GeoserverConfig;
|
import com.ard.work.domain.dto.wayline.ArdPositionDTO;
|
import com.ard.work.service.IGeoToolsService;
|
import com.ard.work.util.GeoTools;
|
import com.ard.work.util.Vertex;
|
import lombok.extern.slf4j.Slf4j;
|
import org.locationtech.jts.geom.*;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import java.io.*;
|
import java.util.ArrayList;
|
import java.util.List;
|
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<ArdPositionDTO> calConvexHull(List<ArdPositionDTO> polygonPoints){
|
//缓冲距离
|
List<ArdPositionDTO> convexHullPoints = new ArrayList<>();
|
try{
|
//用户定义区域
|
Integer pCount = polygonPoints.size();
|
//首位最后一个点
|
Coordinate[] uPoints = new Coordinate[pCount+1];
|
int i = 0;
|
for(i=0;i<pCount;i++){
|
uPoints[i] = new Coordinate(polygonPoints.get(i).getLongitude(),polygonPoints.get(i).getLatitude());
|
}
|
//将第一个点重新加入到最后,形成闭环
|
uPoints[i] = new Coordinate(polygonPoints.get(0).getLongitude(),polygonPoints.get(0).getLatitude());
|
//多边形点的个数必须大于等于3
|
if(uPoints.length >= 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<shpFiles.length;i++){
|
File _file = new File(shpFiles[i]);
|
input = new FileInputStream(_file);
|
zipOut.putNextEntry(new ZipEntry(_file.getName()));
|
int temp = 0;
|
while ((temp = input.read()) != -1) {
|
zipOut.write(temp);
|
}
|
input.close();
|
}
|
zipOut.close();
|
}catch (Exception e){
|
e.printStackTrace();
|
}
|
}
|
|
}
|