liuwenliang的个人博客分享 http://blog.sciencenet.cn/u/liuwenliang

博文

[转载]GIS算法:6_JAVA拓扑套件JTS

已有 1758 次阅读 2021-12-16 09:50 |个人分类:GIS分析|系统分类:科研笔记|文章来源:转载

转自:https://blog.csdn.net/sinat_41310868/article/details/106699511

常用可以用于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>

 


 


构建RTree:生成50×50个点,根据这些点生成缓冲1.0的圆,用这些圆构建RTree,在RTree中查点[1,1]落在哪些圆里。




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个面。


 


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),结果如下:


 


 


 


结果可以在JTS TestBuilder中查验。


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




 

————————————————

版权声明:本文为CSDN博主「才华横溢吴道简」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/sinat_41310868/article/details/106699511




https://blog.sciencenet.cn/blog-3409972-1316798.html

上一篇:[转载]GIS算法:3_拓扑空间关系计算模型DE-9IM
下一篇:[转载]GeoTools深入解析:GeoTools概述
收藏 IP: 210.72.26.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-5-18 21:16

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部