我有一系列LineString,例如lineString1
和lineString2
其中lineString1.getEndPoint() == lineString2.getStartPoint()
或
lineString1.getStartPoint() == lineString2.getEndPoint()
我想得到LineString
对象(不是MultiLineString
),它将由lineString1
和lineString2
组合而成。
我没有找到任何好的解决方案。
答案 0 :(得分:2)
你需要使用JTS LineMerger
来实现这一点,它会返回一个Collection<LineString>
,但在你的例子中,这是一行加入(在其他情况下只有一些行可能会触及)。
import java.util.Collection;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;
import com.vividsolutions.jts.operation.linemerge.LineMerger;
public class LineJoin {
public static void main(String[] args) throws ParseException {
/*String[] WKTS = { "LINESTRING (254058.76074485347 475001.2186020431, 255351.04293761664 474966.9279243938)",
"LINESTRING (255351.04293761664 474966.9279243938, 255529.29662365236 474272.4599921228)",
"LINESTRING (255529.29662365236 474272.4599921228, 256166.05830998957 473979.44920198264)",
"LINESTRING (256166.05830998957 473979.44920198264, 256082.8878282134 472762.2531920295)",
"LINESTRING (256082.8878282134 472762.2531920295, 254245.37250651795 472802.681197444)",
"LINESTRING (254245.37250651795 472802.681197444, 254294.87550167093 473782.10435392085)",
"LINESTRING (255802.5570897992 475306.663459122, 256973.3861333156 473826.71649389854)",
"LINESTRING (256973.3861333156 473826.71649389854, 256996.781511873 472271.0759416939)",
"LINESTRING (254134.08645022905 471750.25133671844, 253091.20265363617 473772.75685744354)",
"LINESTRING (252764.19811300343 475015.97340571403, 253526.90397945273 476194.3201198712)",
"LINESTRING (253526.90397945273 476194.3201198712, 254983.29259833007 475930.5996061625)" };*/
String[] WKTS = {"LINESTRING(10 40,11 41)","LINESTRING(11 41,12 42)"};
WKTReader reader = new WKTReader();
LineMerger merger = new LineMerger();
for (String wkt : WKTS) {
Geometry geom = reader.read(wkt);
merger.add(geom);
}
Collection<LineString> collection = merger.getMergedLineStrings();
for (LineString l : collection) {
System.out.println(l);
}
}
}
对于您的测试输入,它给出:
LINESTRING (10 40, 11 41, 12 42)
对于注释掉的WKT,你会得到:
LINESTRING (252764.19811300343 475015.97340571403, 253526.90397945273 476194.3201198712, 254983.29259833007 475930.5996061625)
LINESTRING (254134.08645022905 471750.25133671844, 253091.20265363617 473772.75685744354)
LINESTRING (254058.76074485347 475001.2186020431, 255351.04293761664 474966.9279243938, 255529.29662365236 474272.4599921228, 256166.05830998957 473979.44920198264, 256082.8878282134 472762.2531920295, 254245.37250651795 472802.681197444, 254294.87550167093 473782.10435392085)
LINESTRING (255802.5570897992 475306.663459122, 256973.3861333156 473826.71649389854, 256996.781511873 472271.0759416939)
答案 1 :(得分:0)
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
LineString combinedLineString = GeometryFactory.createLineString(
lineString1.getControlPoints()
.addAll(lineString2.getControlPoints())
);
答案 2 :(得分:0)
您可以使用GeometryCollection
的{{3}}方法加入LineStrings。请参阅文档union
- 对于线串的集合,它具有完全编码和消除线条的效果。
static Geometry combineIntoOneGeometry( Collection<Geometry> geometryCollection ){
GeometryFactory factory = FactoryFinder.getGeometryFactory( null );
// note the following geometry collection may be invalid (say with overlapping polygons)
GeometryCollection geometryCollection =
(GeometryCollection) factory.buildGeometry( geometryCollection );
return geometryCollection.union();
}