jeudi 26 mars 2015

calculating distance between successive csv gps points using java


here is part of code that i tried to execute but it dosent work ... the problem is in latitude2 and longitude i didnt figure out calculate distance between row and row+1 nay help please !!!!



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();
}




Aucun commentaire:

Enregistrer un commentaire