在路网操作中,会遇到将首尾相连的多条道路合并成一条道路的情况,使用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)]