百度地图工具类:点到点、点到面、面积等计算

百度地图的工具类

本工具类中包含方法:

  1. 计算两个坐标的距离(百度坐标系)
  2. 判断点是否在区域范围内
  3. 得到指定坐标点到围栏中的最短距离
  4. 计算经纬度围成的实际面积

注:个人在区域围栏的坐标是json对象,在使用时请根据自己的需求修改该部分代码

{
    
    
	"paths": [{
    
    
			"lng": 104.049857,
			"lat": 30.661122
		},
		{
    
    
			"lng": 104.051941,
			"lat": 30.659196
		},
		{
    
    
			"lng": 104.048491,
			"lat": 30.658699
		},
		{
    
    
			"lng": 104.048851,
			"lat": 30.660936
		},
		{
    
    
			"lng": 104.049857,
			"lat": 30.661122
		}
	],
	"centerPosition": {
    
    
		"lng": 104.049426,
		"lat": 30.658885
	}
}

从上面可以看到,这是我自己项目中围栏的json,paths是围栏坐标,centerPosition是中心坐标点。


import com.alibaba.fastjson.JSONObject;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;

import java.math.BigDecimal;
import java.util.List;

import static com.alibaba.fastjson.JSON.*;

/**
 * @author gongl
 * @date 2022-02-25
 */
public class BaiDuUtils {
    
    


    /**
     * 计算两个坐标的距离(百度坐标系)
     *
     * @param lng1 第一个坐标的经度
     * @param lat1 第一个坐标的维度
     * @param lng2 第二个坐标的经度
     * @param lat2 第二个坐标的维度
     * @return 两个坐标之间的距离(单位:米,保留2位小数)
     */
    public static double getDistanceMeter(double lng1, double lat1, double lng2, double lat2) {
    
    
        GlobalCoordinates source = new GlobalCoordinates(lat1, lng1);
        GlobalCoordinates target = new GlobalCoordinates(lat2, lng2);
        GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(Ellipsoid.Sphere, source, target);
        BigDecimal b = BigDecimal.valueOf(geoCurve.getEllipsoidalDistance());
        return b.setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
    }


    /**
     * 判断点是否在区域范围内 ( {"paths":[{"lng":104.049857,"lat":30.661122},{"lng":104.051941,"lat":30.659196},{"lng":104.048491,"lat":30.658699},{"lng":104.048851,"lat":30.660936}],"centerPosition":{"lng":104.049426,"lat":30.658885}})
     *
     * @param lng  点的经度
     * @param lat  点的维度
     * @param area 区域坐标
     * @return 结果
     */
    public static boolean IsPtInPoly(double lng, double lat, String area) {
    
    
        JSONObject jsonObject = parseObject(area);
        String paths = jsonObject.getString("paths");
        List<PointInfo> pts = parseArray(paths, PointInfo.class);

        int N = pts.size();
        int intersectCount = 0; //交叉点的个数
        double precision = 2e-10; //浮点类型计算时候与0比较时候的容差
        PointInfo p1, p2;// 相邻的顶点
        p1 = pts.get(0);
        for (int i = 1; i <= N; ++i) {
    
    
            if (lng == p1.getLng() && lat == p1.getLat()) {
    
    //在点上
                return true;
            }
            p2 = pts.get(i % N);
            if (lng < Math.min(p1.getLng(), p2.getLng()) || lng > Math.max(p1.getLng(), p2.getLng())) {
    
    
                p1 = p2;
                continue;
            }
            if (lng > Math.min(p1.getLng(), p2.getLng()) && lng < Math.max(p1.getLng(), p2.getLng())) {
    
    
                if (lat <= Math.max(p1.getLat(), p2.getLat())) {
    
    
                    if (p1.getLng().doubleValue() == p2.getLng().doubleValue() && lat >= Math.min(p1.getLat(), p2.getLat())) {
    
    
                        return true;
                    }
                    if (p1.getLat().doubleValue() == p2.getLat().doubleValue()) {
    
    
                        if (p1.getLat() == lat) {
    
    
                            return true;
                        } else {
    
    
                            ++intersectCount;
                        }
                    } else {
    
    
                        double xinters = (lng - p1.getLng()) * (p2.getLat() - p1.getLat()) / (p2.getLng() - p1.getLng()) + p1.getLat();
                        if (Math.abs(lat - xinters) < precision) {
    
    
                            return true;
                        }
                        if (lat < xinters) {
    
    
                            ++intersectCount;
                        }
                    }
                }
            } else {
    
    
                if (lng == p2.getLng() && lat <= p2.getLat()) {
    
    
                    PointInfo p3 = pts.get((i + 1) % N);
                    if (lng >= Math.min(p1.getLng(), p3.getLng()) && lng <= Math.max(p1.getLng(), p3.getLng())) {
    
    
                        ++intersectCount;
                    } else {
    
    
                        intersectCount += 2;
                    }
                }
            }
            p1 = p2;
        }
        return intersectCount % 2 != 0;
    }


    /**
     * 得到指定坐标点到围栏中的最短距离
     * {"paths":[{"lng":104.049857,"lat":30.661122},{"lng":104.051941,"lat":30.659196},{"lng":104.048491,"lat":30.658699},{"lng":104.048851,"lat":30.660936}],"centerPosition":{"lng":104.049426,"lat":30.658885}}
     *
     * @param lng  点的经度
     * @param lat  点的维度
     * @param area 区域坐标
     * @return 结果 (单位:米,保留2位小数)
     */
    public static double getDistance(double lng, double lat, String area) {
    
    
        JSONObject jsonObject = parseObject(area);
        String paths = jsonObject.getString("paths");
        List<PointInfo> ps = parseArray(paths, PointInfo.class);

        //得到最小距离的坐标点
        PointInfo minPoint;
        int index = -1;
        double min = Double.MAX_VALUE;
        for (int i = 0; i < ps.size() - 1; i++) {
    
    
            double x = ps.get(i).getLng();
            double y = ps.get(i).getLat();
            double sqrt = getDistanceMeter(lng, lat, x, y);
            if (sqrt <= min) {
    
    
                min = sqrt;
                index = i;
            }
        }
        if (min == 0) {
    
    
            return 0;
        }
        //获取距离最近的点
        if (index != -1) {
    
    
            minPoint = ps.get(index);
            PointInfo beforePoint;
            PointInfo afterPoint;
            if (index == ps.size() - 1) {
    
    
                afterPoint = ps.get(0);
            } else {
    
    
                afterPoint = ps.get(index + 1);
            }
            if (index == 0) {
    
    
                beforePoint = ps.get(ps.size() - 1);
            } else {
    
    
                beforePoint = ps.get(index - 1);
            }
            double a1;
            double b1;
            double c1;
            // 海伦公式 得到三点围成三角形的面积
            a1 = getDistanceMeter(lng, lat, minPoint.lng, minPoint.lat);
            b1 = getDistanceMeter(minPoint.lng, minPoint.lat, beforePoint.lng, beforePoint.lat);
            c1 = getDistanceMeter(lng, lat, beforePoint.lng, beforePoint.lat);
            //判断三角形是否为锐角三角形
            boolean acuteAngle1 = isAcuteAngle(a1, b1, c1);
            double h1 = a1;//距离 如果是锐角三角形 则高为最短距离,如果是钝角三角形,则最短距离是锁到最近点
            if (acuteAngle1) {
    
    
                double p1 = (a1 + b1 + c1) / 2;
                double S1 = Math.sqrt(p1 * (p1 - a1) * (p1 - b1) * (p1 - c1));
                //求高
                h1 = (2 * S1) / b1;
            }
            double b2;
            double c2;
            // 海伦公式 得到三点围城三角形的面积
            b2 = getDistanceMeter(afterPoint.lng, afterPoint.lat, minPoint.lng, minPoint.lat);
            c2 = getDistanceMeter(lng, lat, afterPoint.lng, afterPoint.lat);
            double h2 = a1;
            boolean acuteAngel2 = isAcuteAngle(a1, b2, c2);
            if (acuteAngel2) {
    
    
                double p2 = (a1 + b1 + c1) / 2;
                double S2 = Math.sqrt(p2 * (p2 - a1) * (p2 - b2) * (p2 - c2));
                //求高
                h2 = (2 * S2) / b1;
            }
            return Math.min(h1, h2);
        }
        return 0;
    }


    /**
     * 计算经纬度围成的实际面积
     *
     * @param area 区域点位坐标
     * @return 结果  (单位:平方米)
     */
    public static double getArea(String area) {
    
    
        JSONObject jsonObject = parseObject(area);
        String paths = jsonObject.getString("paths");
        List<PointInfo> ps = parseArray(paths, PointInfo.class);

        double sJ = 6378137;
        double Hq = 0.017453292519943295;
        double c = sJ * Hq;
        double d = 0;
        if (3 > ps.size()) {
    
    
            return 0;
        }
        for (int i = 0; i < ps.size() - 1; i++) {
    
    
            PointInfo h = ps.get(i);
            PointInfo k = ps.get(i + 1);
            double u = h.lng * c * Math.cos(h.lat * Hq);

            double hhh = h.lat * c;
            double v = k.lng * c * Math.cos(k.lat * Hq);
            d = d + (u * k.lat * c - v * hhh);
        }

        PointInfo g1 = ps.get(ps.size() - 1);
        PointInfo point = ps.get(0);
        double eee = g1.lng * c * Math.cos(g1.lat * Hq);
        double g2 = g1.lat * c;

        double k = point.lng * c * Math.cos(point.lat * Hq);
        d += eee * point.lat * c - k * g2;
        return BigDecimal.valueOf(0.5 * Math.abs(d)).doubleValue();
    }


    /**
     * 判断是否锐角三角形
     */
    private static boolean isAcuteAngle(double a, double b, double c) {
    
    
        return ((a * a + b * b - c * c) >= 0) && ((a * a + c * c - b * b) >= 0);
    }

    public static class PointInfo {
    
    
        private Double lng;
        private Double lat;

        public PointInfo() {
    
    
        }

        public PointInfo(Double lng, Double lat) {
    
    
            this.lng = lng;
            this.lat = lat;
        }

        public Double getLng() {
    
    
            return lng;
        }

        public void setLng(Double lng) {
    
    
            this.lng = lng;
        }

        public Double getLat() {
    
    
            return lat;
        }

        public void setLat(Double lat) {
    
    
            this.lat = lat;
        }
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_47914635/article/details/123549995