import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.DecimalFormat;
import static java.lang.Double.parseDouble;
public class Distance
{
private static final double EARTH_RADIUS = 6378137;
private static double rad(double d)
{
return d * Math.PI / 180.0;
}
/**
* 根据两点间经纬度坐标(double值),计算两点间距离,单位为米
* @param lng1
* @param lat1
* @param lng2
* @param lat2
* @return 返回单位m
*/
public static double GetDistance(double lng1, double lat1, double lng2, double lat2)
{
double radLat1 = rad(lat1);
double radLat2 = rad(lat2);
double a = radLat1 - radLat2;
double b = rad(lng1) - rad(lng2);
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 * EARTH_RADIUS;
s = Math.round(s * 10000) / 10000;
return s;
}
public static double[] assignment(File file){
double[] su = new double[835];
int i=0;
try{
BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
String s = null;
while((s = br.readLine())!=null){//使用readLine方法,一次读一行
su[i++] = parseDouble(System.lineSeparator()+s);
}
br.close();
}catch(Exception e){
e.printStackTrace();
}
return su;
}
public static double[] vip(File file){
double[] su = new double[1877];
int i=0;
try{
BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
String s = null;
while((s = br.readLine())!=null){//使用readLine方法,一次读一行
su[i++] = parseDouble(System.lineSeparator()+s);
}
br.close();
}catch(Exception e){
e.printStackTrace();
}
return su;
}
/**
* @param args
*/
public static void main(String[] args)
{
File file = new File("C:\\Users\\TWT\\Desktop\\任务经度.txt");
double[] assignmentLong = assignment(file);
file = new File("C:\\Users\\TWT\\Desktop\\任务纬度.txt");
double[] assignmentLat = assignment(file);
file = new File("C:\\Users\\TWT\\Desktop\\会员经度.txt");
double[] vipLong = vip(file);
file = new File("C:\\Users\\TWT\\Desktop\\会员纬度.txt");
double[] vipLat = vip(file);
int[] count = new int[835];
double[] consistency = new double[835];
for (int i = 0; i < 835; i++) {
count[i]=0;
}
for (int i = 0; i < 835; i++) {
for (int j = 0; j < 1877; j++) {
double distance = GetDistance(assignmentLong[i],assignmentLat[i],vipLong[j],vipLat[j]);
if(distance<5000) {
count[i]++;
}
}
}
for (int i = 0; i < 835; i++) {
DecimalFormat df = new DecimalFormat(".000");
consistency[i] = Double.parseDouble(df.format(count[i]/(5*5*Math.PI)));
//System.out.println(consistency[i]);
System.out.println(count[i]);
}
}
}
经纬度距离测算
最新推荐文章于 2022-11-10 22:27:29 发布