使用java计算两个csv gps点之间的距离

时间:2015-03-25 11:33:21

标签: java csv shapefile

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

这是我的代码的一部分...问题是在纬度2和经度我没有弄清楚如何用正确的经度和纬度实现它们任何帮助请!!

                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;
        for (line = reader.readLine(); line != null; line = reader.readLine()) {
            if (line.trim().length() > 0) { // skip blank lines

                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+?; // i dont know how to implement
                double longitude2 = longitude+?;  // i dont know how to implement them
                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);


                }
            }
        }
    } finally {
        reader.close();
    }

3 个答案:

答案 0 :(得分:0)

有不同的解决方法,但我会留在你的代码片段中。

boolean checker = true;
double currentLongitude = 0;
double currentLatitude = 0;
    for (....) {
                ....
    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 = currentLatitude - latitude; 
   double longitude2 = currentLongitude - longitude;
   .....
   currentLatitude = latitude;
   currentLongitude = longitude;  
}

答案 1 :(得分:0)

                  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]);


                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 latitude2 = latitude - currentLatitude;
                double longitude2 = longitude - currentLongitude;
                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();
    }

答案 2 :(得分:0)

取自this link

public void calc(File file) throws Exception {

        BufferedReader reader = new BufferedReader(new FileReader(file));
            /* First line of the data file is the header */
            String line = reader.readLine();
            System.out.println("Header: " + line);
            double[] oldCoordinates = parseCoordinates(reader.readLine());
            for (line = reader.readLine(); line != null; line = reader.readLine()) {
                    double[] newCoordinates = parseCoordinates(line);
                    System.out.println(distance(oldCoordinates[0],oldCoordinates[1],newCoordinates[0],newCoordinates[1],'K'));
                    oldCoordinates = newCoordinates;
            }

    }


        private double[] parseCoordinates(String line){
            double[]result = new double[2];
            String tokens[] = line.split("\\,");
            result[0]=Double.parseDouble(tokens[2]);
            result[1]=Double.parseDouble(tokens[3]);
            return result;
        }

             private double distance(double lat1, double lon1, double lat2, double lon2, char unit) {
              double theta = lon1 - lon2;
              double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
              dist = Math.acos(dist);
              dist = rad2deg(dist);
              dist = dist * 60 * 1.1515;
              if (unit == "K") {
                dist = dist * 1.609344;
              } else if (unit == "N") {
                dist = dist * 0.8684;
                }
              return (dist);
            }

            /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
            /*::  This function converts decimal degrees to radians             :*/
            /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
            private double deg2rad(double deg) {
              return (deg * Math.PI / 180.0);
            }

            /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
            /*::  This function converts radians to decimal degrees             :*/
            /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
            private double rad2deg(double rad) {
              return (rad * 180 / Math.PI);
            }