gpt4 book ai didi

java - 使用 JTS 从多个 LineStrings 计算交点

转载 作者:行者123 更新时间:2023-11-29 05:02:43 26 4
gpt4 key购买 nike

我有 3 条线,定义为 A 线、B 线和 C 线,我想计算 B 线和 C 线与 A 的交点。JTS 中有一个函数 LineIntersector这应该有助于实现这一目标。我需要帮助将此功能应用于线条以找到交点,即。像 computeIntersection(A 行,B 行)之类的东西。谢谢!

import com.vividsolutions.jts.geom.*;
import com.vividsolutions.jts.algorithm.*;

public class PointTest {

public static void main(String[] args){

// We have to have an even number of arguments - to have coordinate pairs for points.

if (args.length % 2 == 1) {
System.out.println("Wrong input. You did not enter a list of coordinage pairs. Try again.");
}


else {

int i=0;
String[] coordA = {"12", "2", "12", "13", "12", "19"};
String[] coordB = {"2", "10", "10", "10", "21", "11"};
String[] coordC = {"1","1", "9","9", "20", "20"};





// Create a new empty array of coordinates.

//Coordinate[] coordinates = new Coordinate[args.length/2];

Coordinate[] coordinatesA = new Coordinate[coordA.length/2];
Coordinate[] coordinatesB = new Coordinate[coordB.length/2];
Coordinate[] coordinatesC = new Coordinate[coordC.length/2];
// Go through the args and add each point as a Coordinate object to the coordinates array.

//Geometry g1 = new GeometryFactory().createLineString(coordinatesA);

//System.out.println(g1);

while (i < coordA.length) {
// transform string arguments into double values
double x = Double.parseDouble(coordA[i]);
double y = Double.parseDouble(coordA[i+1]);
double xx = Double.parseDouble(coordB[i]);
double yy = Double.parseDouble(coordB[i+1]);
double xxx = Double.parseDouble(coordC[i]);
double yyy = Double.parseDouble(coordC[i+1]);
// create a new Coordinate object and add it to the coordinates array
Coordinate newCoord = new Coordinate(x,y);
coordinatesA[i/2] = newCoord;
Coordinate newCoordB = new Coordinate(xx,yy);
coordinatesB[i/2] = newCoordB;
Coordinate newCoordC = new Coordinate(xxx,yyy);
coordinatesC[i/2] = newCoordC;
//System.out.println(newCoordB.toString());
i=i+2;
} // while

// Create a new Geometry from the array of coordinates.
LineString lineA = new GeometryFactory().createLineString(coordinatesA);
LineString lineB = new GeometryFactory().createLineString(coordinatesB);
LineString lineC = new GeometryFactory().createLineString(coordinatesC);
System.out.println("Line A is "+ lineA);
System.out.println("Line B is "+ lineB);
System.out.println("Line C is "+ lineC);

// Read the start and end point of the line and write them on the screen.
Point startPointA = lineA.getStartPoint();
Point endPointA = lineA.getEndPoint();
//System.out.println("The start point of the line is: " + startPointA.toString());
//System.out.println("The end point of the line is: " + endPointA.toString());


} // else

} //main

}

最佳答案

在使用此 API 方面,我可能比新手高出一个档次,但您的问题与我一直在处理的问题很接近,我可以分享到目前为止我发现的问题。我的问题是给定两个 LineString,找到它们相交的点。我已经使用 LineIntersector 解决了我的问题——它也可以解决你的问题——但最好了解 LineIntersector 帮助解决的更一般的问题。

您要做出的第一个区别是两个 LineString交叉还是交叉。交叉时,没有共享的 Coordinate,但连接至少两对 Coordinate 的线相互跨越。如果两个 LineString 相交,则其中一个 LineString 上会有一个点落在“公差”范围内。我使用 buffer() 方法来指定匹配的容差:

if (lineStringA.buffer(0.0001).intersects(lineStringB)) { ... }

同样用于测试(限制较少的)交叉:

if (lineStringA.buffer(0.0001).crosses(lineStringB)) { ... }

如果两个 LineString 相交,我发现可以直接遍历每个坐标,直到找到位于第二个 LineString 上的第一个坐标.那个公共(public)点就是交点。

如果两个 LineString 交叉但不相交,我会拿出 LineIntersector.computeIntersection() 方法来帮忙,但是这个方法的接口(interface)需要对 LineString 进行一些准备,以找到要使用的合适的 Coordinate

这是我用来遍历第一个 LineString 以找到与第二个 LineString 交叉的两个点的方法:

private LineString findCrossingPair(LineString workingLineString,
LineString fixedLineString) {
// Pick up our factory instance
GeometryFactory factory = fixedLineString.getFactory();

Coordinate[] coordinates = workingLineString.getCoordinates();
int length = coordinates.length;
int indexOfCrossing = 0;

// Walk the workingLineString for as long as it crosses the fixedLineString
for (int i = 1; workingLineString.crosses(fixedLineString)
&& i < (length - 1); i++) {
workingLineString = factory.createLineString(
Arrays.copyOfRange(coordinates, i, length));
indexOfCrossing = i;
}

Coordinate[] crossingPair = Arrays.copyOfRange(coordinates,
indexOfCrossing - 1, indexOfCrossing + 1);
LineString crossingPiece = factory.createLineString(crossingPair);

return crossingPiece;
}

我调用它一次以找到第一对 Coordinates(以 LineString 形式返回),然后将其转过来针对第二个 LineString 运行它。下面是调用 findCrossingPair() 方法两次以获取两对坐标的示例:

    LineString firstPiece = findCrossingPair(LineStringA,
LineStringB);
LineString secondPiece = findCrossingPair(LineStringB,
firstPiece);

// Now we have two 2-point LineStrings which we can pass to the
// LineIntersector

LineIntersector lineIntersector = new RobustLineIntersector();
lineIntersector.computeIntersection(
firstPiece.getStartPoint().getCoordinate(),
firstPiece.getEndPoint().getCoordinate(),
secondPiece.getStartPoint().getCoordinate(),
secondPiece.getEndPoint().getCoordinate()
);
Coordinate intersect = lineIntersector.getIntersection(0);
System.out.println("Intersection at " + intersect);

请注意,在一般情况下,LineIntersector 可以找到 0、1 或 2 个相交点。这就是 LineIntersector 接口(interface)有一个索引被传递给 getIntersection() 方法的原因。交叉与相交的测试限制了此过程可以找到的交叉点的数量。

关于java - 使用 JTS 从多个 LineStrings 计算交点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31520536/

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