添加打点逻辑

This commit is contained in:
user 2024-04-17 17:41:06 +08:00
parent 81edf6c78c
commit 1307698f4a
11 changed files with 194 additions and 17 deletions

View File

@ -1,16 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="nexus-aliyun" />
<option name="name" value="nexus-aliyun" />
<option name="url" value="https://maven.aliyun.com/repository/public" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@ -21,7 +21,7 @@ public class Caclelate {
long time=System.currentTimeMillis();
File file = new File("src\\main\\resources\\public\\data\\ASTGTMV003_N23E113_dem.tif");
double[] coor = new double[]{113.530987,23.652041};
double[] coor = new double[]{113.52912766646999, 23.65389470026306};//((113.52912766646999, 23.65389470026306, NaN))
/**
* Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER设置经度为第一轴顺序
*

View File

@ -7,8 +7,13 @@ import org.geotools.api.data.FileDataStoreFinder;
import org.geotools.api.data.SimpleFeatureSource;
import org.geotools.api.feature.simple.SimpleFeature;
import org.geotools.api.feature.simple.SimpleFeatureType;
import org.geotools.api.geometry.Position;
import org.geotools.api.referencing.crs.CoordinateReferenceSystem;
import org.geotools.api.style.Style;
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.data.DataUtilities;
import org.geotools.data.collection.CollectionFeatureSource;
import org.geotools.data.collection.ListFeatureCollection;
@ -20,6 +25,8 @@ import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.filter.FilterFactoryImpl;
import org.geotools.filter.function.color.ConstrastFunction;
import org.geotools.gce.geotiff.GeoTiffReader;
import org.geotools.geometry.Position2D;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.grid.Grids;
@ -32,14 +39,17 @@ import org.geotools.styling.FillImpl;
import org.geotools.styling.SLD;
import org.geotools.swing.JMapFrame;
import org.geotools.swing.data.JFileDataStoreChooser;
import org.geotools.util.factory.Hints;
import org.locationtech.jts.geom.*;
import org.locationtech.jts.operation.buffer.BufferOp;
import org.locationtech.jts.operation.buffer.BufferParameters;
import java.awt.image.RenderedImage;
import java.io.File;
import java.util.*;
public class CreateGrid {
public static GridCoverage2D coverage;
private static SimpleFeatureType createFeatureType() {
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
@ -56,8 +66,20 @@ public class CreateGrid {
return LOCATION;
}
public static void main(String[] args) throws Exception{
private static SimpleFeatureType createPointFeatureType() {
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.setName("Location");
builder.setCRS(DefaultGeographicCRS.WGS84); // <- Coordinate reference system
// add attributes in order
builder.add("the_geom", Point.class);
// builder.length(15).add("name", String.class); // <- 15 chars width for name field
builder.add("id", String.class);
return builder.buildFeatureType();
}
public static void main(String[] args) throws Exception{
loadDem();
// 假设我们使用WGS84坐标系统
CoordinateReferenceSystem crs = CRS.decode("EPSG:4326");
@ -86,15 +108,20 @@ public class CreateGrid {
// 创建GeometryFactory
GeometryFactory geometryFactory = new GeometryFactory();
List<Map<String,Object>> featuresProperties = new ArrayList<>();
List<Map<String,Object>> pointFeaturesProperties = new ArrayList<>();
// 创建多边形矩形
Map innerPolygon = new HashMap<>();
Map<String,Object> innerPolygon = new HashMap<>();
Polygon rectangle = geometryFactory.createPolygon(coordinates);
innerPolygon.put("geom",rectangle);
innerPolygon.put("name","输入的面");
innerPolygon.put("id",1);
featuresProperties.add(innerPolygon);
// 获取中心点的高程
double targetHeight = getElevation(rectangle.getCentroid());
System.out.println("设计标高:"+targetHeight);
// 缓冲区建立外扩100m
double meter = 100;
double degree = meter/(2*Math.PI*6371004)*360;
@ -109,7 +136,7 @@ public class CreateGrid {
Coordinate[] pcoords = rectangle.getCoordinates();
Coordinate[] bgcoords = bg.getCoordinates();
Map extPolygon1 = new HashMap<>();
Map<String,Object> extPolygon1 = new HashMap<>();
Coordinate[] cords1 = new Coordinate[] {bgcoords[0],bgcoords[1],pcoords[1],pcoords[0],bgcoords[0]};
Polygon p1 = geometryFactory.createPolygon(cords1);
extPolygon1.put("geom",p1);
@ -117,29 +144,37 @@ public class CreateGrid {
extPolygon1.put("id",2);
featuresProperties.add(extPolygon1);
Map extPolygon2 = new HashMap<>();
Map<String,Object> extPolygon2 = new HashMap<>();
Coordinate[] cords2 = new Coordinate[] {bgcoords[1],bgcoords[2],pcoords[2],pcoords[1],bgcoords[1]};
Polygon p2 = geometryFactory.createPolygon(cords2);
extPolygon2.put("geom",p2);
extPolygon2.put("name","第二个面");
extPolygon2.put("id",3);
featuresProperties.add(extPolygon2);
// featuresProperties.add(extPolygon2);
//
Map extPolygon3 = new HashMap<>();
Map<String,Object> extPolygon3 = new HashMap<>();
Coordinate[] cords3 = new Coordinate[] {bgcoords[2],bgcoords[3],pcoords[3],pcoords[2],bgcoords[2]};
Polygon p3 = geometryFactory.createPolygon(cords3);
extPolygon3.put("geom",p3);
extPolygon3.put("name","第3个面");
extPolygon3.put("id",4);
featuresProperties.add(extPolygon3);
// featuresProperties.add(extPolygon3);
Map extPolygon4 = new HashMap<>();
Map<String,Object> extPolygon4 = new HashMap<>();
Coordinate[] cords4 = new Coordinate[] {bgcoords[3],bgcoords[0],pcoords[0],pcoords[3],bgcoords[3]};
Polygon p4 = geometryFactory.createPolygon(cords4);
extPolygon4.put("geom",p4);
extPolygon4.put("name","第4个面");
extPolygon4.put("id",5);
featuresProperties.add(extPolygon4);
// featuresProperties.add(extPolygon4);
Map<String,Object> outPolygon1 = new HashMap<>();
// Coordinate[] cords4 = new Coordinate[] {bgcoords[3],bgcoords[0],pcoords[0],pcoords[3],bgcoords[3]};
Polygon out1 = (Polygon) p1.getEnvelope();//geometryFactory.createPolygon(cords4);
outPolygon1.put("geom",out1);
outPolygon1.put("name","第1个面的外边框");
outPolygon1.put("id",6);
// featuresProperties.add(outPolygon1);
final SimpleFeatureType TYPE =createFeatureType();
DefaultFeatureCollection featureCollection = new DefaultFeatureCollection();
@ -155,8 +190,9 @@ public class CreateGrid {
// ReferencedEnvelope bounds = featureCollection.getBounds();
// 创建grids0.2度为间隔
// 创建gridsside为间隔单位为米
double side = 1/(2*Math.PI*6371004)*360;
ListFeatureCollection collection = new ListFeatureCollection(TYPE);
Iterator<SimpleFeature> iterator = featureCollection.stream().iterator();
iterator.next();
@ -165,6 +201,77 @@ public class CreateGrid {
IntersectionBuilder intersectionBuilder = new IntersectionBuilder(TYPE, source);
SimpleFeatureSource grid = Grids.createSquareGrid(collection.getBounds(), side, -1);
// 打点
Coordinate pc1 = bgcoords[0];
Coordinate pc2 = bgcoords[1];
Coordinate pc3 = bgcoords[2];
double t1 = (pc2.y - pc1.y)/(pc2.x-pc1.x);
double t2 = (pc3.y - pc2.y)/(pc3.x-pc2.x);
System.out.println("t1:"+t1);
System.out.println("t2:"+t2);
List<Geometry> centerPointList = new ArrayList();
Coordinate center1 = new Coordinate((pc1.x+pc2.x)/2,(pc1.y+pc2.y)/2);
Map<String,Object> centerPointMap = new HashMap<>();
Point center = geometryFactory.createPoint(center1);
// centerPointList.add(center);
// centerPointMap.put("geom",center);
// centerPointMap.put("id",10001);
// pointFeaturesProperties.add(centerPointMap);
//
// for(int i=1;i<200;i++){
// double x1 = Math.sin(Math.atan(t1))*side*i + center1.x;
// double y1 = - Math.cos(Math.atan(t1))*side*i + center1.y;
//
// Point p = geometryFactory.createPoint(new Coordinate(x1,y1));
//// double height = getElevation(p);
// boolean contains = p1.contains(p);
// if(contains){
// Map<String,Object> tmpPoint = new HashMap<>();
// centerPointList.add(p);
// tmpPoint.put("geom",p);
// tmpPoint.put("id",i);
// pointFeaturesProperties.add(tmpPoint);
// }else{
// break;
// }
// }
// 下一个点
// double nextX= center1.x + side*Math.cos(Math.atan(t1));
// double nextY = center1.y + side*Math.sin(Math.atan(t1));
// for(int i=1;i<200;i++){
// double x1 = Math.sin(Math.atan(t1))*side*i + nextX;
// double y1 = - Math.cos(Math.atan(t1))*side*i + nextY;
//
// Point p = geometryFactory.createPoint(new Coordinate(x1,y1));
//// double height = getElevation(p);
// boolean contains = p1.contains(p);
// if(contains){
// Map<String,Object> tmpPoint = new HashMap<>();
// centerPointList.add(p);
// tmpPoint.put("geom",p);
// tmpPoint.put("id",2000+i);
// pointFeaturesProperties.add(tmpPoint);
// }else{
// break;
// }
// }
Coordinate[] coordArr = new Coordinate[]{bgcoords[0],bgcoords[1],bgcoords[2]};
addPointSet(coordArr,geometryFactory,side,p1,pointFeaturesProperties,center);
final SimpleFeatureType PTYPE =createPointFeatureType();
DefaultFeatureCollection pointFeatureCollection = new DefaultFeatureCollection();
SimpleFeatureBuilder pointFeatureBuilder = new SimpleFeatureBuilder(PTYPE);
pointFeaturesProperties.forEach(props->{
pointFeatureBuilder.add(props.get("geom"));
pointFeatureBuilder.add(props.get("id"));
SimpleFeature feature = pointFeatureBuilder.buildFeature((String.valueOf(props.get("id"))));
pointFeatureCollection.add(feature);
});
// Create a map content and add our shapefile to it
MapContent map = new MapContent();
map.setTitle("填挖方计算-创建网格");
@ -173,13 +280,77 @@ public class CreateGrid {
Layer layer = new FeatureLayer(featureCollection, style);
map.addLayer(layer);
// 添加点
Style pointStyle = SLD.createSimpleStyle(pointFeatureCollection.getSchema());
Layer pointLlayer = new FeatureLayer(pointFeatureCollection, pointStyle);
map.addLayer(pointLlayer);
// 添加网格图层
Style styleBox = SLD.createSimpleStyle(grid.getSchema());
Layer layerBox = new FeatureLayer(grid, styleBox);
map.addLayer(layerBox);
// map.addLayer(layerBox);
// Now display the map
JMapFrame.showMap(map);
}
public static void addPointSet(Coordinate[] bgcoords,
GeometryFactory geometryFactory,
double side,
Polygon p1,
List<Map<String,Object>> pointFeaturesProperties,
Point startPoint){
Coordinate pc1 = bgcoords[0];
Coordinate pc2 = bgcoords[1];
Coordinate pc3 = bgcoords[2];
double t1 = (pc2.y - pc1.y)/(pc2.x-pc1.x);
double t2 = (pc3.y - pc2.y)/(pc3.x-pc2.x);
System.out.println("t1:"+t1);
System.out.println("t2:"+t2);
List<Geometry> centerPointList = new ArrayList();
Coordinate center1 = new Coordinate((pc1.x+pc2.x)/2,(pc1.y+pc2.y)/2);
Map<String,Object> centerPointMap = new HashMap<>();
Point center = geometryFactory.createPoint(center1);
centerPointList.add(center);
centerPointMap.put("geom",startPoint);
centerPointMap.put("id",UUID.randomUUID());
pointFeaturesProperties.add(centerPointMap);
for(int i=1;i<200;i++){
double x1 = Math.sin(Math.atan(t1))*side*i + startPoint.getX();
double y1 = - Math.cos(Math.atan(t1))*side*i + startPoint.getY();
Point p = geometryFactory.createPoint(new Coordinate(x1,y1));
boolean contains = p1.contains(p);
if(contains){
Map<String,Object> tmpPoint = new HashMap<>();
centerPointList.add(p);
tmpPoint.put("geom",p);
tmpPoint.put("id",UUID.randomUUID());
pointFeaturesProperties.add(tmpPoint);
}else{
break;
}
}
}
public static void loadDem() throws Exception{
File file = new File("src\\main\\resources\\public\\data\\ASTGTMV003_N23E113_dem.tif");
// Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER设置经度为第一轴顺序
GeoTiffReader reader = new GeoTiffReader(file, new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE));
GridCoverage2D coverage = reader.read(null);
//设置坐标系
CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4326");
// GridCoverage 进行重采样转换为另一个 CRS
CreateGrid.coverage = (GridCoverage2D) Operations.DEFAULT.resample(coverage, targetCRS);
}
public static double getElevation(Point p) throws Exception{
// 设置经纬度及坐标系等信息
Position position = new Position2D(p.getY(), p.getX());
GridGeometry2D gridGeometry = CreateGrid.coverage.getGridGeometry();
GridCoordinates2D gridPoint = gridGeometry.worldToGrid(position);
double[] elevation = new double[1];
CreateGrid.coverage.evaluate(gridPoint,elevation);
double height = elevation[0]; // 这里的高度就是查询位置的高程值
// System.out.println("---------"+height);//---------108.0
return height;
}
}