cutfilldemo/src/main/java/org/example/controller/RingGridController.java

77 lines
3.0 KiB
Java
Raw Normal View History

2024-05-07 10:08:30 +08:00
package org.example.controller;
import com.alibaba.fastjson.JSONArray;
import org.example.dto.FillCutDto;
import org.example.service.RingGridService;
import org.example.test.CreateRingGridFix;
import org.example.utils.CommonResult;
import org.geotools.api.geometry.Position;
import org.geotools.api.referencing.crs.CoordinateReferenceSystem;
import org.geotools.coverage.grid.GridCoordinates2D;
import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.coverage.grid.GridGeometry2D;
import org.geotools.coverage.processing.Operations;
import org.geotools.gce.geotiff.GeoTiffReader;
import org.geotools.geometry.Position2D;
import org.geotools.referencing.CRS;
import org.geotools.util.factory.Hints;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@RestController
public class RingGridController {
@Autowired
public RingGridService ringGridService;
@RequestMapping("/fillcut")
public CommonResult<Map<String,Object>> calcFillCut(FillCutDto fcd) throws Exception {
Coordinate[] coordinates = new Coordinate[5];
double range = 10; // 容差
double paramRange = fcd.getRange();
String paramCoordinate = fcd.getCoordinates();
if(paramRange != 0) range = paramRange;
if(!paramCoordinate.isEmpty()){
try{
// 解析字符串
JSONArray coordinateArray = (JSONArray)JSONArray.parse(paramCoordinate);
coordinates = new Coordinate[coordinateArray.size()+1];
for(int i=0;i<coordinateArray.size();i++){
JSONArray cd = (JSONArray)coordinateArray.get(i);
coordinates[i] = new Coordinate(
Double.parseDouble(cd.get(0).toString()),
Double.parseDouble(cd.get(1).toString())
);
}
// 闭合多边形,复制第一个坐标作为最后一个坐标
JSONArray ja = (JSONArray)coordinateArray.get(0);
coordinates[coordinateArray.size()] = new Coordinate(
Double.parseDouble(ja.get(0).toString()),
Double.parseDouble(ja.get(1).toString())
);
}catch (Exception e){
e.printStackTrace();
return new CommonResult<>(false,"解析坐标失败!");
}
}else{
return new CommonResult<>(false,"缺少必要参数!");
}
return ringGridService.calcFillCut(coordinates,range);
}
}