使用java计算连续csv gps点之间的距离

时间:2015-03-26 09:14:37

标签: java csv shapefile

我试图计算csv文件中存储的连续gps点之间的距离.....

这里是我试图执行的代码的一部分,但它有效...问题是在纬度2和经度我没有弄清楚计算行和行之间的距离+ 1请帮助请!!!!

                          BufferedReader reader = new BufferedReader(new FileReader(file));
    try {
        /* First line of the data file is the header */
        String line = reader.readLine();
        System.out.println("Header: " + line);

        boolean foundAnyRowHigherThan5 = false;
        boolean checker = true;
        double currentLongitude = 0;
        double currentLatitude = 0;


        for (line = reader.readLine(); line != null; line = reader.readLine()) {
            if (line.trim().length() > 0) { // skip blank lines
                {
                    if(checker){
                    String currentTokens[] = line.split("\\,");
                    String currentName1 = currentTokens[0].trim();
                    String currentName2 = currentTokens[1].trim();
                    currentLatitude = Double.parseDouble(currentTokens[2]);
                    currentLongitude = Double.parseDouble(currentTokens[3]);
                    checker = false;
                    continue;
                    } 




                String tokens[] = line.split("\\,");
                String name1 = tokens[0].trim();
                String name2 = tokens[1].trim();
                double latitude = Double.parseDouble(tokens[2]);
                double longitude = Double.parseDouble(tokens[3]);

                double latitude2 = latitude - currentLatitude;
                double longitude2 = longitude - currentLongitude;

                String speedString = tokens[5].trim();
                double dist = Double.parseDouble(tokens[4]);
                float speedFloat = Float.parseFloat(speedString);



              if(foundAnyRowHigherThan5 || speedFloat > 5.0) {
                // a partir de ce point on ajoutera touts les points ,
                if(!foundAnyRowHigherThan5) {
                  foundAnyRowHigherThan5 = true;
                }

                /* Longitude (= x coord) first ! */
                Point point = geometryFactory.createPoint(new Coordinate(longitude, latitude));

                featureBuilder.add(point);

                double earthRadius = 6371; //kilometers

                double dLat = Math.toRadians(latitude-latitude2);
                double dLng = Math.toRadians(longitude-longitude2);
                double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                           Math.cos(Math.toRadians(latitude)) * Math.cos(Math.toRadians(latitude2)) *
                           Math.sin(dLng/2) * Math.sin(dLng/2);
                double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
                 dist = (double) (earthRadius * c);


                featureBuilder.add(name1);
                featureBuilder.add(name2);

                featureBuilder.add(speedString);
                featureBuilder.add(dist);
                SimpleFeature feature = featureBuilder.buildFeature(null);

                features.add(feature);

                currentLatitude = latitude;
                currentLongitude = longitude;


                }
            }
        }

        }  
    } 
        finally {
        reader.close();
    }

0 个答案:

没有答案