mitao
2025-02-21 31573d6180d15ef65ed0df9c2732495f40b12663
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.panzhihua.common.utlis;
 
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
 
import com.panzhihua.common.model.vos.grid.LatLngVO;
 
public class LngLatUtils {
 
    public static boolean check(Point2D.Double _point, List<Point2D.Double> polygon) {
        java.awt.geom.GeneralPath peneralPath = new java.awt.geom.GeneralPath();
 
        Point2D.Double first = polygon.get(0);
        peneralPath.moveTo(first.x, first.y);
        polygon.remove(0);
 
        for (Point2D.Double d : polygon) {
            peneralPath.lineTo(d.x, d.y);
        }
 
        peneralPath.lineTo(first.x, first.y);
        peneralPath.closePath();
 
        return peneralPath.contains(_point);
    }
 
    /**
     *
     * @param bound
     *            经纬度数组
     * @param pointlng
     *            短
     * @param pointLat
     *            长
     * @return
     */
    public static boolean isInPolygon(List<LatLngVO> bound, double pointlng, double pointLat) {
        Point2D.Double point = new Point2D.Double(pointlng, pointLat);
        List<Point2D.Double> pointList = new ArrayList<>();
        for (int i = 0; i < bound.size(); i++) {
            pointList.add(new Point2D.Double(bound.get(i).getLng(), bound.get(i).getLat()));
        }
        return check(point, pointList);
    }
 
    /**
     * 从txt数据中获取经纬度
     * 
     * @param lngLatTxt
     * @return
     */
    public static List<LatLngVO> getLatLngFromText(String lngLatTxt) {
        List<LatLngVO> rtResult = new ArrayList<>();
        String str = lngLatTxt.replace("[[", "");
 
        str = str.replace("]]", "");
        String[] loca = str.split("],\\[");
        for (int i = 0; i < loca.length; i++) {
            String[] loca1 = loca[i].split(",");
            rtResult.add(new LatLngVO(Double.valueOf(loca1[0]), Double.valueOf(loca1[1])));
        }
        return rtResult;
    }
 
    public static void main(String[] args) {
        String latLngTxt =
            "[[104.066974,30.660293],[104.053394,30.658114],[104.059921,30.657076],[104.065231,30.657086]]";
        List<LatLngVO> latLngList = getLatLngFromText(latLngTxt);
 
        System.out.println(isInPolygon(latLngList, 104.063398, 30.657929));
    }
 
}