火点重构-完善地物类型百分比

This commit is contained in:
liuchengqian 2023-04-13 17:21:33 +08:00
parent 966835c304
commit e2a7b8aff8
3 changed files with 263 additions and 0 deletions

90
pom.xml
View File

@ -35,6 +35,7 @@
<dysmsapi20170525.version>2.0.4</dysmsapi20170525.version> <dysmsapi20170525.version>2.0.4</dysmsapi20170525.version>
<hutool-all.version>4.4.3</hutool-all.version> <hutool-all.version>4.4.3</hutool-all.version>
<tus-client.version>0.4.0</tus-client.version> <tus-client.version>0.4.0</tus-client.version>
<geotools.version>28.2</geotools.version>
</properties> </properties>
<dependencies> <dependencies>
@ -255,8 +256,97 @@
<version>1.13.1</version> <version>1.13.1</version>
</dependency> </dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-xml</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-main</artifactId>
<version>${geotools.version}</version>
<exclusions>
<exclusion>
<artifactId>jts-core</artifactId>
<groupId>org.locationtech.jts</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.geotools.xsd</groupId>
<artifactId>gt-xsd-sld</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-swing</artifactId>
<version>${geotools.version}</version>
<exclusions>
<exclusion>
<artifactId>jts-core</artifactId>
<groupId>org.locationtech.jts</groupId>
</exclusion>
<exclusion>
<artifactId>guava</artifactId>
<groupId>com.google.guava</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geojson</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-epsg-hsql</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools.jdbc</groupId>
<artifactId>gt-jdbc-postgis</artifactId>
<version>${geotools.version}</version>
</dependency>
</dependencies> </dependencies>
<repositories>
<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>
<build> <build>
<resources> <resources>
<resource> <resource>

View File

@ -0,0 +1,123 @@
package com.xkrs.utilsnew;
import org.apache.hc.core5.util.TextUtils;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.filter.text.cql2.CQL;
import org.geotools.filter.text.cql2.CQLException;
import org.locationtech.jts.geom.*;
import org.locationtech.jts.operation.buffer.BufferParameters;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.filter.Filter;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class BufferAnalysisUtils {
private BufferAnalysisUtils() {
}
public static void main(String[] args) throws CQLException {
String shapeFilePath = "/Users/liuchengqian/Desktop/data/shandong_land_type_2022.shp";
SimpleFeatureCollection simpleFeatureCollection = ShpFileUtils.readShpFileFeatureCollection(shapeFilePath);
double longitude = 116.218;//经度
double latitude = 36.0852;//纬度
double distance = getBufferDistance(1500L);// buffer 宽度 1.5公里
GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(PrecisionModel.FLOATING), 4326);
Coordinate[] coordinates = new Coordinate[]{new Coordinate(longitude, latitude)};
Point point = new Point(geometryFactory.getCoordinateSequenceFactory().create(coordinates), geometryFactory);
Geometry pointBuffer = point.buffer(distance, 40, BufferParameters.CAP_ROUND);
String pointBufferWkt = pointBuffer.toText();
double pointBufferArea = pointBuffer.getArea();//总面积
// 相交过滤
Filter intersectsFilter = CQL.toFilter(String.format("INTERSECTS(the_geom, %s)", pointBufferWkt));
SimpleFeatureCollection filterSimpleFeatureCollection = simpleFeatureCollection.subCollection(intersectsFilter);
//地物类型对应的字段
String landTypeProperty = "tblxmc";
String defaultLandType = "其它";
List<String> targetLandTypeList = new ArrayList<>();
targetLandTypeList.add("林地");
targetLandTypeList.add("农作物");
targetLandTypeList.add("水域");
targetLandTypeList.add("建筑物");
targetLandTypeList.add("水产养殖设施农用地");
// targetLandTypeList.add("其他土地");
Map<String, Double> targetLandTypeAreaMap = new HashMap<>();
for (String targetLandType : targetLandTypeList) {
targetLandTypeAreaMap.put(targetLandType, 0D);
}
SimpleFeatureIterator featureIterator = filterSimpleFeatureCollection.features();
while (featureIterator.hasNext()) {
try {
SimpleFeature simpleFeature = featureIterator.next();
if (simpleFeature == null) {
System.out.println("simpleFeature == null");
continue;
}
Geometry geometry = (Geometry) simpleFeature.getDefaultGeometry();
if (geometry == null) {
System.out.println("geometry == null");
continue;
}
String landType = simpleFeature.getProperty(landTypeProperty).getValue().toString().trim();
if (TextUtils.isEmpty(landType) || (!targetLandTypeAreaMap.containsKey(landType))) {
System.out.println("!targetLandTypeMap.containsKey(landType) landType = " + landType);
continue;
}
// 检查多边形是否存在自相交或重叠的情况如果有则修复
Geometry geometryBuffer = geometry.buffer(0D);
if (!pointBuffer.intersects(geometryBuffer)) {
System.out.println("!pointBuffer.intersects(geometryBuffer)");
continue;
}
Geometry intersectGeometry = pointBuffer.intersection(geometryBuffer);
if (intersectGeometry == null) {
System.out.println("intersectGeometry == null");
continue;
}
double intersectArea = intersectGeometry.getArea();
if (intersectArea < 0D) {
System.out.println("intersectArea < 0");
continue;
}
targetLandTypeAreaMap.put(landType, targetLandTypeAreaMap.get(landType) + intersectArea);
} catch (Exception e) {
e.printStackTrace();
}
}
double targetLandAreaSum = 0D;
StringBuilder builder = new StringBuilder();
for (String targetLandType : targetLandTypeList) {
double currentArea = targetLandTypeAreaMap.get(targetLandType);
targetLandAreaSum += currentArea;
builder.append(targetLandType);
builder.append("=");
builder.append(formatDoublePer(currentArea / pointBufferArea));
builder.append(", ");
}
builder.append(defaultLandType);
builder.append("=");
builder.append(formatDoublePer((pointBufferArea - targetLandAreaSum) / pointBufferArea));
String result = builder.toString();
System.out.println(result);
}
public static final int RADIUS = 6377830;
private static double getBufferDistance(long meter) {
return meter * 180.D / Math.PI / RADIUS;
}
private static final DecimalFormat decimalFormat = new DecimalFormat("0.00");
private static String formatDoublePer(double value) {
return decimalFormat.format(Math.abs(value) * 100D) + "%";
}
}

View File

@ -0,0 +1,50 @@
package com.xkrs.utilsnew;
import org.geotools.data.FeatureSource;
import org.geotools.data.Query;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.opengis.feature.type.AttributeDescriptor;
import org.opengis.feature.type.GeometryType;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
public class ShpFileUtils {
private ShpFileUtils() {
}
public static SimpleFeatureCollection readShpFileFeatureCollection(String shpPath) {
File shpFile = new File(shpPath);
SimpleFeatureCollection simpleFeatureCollection = null;
try {
ShapefileDataStore shapefileDataStore = new ShapefileDataStore(shpFile.toURI().toURL());
// 设置编码,防止属性的中文字符出现乱码
shapefileDataStore.setCharset(StandardCharsets.UTF_8);
// 这个typeNamae不传递默认是文件名称
FeatureSource featuresource = shapefileDataStore.getFeatureSource(shapefileDataStore.getTypeNames()[0]);
// 读取bbox
ReferencedEnvelope bbox = featuresource.getBounds();
// 读取投影
CoordinateReferenceSystem crs = featuresource.getSchema().getCoordinateReferenceSystem();
// 特征总数
int count = featuresource.getCount(Query.ALL);
// 获取当前数据的geometry类型线
GeometryType geometryType = featuresource.getSchema().getGeometryDescriptor().getType();
// 读取要素
simpleFeatureCollection = (SimpleFeatureCollection) featuresource.getFeatures();
// 获取当前矢量数据有哪些属性字段值
List<AttributeDescriptor> attributes = simpleFeatureCollection.getSchema().getAttributeDescriptors();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("读取完成!");
return simpleFeatureCollection;
}
}