添加项目
This commit is contained in:
commit
81edf6c78c
|
|
@ -0,0 +1,3 @@
|
||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="CompilerConfiguration">
|
||||||
|
<annotationProcessing>
|
||||||
|
<profile name="Maven default annotation processors profile" enabled="true">
|
||||||
|
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||||
|
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||||
|
<outputRelativeToContentRoot value="true" />
|
||||||
|
<module name="CutFillDemo" />
|
||||||
|
</profile>
|
||||||
|
</annotationProcessing>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Encoding">
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
|
|
@ -0,0 +1,40 @@
|
||||||
|
<?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="Maven Central repository" />
|
||||||
|
<option name="url" value="https://repo1.maven.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="jboss.community" />
|
||||||
|
<option name="name" value="JBoss Community repository" />
|
||||||
|
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
|
||||||
|
</remote-repository>
|
||||||
|
<remote-repository>
|
||||||
|
<option name="id" value="osgeo-snapshot" />
|
||||||
|
<option name="name" value="OSGeo Snapshot Repository" />
|
||||||
|
<option name="url" value="https://repo.osgeo.org/repository/snapshot/" />
|
||||||
|
</remote-repository>
|
||||||
|
<remote-repository>
|
||||||
|
<option name="id" value="osgeo" />
|
||||||
|
<option name="name" value="OSGeo Release Repository" />
|
||||||
|
<option name="url" value="https://repo.osgeo.org/repository/release/" />
|
||||||
|
</remote-repository>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||||
|
<component name="MavenProjectsManager">
|
||||||
|
<option name="originalFiles">
|
||||||
|
<list>
|
||||||
|
<option value="$PROJECT_DIR$/pom.xml" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
|
|
@ -0,0 +1,86 @@
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>org.example</groupId>
|
||||||
|
<artifactId>CutFillDemo</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>CutFillDemo</name>
|
||||||
|
<url>http://maven.apache.org</url>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<geotools.version>32-SNAPSHOT</geotools.version>
|
||||||
|
</properties>
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>nexus-aliyun</id>
|
||||||
|
<name>nexus-aliyun</name>
|
||||||
|
<url>https://maven.aliyun.com/repository/public</url>
|
||||||
|
<releases>
|
||||||
|
<enabled>true</enabled>
|
||||||
|
</releases>
|
||||||
|
<snapshots>
|
||||||
|
<enabled>false</enabled>
|
||||||
|
</snapshots>
|
||||||
|
</repository>
|
||||||
|
<repository>
|
||||||
|
<id>osgeo</id>
|
||||||
|
<name>OSGeo Release Repository</name>
|
||||||
|
<url>https://repo.osgeo.org/repository/release/</url>
|
||||||
|
<snapshots><enabled>false</enabled></snapshots>
|
||||||
|
<releases><enabled>true</enabled></releases>
|
||||||
|
</repository>
|
||||||
|
<repository>
|
||||||
|
<id>osgeo-snapshot</id>
|
||||||
|
<name>OSGeo Snapshot Repository</name>
|
||||||
|
<url>https://repo.osgeo.org/repository/snapshot/</url>
|
||||||
|
<snapshots><enabled>true</enabled></snapshots>
|
||||||
|
<releases><enabled>false</enabled></releases>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
<version>2.0.4.RELEASE</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.geotools</groupId>
|
||||||
|
<artifactId>gt-epsg-hsql</artifactId>
|
||||||
|
<version>${geotools.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.geotools</groupId>
|
||||||
|
<artifactId>gt-geotiff</artifactId>
|
||||||
|
<version>${geotools.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.geotools</groupId>
|
||||||
|
<artifactId>gt-image</artifactId>
|
||||||
|
<version>${geotools.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.geotools</groupId>
|
||||||
|
<artifactId>gt-shapefile</artifactId>
|
||||||
|
<version>${geotools.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.geotools</groupId>
|
||||||
|
<artifactId>gt-wms</artifactId>
|
||||||
|
<version>${geotools.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.geotools</groupId>
|
||||||
|
<artifactId>gt-swing</artifactId>
|
||||||
|
<version>${geotools.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.geotools</groupId>
|
||||||
|
<artifactId>gt-grid</artifactId>
|
||||||
|
<version>${geotools.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
package org.example;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hello world!
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@SpringBootApplication
|
||||||
|
public class CutFillDemo
|
||||||
|
{
|
||||||
|
public static void main( String[] args )
|
||||||
|
{
|
||||||
|
SpringApplication.run(CutFillDemo.class,args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,62 @@
|
||||||
|
package org.example.test;
|
||||||
|
|
||||||
|
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.GridEnvelope2D;
|
||||||
|
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 java.awt.image.RenderedImage;
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
public class Caclelate {
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
|
||||||
|
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};
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
coverage = (GridCoverage2D) Operations.DEFAULT.resample(coverage, targetCRS);
|
||||||
|
|
||||||
|
CoordinateReferenceSystem crs = coverage.getCoordinateReferenceSystem2D();
|
||||||
|
|
||||||
|
// Envelope env = coverage.getEnvelope();
|
||||||
|
RenderedImage image = coverage.getRenderedImage();
|
||||||
|
// 设置经纬度及坐标系等信息
|
||||||
|
Position position = new Position2D(coor[1], coor[0]);
|
||||||
|
GridGeometry2D gridGeometry = coverage.getGridGeometry();
|
||||||
|
GridCoordinates2D gridPoint = gridGeometry.worldToGrid(position);
|
||||||
|
double[] elevation = new double[1];
|
||||||
|
coverage.evaluate(gridPoint,elevation);
|
||||||
|
double height = elevation[0]; // 这里的高度就是查询位置的高程值
|
||||||
|
System.out.println("---------"+height);//---------108.0
|
||||||
|
//
|
||||||
|
// // assume double
|
||||||
|
// float[] f1 = (float[]) coverage.evaluate(position);
|
||||||
|
// double[] doubleArray = new double[f1.length];
|
||||||
|
// for (int i = 0; i < f1.length; i++) {
|
||||||
|
// doubleArray[i] = (double) f1[i];
|
||||||
|
// }
|
||||||
|
// double[] sample = doubleArray;
|
||||||
|
//
|
||||||
|
// // resample with the same array
|
||||||
|
// sample = coverage.evaluate(position, sample);
|
||||||
|
// System.out.println(sample);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,185 @@
|
||||||
|
package org.example.test;
|
||||||
|
|
||||||
|
import org.example.utils.IntersectionBuilder;
|
||||||
|
import org.geotools.api.data.FeatureSource;
|
||||||
|
import org.geotools.api.data.FileDataStore;
|
||||||
|
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.referencing.crs.CoordinateReferenceSystem;
|
||||||
|
import org.geotools.api.style.Style;
|
||||||
|
import org.geotools.data.DataUtilities;
|
||||||
|
import org.geotools.data.collection.CollectionFeatureSource;
|
||||||
|
import org.geotools.data.collection.ListFeatureCollection;
|
||||||
|
import org.geotools.data.simple.SimpleFeatureCollection;
|
||||||
|
import org.geotools.data.store.EmptyFeatureCollection;
|
||||||
|
import org.geotools.feature.DefaultFeatureCollection;
|
||||||
|
import org.geotools.feature.FeatureCollection;
|
||||||
|
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.geometry.jts.JTSFactoryFinder;
|
||||||
|
import org.geotools.geometry.jts.ReferencedEnvelope;
|
||||||
|
import org.geotools.grid.Grids;
|
||||||
|
import org.geotools.map.FeatureLayer;
|
||||||
|
import org.geotools.map.Layer;
|
||||||
|
import org.geotools.map.MapContent;
|
||||||
|
import org.geotools.referencing.CRS;
|
||||||
|
import org.geotools.referencing.crs.DefaultGeographicCRS;
|
||||||
|
import org.geotools.styling.FillImpl;
|
||||||
|
import org.geotools.styling.SLD;
|
||||||
|
import org.geotools.swing.JMapFrame;
|
||||||
|
import org.geotools.swing.data.JFileDataStoreChooser;
|
||||||
|
import org.locationtech.jts.geom.*;
|
||||||
|
import org.locationtech.jts.operation.buffer.BufferOp;
|
||||||
|
import org.locationtech.jts.operation.buffer.BufferParameters;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class CreateGrid {
|
||||||
|
private static SimpleFeatureType createFeatureType() {
|
||||||
|
|
||||||
|
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
|
||||||
|
builder.setName("Location");
|
||||||
|
builder.setCRS(DefaultGeographicCRS.WGS84); // <- Coordinate reference system
|
||||||
|
|
||||||
|
// add attributes in order
|
||||||
|
builder.add("the_geom", Polygon.class);
|
||||||
|
builder.length(15).add("name", String.class); // <- 15 chars width for name field
|
||||||
|
builder.add("id", Integer.class);
|
||||||
|
|
||||||
|
// build the type
|
||||||
|
final SimpleFeatureType LOCATION = builder.buildFeatureType();
|
||||||
|
|
||||||
|
return LOCATION;
|
||||||
|
}
|
||||||
|
public static void main(String[] args) throws Exception{
|
||||||
|
|
||||||
|
// 假设我们使用WGS84坐标系统
|
||||||
|
CoordinateReferenceSystem crs = CRS.decode("EPSG:4326");
|
||||||
|
|
||||||
|
// 定义矩形的左下角和右上角坐标(WGS84)
|
||||||
|
List<double[]> coords = new ArrayList(){{
|
||||||
|
add(new double[]{113.52896835361318, 23.654018962759377});
|
||||||
|
add(new double[]{113.529270627066524, 23.654027766840539 });
|
||||||
|
add(new double[]{ 113.529294104616298, 23.653772448486752});
|
||||||
|
add(new double[]{ 113.528977157694356, 23.653763644405586 });
|
||||||
|
add(new double[]{113.52896835361318, 23.654018962759377});
|
||||||
|
}};
|
||||||
|
// 创建矩形的四个角点坐标
|
||||||
|
Coordinate[] coordinates = new Coordinate[5];
|
||||||
|
coordinates[0] = new Coordinate(coords.get(0)[0], coords.get(0)[1]);
|
||||||
|
coordinates[1] = new Coordinate(coords.get(1)[0], coords.get(1)[1]);
|
||||||
|
coordinates[2] = new Coordinate(coords.get(2)[0], coords.get(2)[1]);
|
||||||
|
coordinates[3] = new Coordinate(coords.get(3)[0], coords.get(3)[1]);
|
||||||
|
// 闭合多边形,复制第一个坐标作为最后一个坐标
|
||||||
|
coordinates[4] = new Coordinate(coords.get(0)[0], coords.get(0)[1]);
|
||||||
|
// coordinates[0] = new Coordinate(0, 0);
|
||||||
|
// coordinates[1] = new Coordinate(0, 30);
|
||||||
|
// coordinates[2] = new Coordinate(30, 30);
|
||||||
|
// coordinates[3] = new Coordinate(30, 0);
|
||||||
|
// // 闭合多边形,复制第一个坐标作为最后一个坐标
|
||||||
|
// coordinates[4] = new Coordinate(0, 0);
|
||||||
|
// 创建GeometryFactory
|
||||||
|
GeometryFactory geometryFactory = new GeometryFactory();
|
||||||
|
List<Map<String,Object>> featuresProperties = new ArrayList<>();
|
||||||
|
|
||||||
|
// 创建多边形(矩形)
|
||||||
|
Map innerPolygon = new HashMap<>();
|
||||||
|
Polygon rectangle = geometryFactory.createPolygon(coordinates);
|
||||||
|
innerPolygon.put("geom",rectangle);
|
||||||
|
innerPolygon.put("name","输入的面");
|
||||||
|
innerPolygon.put("id",1);
|
||||||
|
featuresProperties.add(innerPolygon);
|
||||||
|
|
||||||
|
// 缓冲区建立,外扩100m
|
||||||
|
double meter = 100;
|
||||||
|
double degree = meter/(2*Math.PI*6371004)*360;
|
||||||
|
|
||||||
|
BufferParameters bufferParameters = new BufferParameters();
|
||||||
|
bufferParameters.setEndCapStyle(BufferParameters.CAP_FLAT);
|
||||||
|
bufferParameters.setJoinStyle(BufferParameters.JOIN_MITRE);
|
||||||
|
BufferOp bufOp = new BufferOp(rectangle,bufferParameters);
|
||||||
|
Geometry bg = bufOp.getResultGeometry(degree);
|
||||||
|
|
||||||
|
// 构造外边界
|
||||||
|
Coordinate[] pcoords = rectangle.getCoordinates();
|
||||||
|
Coordinate[] bgcoords = bg.getCoordinates();
|
||||||
|
|
||||||
|
Map 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);
|
||||||
|
extPolygon1.put("name","第一个面");
|
||||||
|
extPolygon1.put("id",2);
|
||||||
|
featuresProperties.add(extPolygon1);
|
||||||
|
|
||||||
|
Map 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);
|
||||||
|
//
|
||||||
|
Map 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);
|
||||||
|
|
||||||
|
Map 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);
|
||||||
|
|
||||||
|
final SimpleFeatureType TYPE =createFeatureType();
|
||||||
|
DefaultFeatureCollection featureCollection = new DefaultFeatureCollection();
|
||||||
|
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
|
||||||
|
|
||||||
|
featuresProperties.forEach(props->{
|
||||||
|
featureBuilder.add(props.get("geom"));
|
||||||
|
featureBuilder.add(props.get("name"));
|
||||||
|
featureBuilder.add(props.get("id"));
|
||||||
|
SimpleFeature feature = featureBuilder.buildFeature((String.valueOf(props.get("id"))));
|
||||||
|
featureCollection.add(feature);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
// ReferencedEnvelope bounds = featureCollection.getBounds();
|
||||||
|
// 创建grids,0.2度为间隔
|
||||||
|
double side = 1/(2*Math.PI*6371004)*360;
|
||||||
|
ListFeatureCollection collection = new ListFeatureCollection(TYPE);
|
||||||
|
Iterator<SimpleFeature> iterator = featureCollection.stream().iterator();
|
||||||
|
iterator.next();
|
||||||
|
collection.add(iterator.next());
|
||||||
|
SimpleFeatureSource source = new CollectionFeatureSource(collection);
|
||||||
|
IntersectionBuilder intersectionBuilder = new IntersectionBuilder(TYPE, source);
|
||||||
|
SimpleFeatureSource grid = Grids.createSquareGrid(collection.getBounds(), side, -1);
|
||||||
|
|
||||||
|
// Create a map content and add our shapefile to it
|
||||||
|
MapContent map = new MapContent();
|
||||||
|
map.setTitle("填挖方计算-创建网格");
|
||||||
|
// 添加初始范围和扩展面图层
|
||||||
|
Style style = SLD.createSimpleStyle(featureCollection.getSchema());
|
||||||
|
Layer layer = new FeatureLayer(featureCollection, style);
|
||||||
|
map.addLayer(layer);
|
||||||
|
|
||||||
|
// 添加网格图层
|
||||||
|
Style styleBox = SLD.createSimpleStyle(grid.getSchema());
|
||||||
|
Layer layerBox = new FeatureLayer(grid, styleBox);
|
||||||
|
map.addLayer(layerBox);
|
||||||
|
|
||||||
|
// Now display the map
|
||||||
|
JMapFrame.showMap(map);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,53 @@
|
||||||
|
package org.example.tutorial;
|
||||||
|
|
||||||
|
import org.geotools.api.data.FileDataStore;
|
||||||
|
import org.geotools.api.data.FileDataStoreFinder;
|
||||||
|
import org.geotools.api.data.SimpleFeatureSource;
|
||||||
|
import org.geotools.api.style.Style;
|
||||||
|
import org.geotools.data.DataUtilities;
|
||||||
|
import org.geotools.data.collection.SpatialIndexFeatureCollection;
|
||||||
|
import org.geotools.map.FeatureLayer;
|
||||||
|
import org.geotools.map.Layer;
|
||||||
|
import org.geotools.map.MapContent;
|
||||||
|
import org.geotools.styling.SLD;
|
||||||
|
import org.geotools.swing.JMapFrame;
|
||||||
|
import org.geotools.swing.data.JFileDataStoreChooser;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
public class Lession01 {
|
||||||
|
private static final Logger LOGGER = org.geotools.util.logging.Logging.getLogger(Lession01.class);
|
||||||
|
/**
|
||||||
|
* GeoTools Quickstart demo application. Prompts the user for a shapefile and displays its
|
||||||
|
* contents on the screen in a map frame
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
// display a data store file chooser dialog for shapefiles
|
||||||
|
LOGGER.info( "Quickstart");
|
||||||
|
LOGGER.config( "Welcome Developers");
|
||||||
|
LOGGER.info("java.util.logging.config.file="+System.getProperty("java.util.logging.config.file"));
|
||||||
|
File file = JFileDataStoreChooser.showOpenFile("shp", null);
|
||||||
|
if (file == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
LOGGER.config("File selected "+file);
|
||||||
|
|
||||||
|
FileDataStore store = FileDataStoreFinder.getDataStore(file);
|
||||||
|
SimpleFeatureSource featureSource = store.getFeatureSource();
|
||||||
|
// 缓存shp文件
|
||||||
|
SimpleFeatureSource cachedSource =
|
||||||
|
DataUtilities.source(
|
||||||
|
new SpatialIndexFeatureCollection(featureSource.getFeatures()));
|
||||||
|
// Create a map content and add our shapefile to it
|
||||||
|
MapContent map = new MapContent();
|
||||||
|
map.setTitle("Quickstart");
|
||||||
|
|
||||||
|
Style style = SLD.createSimpleStyle(featureSource.getSchema());
|
||||||
|
Layer layer = new FeatureLayer(cachedSource, style);
|
||||||
|
map.addLayer(layer);
|
||||||
|
|
||||||
|
// Now display the map
|
||||||
|
JMapFrame.showMap(map);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,208 @@
|
||||||
|
package org.example.tutorial;
|
||||||
|
|
||||||
|
import org.geotools.api.data.SimpleFeatureSource;
|
||||||
|
import org.geotools.api.data.SimpleFeatureStore;
|
||||||
|
import org.geotools.api.data.Transaction;
|
||||||
|
import org.geotools.api.feature.simple.SimpleFeature;
|
||||||
|
import org.geotools.api.feature.simple.SimpleFeatureType;
|
||||||
|
import org.geotools.data.DataUtilities;
|
||||||
|
import org.geotools.data.DefaultTransaction;
|
||||||
|
import org.geotools.data.collection.ListFeatureCollection;
|
||||||
|
import org.geotools.data.shapefile.ShapefileDataStore;
|
||||||
|
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
|
||||||
|
import org.geotools.data.simple.SimpleFeatureCollection;
|
||||||
|
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.geotools.swing.data.JFileDataStoreChooser;
|
||||||
|
import org.locationtech.jts.geom.Coordinate;
|
||||||
|
import org.locationtech.jts.geom.GeometryFactory;
|
||||||
|
import org.locationtech.jts.geom.Point;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class Lession02_Feature {
|
||||||
|
public static void main(String[] args) throws Exception{
|
||||||
|
// Set cross-platform look & feel for compatability
|
||||||
|
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
|
||||||
|
|
||||||
|
// File file = JFileDataStoreChooser.showOpenFile("csv", null);
|
||||||
|
// if (file == null) {
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
File file = new File("src\\main\\resources\\public\\data\\location.csv");
|
||||||
|
|
||||||
|
/*
|
||||||
|
* We use the DataUtilities class to create a FeatureType that will describe the data in our
|
||||||
|
* shapefile.
|
||||||
|
*
|
||||||
|
* See also the createFeatureType method below for another, more flexible approach.
|
||||||
|
*/
|
||||||
|
final SimpleFeatureType TYPE =
|
||||||
|
DataUtilities.createType(
|
||||||
|
"Location",
|
||||||
|
"the_geom:Point:srid=4326,"
|
||||||
|
+ // <- the geometry attribute: Point type
|
||||||
|
"name:String,"
|
||||||
|
+ // <- a String attribute
|
||||||
|
"number:Integer" // a number attribute
|
||||||
|
);
|
||||||
|
System.out.println("TYPE:" + TYPE);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* A list to collect features as we create them.
|
||||||
|
*/
|
||||||
|
List<SimpleFeature> features = new ArrayList<>();
|
||||||
|
/*
|
||||||
|
* 使用 SimpleFeatureBuilder 创建要素 feature
|
||||||
|
* GeometryFactory will be used to create the geometry attribute of each feature,
|
||||||
|
* using a Point object for the location.
|
||||||
|
*/
|
||||||
|
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
|
||||||
|
|
||||||
|
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
|
||||||
|
|
||||||
|
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
|
||||||
|
/* First line of the data file is the header */
|
||||||
|
String line = reader.readLine();
|
||||||
|
System.out.println("Header: " + line);
|
||||||
|
|
||||||
|
for (line = reader.readLine(); line != null; line = reader.readLine()) {
|
||||||
|
if (line.trim().length() > 0) { // skip blank lines
|
||||||
|
String[] tokens = line.split("\\,");
|
||||||
|
|
||||||
|
double latitude = Double.parseDouble(tokens[0]);
|
||||||
|
double longitude = Double.parseDouble(tokens[1]);
|
||||||
|
String name = tokens[2].trim();
|
||||||
|
int number = Integer.parseInt(tokens[3].trim());
|
||||||
|
|
||||||
|
/* Longitude (= x coord) first ! */
|
||||||
|
Point point = geometryFactory.createPoint(new Coordinate(longitude, latitude));
|
||||||
|
|
||||||
|
featureBuilder.add(point);
|
||||||
|
featureBuilder.add(name);
|
||||||
|
featureBuilder.add(number);
|
||||||
|
SimpleFeature feature = featureBuilder.buildFeature(null);
|
||||||
|
features.add(feature);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* Get an output file name and create the new shapefile
|
||||||
|
*/
|
||||||
|
File newFile = getNewShapeFile(file);
|
||||||
|
|
||||||
|
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
|
||||||
|
|
||||||
|
Map<String, Serializable> params = new HashMap<>();
|
||||||
|
params.put("url", newFile.toURI().toURL());
|
||||||
|
params.put("create spatial index", Boolean.TRUE);
|
||||||
|
|
||||||
|
ShapefileDataStore newDataStore =
|
||||||
|
(ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
|
||||||
|
/*
|
||||||
|
* TYPE is used as a template to describe the file contents
|
||||||
|
*/
|
||||||
|
newDataStore.createSchema(TYPE);
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Write the features to the shapefile
|
||||||
|
*/
|
||||||
|
Transaction transaction = new DefaultTransaction("create");
|
||||||
|
|
||||||
|
String typeName = newDataStore.getTypeNames()[0];
|
||||||
|
SimpleFeatureSource featureSource = newDataStore.getFeatureSource(typeName);
|
||||||
|
SimpleFeatureType SHAPE_TYPE = featureSource.getSchema();
|
||||||
|
/*
|
||||||
|
* The Shapefile format has a couple limitations:
|
||||||
|
* - "the_geom" is always first, and used for the geometry attribute name
|
||||||
|
* - "the_geom" must be of type Point, MultiPoint, MuiltiLineString, MultiPolygon
|
||||||
|
* - Attribute names are limited in length
|
||||||
|
* - Not all data types are supported (example Timestamp represented as Date)
|
||||||
|
*
|
||||||
|
* Each data store has different limitations so check the resulting SimpleFeatureType.
|
||||||
|
*/
|
||||||
|
System.out.println("SHAPE:" + SHAPE_TYPE);
|
||||||
|
|
||||||
|
if (featureSource instanceof SimpleFeatureStore) {
|
||||||
|
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
|
||||||
|
/*
|
||||||
|
* SimpleFeatureStore has a method to add features from a
|
||||||
|
* SimpleFeatureCollection object, so we use the ListFeatureCollection
|
||||||
|
* class to wrap our list of features.
|
||||||
|
*/
|
||||||
|
SimpleFeatureCollection collection = new ListFeatureCollection(TYPE, features);
|
||||||
|
featureStore.setTransaction(transaction);
|
||||||
|
try {
|
||||||
|
featureStore.addFeatures(collection);
|
||||||
|
transaction.commit();
|
||||||
|
} catch (Exception problem) {
|
||||||
|
problem.printStackTrace();
|
||||||
|
transaction.rollback();
|
||||||
|
} finally {
|
||||||
|
transaction.close();
|
||||||
|
}
|
||||||
|
System.exit(0); // success!
|
||||||
|
} else {
|
||||||
|
System.out.println(typeName + " does not support read/write access");
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Prompt the user for the name and path to use for the output shapefile
|
||||||
|
*
|
||||||
|
* @param csvFile the input csv file used to create a default shapefile name
|
||||||
|
* @return name and path for the shapefile as a new File object
|
||||||
|
*/
|
||||||
|
private static File getNewShapeFile(File csvFile) {
|
||||||
|
String path = csvFile.getAbsolutePath();
|
||||||
|
String newPath = path.substring(0, path.length() - 4) + ".shp";
|
||||||
|
|
||||||
|
JFileDataStoreChooser chooser = new JFileDataStoreChooser("shp");
|
||||||
|
chooser.setDialogTitle("Save shapefile");
|
||||||
|
chooser.setSelectedFile(new File(newPath));
|
||||||
|
|
||||||
|
int returnVal = chooser.showSaveDialog(null);
|
||||||
|
|
||||||
|
if (returnVal != JFileDataStoreChooser.APPROVE_OPTION) {
|
||||||
|
// the user cancelled the dialog
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
File newFile = chooser.getSelectedFile();
|
||||||
|
if (newFile.equals(csvFile)) {
|
||||||
|
System.out.println("Error: cannot replace " + csvFile);
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
return newFile;
|
||||||
|
}
|
||||||
|
private static SimpleFeatureType createFeatureType() {
|
||||||
|
|
||||||
|
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("number", Integer.class);
|
||||||
|
|
||||||
|
// build the type
|
||||||
|
final SimpleFeatureType LOCATION = builder.buildFeatureType();
|
||||||
|
|
||||||
|
return LOCATION;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,49 @@
|
||||||
|
package org.example.utils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Map;
|
||||||
|
import org.geotools.api.data.SimpleFeatureSource;
|
||||||
|
import org.geotools.api.feature.simple.SimpleFeatureType;
|
||||||
|
import org.geotools.api.filter.Filter;
|
||||||
|
import org.geotools.api.filter.FilterFactory;
|
||||||
|
import org.geotools.factory.CommonFactoryFinder;
|
||||||
|
import org.geotools.geometry.jts.JTSFactoryFinder;
|
||||||
|
import org.geotools.grid.GridElement;
|
||||||
|
import org.geotools.grid.GridFeatureBuilder;
|
||||||
|
import org.geotools.grid.PolygonElement;
|
||||||
|
import org.locationtech.jts.geom.Coordinate;
|
||||||
|
import org.locationtech.jts.geom.Geometry;
|
||||||
|
import org.locationtech.jts.geom.GeometryFactory;
|
||||||
|
|
||||||
|
public class IntersectionBuilder extends GridFeatureBuilder {
|
||||||
|
final FilterFactory ff2 = CommonFactoryFinder.getFilterFactory();
|
||||||
|
final GeometryFactory gf = JTSFactoryFinder.getGeometryFactory();
|
||||||
|
|
||||||
|
final SimpleFeatureSource source;
|
||||||
|
int id = 0;
|
||||||
|
|
||||||
|
public IntersectionBuilder(SimpleFeatureType type, SimpleFeatureSource source) {
|
||||||
|
super(type);
|
||||||
|
this.source = source;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAttributes(GridElement el, Map<String, Object> attributes) {
|
||||||
|
attributes.put("id", ++id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getCreateFeature(GridElement el) {
|
||||||
|
Coordinate c = ((PolygonElement) el).getCenter();
|
||||||
|
Geometry p = gf.createPoint(c);
|
||||||
|
Filter filter = ff2.intersects(ff2.property("the_geom"), ff2.literal(p));
|
||||||
|
boolean result = false;
|
||||||
|
|
||||||
|
try {
|
||||||
|
result = !source.getFeatures(filter).isEmpty();
|
||||||
|
} catch (IOException ex) {
|
||||||
|
throw new IllegalStateException(ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
Binary file not shown.
Binary file not shown.
|
|
@ -0,0 +1,8 @@
|
||||||
|
{
|
||||||
|
"type": "FeatureCollection",
|
||||||
|
"name": "bound1",
|
||||||
|
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
|
||||||
|
"features": [
|
||||||
|
{ "type": "Feature", "properties": { "id": 1, "name": "1" }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 113.52896835361318, 23.654018962759377 ], [ 113.529270627066524, 23.654027766840539 ], [ 113.529270627066524, 23.654027766840539 ], [ 113.529294104616298, 23.653772448486752 ], [ 113.528977157694356, 23.653763644405586 ], [ 113.52896835361318, 23.654018962759377 ] ] ] ] } }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
LAT, LON, CITY, NUMBER
|
||||||
|
46.066667, 11.116667, Trento, 140
|
||||||
|
44.9441, -93.0852, St Paul, 125
|
||||||
|
13.752222, 100.493889, Bangkok, 150
|
||||||
|
45.420833, -75.69, Ottawa, 200
|
||||||
|
44.9801, -93.251867, Minneapolis, 350
|
||||||
|
46.519833, 6.6335, Lausanne, 560
|
||||||
|
48.428611, -123.365556, Victoria, 721
|
||||||
|
-33.925278, 18.423889, Cape Town, 550
|
||||||
|
-33.859972, 151.211111, Sydney, 436
|
||||||
|
41.383333, 2.183333, Barcelona, 914
|
||||||
|
39.739167, -104.984722, Denver, 869
|
||||||
|
52.95, -1.133333, Nottingham, 800
|
||||||
|
45.52, -122.681944, Portland, 840
|
||||||
|
37.5667,129.681944,Seoul,473
|
||||||
|
50.733992,7.099814,Bonn,700,2016
|
||||||
|
Can't render this file because it has a wrong number of fields in line 16.
|
Binary file not shown.
Binary file not shown.
|
|
@ -0,0 +1 @@
|
||||||
|
GEOGCS["WGS 84", DATUM["World Geodetic System 1984", SPHEROID["WGS 84", 6378137.0, 298.257223563, AUTHORITY["EPSG","7030"]], AUTHORITY["EPSG","6326"]], PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]], UNIT["degree", 0.017453292519943295], AXIS["Geodetic latitude", NORTH], AXIS["Geodetic longitude", EAST], AUTHORITY["EPSG","4326"]]
|
||||||
Binary file not shown.
Binary file not shown.
|
|
@ -0,0 +1,38 @@
|
||||||
|
package org.example;
|
||||||
|
|
||||||
|
import junit.framework.Test;
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
import junit.framework.TestSuite;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unit test for simple App.
|
||||||
|
*/
|
||||||
|
public class AppTest
|
||||||
|
extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Create the test case
|
||||||
|
*
|
||||||
|
* @param testName name of the test case
|
||||||
|
*/
|
||||||
|
public AppTest( String testName )
|
||||||
|
{
|
||||||
|
super( testName );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the suite of tests being tested
|
||||||
|
*/
|
||||||
|
public static Test suite()
|
||||||
|
{
|
||||||
|
return new TestSuite( AppTest.class );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rigourous Test :-)
|
||||||
|
*/
|
||||||
|
public void testApp()
|
||||||
|
{
|
||||||
|
assertTrue( true );
|
||||||
|
}
|
||||||
|
}
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -0,0 +1,8 @@
|
||||||
|
{
|
||||||
|
"type": "FeatureCollection",
|
||||||
|
"name": "bound1",
|
||||||
|
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
|
||||||
|
"features": [
|
||||||
|
{ "type": "Feature", "properties": { "id": 1, "name": "1" }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 113.52896835361318, 23.654018962759377 ], [ 113.529270627066524, 23.654027766840539 ], [ 113.529270627066524, 23.654027766840539 ], [ 113.529294104616298, 23.653772448486752 ], [ 113.528977157694356, 23.653763644405586 ], [ 113.52896835361318, 23.654018962759377 ] ] ] ] } }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
LAT, LON, CITY, NUMBER
|
||||||
|
46.066667, 11.116667, Trento, 140
|
||||||
|
44.9441, -93.0852, St Paul, 125
|
||||||
|
13.752222, 100.493889, Bangkok, 150
|
||||||
|
45.420833, -75.69, Ottawa, 200
|
||||||
|
44.9801, -93.251867, Minneapolis, 350
|
||||||
|
46.519833, 6.6335, Lausanne, 560
|
||||||
|
48.428611, -123.365556, Victoria, 721
|
||||||
|
-33.925278, 18.423889, Cape Town, 550
|
||||||
|
-33.859972, 151.211111, Sydney, 436
|
||||||
|
41.383333, 2.183333, Barcelona, 914
|
||||||
|
39.739167, -104.984722, Denver, 869
|
||||||
|
52.95, -1.133333, Nottingham, 800
|
||||||
|
45.52, -122.681944, Portland, 840
|
||||||
|
37.5667,129.681944,Seoul,473
|
||||||
|
50.733992,7.099814,Bonn,700,2016
|
||||||
|
Can't render this file because it has a wrong number of fields in line 16.
|
Binary file not shown.
Binary file not shown.
|
|
@ -0,0 +1 @@
|
||||||
|
GEOGCS["WGS 84", DATUM["World Geodetic System 1984", SPHEROID["WGS 84", 6378137.0, 298.257223563, AUTHORITY["EPSG","7030"]], AUTHORITY["EPSG","6326"]], PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]], UNIT["degree", 0.017453292519943295], AXIS["Geodetic latitude", NORTH], AXIS["Geodetic longitude", EAST], AUTHORITY["EPSG","4326"]]
|
||||||
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue