1.计算道路与起点之间指定距离之间的交点坐标
已知:道路、在道路的距离
求解:到起点坐标距离的点
public static Coordinate lengthOnLineString2(Geometry roadLine, double length) {
LocationIndexedLine locationIndexedLine = new LocationIndexedLine(roadLine);
LinearLocation linearLocation = LengthLocationMap.getLocation(roadLine, length);
Coordinate result = locationIndexedLine.extractPoint(linearLocation);
return result;
}
根据起始点求一个线的子线
已知:起点、终点、道路
求解:其在道路上的子轨迹
GeometryFactory GeometryFactory = new GeometryFactory();
WKTReader reader = new WKTReader(GeometryFactory);
Geometry geom = reader.read("LINESTRING(0 0, 10 0, 10 10, 20 10)");
LocationIndexedLine lil = new LocationIndexedLine(geom);
LinearLocation start = lil.indexOf(new Coordinate(8, 5));
LinearLocation end = lil.indexOf(new Coordinate(17, 10));
Geometry result = lil.extractLine(start, end);
System.out.println(result.toText());
// 结果
LINESTRING (10 5, 10 10, 17 10)
几何到另一个几何最近的点
GeometryFactory gf = JTSFactoryFinder.getGeometryFactory(null);
WKTReader reader = new WKTReader(gf);
Geometry line2 = reader.read("LINESTRING(0 0, 10 0, 10 10, 20 10)");
Coordinate c = new Coordinate(5, 5);
PointPairDistance ppd = new PointPairDistance();
DistanceToPoint.computeDistance(line2, c, ppd);
System.out.println(ppd.getDistance());
for (Coordinate cc : ppd.getCoordinates()) {
System.out.println(cc);
}
求几何的交点
// create ring: P1(0,0) - P2(0,10) - P3(10,10) - P4(0,10)
LinearRing lr = new GeometryFactory().createLinearRing(new Coordinate[]{new Coordinate(0,0), new Coordinate(0,10), new Coordinate(10,10), new Coordinate(10,0), new Coordinate(0,0)});
// create line: P5(5, -1) - P6(5, 11) -> crossing the ring vertically in the middle
LineString ls = new GeometryFactory().createLineString(new Coordinate[]{new Coordinate(5,-1), new Coordinate(5,11)});
// calculate intersection points
Geometry intersectionPoints = lr.intersection(ls);
// simple output of points
for(Coordinate c : intersectionPoints.getCoordinates()){
System.out.println(c.toString());
}
利用缓冲画圆
geometry = point
Geometry buffer = geometry.buffer( 2.0 ); // note distance is in same units as geometry
距离、长度、面积计算
- 1.先把坐标系都转成墨卡托
- 2.用jts计算距离
- 3.由于转成墨卡托坐标系,结果比实际偏大,要乘以系数
// WGS84(一般项目中常用的是CRS:84和EPSG:4326)
CoordinateReferenceSystem sourceCRS = CRS.decode("CRS:84");
// Pseudo-Mercator(墨卡托投影)
CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:3857");
MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS, false);
GeometryFactory GeometryFactory = new GeometryFactory();
Coordinate k96 = new Coordinate(117.41722, 31.975379);
Coordinate k97 = new Coordinate(117.426387, 31.970712);
Geometry line = JTS.transform(GeometryFactory.createLineString(new Coordinate[]{k96, k97}), transform);
//double rate1 = Math.cos(Math.toRadians(k96.y)); 距离短的话乘以这个就行, 大概为: 0.8482973000510488
double rate = Math.cos((Math.toRadians(k96.y) + Math.toRadians(k97.y)) / 2.000000);//距离长的话用这个
System.out.println("rate: " + rate);
// 面积、周长
System.out.println(line.getArea());// 面积未验证是乘以 rate 还是其平方
System.out.println(line.getLength() * rate);
Coordinate tmp = new Coordinate(117.417145, 31.975465);
Geometry point = JTS.transform(GeometryFactory.createPoint(tmp), transform);
System.out.println(point.distance(line) * rate);