public static double getDistance(String trkId,String meshId) throws SQLException{
double length = 0;
Connection conn = new DBManager().getConn();
String sql = "select t.x2,t.y2 from xytable t where t.trk_id=" + trkId;
if(StringUtils.isNotEmpty(meshId)){
sql += " and meshid=" + meshId;
}
sql += " order by t.trk_point_id";
PreparedStatement prep = conn.prepareStatement(sql);
ResultSet rs = prep.executeQuery();
double x=0d;
double y=0d;
int flag = 0;
while(rs.next()){
if(flag != 0){
double l = DistanceOfTwoPoints(x,y,rs.getDouble(1),rs.getDouble(2),GaussSphere.WGS84);
if(l < 60){
length += l;
}
}
x = rs.getDouble(1);
y = rs.getDouble(2);
flag++;
}
return length;
}
public static enum GaussSphere{
Beijing54,
Xian80,
WGS84,
}
private static double Rad(double d,String xy){
if ((Math.abs(d) <= 180 && xy.equals("x")) || (Math.abs(d) <= 180 && xy.equals("y"))){
d = d * 3600;
}
return d /3600 * Math.PI / 180.0;
}
//注意传入lng和lat的顺序,否则计算出来的距离有偏差
private static double DistanceOfTwoPoints(double lng1,double lat1,double lng2,double lat2, GaussSphere gs){
double radLat1 = Rad(lat1,"y");
double radLat2 = Rad(lat2,"y");
double a = radLat1 - radLat2;
double b = Rad(lng1,"x") - Rad(lng2,"x");
double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b/2),2)));
s = s * (gs == GaussSphere.WGS84 ? 6378137.0 : (gs == GaussSphere.Xian80 ? 6378140.0 : 6378245.0));
// s = Math.round(s * 10000) / 10000;
return s;
}