JTS介绍
The JTS Topology Suite is a Java API that implements a core set of spatial data operations
using an explicit precision model and robust geometric algorithms. It provides a complete
model for specifying 2-D linear Geometry. Many common operations in computational
geometry and spatial data processing are exposed in a clear, consistent and integrated API.
JTS is intended to be used in the development of applications that support the validation,
cleaning, integration and querying of spatial datasets.
This document is intended for developers who would like to use JTS to accomplish their
spatial data processing requirements. It describes common uses of the JTS API and gives
code examples
官方说它是一个java的api用来计算几何空间的操作工具
源码地址文档在doc目录下
JTS在Geotools中的使用
下载Geotoos的源码我们可以看到GeoTools使用了JTS的类
即Geotools依赖JTS的包
根据上述依赖关系可以知道Geomerty是所有图形的父类
- 创建Point点
GeometryFactory geometryFactory = new GeometryFactory();
Point point = geometryFactory.createPoint(transform(new Coordinate(113.549006,22.388645), 4490, 3857)); //可以将4490坐标系进行转换成3857
- 创建线段
/**
* 生成线段
* @param wkt
* @return
* @throws ParseException
*/
public static LineString createLineString(String wkt) throws ParseException {
WKTReader reader = new WKTReader( geometryFactory );
return (LineString) reader.read(wkt);
}
/**
* 生成线段
* @param coords
* @return
*/
public static LineString createLineString(Coordinate[] coords) {
GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(),4326);
return geometryFactory.createLineString(coords);
}
- 创建多边形Polygon
/**
* 根据左上和右下创建矩形
* @param leftBottom
* @param rightTop
* @return
*/
public static Polygon createRectangle(Coordinate leftBottom, Coordinate rightTop){
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
// 创建矩形的边
Coordinate[] coordinates = new Coordinate[]{
leftBottom,
new Coordinate(rightTop.x, leftBottom.y), // 左上角
rightTop,
new Coordinate(leftBottom.x, rightTop.y), // 右下角
leftBottom // 闭合
};
//Polygon polygon = geometryFactory.createPolygon(polygonCoordinates);
// 创建Polygon
// 输出Polygon的WKT表示
return geometryFactory.createPolygon(coordinates);
}
详见GeometryFactory类,这个类就是工厂类创建
创建完成图形后就可以使用以上方法判断多变的情况
disjoint(), //不相交 几何形状的点不相交
touches(), // 接触 几何形状接触
equals(),// 几何形状拓扑上相等。
intersects(),//相交
crosses(),//交叉
within(),//内涵
contains(),//包含
overlaps()//重叠
示例
判断点在不在多边形内
/**
* 判断是否在多边形区域内
* @param latitude 经度
* @param longitude 纬度
* @param polygonCoordinates 多边形点集合 多边形要闭合才可以
* @return
*/
public static boolean isPointInPolygon(double longitude, double latitude, Coordinate[] polygonCoordinates) {
GeometryFactory geometryFactory = new GeometryFactory();
Point point = geometryFactory.createPoint(new Coordinate(longitude, latitude));
Polygon polygon = geometryFactory.createPolygon(polygonCoordinates);
return polygon.touches(point) || polygon.contains(point);
}
判断点在不在线段上
这里要注意是通过把经纬度变成一个区域和线段进行相交等其他判断
/**
* 判断点在不在线段上
*
* @param longitude
* @param latitude
* @param lineString
* @return
*/
public static boolean isSegmentInPolygon(double longitude, double latitude, LineString lineString) {
GeometryFactory geometryFactory = new GeometryFactory();
Point point = geometryFactory.createPoint(transform(new Coordinate(longitude,latitude),4326,3857));
LineString targetLinString = transform(lineString.toText(), 4326, 3857);
double distance = targetLinString.distance(point);
log.debug("点到直线的距离是:{}",distance);
//a polygonal geometry representing the buffer region (which may be empty)
Geometry buffer = point.buffer( ConstantUtil.ROUTE_LIMIT );
// return targetLinString.isWithinDistance(point,ConstantUtil.ROUTE_LIMIT);
// return true;
return targetLinString.touches(buffer)||targetLinString.crosses(buffer)
||targetLinString.intersects(buffer);
}
JTS中的距离计算
在进行距离计算的时候我们首先要把经纬度进行转换转换方案可参考上一篇博客内容,当完成转换后我们就可以通过JTS提供的api进行距离的计算,如下:
GeometryFactory geometryFactory = new GeometryFactory();
Point point = geometryFactory.createPoint(transform(new Coordinate(113.549006,22.388645), 4490, 3857));
System.out.println(point);
Point point1 = geometryFactory.createPoint(transform(new Coordinate(113.541006, 22.388645),4490,3857));
// System.out.println(transform(point1.toText(), 4326, 4490).toString());
System.out.println(point1.distance(point));
点到线、点到点、点到面、面到到面都可以通过这个示例进行计算。
观察JTS的源码可以知道只要继承了Geometry就可以使用这些方法了。
三维中的几何距离计算
有时候我们需要计算三维的距离,但是目前的Geometry方法提供了二维的计算,通过jts的源码我们可以找到JTS已经支持了三维的空间距离计算如下图:
JTS提供了三维图像的计算示例如下:
GeometryFactory geometryFactory = new GeometryFactory();
Point point = geometryFactory.createPoint(transform(new Coordinate(longitude,latitude,height),4326,3857));
LineString targetLinString = transform(genderLineStrWkt(lineString.getCoordinates()), 4326, 3857);
double distance = targetLinString.distance(point);
log.debug("点到直线的距离是:{}",distance);
// Geometry buffer = point.buffer( ConstantUtil.ROUTE_LIMIT );
// LinearRing 可以是任意的多边形
double distance1 = Distance3DOp.distance(point, targetLinString);