- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 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/
来自 here线性/区域应该工作。但是下面的代码导致编译错误? #include #include #include #include #include #include #include
我对 mySQL 空间函数有疑问。我的目标是查明 LINESTRING 对象是否穿过 POLYGON 对象。为了确定我已经尝试用两个 LINESTRING 对象进行试验以确定它们是否交叉。 SET @
所以我在 PostgreSQL 数据库中有一列同时包含 POINT 对象和 LINESTRING 对象。现在我的最终目标是从这些对象中提取纬度和经度。对于 POINT 对象,我可以通过查询简单地做到这
我一直在 iOS 版谷歌地图中以自定义方式规划路线。 如何解析 LINESTRING 中传入的 JSON? 我的线路: "coordInfo": "LINESTRING (28.64675172929
我有 geojson 文件。此文件包含 LineString 列表。我想从此列表创建多边形。但是每个 LineString 都是一个特征,我不知道哪个 LineString 创建了具体的多边形。注意
我的数据集包含一个 LineString我想过滤掉这个 LineString 的各个线段.更准确地说,每一个街道段。 到目前为止,我已经从数据集中提取了各个点并将它们保存在一个单独的列表中。此外,我想
我有一个渲染 LineStrings 的图层,并尝试对线条应用发光效果。我创建的样式使用自定义渲染器来创建具有垂直于每个线段的渐变的笔划: const glow_style = new Style({
我正在使用 SQL Server 2008 和 Geometry 数据类型来存储英国道路列表,该列表是我从 Ordanance Survey STRATEGI 导入的。数据集。 每条道路被分成多行,每
我有一个渲染 LineStrings 的图层,并尝试对线条应用发光效果。我创建的样式使用自定义渲染器来创建具有垂直于每个线段的渐变的笔划: const glow_style = new Style({
我尝试使用最新的稳定 OpenLayers 库绘制宽度以米为单位的 polyLine(道路车道),但它没有正确的实际宽度(在 Google Maps 图层顶部检查)。 这是对我有用的代码片段: pol
很抱歉打扰你们,但我已经被他的问题困扰了半天。 我想使用 LineString 对象在 OpenLayers 中绘制折线,所以我从文档中复制了示例。它运行正常,但我在屏幕上看不到这条线 代码是这样的
我有一个 LineString 序列,例如 lineString1 和 lineString2 其中 lineString1.getEndPoint() == lineString2.getStart
我使用 LINESTRING() 将路线的 GPS 坐标存储在一行中。当我想处理这些数据时,我需要使用另一个函数,即 X()、Y()、ASTEXT()、 GEOMFROMTEXT()等 我在想,如果再
好的,所以我知道如何将数据从 php 格式转换为 geojson 格式,但我不明白如何将其转换为 lineString 类型这是代码,所以问题是如何将坐标数据转换为一个数组: include(
我有一个名为 locations 的表,其中包含这些行: id uuid NOT NULL, "deviceId" text COLLATE pg_catalog."default", "userId
在 PostGIS 中,您可以使用以下方法使两个几何相交: geometry ST_Intersection (geometry geomA, geometry geomB); 在我的例子中,geom
在使用 shapely 时,我遇到了一个奇怪的问题。有 2 个点 p1 和 p2,第一个属于多边形,第二个不属于。当我试图找到包含这 2 个点的 LineString 与多边形边界线的端点之间的交点时
我有一组 GPS 位置,我用 Mapbox 将其放在 map 上,然后在点之间画一条线。 有没有办法画一条沿着道路的平滑/智能线?而不是我在图纸上得到的? Single marker
我正在尝试创建两个数组的单个 LineString 对象。我已经尝试了多种方法,但我不断得到: >>> array = [(0,0),(1,0),(2,0),(3,0)][(1,3),(1.4,2),
我正在尝试在离其他坐标最近的点处拆分 Shapely LineString。我可以使用 project 和 interpolate 获得直线上的最近点,但此时我无法拆分直线,因为它不是顶点。 我需要沿
我是一名优秀的程序员,十分优秀!