JTS Geometry用例分析

article/2025/8/15 3:01:42

微信搜索:“二十同学” 公众号,欢迎关注一条不一样的成长之路

拓扑关系


      
GeometryTest

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryCollection;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.LinearRing;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Polygon;
import com.vividsolutions.jts.geom.MultiPolygon;
import com.vividsolutions.jts.geom.MultiLineString;
import com.vividsolutions.jts.geom.MultiPoint;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;public class GeometryTest {private GeometryFactory geometryFactory = new GeometryFactory();/*** create a point* @return*/public Point createPoint(){Coordinate coord = new Coordinate(109.013388, 32.715519);Point point = geometryFactory.createPoint( coord );return point;}/*** create a point by WKT* @return* @throws ParseException */public Point createPointByWKT() throws ParseException{WKTReader reader = new WKTReader( geometryFactory );Point point = (Point) reader.read("POINT (109.013388 32.715519)");return point;}/*** create multiPoint by wkt* @return*/public MultiPoint createMulPointByWKT()throws ParseException{WKTReader reader = new WKTReader( geometryFactory );MultiPoint mpoint = (MultiPoint) reader.read("MULTIPOINT(109.013388 32.715519,119.32488 31.435678)");return mpoint;}/*** * create a line* @return*/public LineString createLine(){Coordinate[] coords  = new Coordinate[] {new Coordinate(2, 2), new Coordinate(2, 2)};LineString line = geometryFactory.createLineString(coords);return line;}/*** create a line by WKT* @return* @throws ParseException*/public LineString createLineByWKT() throws ParseException{WKTReader reader = new WKTReader( geometryFactory );LineString line = (LineString) reader.read("LINESTRING(0 0, 2 0)");return line;}/*** create multiLine * @return*/public MultiLineString createMLine(){Coordinate[] coords1  = new Coordinate[] {new Coordinate(2, 2), new Coordinate(2, 2)};LineString line1 = geometryFactory.createLineString(coords1);Coordinate[] coords2  = new Coordinate[] {new Coordinate(2, 2), new Coordinate(2, 2)};LineString line2 = geometryFactory.createLineString(coords2);LineString[] lineStrings = new LineString[2];lineStrings[0]= line1;lineStrings[1] = line2;MultiLineString ms = geometryFactory.createMultiLineString(lineStrings);return ms;}/*** create multiLine by WKT* @return* @throws ParseException*/public MultiLineString createMLineByWKT()throws ParseException{WKTReader reader = new WKTReader( geometryFactory );MultiLineString line = (MultiLineString) reader.read("MULTILINESTRING((0 0, 2 0),(1 1,2 2))");return line;}/*** create a polygon(多边形) by WKT* @return* @throws ParseException*/public Polygon createPolygonByWKT() throws ParseException{WKTReader reader = new WKTReader( geometryFactory );Polygon polygon = (Polygon) reader.read("POLYGON((20 10, 30 0, 40 10, 30 20, 20 10))");return polygon;}/*** create multi polygon by wkt* @return* @throws ParseException*/public MultiPolygon createMulPolygonByWKT() throws ParseException{WKTReader reader = new WKTReader( geometryFactory );MultiPolygon mpolygon = (MultiPolygon) reader.read("MULTIPOLYGON(((40 10, 30 0, 40 10, 30 20, 40 10),(30 10, 30 0, 40 10, 30 20, 30 10)))");return mpolygon;}/*** create GeometryCollection  contain point or multiPoint or line or multiLine or polygon or multiPolygon* @return* @throws ParseException*/public GeometryCollection createGeoCollect() throws ParseException{LineString line = createLine();Polygon poly =  createPolygonByWKT();Geometry g1 = geometryFactory.createGeometry(line);Geometry g2 = geometryFactory.createGeometry(poly);Geometry[] garray = new Geometry[]{g1,g2};GeometryCollection gc = geometryFactory.createGeometryCollection(garray);return gc;}/*** create a Circle  创建一个圆,圆心(x,y) 半径RADIUS* @param x* @param y* @param RADIUS* @return*/public Polygon createCircle(double x, double y, final double RADIUS){final int SIDES = 32;//圆上面的点个数Coordinate coords[] = new Coordinate[SIDES+1];for( int i = 0; i < SIDES; i++){double angle = ((double) i / (double) SIDES) * Math.PI * 2.0;double dx = Math.cos( angle ) * RADIUS;double dy = Math.sin( angle ) * RADIUS;coords[i] = new Coordinate( (double) x + dx, (double) y + dy );}coords[SIDES] = coords[0];LinearRing ring = geometryFactory.createLinearRing( coords );Polygon polygon = geometryFactory.createPolygon( ring, null );return polygon;}/*** @param args* @throws ParseException */public static void main(String[] args) throws ParseException {GeometryDemo gt = new GeometryDemo();Polygon p = gt.createCircle(0, 1, 2);//圆上所有的坐标(32个)Coordinate coords[] = p.getCoordinates();for(Coordinate coord:coords){System.out.println(coord.x+","+coord.y);}}
}



 

Geometry之间的关系有下述几种:

import com.vividsolutions.jts.geom.*;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;/*** gemotry之间的关系使用*/
public class GeometryTest {private GeometryFactory geometryFactory = new GeometryFactory();/***  两个几何对象是否是重叠的* @return* @throws ParseException*/public boolean equalsGeo() throws ParseException{WKTReader reader = new WKTReader( geometryFactory );LineString geometry1 = (LineString) reader.read("LINESTRING(0 0, 2 0, 5 0)");LineString geometry2 = (LineString) reader.read("LINESTRING(5 0, 0 0)");return geometry1.equals(geometry2);//true}/*** 几何对象没有交点(相邻)* @return* @throws ParseException*/public boolean disjointGeo() throws ParseException{WKTReader reader = new WKTReader( geometryFactory );LineString geometry1 = (LineString) reader.read("LINESTRING(0 0, 2 0, 5 0)");LineString geometry2 = (LineString) reader.read("LINESTRING(0 1, 0 2)");return geometry1.disjoint(geometry2);}/*** 至少一个公共点(相交)* @return* @throws ParseException*/public boolean intersectsGeo() throws ParseException{WKTReader reader = new WKTReader( geometryFactory );LineString geometry1 = (LineString) reader.read("LINESTRING(0 0, 2 0, 5 0)");LineString geometry2 = (LineString) reader.read("LINESTRING(0 0, 0 2)");Geometry interPoint = geometry1.intersection(geometry2);//相交点System.out.println(interPoint.toText());//输出 POINT (0 0)return geometry1.intersects(geometry2);}/*** 判断以x,y为坐标的点point(x,y)是否在geometry表示的Polygon中* @param x* @param y* @param geometry wkt格式* @return*/public boolean withinGeo(double x,double y,String geometry) throws ParseException {Coordinate coord = new Coordinate(x,y);Point point = geometryFactory.createPoint( coord );WKTReader reader = new WKTReader( geometryFactory );Polygon polygon = (Polygon) reader.read(geometry);return point.within(polygon);}/*** @param args* @throws ParseException */public static void main(String[] args) throws ParseException {GeometryRelated gr = new GeometryRelated();System.out.println(gr.equalsGeo());System.out.println(gr.disjointGeo());System.out.println(gr.intersectsGeo());System.out.println(gr.withinGeo(5,5,"POLYGON((0 0, 10 0, 10 10, 0 10,0 0))"));}}

 

关系类型

 

  

import java.util.ArrayList;
import java.util.List;import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;public class GeometryTest {private GeometryFactory geometryFactory = new GeometryFactory();/*** create a Point** @param x* @param y* @return*/public Coordinate point(double x, double y) {return new Coordinate(x, y);}/*** create a line** @return*/public LineString createLine(List<Coordinate> points) {Coordinate[] coords = (Coordinate[]) points.toArray(new Coordinate[points.size()]);LineString line = geometryFactory.createLineString(coords);return line;}/*** 返回a指定距离内的多边形和多多边形** @param a* @param distance* @return*/public Geometry bufferGeo(Geometry a, double distance) {return a.buffer(distance);}/*** 返回(A)与(B)中距离最近的两个点的距离** @param a* @param b* @return*/public double distanceGeo(Geometry a, Geometry b) {return a.distance(b);}/*** 两个几何对象的交集** @param a* @param b* @return*/public Geometry intersectionGeo(Geometry a, Geometry b) {return a.intersection(b);}/*** 几何对象合并** @param a* @param b* @return*/public Geometry unionGeo(Geometry a, Geometry b) {return a.union(b);}/*** 在A几何对象中有的,但是B几何对象中没有** @param a* @param b* @return*/public Geometry differenceGeo(Geometry a, Geometry b) {return a.difference(b);}public static void main(String[] args) {Operation op = new Operation();//创建一条线List<Coordinate> points1 = new ArrayList<Coordinate>();points1.add(op.point(0, 0));points1.add(op.point(1, 3));points1.add(op.point(2, 3));LineString line1 = op.createLine(points1);//创建第二条线List<Coordinate> points2 = new ArrayList<Coordinate>();points2.add(op.point(3, 0));points2.add(op.point(3, 3));points2.add(op.point(5, 6));LineString line2 = op.createLine(points2);System.out.println(op.distanceGeo(line1, line2));//out 1.0System.out.println(op.intersectionGeo(line1, line2));//out GEOMETRYCOLLECTION EMPTYSystem.out.println(op.unionGeo(line1, line2)); //out MULTILINESTRING ((0 0, 1 3, 2 3), (3 0, 3 3, 5 6))System.out.println(op.differenceGeo(line1, line2));//out LINESTRING (0 0, 1 3, 2 3)}
}

 


http://chatgpt.dhexx.cn/article/FLnuG2ad.shtml

相关文章

JTS 笔记

简介 JTS由加拿大的VividSolutions公司开发&#xff0c;是一个用Java语言描述的几何拓扑套件&#xff0c;遵循OpenGIS的Simple Feature Specification&#xff0c;封装了2D几何类型和非常多的空间分析操作&#xff0c;而且包含了不少常见的计算几何算法实现。     JTS被广泛…

jts 简介、中文文档、中英对照文档 下载

jts 文档 下载链接&#xff08;含jar包、源码、pom&#xff09; 组件名称中文-文档-下载链接中英对照-文档-下载链接jts-1.13.jarjts-1.13-API文档-中文版.zipjts-1.13-API文档-中英对照版.zip jts 简介 JTS拓扑套件 JTS拓扑套件是一个用于建模和操作二维线性几何的API。它…

GeoTools——JTS的相关介绍和操作

GeoTools——JTS的相关介绍&#xff08;一&#xff09; JTS拓扑套件是GeoTools用于提供Geometry数据结构的实现&#xff0c;Geometry主要是指几何形状。 想要使用geoTools——JTS相关的操作可以导入以下的依赖<properties><geotools.version>17.1</geotools.ver…

java topo: 开源jtopo框架

java web 非本人研究方向&#xff0c;只是作为拓展知识涉猎范围 原文链接&#xff1a;http://www.jtopo.com/index.html jTopo是什么? jTopo&#xff08;Javascript Topology library)是一款完全基于HTML5 Canvas的关系、拓扑图形化界面开发工具包。 jTopo关注于数据的图形展…

JTopo踩坑记 -- React项目中使用JTopo

JTopo踩坑记 – React项目中使用JTopo JTopo-in-node & JTopo 最近实习公司一个项目需要绘制电网的拓扑图&#xff0c;大致要求的效果如下&#xff1a; 首先想到的是Echarts和d3&#xff0c;因为这个项目其他图表都是使用的Echarts&#xff0c; 但是在Echarts的官方示例…

jtopo的项目实战(七)

话接上回&#xff0c;继续实现jtopo本身不具有的一些功能&#xff0c;有讲的不对的地方&#xff0c;欢迎大家在评论区留言提出&#xff0c;博主基本每天在线&#xff0c;看到都会及时回复的&#xff0c;我们一起进步&#xff0c;奥利给&#xff0c;还有&#xff0c;码字不易&am…

关于vue3.0项目引入jtopo出现的各种问题汇总

jtopo官网demo网址&#xff1a;http://www.jtopo.com/demo.html 最近的项目需要用到jtopo&#xff0c;关于jtopo的引入&#xff0c;之前自己也都尝试过&#xff0c;没想到这次引入会出现这么多问题&#xff0c;所以大概总结一下 1、借用同事以前的经验来引入 npm install jto…

JTopo 框架教程之一:初始JTopo 框架

JTopo 是什么&#xff1f; JTopo&#xff08;Javascript Topology library)是一款完全基于HTML5 Canvas的关系、拓扑图形化界面开发工具包。jTopo关注于数据的图形展示&#xff0c;它是面向开发人员的&#xff0c;需要进行二次开发。使用jTopo很简单&#xff0c;可以快速创建一…

jtopo的项目实战(一)

前言 初识jtopo是因为项目中需要一个能够动态绘制网络拓扑图的插件&#xff0c;不但能画出基本的网络拓扑结构&#xff0c;还要能够标识出网络设备的类型以及端口设置&#xff0c;甚至是网络连线的流量信息等&#xff0c;基于这个需求查找了很多资料&#xff0c;初步选中了几款…

jtopo的项目实战(二)

话接上回&#xff0c;继续实现jtopo本身不具有的一些功能&#xff0c;有讲的不对的地方&#xff0c;欢迎大家留言提出&#xff0c;我们一起进步&#xff0c;奥利给&#xff0c;还有&#xff0c;码字不易&#xff0c;如果觉得博主写的不错的欢迎打赏哈&#xff01; 2.美化节点之…

vue、jtopo绘制鱼骨图

效果如下图所示&#xff1a; 一、文件目录结构&#xff1a; 二、绘制png的鱼头、鱼尾图片 fish_head.png&#xff1a; fish_tail.png&#xff1a; 三、下载js文件 jquery、jtopo请到官网下载。 四、编写鱼骨图核心文件&#xff1a;MakFishBone.js let MakFishBo…

通过JTopo.js在网页绘制网络拓扑图

项目中遇到要在网页中绘制网络拓扑图的需求&#xff0c;要求节点具备点击、拖拽、缩放等交互功能&#xff0c;并且可以显示/隐藏详细信息&#xff0c;数据是设备实时上报来的。 综上&#xff0c;用画拓扑图的工具画一张固定的图片偷懒显然是不行的&#xff0c;最好是找到一个封…

JTopo添加动态连线

效果如下 先在 JTopo 的 link 原型上定义一个方法 window.requestAnimationFrame window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame JTopo.Link.prototype.drawanimepic functi…

JTopo + Vue 实现自定义拖拽流程图

JTopo Vue 实现自定义拖拽流程图 进入体验效果 进入github 市场上做流程图的插件比较多&#xff0c;这里介绍一种基于canvas编写的js插件&#xff0c;结合vue框架做出精美的流程图 首先搭建vue框架&#xff0c;这里就不做介绍&#xff0c;由于jtopo官方demo里用到了jquery&am…

jtopo连线绘制脉冲动画效果

随着需求的日益精细化&#xff0c;人们越来越多的关注到了画面的美观&#xff0c;单纯的节点间连线已经不满足人们的审美观念了&#xff0c;那么能不能让节点间的连线动起来呢&#xff1f;答案是可以的&#xff0c;老规矩&#xff0c;先上图&#xff0c;再说怎么实现。 同样&am…

jTopo入门 简单实现拓扑图

最近项目中需要绘制拓扑图&#xff0c;于是研究了下绘制拓扑图的组件&#xff0c;jtopo是一款比较简单易上手的开发工具包&#xff0c;分享给大家。 jtopo特点 1、完全基于HTML5 Canvas开发&#xff0c;API平易近人、几乎简单到了极致。 2、不依赖任何其他库、执行仅需一个Ca…

jtopo 实现一键布局

最近很忙、也很懒&#xff0c;一堆烦心事&#xff0c;jtopo后面不准备再深究了&#xff0c;本身东西也不多&#xff0c;做出的新功能&#xff0c;新特效也都写到博客中来了&#xff0c;今天给大家分享最近研究的一个新技能——jtopo一键布局&#xff0c;写给大家、也写给自己。…

jtopo简单实例

原贴地址 http://cn-arthurs.iteye.com/blog/2009345 说明: jtopo是一个基于canvas的js拓扑图形组件.比canvasexpress容易多了. 可以方便地加点,加连线,加鼠标事件,拖曳. 号称跨浏览器,不过实际上不支持ie678,加上excanvas.js也没用,除非像canvasexpress那样使用chrome插件. …

Vue — jTopo

近期在Vue项目中使用jTopo来制作集群节点拓扑图&#xff0c;官网http://www.jtopo.com/ 使用vue-cli搭建的模块化开发项目&#xff0c;使用第三方库最好的方式就是通过npm install xxx安装&#xff0c;然后在项目 里import xxx来使用&#xff1b;但是在JTopo官网上并没有发现有…

【Vue引入JTopo及所遇到的问题】

Vue引入JTopo及所遇到的问题 前言一、方案选型二、使用步骤三、总结 前言 项目过程中总是会遇到稀奇古怪的需求&#xff0c;这不&#xff0c;咱老大又让我画一个系统拓扑图放在首页&#xff0c;要求部分数据需从后端获取&#xff0c;动态展示在页面上。对于一个后端人猿来说&a…