Geotools系列说明之JTS空间几何介绍

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是所有图形的父类

  1. 创建Point点
GeometryFactory geometryFactory = new GeometryFactory();
        Point point = geometryFactory.createPoint(transform(new Coordinate(113.549006,22.388645), 4490, 3857)); //可以将4490坐标系进行转换成3857
  1. 创建线段
  /**
     * 生成线段
     * @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);
    }
  1. 创建多边形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);

在这里插入图片描述

  • 21
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

stayhungerstayflush

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值