对于线性距离的特殊坡度,我需要检查给定纬度/经度的新点相对于存储在地图中的所有其他点是否具有大于10 km的距离。我想避免,地图中的所有点都是按顺序比较的。
我尝试了以下代码:
private static Map<Long,String> getTop10DSEGs() throws IOException {
Map<Long,String> top10Dsegs = new HashMap<>();
List<String> allDsegs = loadDsegRanking(rFiles);
//the list allDsegs is a csv with the following structure
//dsegID, Latitide, Longitude
//1167317891449551556,51.435550689697266,6.695819854736328
double LatFromList, LonFromList;
double LatFromMap, LonFromMap;
String[] listArray;
String[] mapArray;
Long firstDseg = Long.parseLong(allDsegs.get(0).substring(0, 19));
top10Dsegs.put(firstDseg, allDsegs.get(0).substring(20));
//Iterating through all dsegs
for(String line : allDsegs){
Long nextID = null;
String nextCoordinates = "0.0000,0.0000";
listArray = line.split(",");
LatFromList = Double.valueOf(listArray[1]);
LonFromList = Double.valueOf(listArray[2]);
//checking all coordinates of maped dsegs
for(Map.Entry<Long, String> entry : top10Dsegs.entrySet()){
List<Double> distanceList = new ArrayList<>();
mapArray = entry.getValue().split(",");
LatFromMap = Double.valueOf(mapArray[0]);
LonFromMap = Double.valueOf(mapArray[1]);
//calculating the distance between the next city from the list and the other dsegs from the map
//result of dist is a double and it's metric is Km.
Double dist = Implements.DistanceCalculator.distance(LatFromMap, LonFromMap, LatFromList, LonFromList, "K");
distanceList.add(dist);
for(Double value : distanceList){
if (dist>10){
nextID = Long.parseLong(listArray[0]);
nextCoordinates = listArray[1] + "," + listArray[2];
}
}
}
if(nextID != null && top10Dsegs.size() < 10){
top10Dsegs.put(nextID, nextStartCoordinates);
}
return top10Dsegs;
}
最佳答案
为了避免计算和比较所有已存储点和新点之间的距离,可以使用某种由(多)映射或数组实现的网格(或矩阵)。网格的每个单元的大小可以是sqrt(10km),包含所有具有相关坐标的点。用sqrt(10)除以坐标,可以方便地计算出该点的合适单元的坐标。
因此,当添加下一个点时,可以只检查9个单元格(own+around)中的点之间的距离。我想它比地图上所有的点都要少。