gpt4 book ai didi

java - 组合 LineString 序列

转载 作者:行者123 更新时间:2023-11-30 02:11:39 30 4
gpt4 key购买 nike

我有一个 LineString 序列,例如 lineString1lineString2 enter image description here

其中 lineString1.getEndPoint() == lineString2.getStartPoint()lineString1.getStartPoint() == lineString2.getEndPoint()我想要获取由 lineString1lineString2 组合而成的 LineString 对象(不是 MultiLineString)。

我没有找到任何好的解决方案。

最佳答案

您需要使用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)

关于java - 组合 LineString 序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49883480/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com