I am a Java developer from China. I recently encountered a problem. I have two coordinate points, AB, with both longitude and latitude. I also have a wind direction with an azimuth angle of 0 degrees due north. Now I want to point A As the center point, give the point a radius r and a fan angle d. The azimuth angle of the wind direction is the bisector of the fan angle. Then I check whether point b is inside the fan-shaped area. I don’t know much about geography. Is there any class library or Can the tool class help me implement this function? I have some problems with coordinate calculation in my current code. Can anyone help me? Thank you.
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Polygon;
public class PollutionTracingUtil {
private static final double EARTH_RADIUS = 6371.009;
private static final GeometryFactory geometryFactory = new GeometryFactory();
public static boolean isInSector(
double lng1, double lat1, double lng2, double lat2,
double windDirection,
double distance, double sectorAngle
) {
// 示例数据:点B的经纬度坐标
Coordinate pointB = new Coordinate(lng2, lat2);
Coordinate pointA = new Coordinate(lng1, lat1);
return isInSector(pointA, pointB, windDirection, distance, sectorAngle);
}
public static boolean isInSector(Coordinate pointA, Coordinate pointB, double windDirection,
double distance, double sectorAngle) {
Polygon sectorPolygon = createSectorPolygon(pointA, distance, windDirection, sectorAngle);
// 判断点B是否在扇形范围内
return sectorPolygon.contains(geometryFactory.createPoint(pointB));
}
private static Polygon createSectorPolygon(Coordinate center, double distance,
double direction, double sectorAngle) {
// 扇形的顶点坐标
// 计算扇形的起始角度和终止角度
double startAngle = normalizeAngle(direction - sectorAngle / 2);
double endAngle = normalizeAngle(direction + sectorAngle / 2);
System.out.println(sectorAngle);
System.out.println(endAngle);
// 计算扇形的顶点坐标
Coordinate[] sectorVertices = calculateSectorVertices(center, distance, startAngle, endAngle);
System.out.println(startAngle);
System.out.println(endAngle);
// 扇形的起始点
sectorVertices[0] = center;
sectorVertices[1] = calculateVertex(center, distance, startAngle);
// 扇形的终止点
sectorVertices[2] = calculateVertex(center, distance, endAngle);
sectorVertices[3] = center;
// 创建扇形范围的多边形对象
// 打印扇形的顶点坐标
for (Coordinate vertex : sectorVertices) {
System.out.println("Vertex: " + vertex.x + ", " + vertex.y);
}
return geometryFactory.createPolygon(sectorVertices);
}
private static double normalizeAngle(double angle) {
// 角度归一化至 0 到 360 度之间
return (angle % 360 + 360) % 360;
}
private static Coordinate[] calculateSectorVertices(Coordinate center, double radius,
double startAngle, double endAngle) {
// 扇形的起始点
Coordinate startVertex = calculateVertex(center, radius, startAngle);
// 扇形的终止点
Coordinate endVertex = calculateVertex(center, radius, endAngle);
// todo error in calculate startVertex and endVertex =
return new Coordinate[]{center, startVertex, endVertex, center};
}
private static Coordinate calculateVertex(Coordinate center, double radius, double angle) {
double longitude = calculateEndpointLongitude(center.getX(), angle, radius);
double latitude = calculateEndpointLatitude(center.getY(), angle, radius);
System.out.println(longitude);
System.out.println(latitude);
return new Coordinate(longitude, latitude);
}
private static double calculateEndpointLatitude(double latitude, double azimuth, double distance) {
// Convert the latitude to radians.
double latitudeRadians = latitude * Math.PI / 180;
// Convert the azimuth to radians.
double azimuthRadians = azimuth * Math.PI / 180;
// Calculate the endpoint latitude.
return Math.asin(Math.sin(latitudeRadians) * Math.cos(distance / EARTH_RADIUS) + Math.cos(latitudeRadians) * Math.sin(distance / EARTH_RADIUS) * Math.cos(azimuthRadians));
}
private static double calculateEndpointLongitude(double longitude, double azimuth, double distance) {
// Convert the longitude to radians.
double longitudeRadians = longitude * Math.PI / 180;
// Convert the azimuth to radians.
double azimuthRadians = azimuth * Math.PI / 180;
// Calculate the endpoint longitude.
return longitudeRadians + distance * Math.cos(azimuthRadians) / EARTH_RADIUS;
}
public static void main(String[] args) {
double lng1 = 114.5470250;
double lat1 = 38.0377410;
double lng2 = 114.6317360;
double lat2 = 38.0776860;
boolean inSector = isInSector(lng1, lat1, lng2, lat2, 0, 1, 90);
System.out.println(inSector);
}
}
i tried with chatgpt , the above code still not work; i want a correct longtitude and latitude