1.导入依赖
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-main</artifactId>
<version>28.3</version>
</dependency>
2.编写工具类
package org.jmis.riskassess.util;
import lombok.Data;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.referencing.GeodeticCalculator;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
@Data
public class MineLocator {
private GeometryFactory geometryFactory;
private List<Mine> mines;
public MineLocator(List<Mine> mines) {
this.mines = mines;
this.geometryFactory = JTSFactoryFinder.getGeometryFactory();
}
@Data
public static class Mine {
private String name;
private double longitude;
private double latitude;
private String leader;
private String isBlast;
private String isRoofFall;
private String isEffect;
private String other;
private String acceptAlarmPerson;
private String acceptAlarmTime;
public Mine(String name, Double longitude, Double latitude, String leader, String isBlast, String isRoofFall, String isEffect, String other, String acceptAlarmPerson, String acceptAlarmTime) {
this.name = name;
this.longitude = longitude;
this.latitude = latitude;
this.leader = leader;
this.isBlast = isBlast;
this.isRoofFall = isRoofFall;
this.other = other;
this.acceptAlarmPerson = acceptAlarmPerson;
this.acceptAlarmTime = acceptAlarmTime;
this.isEffect = isEffect;
}
}
// 获取方圆范围内的矿名称和坐标
public List<Mine> getMinesInRadius(Point center, double radius) {
List<Mine> minesInRadius = new ArrayList<>();
// 计算方圆范围的几何对象
GeodeticCalculator calculator = new GeodeticCalculator();
calculator.setStartingGeographicPoint(center.getX(), center.getY());
calculator.setDirection(0, radius); // 方向为0度,距离为给定半径
Coordinate[] coordinates = new Coordinate[33];
for (int i = 0; i < 33; i++) {
double azimuth = i * 360.0 / 32.0;
calculator.setDirection(azimuth, radius);
Point2D result = calculator.getDestinationGeographicPoint();
coordinates[i] = new Coordinate(result.getX(), result.getY());
}
Geometry circle = geometryFactory.createPolygon(coordinates);
// 查找方圆范围内的矿名称和坐标
for (Mine mine : mines) {
Point minePoint = geometryFactory.createPoint(new Coordinate(mine.getLongitude(), mine.getLatitude()));
if (circle.contains(minePoint)) {
minesInRadius.add(mine);
}
}
return minesInRadius;
}
}