欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java经纬度生成 判断一个点是否在一个面上

程序员文章站 2022-04-02 18:49:31
...

经纬度生成

让前端圈一个面给你,然后文件读取出来,生成范围内的点。

/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author maruko
 * @date 2021/4/23 19:02
 * @since 1.0.0
 */

public class Test {


    public static void main(String[] args) {
        Double[][] point=new Double[10000][2];
        int num=0;
        Double[][] jwds = jwdFile("D:\\桌面文件\\bj/bj.txt");
        double minLon = 75d;
        double maxLon = 130d;
        double minLat = 19d;
        double maxLat = 55d;
        for (int i = 0; i < 10000; i++) {
            Double[] jwd = randomLonLat(minLon, maxLon, minLat, maxLat);
            boolean ptInPoly = isPtInPoly(jwd, jwds);
            if(ptInPoly){
//                point[num]=jwd;
                num++;
                System.err.println(jwd[0]+","+jwd[1]);
            }
        }

    }
/*    public static void main(String[] args) {

        Double[][] ps = new Double[][] { new Double[]{114.309914d,30.599556d},

                new Double[]{114.295688,30.592879},
                new Double[]{114.292812,30.587726},
        new Double[]{114.292812,30.587726},
        new Double[]{114.30058,30.580318},
        new Double[]{114.303606,30.586959},
        new Double[]{114.304534,30.594751},
        new Double[]{114.30838,30.590131},
        new Double[]{114.308651,30.584182},
        new Double[]{114.304495,30.584015},
        new Double[]{114.301301,30.578759},
        new Double[]{114.309437,30.578528},
        new Double[]{114.323282,30.592786}};
        Double[] n1 = new Double[]{114.303217,30.583553};
        Double[] n2 = new Double[]{114.307336,30.597592};
        Double[] n3 = new Double[]{114.286565,30.590056};
        Double[] y1 = new Double[]{114.227342,30.587987};
        Double[] y2 = new Double[]{120.1866 , 30.2672};
        Double[] y4 = new Double[]{120.1869 , 30.2718};
        System.out.println( "n1:" + isPtInPoly(n1[0] , n1[1] , ps));
        System.out.println( "n2:" + isPtInPoly(n2[0] , n2[1] , ps));
        System.out.println( "n3:" + isPtInPoly(n3[0] , n3[1] , ps));
        System.out.println( "y1:" + isPtInPoly(y1[0] , y1[1] , ps));
        System.out.println( "y2:" + isPtInPoly(y2[0] , y2[1] , ps));
        System.out.println( "y4:" + isPtInPoly(y4[0] , y4[1] , ps));
    }
    */

    /**
     * 随机生成经度或者维度
     *
     * @param MinLon 最小经度
     * @param MaxLon 最大经度
     * @param MinLat 最小纬度
     * @param MaxLat 最大维度
     * @return
     */
    public static Double[] randomLonLat(double MinLon, double MaxLon, double MinLat, double MaxLat) {
        Double[] jwd = new Double[2];
        BigDecimal db = new BigDecimal(Math.random() * (MaxLon - MinLon) + MinLon);
        jwd[0] = Double.parseDouble(db.setScale(6, BigDecimal.ROUND_HALF_UP).toString());
        db = new BigDecimal(Math.random() * (MaxLat - MinLat) + MinLat);
        jwd[1] = Double.parseDouble(db.setScale(6, BigDecimal.ROUND_HALF_UP).toString());
        return jwd;
    }

    /**
     * 读取经纬度范围文件
     *
     * @param filePath
     * @return
     */
    public static Double[][] jwdFile(String filePath) {
        //D:\桌面文件\bj/bj.txt
        File file = new File(filePath);
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                sbf.append(tempStr);
            }
            reader.close();
//            System.out.println(sbf.toString());

            String[] split = sbf.toString().split(";");
            Double[][] str = new Double[split.length][2];
            for (int i = 0; i < split.length; i++) {
                String[] split1 = split[i].split(",");
                for (int j = 0; j < split1.length; j++) {
                    str[i][j] = Double.parseDouble(split1[j]);
                }
            }
            return str;


//            for (int i = 0; i < split.length; i++) {
//                str[i][0] = doubles[i];
//            }
//            for (String s : split) {
//                System.out.println(s);
//            }
//            List<Object> list = JSON.parseArray(sbf.toString());
//            for (Object o : list) {
//                System.out.println(o.toString());
//            }
//            List<Object> bj = new ArrayList<>();
//            List<Object> list1 = list.subList(0, 3049);

//            List<Object> list = Arrays.asList(split);
//            Map map = GeoJsonUtil.cerateFqh(list);
//            ObjectMapper mapper = new ObjectMapper();
//            try {
//                mapper.writeValue(new File("D:\\桌面文件\\bj/2020.geojson"), str);
//            } catch (
//                    Exception e) {
//                e.printStackTrace();
//            }
//            System.out.println(list.size());


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * 判断一个点是否在一个面上面
     *
     * @param jwd
     * @param jwds
     * @return
     */
    public static boolean isPtInPoly(Double[] jwd, Double[][] jwds) {
        Double longitude = jwd[0];
        Double latitude = jwd[1];
        int iSum, iCount, iIndex;
        double dLon1 = 0, dLon2 = 0, dLat1 = 0, dLat2 = 0, dLon;
        if (jwds.length < 3) {
            return false;
        }
        iSum = 0;
        iCount = jwds.length;
        for (iIndex = 0; iIndex < iCount; iIndex++) {
            if (iIndex == iCount - 1) {
                dLon1 = jwds[iIndex][0];
                dLat1 = jwds[iIndex][1];
                dLon2 = jwds[0][0];
                dLat2 = jwds[0][1];
            } else {
                dLon1 = jwds[iIndex][0];
                dLat1 = jwds[iIndex][1];
                dLon2 = jwds[iIndex + 1][0];
                dLat2 = jwds[iIndex + 1][1];
            }
            // 以下语句判断A点是否在边的两端点的水平平行线之间,在则可能有交点,开始判断交点是否在左射线上
            if (((latitude >= dLat1) && (latitude < dLat2)) || ((latitude >= dLat2) && (latitude < dLat1))) {
                if (Math.abs(dLat1 - dLat2) > 0) {
                    //得到 A点向左射线与边的交点的x坐标:
                    dLon = dLon1 - ((dLon1 - dLon2) * (dLat1 - latitude)) / (dLat1 - dLat2);
                    // 如果交点在A点左侧(说明是做射线与 边的交点),则射线与边的全部交点数加一:
                    if (dLon < longitude) {
                        iSum++;
                    }
                }
            }
        }
        if ((iSum % 2) != 0) {
            return true;
        }
        return false;
    }
}

相关标签: java