我正在构建一个Java应用程序来进行地图匹配。我有一个.csv文件,其中包含从 OpenStreetMap 的shapefile中提取的有关国家道路网络的信息。 该文件的结构如下:
osm_id,code,fclass,name,ref,oneway,maxspeed,layer,bridge,tunnel,geometry
"4263045","5122",residential,,,F,"0","0",F,F,"MultiLineString ((23.4567651 37.5042109, 23.456488 37.5039174, 23.4567841 37.503815, 23.4573291 37.5036181, 23.4576116 37.5034565, 23.4591485 37.5025756))"
使用 opencsv 和 geotools 库,我可以使用最小距离找到最接近任何点的道路(几何对象)。 但是该应用程序每次都会检查所有可用的道路,因此速度很慢。
为克服此问题,我创建了一个网格,以便在使用任何点时,可以确定包含该网格的网格单元。
我的目标是为每个网格单元创建一个唯一的数字,然后创建一个哈希图,对于代表网格单元的每个数字,它指向包含所有几何对象(道路和其他点)的ArrayList
来自该单元格中存在的另一个.csv文件)。
这样做的目的是使代码仅检查到我想要的点到周围道路的最小距离,而不是全部检查。
我如何确定哪些道路属于哪些单元格,以便我可以成功创建哈希图?
这是网格类:
public class Grid {
private double minX;
private double minY;
private double maxX;
private double maxY;
private double n;
public Grid(double minX, double minY, double maxX, double maxY, double n) {
this.minX = minX;
this.minY = minY;
this.maxX = maxX;
this.maxY = maxY;
this.n = n;
}
public Cell findGridCell (double x, double y){
double difX = maxX - minX;
double difY = maxY - minY;
double stepX = difX/n;
double stepY = difY/n;
double i = ((x - minX)/ stepX);
double j = ((y - minY)/ stepY);
Cell myCell = new Cell(i, j);
return myCell;
}
}
这是Main
中与我上面提到的内容相关的部分:
try (
Reader reader3 = Files.newBufferedReader(Paths.get(CSV_FILE_PATH_4));
) {
CsvToBean<RoadsInfo> csvToBean2 = new CsvToBeanBuilder(reader3)
.withType(RoadsInfo.class)
.withIgnoreLeadingWhiteSpace(true)
.build();
Iterator<RoadsInfo> roadsInfoIterator = csvToBean2.iterator();
while (roadsInfoIterator.hasNext()) {
RoadsInfo roadsInfo = roadsInfoIterator.next();
try {
//take geometry string, extract coordinates, put them in table and find mins and maxes
String coord = roadsInfo.getGeometry();
String C2 = coord.replaceAll("[^0-9?!\\.]", " ");
String C3 = C2.trim().replaceAll("\\s{2,}", " ");
String[] ar = C3.split(" ");
double[] table = new double[ar.length];
for (int i=0; i < ar.length; i++){
table[i] = Double.parseDouble(ar[i]);
}
if(table.length % 2 ==0) {
for (int i = 0; i < table.length; i = i + 2) {
if (table[i] < minX) {
minX = table[i];
}
if (table[i] > maxX) {
maxX = table[i];
}
}
for (int i = 1; i <= table.length; i = i + 2) {
if (table[i] < minY) {
minY = table[i];
}
if (table[i] > maxY) {
maxY = table[i];
}
}
}
else{
for (int i = 0; i <= table.length; i = i + 2) {
if (table[i] < minX) {
minX = table[i];
}
if (table[i] > maxX) {
maxX = table[i];
}
}
for (int i = 1; i < table.length; i = i + 2) {
if (table[i] < minY) {
minY = table[i];
}
if (table[i] > maxY) {
maxY = table[i];
}
}
}
//create grid
Grid myGrid = new Grid(minX, minY, maxX, maxY, 50 );
Cell cell1 = myGrid.findGridCell(29.63211581, 34.8756767);
System.out.println("The cell is: (" + cell1.getI() +","+cell1.getJ()+")" );
我认为哈希图将像这样实现:
HashMap<Integer, ArrayList<Geometry>>