在路網操作中,會遇到將首尾相連的多條道路合並成一條道路的情況,使用JTS中的LineMerger類,可以很好的完成這個操作,代碼:
WKTReader reader = new WKTReader(); Geometry geom_1 = reader.read("LINESTRING (116.96832000000000562 36.64882000000000062, 116.96849000000000274 36.64882000000000062)"); Geometry geom_2 = reader.read("LINESTRING (116.96849000000000274 36.64882000000000062, 116.96862000000000137 36.64882000000000062)"); Geometry geom_3 = reader.read("LINESTRING (116.96862000000000137 36.64882000000000062, 116.96877999999999531 36.64880999999999744)"); LineMerger lineMerger = new LineMerger();
//添加幾何對象不需要按照順序,只要道路首尾坐標點重合即可 lineMerger.add(geom_1); lineMerger.add(geom_2); lineMerger.add(geom_3); Collection mergedLineStrings = lineMerger.getMergedLineStrings(); System.out.println(mergedLineStrings.toString());
打印結果:[LINESTRING (116.96832 36.64882, 116.96849 36.64882, 116.96862 36.64882, 116.96878 36.64881)]