GIS算法:JAVA拓扑套件JTS

本文介绍JTS Java包的基础使用方法,包括RTree空间索引构建与查询,以及线裁切面的空间操作。通过实例展示了如何利用JTS进行高效的空间数据处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

常用可以用于GIS数据处理和空间计算的java包有geotool和jts。

相对来说,geotool功能更全面,还可以用于数据转换、瓦片地图发布、栅格影像分析等,jts只能进行基本的数据处理和空间计算。

但大多数情况下jts就完全够用了。

geotool的官网:https://www.geotools.org/

本例只讲jts的用法:

maven依赖:

<dependency>
  <groupId>com.vividsolutions</groupId>
  <artifactId>jts</artifactId>
  <version>1.13</version>
</dependency>
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.index.strtree.STRtree;
 
import java.util.List;
 
public class RTreeDemo {
    public static void main(String[] args){
        //声明STRtree
        STRtree strTree = new STRtree();
        GeometryFactory geometryFactory = new GeometryFactory();
        for (int i=0;i<50;i++){
            for (int j=0;j<50;j++){
                //新建一个点
                Geometry p=geometryFactory.createPoint(new Coordinate(i,j));
                //以点为中心,取一个半径为1.0的圆,插入RTree
//insert(Envelope itemEnv, Object item)
                strTree.insert(p.buffer(1.0).getEnvelopeInternal(),p.buffer(1.0));
            }
        }
        //构建RTree
        strTree.build();
        //查询点[1,1]落在哪些圆中
        List querys=strTree.query(geometryFactory.createPoint(new Coordinate(1,1)).getEnvelopeInternal());
        for (Object obj:querys) {
            System.out.println(obj);
        }
    }
}

线裁切面:如图所示,用红色线去裁切蓝色面,结果会生成3个面。

img img

import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;
import com.vividsolutions.jts.operation.polygonize.Polygonizer;
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
 
public class PolygonDemo {
    public static void main(String[] args){
        // wkt工具,将wkt文本转为geometry对象
        WKTReader wktReader=new WKTReader();
        try {
            //读被裁切面
            Geometry polygon=wktReader.read("POLYGON ((220 350, 400 440, 635 249, 380 80, 174 164, 179 265, 220 350))");
            //读裁切线
            Geometry polyline=wktReader.read("LINESTRING (570 400, 392 315, 299 215, 430 140, 530 240, 450 360, 460 480)");
            //取面的边线
            Geometry boundary=polygon.getBoundary();
            //将裁切线与面的边线联合,交点会被打断
            polyline=polyline.union(boundary);
            List<Geometry> clipPolygon = new ArrayList<>();
            List<Geometry> lineList=new ArrayList<>();
            for(int i=0;i<polyline.getNumGeometries();i++){
                lineList.add(polyline.getGeometryN(i));
            }
            // 构造面生成器
            Polygonizer p = new Polygonizer();
            p.add(lineList);
            //取构面结果
            Collection<Geometry> polys = p.getPolygons();
            //取buffer,以免因精度损失,遗漏构面结果
            Geometry buffer=polygon.buffer(1);
            for(Geometry geometry:polys){
                //如果包含在buffer中,则添加
                if(buffer.contains(geometry)){
                    clipPolygon.add(geometry);
                }
            }
            for (Geometry presult:clipPolygon) {
                System.out.println(presult);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

将裁切线换成LINESTRING (320 330, 500 280, 400 150, 290 200, 400 360),结果如下:

img img

结果可以在JTS TestBuilder中查验。

在控制台input的文本框中输入wkt文本,点击load geometrys,就可以在面板中展示图形。

img

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Archie_java

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

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

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

打赏作者

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

抵扣说明:

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

余额充值