package com.ard.work.utils.gis;
|
|
import com.ard.work.api.domian.Point;
|
import com.ard.work.api.domian.PtzDto;
|
import org.gavaghan.geodesy.*;
|
|
import java.awt.geom.Point2D;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
public class GisUtil {
|
|
public static GeodeticCalculator geodeticCalculator = new GeodeticCalculator();
|
|
/**
|
* 根据经纬度,计算两点间的距离
|
*
|
* @param From 第一个点的经纬度
|
* @param To 第二个点的经纬度
|
* @return 返回距离 单位米
|
*/
|
public static double getDistance(double[] From, double[] To) {
|
double longitudeFrom = From[0];
|
double latitudeFrom = From[1];
|
double longitudeTo = To[0];
|
double latitudeTo = To[1];
|
GlobalCoordinates source = new GlobalCoordinates(latitudeFrom, longitudeFrom);
|
GlobalCoordinates target = new GlobalCoordinates(latitudeTo, longitudeTo);
|
return geodeticCalculator.calculateGeodeticCurve(Ellipsoid.WGS84, source, target).getEllipsoidalDistance();
|
}
|
|
public static double getDistance(Double[] From, Double[] To) {
|
double longitudeFrom = From[0];
|
double latitudeFrom = From[1];
|
double longitudeTo = To[0];
|
double latitudeTo = To[1];
|
GlobalCoordinates source = new GlobalCoordinates(latitudeFrom, longitudeFrom);
|
GlobalCoordinates target = new GlobalCoordinates(latitudeTo, longitudeTo);
|
return geodeticCalculator.calculateGeodeticCurve(Ellipsoid.WGS84, source, target).getEllipsoidalDistance();
|
}
|
|
/**
|
* 根据经纬度,计算两点间的距离
|
*
|
* @param longitudeFrom 第一个点的经度
|
* @param latitudeFrom 第一个点的纬度
|
* @param longitudeTo 第二个点的经度
|
* @param latitudeTo 第二个点的纬度
|
* @return 返回距离 单位米
|
*/
|
public static double getDistance(double longitudeFrom, double latitudeFrom, double longitudeTo, double latitudeTo) {
|
GlobalCoordinates source = new GlobalCoordinates(latitudeFrom, longitudeFrom);
|
GlobalCoordinates target = new GlobalCoordinates(latitudeTo, longitudeTo);
|
return geodeticCalculator.calculateGeodeticCurve(Ellipsoid.WGS84, source, target).getEllipsoidalDistance();
|
}
|
|
public static double getDistance(Point From, Point To) {
|
GlobalCoordinates source = new GlobalCoordinates(From.getLatitude(), From.getLongitude());
|
GlobalCoordinates target = new GlobalCoordinates(To.getLatitude(), To.getLongitude());
|
return geodeticCalculator.calculateGeodeticCurve(Ellipsoid.WGS84, source, target).getEllipsoidalDistance();
|
}
|
/**
|
* 计算从from到to方向的直线与正北方向夹角
|
*
|
* @param longitudeFrom 第一个点的经度
|
* @param latitudeFrom 第一个点的纬度
|
* @param longitudeTo 第二个点的经度
|
* @param latitudeTo 第二个点的纬度
|
* @return 返回角度
|
*/
|
public static double getNorthAngle(double longitudeFrom, double latitudeFrom, double longitudeTo, double latitudeTo) {
|
GlobalPosition source = new GlobalPosition(latitudeFrom, longitudeFrom, 0);
|
GlobalPosition target = new GlobalPosition(latitudeTo, longitudeTo, 0);
|
return geodeticCalculator.calculateGeodeticMeasurement(Ellipsoid.WGS84, source, target).getAzimuth();
|
}
|
|
public static double getNorthAngle(Point From, Point To) {
|
GlobalPosition source = new GlobalPosition(From.getLatitude(), From.getLongitude(), 0);
|
GlobalPosition target = new GlobalPosition(To.getLatitude(), To.getLongitude(), 0);
|
return geodeticCalculator.calculateGeodeticMeasurement(Ellipsoid.WGS84, source, target).getAzimuth();
|
}
|
|
/**
|
* @param camera 经度,纬度,高度 如:{125.097531, 46.60029, 120};
|
* @param lookAt 经度,纬度,高度 如:{125.124731, 46.584808, 0};
|
* @param viewAngle 相机可视角度 如:20
|
* @param viewWidth 视域宽度 如:150
|
* @return ptz 数组 如:[129.5355798969157, -2.5419097807416655, 23.3676043024458]
|
*/
|
public static double[] getCameraPTZ(double[] camera, double[] lookAt, double viewAngle, double viewWidth) {
|
double p = 0, t = 0, z = 0;
|
double distance = getDistance(camera[0], camera[1], lookAt[0], lookAt[1]);
|
double northAngle = getNorthAngle(camera[0], camera[1], lookAt[0], lookAt[1]);
|
double height = lookAt[2] - camera[2];
|
p = northAngle;
|
t = Angle.toDegrees(Math.atan(height / distance));
|
if (t < 0) {
|
t += 360;
|
}
|
z = distance * Math.tan(viewAngle / 2) * 2 / viewWidth;
|
return new double[]{p, t, z};
|
}
|
|
public static PtzDto getCameraPTZ(Point camera, Point lookAt, double viewAngle, double viewWidth) {
|
double[] cameraArr = {camera.getLongitude(),camera.getLatitude(),camera.getAltitude()};
|
double[] lookAtArr = {lookAt.getLongitude(),lookAt.getLatitude(),lookAt.getAltitude()};
|
double[] ptzArr = getCameraPTZ(cameraArr,lookAtArr,viewAngle,viewWidth);
|
PtzDto ptzDto = new PtzDto((float)ptzArr[0], (float)ptzArr[1], (float)ptzArr[2]);
|
return ptzDto;
|
}
|
/**
|
* 根据开始坐标点,角度,计算结束点坐标
|
*
|
* @param startGlobalCoordinates
|
* @param startAngle 方向(以起点为中心)
|
* @param distance 距离(单位:m)
|
* @return
|
*/
|
public static GlobalCoordinates getGlobalCoordinates(GlobalCoordinates startGlobalCoordinates, double startAngle, double distance) {
|
return new GeodeticCalculator().calculateEndingGlobalCoordinates(Ellipsoid.WGS84, startGlobalCoordinates, startAngle, distance);
|
}
|
|
/**
|
* 判断当前位置是否在多边形区域内
|
*
|
* @param orderLocation 当前点
|
* @param partitionLocation 区域顶点
|
* @return
|
*/
|
public static boolean isInPolygon(Point orderLocation, List<Point> partitionLocation) {
|
|
double p_x = orderLocation.getLongitude();
|
double p_y = orderLocation.getLatitude();
|
Point2D.Double point = new Point2D.Double(p_x, p_y);
|
List<Point2D.Double> pointList = new ArrayList<Point2D.Double>();
|
for (Point points : partitionLocation) {
|
double polygonPoint_x = points.getLongitude();
|
double polygonPoint_y = points.getLatitude();
|
Point2D.Double polygonPoint = new Point2D.Double(polygonPoint_x, polygonPoint_y);
|
pointList.add(polygonPoint);
|
}
|
return IsPtInPoly(point, pointList);
|
}
|
|
/**
|
* 判断点是否在多边形内,如果点位于多边形的顶点或边上,也算做点在多边形内,直接返回true
|
*
|
* @param point 检测点
|
* @param pts 多边形的顶点
|
* @return 点在多边形内返回true, 否则返回false
|
*/
|
public static boolean IsPtInPoly(Point2D.Double point, List<Point2D.Double> pts) {
|
|
int N = pts.size();
|
boolean boundOrVertex = true; //如果点位于多边形的顶点或边上,也算做点在多边形内,直接返回true
|
int intersectCount = 0;//cross points count of x
|
double precision = 2e-10; //浮点类型计算时候与0比较时候的容差
|
Point2D.Double p1, p2;//neighbour bound vertices
|
Point2D.Double p = point; //当前点
|
|
p1 = pts.get(0);//left vertex
|
for (int i = 1; i <= N; ++i) {//check all rays
|
if (p.equals(p1)) {
|
return boundOrVertex;//p is an vertex
|
}
|
|
p2 = pts.get(i % N);
|
if (p.x < Math.min(p1.x, p2.x) || p.x > Math.max(p1.x, p2.x)) {
|
p1 = p2;
|
continue;
|
}
|
|
if (p.x > Math.min(p1.x, p2.x) && p.x < Math.max(p1.x, p2.x)) {
|
if (p.y <= Math.max(p1.y, p2.y)) {
|
if (p1.x == p2.x && p.y >= Math.min(p1.y, p2.y)) {
|
return boundOrVertex;
|
}
|
|
if (p1.y == p2.y) {
|
if (p1.y == p.y) {
|
return boundOrVertex;
|
} else {//before ray
|
++intersectCount;
|
}
|
} else {
|
double xinters = (p.x - p1.x) * (p2.y - p1.y) / (p2.x - p1.x) + p1.y;
|
if (Math.abs(p.y - xinters) < precision) {
|
return boundOrVertex;
|
}
|
|
if (p.y < xinters) {
|
++intersectCount;
|
}
|
}
|
}
|
} else {
|
if (p.x == p2.x && p.y <= p2.y) {
|
Point2D.Double p3 = pts.get((i + 1) % N);
|
if (p.x >= Math.min(p1.x, p3.x) && p.x <= Math.max(p1.x, p3.x)) {
|
++intersectCount;
|
} else {
|
intersectCount += 2;
|
}
|
}
|
}
|
p1 = p2;
|
}
|
|
if (intersectCount % 2 == 0) {//偶数在多边形外
|
return false;
|
} else { //奇数在多边形内
|
return true;
|
}
|
}
|
//是否在中国范围
|
public static boolean isInChina(double lon, double lat) {
|
// 定义中国的经纬度范围
|
double minLon = 73.33;
|
double maxLon = 135.05;
|
double minLat = 18.24;
|
double maxLat = 53.33;
|
|
// 判断经度和纬度是否在范围内
|
return minLon <= lon && lon <= maxLon && minLat <= lat && lat <= maxLat;
|
}
|
public static void main(String[] args) {
|
// 125.097531,46.60029, 125.124731,46.584808
|
//相机位置
|
/* double[] camera = {125.146964331147,46.5580925811216,102};//经度,纬度,高度
|
//看向的位置
|
double[] lookAt = {125.155449,46.555108,0};//经度,纬度,高度
|
|
double viewAngle = 20;//相机可视角度
|
double viewWidth = 150;//相机视域宽度
|
|
double[] ptz = GisUtil.getCameraPTZ(camera, lookAt, viewAngle, viewWidth);
|
System.out.println("ptz:" + Arrays.toString(ptz));*/
|
// double[] camera = {125.153903999999997,46.5600799999999992,258.430000000000007};
|
GlobalCoordinates camera = new GlobalCoordinates(45.755847, 126.640557);
|
double p = 39.71;
|
double distance = 164;
|
|
GlobalCoordinates point = getGlobalCoordinates(camera, p, distance);
|
System.out.println("point:");
|
System.out.println("lon:" + point.getLongitude());
|
System.out.println("lat:" + point.getLatitude());
|
|
}
|
}
|