IT评测·应用市场-qidao123.com

标题: JTS 介绍 [打印本页]

作者: 惊雷无声    时间: 2025-3-23 10:01
标题: JTS 介绍
JTS 是一个用于创建和操作向量几何的Java库;本文主要介绍其基本概念及使用,文中所使用到的软件版本:Java 1.8.0_341、JTS 1.20.0。
1、简介

LocationTech JTS 拓扑套件™(JTS)是一个开源的 Java 软件库,提供了平面几何的对象模型以及一套基础的几何函数。JTS 遵循开放 GIS 联盟发布的 SQL 简单要素规范(Simple Features Specification for SQL)。JTS旨在作为基于矢量的地理信息软件(如地理信息系统)的核心组件使用。它还可以作为一个通用库,提供计算几何学中的算法。
JTS 提供了以下空间数据模型:

2、简单使用

2.1、引入依赖
  1. <dependency>
  2.     <groupId>org.locationtech.jts</groupId>
  3.     <artifactId>jts-core</artifactId>
  4.     <version>1.20.0</version>
  5. </dependency>
复制代码
2.2、创建几何图形

2.2.1、点(Point)
  1. @Test
  2. public void point() throws ParseException {
  3.     //Coordinate 方式创建
  4.     Coordinate coordinate = new Coordinate(118.783607,32.058945);
  5.     Point point = geometryFactory.createPoint(coordinate);
  6.     log.info("point={}", point);
  7.     //WKT 方式创建
  8.     point = (Point) wktReader.read("POINT (118.783607 32.058945)");
  9. }
复制代码
2.2.2、多点(MultiPoint)
  1. @Test
  2. public void multiPoint() throws ParseException {
  3.     //Coordinate 方式创建
  4.     Coordinate coordinate = new Coordinate(118.783607,32.058945);
  5.     Point point = geometryFactory.createPoint(coordinate);
  6.     Coordinate coordinate2 = new Coordinate(118.784201,32.050902);
  7.     Point point2 = geometryFactory.createPoint(coordinate2);
  8.     MultiPoint multiPoint = geometryFactory.createMultiPoint(new Point[]{point, point2});
  9.     log.info("multiPoint={}", multiPoint);
  10.     //WKT 方式创建
  11.     multiPoint = (MultiPoint) wktReader.read("MULTIPOINT ((118.783607 32.058945), (118.784201 32.050902))");
  12. }
复制代码
2.2.3、线(LineString)
  1. @Test
  2. public void lineString() throws ParseException {
  3.     //Coordinate 方式创建
  4.     Coordinate[] coordinates  = new Coordinate[] {new Coordinate(370,290), new Coordinate(540,290), new Coordinate(480,210)};
  5.     LineString lineString = geometryFactory.createLineString(coordinates);
  6.     log.info("lineString={}", lineString);
  7.     //WKT 方式创建
  8.     lineString = (LineString) wktReader.read("LINESTRING (370 290, 540 290, 480 210)");
  9. }
复制代码
2.2.4、多线(MultiLineString)
  1. @Test
  2. public void multiLineString() throws ParseException {
  3.     //Coordinate 方式创建
  4.     Coordinate[] coordinates  = new Coordinate[] {new Coordinate(370,290), new Coordinate(540,290), new Coordinate(480,210)};
  5.     LineString lineString = geometryFactory.createLineString(coordinates);
  6.     Coordinate[] coordinates2  = new Coordinate[] {new Coordinate(370, 160), new Coordinate(500,160), new Coordinate(580,180)};
  7.     LineString lineString2 = geometryFactory.createLineString(coordinates2);
  8.     Coordinate[] coordinates3  = new Coordinate[] {new Coordinate(630,230), new Coordinate(728,275), new Coordinate(740, 180)};
  9.     LineString lineString3 = geometryFactory.createLineString(coordinates3);
  10.     MultiLineString multiLineString = geometryFactory.createMultiLineString(new LineString[]{lineString, lineString2, lineString3});
  11.     log.info("multiLineString={}", multiLineString);
  12.     //WKT 方式创建
  13.     multiLineString = (MultiLineString) wktReader.read("MULTILINESTRING ((370 290, 540 290, 480 210), (370 160, 500 160, 580 180), (630 230, 728 275, 740 180))");
  14. }
复制代码
2.2.5、线性环(LinearRing)
  1. @Test
  2. public void linearRing() throws ParseException {
  3.     //Coordinate 方式创建
  4.     Coordinate[] coordinates  = new Coordinate[] {new Coordinate(370,290), new Coordinate(540,290), new Coordinate(480,210), new Coordinate(370,290)};
  5.     LinearRing linearRing = geometryFactory.createLinearRing(coordinates);
  6.     log.info("linearRing={}", linearRing);
  7.     //WKT 方式创建
  8.     linearRing = (LinearRing) wktReader.read("LINEARRING (370 290, 540 290, 480 210, 370 290)");
  9. }
复制代码
2.2.6、多边形(Polygon)
  1. @Test
  2. public void polygon() throws ParseException {
  3.     //Coordinate 方式创建
  4.     Coordinate[] coordinates  = new Coordinate[] {new Coordinate(450,360), new Coordinate(570,380), new Coordinate(570,310), new Coordinate(460,300), new Coordinate(450,360)};
  5.     LinearRing linearRing = geometryFactory.createLinearRing(coordinates);
  6.     coordinates = new Coordinate[] {new Coordinate(486,345), new Coordinate(530,350), new Coordinate(535,319), new Coordinate(486,345)};
  7.     LinearRing hole = geometryFactory.createLinearRing(coordinates);
  8.     Polygon polygon = geometryFactory.createPolygon(linearRing, new LinearRing[]{hole});
  9.     log.info("polygon={}", polygon.getArea());
  10.     //WKT 方式创建
  11.     polygon = (Polygon) wktReader.read("POLYGON ((450 360, 570 380, 570 310, 460 300, 450 360), (486 345, 530 350, 535 319, 486 345))");
  12. }
复制代码
2.2.7、多多边形(MultiPolygon)
  1. @Test
  2. public void multiPolygon() throws ParseException {
  3.     //Coordinate 方式创建
  4.     Coordinate[] coordinates  = new Coordinate[] {new Coordinate(450,360), new Coordinate(570,380), new Coordinate(570,310), new Coordinate(460,300), new Coordinate(450,360)};
  5.     LinearRing linearRing = geometryFactory.createLinearRing(coordinates);
  6.     coordinates = new Coordinate[] {new Coordinate(486,345), new Coordinate(530,350), new Coordinate(535,319), new Coordinate(486,345)};
  7.     LinearRing hole = geometryFactory.createLinearRing(coordinates);
  8.     Polygon polygon = geometryFactory.createPolygon(linearRing, new LinearRing[]{hole});
  9.     Coordinate[] coordinates2  = new Coordinate[]{new Coordinate(650,350), new Coordinate(730,420), new Coordinate(790,380), new Coordinate(765,301), new Coordinate(650,350)};
  10.     LinearRing linearRing2 = geometryFactory.createLinearRing(coordinates2);
  11.     coordinates2 = new Coordinate[] {new Coordinate(710,380), new Coordinate(760,380), new Coordinate(740,330), new Coordinate(710,380)};
  12.     LinearRing hole2 = geometryFactory.createLinearRing(coordinates2);
  13.     Polygon polygon2 = geometryFactory.createPolygon(linearRing2, new LinearRing[]{hole2});
  14.     MultiPolygon multiPolygon = geometryFactory.createMultiPolygon(new Polygon[]{polygon, polygon2});
  15.     log.info("multiPolygon={}", multiPolygon);
  16.     //WKT 方式创建
  17.     multiPolygon = (MultiPolygon) wktReader.read("MULTIPOLYGON (((450 360, 570 380, 570 310, 460 300, 450 360), (486 345, 530 350, 535 319, 486 345)), ((650 350, 730 420, 790 380, 765 301, 650 350), (710 380, 760 380, 740 330, 710 380)))");
  18. }
复制代码
2.2.8、图形集合(GeometryCollection)
  1. @Test
  2. public void geometryCollection() throws ParseException {
  3.     //Coordinate 方式创建
  4.     Coordinate[] coordinates  = new Coordinate[] {new Coordinate(450,360), new Coordinate(570,380), new Coordinate(570,310), new Coordinate(460,300), new Coordinate(450,360)};
  5.     LinearRing linearRing = geometryFactory.createLinearRing(coordinates);
  6.     coordinates = new Coordinate[] {new Coordinate(486,345), new Coordinate(530,350), new Coordinate(535,319), new Coordinate(486,345)};
  7.     LinearRing hole = geometryFactory.createLinearRing(coordinates);
  8.     Polygon polygon = geometryFactory.createPolygon(linearRing, new LinearRing[]{hole});
  9.     LineString lineString = geometryFactory.createLineString(new Coordinate[]{new Coordinate(600,340), new Coordinate(660,340), new Coordinate(680,310)});
  10.     Point point = geometryFactory.createPoint(new Coordinate(720, 330));
  11.     GeometryCollection geometryCollection = geometryFactory.createGeometryCollection(new Geometry[]{polygon, lineString, point});
  12.     log.info("geometryCollection={}", geometryCollection);
  13.     //WKT 方式创建
  14.     geometryCollection = (GeometryCollection) wktReader.read("GEOMETRYCOLLECTION (POLYGON ((450 360, 570 380, 570 310, 460 300, 450 360), (486 345, 530 350, 535 319, 486 345)), LINESTRING (600 340, 660 340, 680 310), POINT (720 330))");
  15. }
复制代码
2.3、几何关系判断

2.3.1、相等(equalsTopo)

判断两个几何图形在拓扑上是否相等。
  1. @Test
  2. public void equalsTopo() throws ParseException {
  3.     LineString lineString = (LineString)wktReader.read("LINESTRING (370 290, 540 290, 480 210)");
  4.     LineString lineString2 = (LineString)wktReader.read("LINESTRING (370 290, 540 290, 480 210)");
  5.     log.info("equalsTopo={}", lineString.equalsTopo(lineString2));//true
  6.     lineString2 = (LineString)wktReader.read("LINESTRING (370 290, 540 290, 480 220)");
  7.     log.info("equalsTopo={}", lineString.equalsTopo(lineString2));//false
  8. }
复制代码
2.3.2、相交(intersects)

判断两个几何图形是否相交,两几何图形至少有一个公共点。
  1. @Test
  2. public void intersects() throws ParseException {
  3.     LineString lineString = (LineString)wktReader.read("LINESTRING (370 290, 540 290, 480 210)");
  4.     LineString lineString2 = (LineString)wktReader.read("LINESTRING (440 230, 460 330, 630 360)");
  5.     log.info("intersects={}", lineString.intersects(lineString2));//true
  6.     lineString2 = (LineString)wktReader.read("LINESTRING (460 290, 495 348, 556 354)");
  7.     log.info("intersects={}", lineString.intersects(lineString2));//true
  8.     lineString2 = (LineString)wktReader.read("LINESTRING (410 300, 450 320, 545 311)");
  9.     log.info("intersects={}", lineString.intersects(lineString2));//false
  10. }
复制代码



2.3.3、不相交(disjoint)

判断两个几何图形是否不相交,disjoint 是 intersects 的反义。
  1. @Test
  2. public void disjoint() throws ParseException {
  3.     LineString lineString = (LineString)wktReader.read("LINESTRING (370 290, 540 290, 480 210)");
  4.     LineString lineString2 = (LineString)wktReader.read("LINESTRING (440 230, 460 330, 630 360)");
  5.     log.info("intersects={}", lineString.disjoint(lineString2));//false
  6.     lineString2 = (LineString)wktReader.read("LINESTRING (460 290, 495 348, 556 354)");
  7.     log.info("intersects={}", lineString.disjoint(lineString2));//false
  8.     lineString2 = (LineString)wktReader.read("LINESTRING (410 300, 450 320, 545 311)");
  9.     log.info("intersects={}", lineString.disjoint(lineString2));//true
  10. }
复制代码
2.3.4、接触(touches)

判断两个几何图形是否相接触,两几何图形至少有一个公共点,但它们的内部不相交。
  1. @Test
  2. public void touches() throws ParseException {
  3.     LineString lineString = (LineString)wktReader.read("LINESTRING (370 290, 540 290, 480 210)");
  4.     LineString lineString2 = (LineString)wktReader.read("LINESTRING (440 230, 460 330, 630 360)");
  5.     log.info("intersects={}", lineString.touches(lineString2));//false
  6.     lineString2 = (LineString)wktReader.read("LINESTRING (460 290, 495 348, 556 354)");
  7.     log.info("intersects={}", lineString.touches(lineString2));//true
  8.     lineString2 = (LineString)wktReader.read("LINESTRING (410 300, 450 320, 545 311)");
  9.     log.info("intersects={}", lineString.touches(lineString2));//false
  10. }
复制代码
2.3.5、交叉(crosses)

判断两个几何图形是否交叉,两个几何图形有一些但不是所有的内部点是公共的。与 intersects 的区别:
intersects 判断的条件更宽松,只要有交集就会返回 true。
crosses 要求交集的部分必须跨越另一个几何图形的内部,通常表示两个几何图形在某个地区穿越对方。
  1. @Test
  2. public void crosses() throws ParseException {
  3.     LineString lineString = (LineString)wktReader.read("LINESTRING (370 290, 540 290, 480 210)");
  4.     LineString lineString2 = (LineString)wktReader.read("LINESTRING (440 230, 460 330, 630 360)");
  5.     log.info("crosses={}", lineString.crosses(lineString2));//true
  6.     lineString2 = (LineString)wktReader.read("LINESTRING (460 290, 495 348, 556 354)");
  7.     log.info("crosses={}", lineString.crosses(lineString2));//false
  8.     lineString2 = (LineString)wktReader.read("LINESTRING (410 300, 450 320, 545 311)");
  9.     log.info("crosses={}", lineString.crosses(lineString2));//false
  10. }
复制代码
2.3.6、内含(within)

判断一个几何图形是否在另一个几何图形内部。
  1. @Test
  2. public void within() throws ParseException {
  3.     Polygon polygon = (Polygon)wktReader.read("POLYGON ((340 270, 470 270, 470 190, 340 190, 340 270))");
  4.     Point point = (Point) wktReader.read("POINT (380 240)");
  5.     log.info("within={}", point.within(polygon));//true
  6.     point = (Point)wktReader.read("POINT (400 270)");
  7.     log.info("within={}", point.within(polygon));//false
  8.     point = (Point)wktReader.read("POINT (540 230)");
  9.     log.info("within={}", point.within(polygon));//false
  10. }
复制代码



2.3.7、包罗(contains)

判断一个几何图形是否包罗另一个几何图形,contains 是 within 的逆操作。
  1. @Test
  2. public void contains() throws ParseException {
  3.     Polygon polygon = (Polygon)wktReader.read("POLYGON ((340 270, 470 270, 470 190, 340 190, 340 270))");
  4.     Point point = (Point) wktReader.read("POINT (380 240)");
  5.     log.info("contains={}", polygon.contains(point));//true
  6.     point = (Point)wktReader.read("POINT (400 270)");
  7.     log.info("contains={}", polygon.contains(point));//false
  8.     point = (Point)wktReader.read("POINT (540 230)");
  9.     log.info("contains={}", polygon.contains(point));//false
  10. }
复制代码
2.3.8、重叠(overlaps)

判断两个几何图形是否重叠,两个几何图形各自至少有一个不与对方共享的点。
  1. @Test
  2. public void overlaps() throws ParseException {
  3.     Polygon polygon = (Polygon)wktReader.read("POLYGON ((340 270, 470 270, 470 190, 340 190, 340 270))");
  4.     Polygon polygon2 = (Polygon) wktReader.read("POLYGON ((440 300, 560 300, 560 230, 440 230, 440 300))");
  5.     log.info("overlaps={}", polygon.overlaps(polygon2));//true
  6.     polygon2 = (Polygon) wktReader.read("POLYGON ((470 330, 530 330, 530 270, 470 270, 470 330))");
  7.     log.info("overlaps={}", polygon.overlaps(polygon2));//false
  8. }
复制代码


2.4、几何关系分析

2.4.1、缓冲区(buffer)

计算几何图形周围的缓冲区地区。
  1. @Test
  2. public void buffer() throws ParseException {
  3.     Polygon polygon = (Polygon)wktReader.read("POLYGON ((340 270, 470 270, 470 190, 340 190, 340 270))");
  4.     Geometry geometry = polygon.buffer(10);
  5.     log.info("geometry={}", geometry);
  6. }
复制代码

2.4.2、凸壳(convexHull)

计算包罗几何图形中所有点的最小凸多边形。
  1. @Test
  2. public void convexHull() throws ParseException {
  3.     Polygon polygon = (Polygon)wktReader.read("POLYGON ((290 330, 290 270, 400 330, 310 310, 290 330))");
  4.     Geometry geometry = polygon.convexHull();
  5.     log.info("geometry={}", geometry);
  6. }
复制代码

2.4.3、交集(intersection)

计算两几何图形的交集,A∩B。
  1. @Test
  2. public void intersection() throws ParseException {
  3.     Polygon polygon = (Polygon)wktReader.read("POLYGON ((340 270, 470 270, 470 190, 340 190, 340 270))");
  4.     Polygon polygon2 = (Polygon)wktReader.read("POLYGON ((416 295, 540 295, 540 230, 416 230, 416 295))");
  5.     Geometry geometry = polygon.intersection(polygon2);
  6.     log.info("geometry={}", geometry);
  7. }
复制代码

2.4.4、并集(union)

计算两几何图形的合集,AUB。
  1. @Test
  2. public void union() throws ParseException {
  3.     Polygon polygon = (Polygon)wktReader.read("POLYGON ((340 270, 470 270, 470 190, 340 190, 340 270))");
  4.     Polygon polygon2 = (Polygon)wktReader.read("POLYGON ((416 295, 540 295, 540 230, 416 230, 416 295))");
  5.     Geometry geometry = polygon.union(polygon2);
  6.     log.info("geometry={}", geometry);
  7. }
复制代码

2.4.5、差集(difference)

计算一几何图形相对另一几何图形的差集,A-A∩B。
  1. @Test
  2. public void difference() throws ParseException {
  3.     Polygon polygon = (Polygon)wktReader.read("POLYGON ((340 270, 470 270, 470 190, 340 190, 340 270))");
  4.     Polygon polygon2 = (Polygon)wktReader.read("POLYGON ((416 295, 540 295, 540 230, 416 230, 416 295))");
  5.     Geometry geometry = polygon.difference(polygon2);
  6.     log.info("geometry={}", geometry);
  7. }
复制代码

2.4.6、对称差集(symDifference)

计算两集合图形的差别部分,AUB-A∩B。
  1. @Test
  2. public void symDifference() throws ParseException {
  3.     Polygon polygon = (Polygon)wktReader.read("POLYGON ((340 270, 470 270, 470 190, 340 190, 340 270))");
  4.     Polygon polygon2 = (Polygon)wktReader.read("POLYGON ((416 295, 540 295, 540 230, 416 230, 416 295))");
  5.     Geometry geometry = polygon.symDifference(polygon2);
  6.     log.info("geometry={}", geometry);
  7. }
复制代码

2.4.7、计算点到几何图形的近来距离的点(computeDistance)
  1. @Test
  2. public void computeDistance() throws ParseException {
  3.     LineString lineString = (LineString)wktReader.read("LINESTRING (350 226, 374 219, 395 223)");
  4.     Coordinate coordinate = new Coordinate(371,227);
  5.     PointPairDistance ppd = new PointPairDistance();
  6.     DistanceToPoint.computeDistance(lineString, coordinate, ppd);
  7.     log.info("distance={}", ppd.getDistance());
  8.     for (Coordinate cc : ppd.getCoordinates()) {
  9.         log.info("cc={}", cc);
  10.     }
  11. }
复制代码
2.4.8、根据起止点计算线的子线(extractLine)
  1. @Test
  2. public void extractLine() throws ParseException {
  3.     LineString lineString = (LineString)wktReader.read("LINESTRING (250 230, 360 320, 526 256, 525 256)");
  4.     LocationIndexedLine locationIndexedLine = new LocationIndexedLine(lineString);
  5.     LinearLocation start = locationIndexedLine.indexOf(new Coordinate(280, 280));
  6.     LinearLocation end = locationIndexedLine.indexOf(new Coordinate(430, 310));
  7.     Geometry geometry = locationIndexedLine.extractLine(start, end);
  8.     log.info("geometry={}", geometry);
  9. }
复制代码

2.4.9、计算线上距离起点一定距离的位置(extractPoint)
  1. @Test
  2. public void extractPoint() throws ParseException {
  3.     LineString lineString = (LineString)wktReader.read("LINESTRING (350 226, 374 219, 395 223)");
  4.     LocationIndexedLine locationIndexedLine = new LocationIndexedLine(lineString);
  5.     LinearLocation linearLocation = LengthLocationMap.getLocation(lineString, 20);
  6.     Coordinate result = locationIndexedLine.extractPoint(linearLocation);
  7.     log.info("result={}", result);
  8. }
复制代码

2.5、完整代码

  1. package com.abc.demo.jts;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.junit.Test;
  4. import org.locationtech.jts.algorithm.distance.DistanceToPoint;
  5. import org.locationtech.jts.algorithm.distance.PointPairDistance;
  6. import org.locationtech.jts.geom.*;
  7. import org.locationtech.jts.io.ParseException;
  8. import org.locationtech.jts.io.WKTReader;
  9. import org.locationtech.jts.linearref.LengthLocationMap;
  10. import org.locationtech.jts.linearref.LinearLocation;
  11. import org.locationtech.jts.linearref.LocationIndexedLine;
  12. @Slf4j
  13. public class JTSCase {
  14.     private final GeometryFactory geometryFactory = new GeometryFactory();
  15.     private final WKTReader wktReader = new WKTReader(geometryFactory);
  16.     @Test
  17.     public void point() throws ParseException {
  18.         //Coordinate 方式创建
  19.         Coordinate coordinate = new Coordinate(118.783607,32.058945);
  20.         Point point = geometryFactory.createPoint(coordinate);
  21.         log.info("point={}", point);
  22.         //WKT 方式创建
  23.         point = (Point) wktReader.read("POINT (118.783607 32.058945)");
  24.     }
  25.     @Test
  26.     public void multiPoint() throws ParseException {
  27.         //Coordinate 方式创建
  28.         Coordinate coordinate = new Coordinate(118.783607,32.058945);
  29.         Point point = geometryFactory.createPoint(coordinate);
  30.         Coordinate coordinate2 = new Coordinate(118.784201,32.050902);
  31.         Point point2 = geometryFactory.createPoint(coordinate2);
  32.         MultiPoint multiPoint = geometryFactory.createMultiPoint(new Point[]{point, point2});
  33.         log.info("multiPoint={}", multiPoint);
  34.         //WKT 方式创建
  35.         multiPoint = (MultiPoint) wktReader.read("MULTIPOINT ((118.783607 32.058945), (118.784201 32.050902))");
  36.     }
  37.     @Test
  38.     public void lineString() throws ParseException {
  39.         //Coordinate 方式创建
  40.         Coordinate[] coordinates  = new Coordinate[] {new Coordinate(370,290), new Coordinate(540,290), new Coordinate(480,210)};
  41.         LineString lineString = geometryFactory.createLineString(coordinates);
  42.         log.info("lineString={}", lineString);
  43.         //WKT 方式创建
  44.         lineString = (LineString) wktReader.read("LINESTRING (370 290, 540 290, 480 210)");
  45.     }
  46.     @Test
  47.     public void multiLineString() throws ParseException {
  48.         //Coordinate 方式创建
  49.         Coordinate[] coordinates  = new Coordinate[] {new Coordinate(370,290), new Coordinate(540,290), new Coordinate(480,210)};
  50.         LineString lineString = geometryFactory.createLineString(coordinates);
  51.         Coordinate[] coordinates2  = new Coordinate[] {new Coordinate(370, 160), new Coordinate(500,160), new Coordinate(580,180)};
  52.         LineString lineString2 = geometryFactory.createLineString(coordinates2);
  53.         Coordinate[] coordinates3  = new Coordinate[] {new Coordinate(630,230), new Coordinate(728,275), new Coordinate(740, 180)};
  54.         LineString lineString3 = geometryFactory.createLineString(coordinates3);
  55.         MultiLineString multiLineString = geometryFactory.createMultiLineString(new LineString[]{lineString, lineString2, lineString3});
  56.         log.info("multiLineString={}", multiLineString);
  57.         //WKT 方式创建
  58.         multiLineString = (MultiLineString) wktReader.read("MULTILINESTRING ((370 290, 540 290, 480 210), (370 160, 500 160, 580 180), (630 230, 728 275, 740 180))");
  59.     }
  60.     @Test
  61.     public void linearRing() throws ParseException {
  62.         //Coordinate 方式创建
  63.         Coordinate[] coordinates  = new Coordinate[] {new Coordinate(370,290), new Coordinate(540,290), new Coordinate(480,210), new Coordinate(370,290)};
  64.         LinearRing linearRing = geometryFactory.createLinearRing(coordinates);
  65.         log.info("linearRing={}", linearRing);
  66.         //WKT 方式创建
  67.         linearRing = (LinearRing) wktReader.read("LINEARRING (370 290, 540 290, 480 210, 370 290)");
  68.     }
  69.     @Test
  70.     public void polygon() throws ParseException {
  71.         //Coordinate 方式创建
  72.         Coordinate[] coordinates  = new Coordinate[] {new Coordinate(450,360), new Coordinate(570,380), new Coordinate(570,310), new Coordinate(460,300), new Coordinate(450,360)};
  73.         LinearRing linearRing = geometryFactory.createLinearRing(coordinates);
  74.         coordinates = new Coordinate[] {new Coordinate(486,345), new Coordinate(530,350), new Coordinate(535,319), new Coordinate(486,345)};
  75.         LinearRing hole = geometryFactory.createLinearRing(coordinates);
  76.         Polygon polygon = geometryFactory.createPolygon(linearRing, new LinearRing[]{hole});
  77.         log.info("polygon={}", polygon.getArea());
  78.         //WKT 方式创建
  79.         polygon = (Polygon) wktReader.read("POLYGON ((450 360, 570 380, 570 310, 460 300, 450 360), (486 345, 530 350, 535 319, 486 345))");
  80.     }
  81.     @Test
  82.     public void multiPolygon() throws ParseException {
  83.         //Coordinate 方式创建
  84.         Coordinate[] coordinates  = new Coordinate[] {new Coordinate(450,360), new Coordinate(570,380), new Coordinate(570,310), new Coordinate(460,300), new Coordinate(450,360)};
  85.         LinearRing linearRing = geometryFactory.createLinearRing(coordinates);
  86.         coordinates = new Coordinate[] {new Coordinate(486,345), new Coordinate(530,350), new Coordinate(535,319), new Coordinate(486,345)};
  87.         LinearRing hole = geometryFactory.createLinearRing(coordinates);
  88.         Polygon polygon = geometryFactory.createPolygon(linearRing, new LinearRing[]{hole});
  89.         Coordinate[] coordinates2  = new Coordinate[]{new Coordinate(650,350), new Coordinate(730,420), new Coordinate(790,380), new Coordinate(765,301), new Coordinate(650,350)};
  90.         LinearRing linearRing2 = geometryFactory.createLinearRing(coordinates2);
  91.         coordinates2 = new Coordinate[] {new Coordinate(710,380), new Coordinate(760,380), new Coordinate(740,330), new Coordinate(710,380)};
  92.         LinearRing hole2 = geometryFactory.createLinearRing(coordinates2);
  93.         Polygon polygon2 = geometryFactory.createPolygon(linearRing2, new LinearRing[]{hole2});
  94.         MultiPolygon multiPolygon = geometryFactory.createMultiPolygon(new Polygon[]{polygon, polygon2});
  95.         log.info("multiPolygon={}", multiPolygon);
  96.         //WKT 方式创建
  97.         multiPolygon = (MultiPolygon) wktReader.read("MULTIPOLYGON (((450 360, 570 380, 570 310, 460 300, 450 360), (486 345, 530 350, 535 319, 486 345)), ((650 350, 730 420, 790 380, 765 301, 650 350), (710 380, 760 380, 740 330, 710 380)))");
  98.     }
  99.     @Test
  100.     public void geometryCollection() throws ParseException {
  101.         //Coordinate 方式创建
  102.         Coordinate[] coordinates  = new Coordinate[] {new Coordinate(450,360), new Coordinate(570,380), new Coordinate(570,310), new Coordinate(460,300), new Coordinate(450,360)};
  103.         LinearRing linearRing = geometryFactory.createLinearRing(coordinates);
  104.         coordinates = new Coordinate[] {new Coordinate(486,345), new Coordinate(530,350), new Coordinate(535,319), new Coordinate(486,345)};
  105.         LinearRing hole = geometryFactory.createLinearRing(coordinates);
  106.         Polygon polygon = geometryFactory.createPolygon(linearRing, new LinearRing[]{hole});
  107.         LineString lineString = geometryFactory.createLineString(new Coordinate[]{new Coordinate(600,340), new Coordinate(660,340), new Coordinate(680,310)});
  108.         Point point = geometryFactory.createPoint(new Coordinate(720, 330));
  109.         GeometryCollection geometryCollection = geometryFactory.createGeometryCollection(new Geometry[]{polygon, lineString, point});
  110.         log.info("geometryCollection={}", geometryCollection);
  111.         //WKT 方式创建
  112.         geometryCollection = (GeometryCollection) wktReader.read("GEOMETRYCOLLECTION (POLYGON ((450 360, 570 380, 570 310, 460 300, 450 360), (486 345, 530 350, 535 319, 486 345)), LINESTRING (600 340, 660 340, 680 310), POINT (720 330))");
  113.     }
  114.     @Test
  115.     public void equalsTopo() throws ParseException {
  116.         LineString lineString = (LineString)wktReader.read("LINESTRING (370 290, 540 290, 480 210)");
  117.         LineString lineString2 = (LineString)wktReader.read("LINESTRING (370 290, 540 290, 480 210)");
  118.         log.info("equalsTopo={}", lineString.equalsTopo(lineString2));//true
  119.         lineString2 = (LineString)wktReader.read("LINESTRING (370 290, 540 290, 480 220)");
  120.         log.info("equalsTopo={}", lineString.equalsTopo(lineString2));//false
  121.     }
  122.     @Test
  123.     public void intersects() throws ParseException {
  124.         LineString lineString = (LineString)wktReader.read("LINESTRING (370 290, 540 290, 480 210)");
  125.         LineString lineString2 = (LineString)wktReader.read("LINESTRING (440 230, 460 330, 630 360)");
  126.         log.info("intersects={}", lineString.intersects(lineString2));//true
  127.         lineString2 = (LineString)wktReader.read("LINESTRING (460 290, 495 348, 556 354)");
  128.         log.info("intersects={}", lineString.intersects(lineString2));//true
  129.         lineString2 = (LineString)wktReader.read("LINESTRING (410 300, 450 320, 545 311)");
  130.         log.info("intersects={}", lineString.intersects(lineString2));//false
  131.     }
  132.     @Test
  133.     public void disjoint() throws ParseException {
  134.         LineString lineString = (LineString)wktReader.read("LINESTRING (370 290, 540 290, 480 210)");
  135.         LineString lineString2 = (LineString)wktReader.read("LINESTRING (440 230, 460 330, 630 360)");
  136.         log.info("intersects={}", lineString.disjoint(lineString2));//false
  137.         lineString2 = (LineString)wktReader.read("LINESTRING (460 290, 495 348, 556 354)");
  138.         log.info("intersects={}", lineString.disjoint(lineString2));//false
  139.         lineString2 = (LineString)wktReader.read("LINESTRING (410 300, 450 320, 545 311)");
  140.         log.info("intersects={}", lineString.disjoint(lineString2));//true
  141.     }
  142.     @Test
  143.     public void touches() throws ParseException {
  144.         LineString lineString = (LineString)wktReader.read("LINESTRING (370 290, 540 290, 480 210)");
  145.         LineString lineString2 = (LineString)wktReader.read("LINESTRING (440 230, 460 330, 630 360)");
  146.         log.info("intersects={}", lineString.touches(lineString2));//false
  147.         lineString2 = (LineString)wktReader.read("LINESTRING (460 290, 495 348, 556 354)");
  148.         log.info("intersects={}", lineString.touches(lineString2));//true
  149.         lineString2 = (LineString)wktReader.read("LINESTRING (410 300, 450 320, 545 311)");
  150.         log.info("intersects={}", lineString.touches(lineString2));//false
  151.     }
  152.     @Test
  153.     public void crosses() throws ParseException {
  154.         LineString lineString = (LineString)wktReader.read("LINESTRING (370 290, 540 290, 480 210)");
  155.         LineString lineString2 = (LineString)wktReader.read("LINESTRING (440 230, 460 330, 630 360)");
  156.         log.info("crosses={}", lineString.crosses(lineString2));//true
  157.         lineString2 = (LineString)wktReader.read("LINESTRING (460 290, 495 348, 556 354)");
  158.         log.info("crosses={}", lineString.crosses(lineString2));//false
  159.         lineString2 = (LineString)wktReader.read("LINESTRING (410 300, 450 320, 545 311)");
  160.         log.info("crosses={}", lineString.crosses(lineString2));//false
  161.     }
  162.     @Test
  163.     public void within() throws ParseException {
  164.         Polygon polygon = (Polygon)wktReader.read("POLYGON ((340 270, 470 270, 470 190, 340 190, 340 270))");
  165.         Point point = (Point) wktReader.read("POINT (380 240)");
  166.         log.info("within={}", point.within(polygon));//true
  167.         point = (Point)wktReader.read("POINT (400 270)");
  168.         log.info("within={}", point.within(polygon));//false
  169.         point = (Point)wktReader.read("POINT (540 230)");
  170.         log.info("within={}", point.within(polygon));//false
  171.     }
  172.     @Test
  173.     public void contains() throws ParseException {
  174.         Polygon polygon = (Polygon)wktReader.read("POLYGON ((340 270, 470 270, 470 190, 340 190, 340 270))");
  175.         Point point = (Point) wktReader.read("POINT (380 240)");
  176.         log.info("contains={}", polygon.contains(point));//true
  177.         point = (Point)wktReader.read("POINT (400 270)");
  178.         log.info("contains={}", polygon.contains(point));//false
  179.         point = (Point)wktReader.read("POINT (540 230)");
  180.         log.info("contains={}", polygon.contains(point));//false
  181.     }
  182.     @Test
  183.     public void overlaps() throws ParseException {
  184.         Polygon polygon = (Polygon)wktReader.read("POLYGON ((340 270, 470 270, 470 190, 340 190, 340 270))");
  185.         Polygon polygon2 = (Polygon) wktReader.read("POLYGON ((440 300, 560 300, 560 230, 440 230, 440 300))");
  186.         log.info("overlaps={}", polygon.overlaps(polygon2));//true
  187.         polygon2 = (Polygon) wktReader.read("POLYGON ((470 330, 530 330, 530 270, 470 270, 470 330))");
  188.         log.info("overlaps={}", polygon.overlaps(polygon2));//false
  189.     }
  190.     @Test
  191.     public void buffer() throws ParseException {
  192.         Polygon polygon = (Polygon)wktReader.read("POLYGON ((340 270, 470 270, 470 190, 340 190, 340 270))");
  193.         Geometry geometry = polygon.buffer(10);
  194.         log.info("geometry={}", geometry);
  195.     }
  196.     @Test
  197.     public void convexHull() throws ParseException {
  198.         Polygon polygon = (Polygon)wktReader.read("POLYGON ((290 330, 290 270, 400 330, 310 310, 290 330))");
  199.         Geometry geometry = polygon.convexHull();
  200.         log.info("geometry={}", geometry);
  201.     }
  202.     @Test
  203.     public void intersection() throws ParseException {
  204.         Polygon polygon = (Polygon)wktReader.read("POLYGON ((340 270, 470 270, 470 190, 340 190, 340 270))");
  205.         Polygon polygon2 = (Polygon)wktReader.read("POLYGON ((416 295, 540 295, 540 230, 416 230, 416 295))");
  206.         Geometry geometry = polygon.intersection(polygon2);
  207.         log.info("geometry={}", geometry);
  208.     }
  209.     @Test
  210.     public void union() throws ParseException {
  211.         Polygon polygon = (Polygon)wktReader.read("POLYGON ((340 270, 470 270, 470 190, 340 190, 340 270))");
  212.         Polygon polygon2 = (Polygon)wktReader.read("POLYGON ((416 295, 540 295, 540 230, 416 230, 416 295))");
  213.         Geometry geometry = polygon.union(polygon2);
  214.         log.info("geometry={}", geometry);
  215.     }
  216.     @Test
  217.     public void difference() throws ParseException {
  218.         Polygon polygon = (Polygon)wktReader.read("POLYGON ((340 270, 470 270, 470 190, 340 190, 340 270))");
  219.         Polygon polygon2 = (Polygon)wktReader.read("POLYGON ((416 295, 540 295, 540 230, 416 230, 416 295))");
  220.         Geometry geometry = polygon.difference(polygon2);
  221.         log.info("geometry={}", geometry);
  222.     }
  223.     @Test
  224.     public void symDifference() throws ParseException {
  225.         Polygon polygon = (Polygon)wktReader.read("POLYGON ((340 270, 470 270, 470 190, 340 190, 340 270))");
  226.         Polygon polygon2 = (Polygon)wktReader.read("POLYGON ((416 295, 540 295, 540 230, 416 230, 416 295))");
  227.         Geometry geometry = polygon.symDifference(polygon2);
  228.         log.info("geometry={}", geometry);
  229.     }
  230.     @Test
  231.     public void computeDistance() throws ParseException {
  232.         LineString lineString = (LineString)wktReader.read("LINESTRING (350 226, 374 219, 395 223)");
  233.         Coordinate coordinate = new Coordinate(371,227);
  234.         PointPairDistance ppd = new PointPairDistance();
  235.         DistanceToPoint.computeDistance(lineString, coordinate, ppd);
  236.         log.info("distance={}", ppd.getDistance());
  237.         for (Coordinate cc : ppd.getCoordinates()) {
  238.             log.info("cc={}", cc);
  239.         }
  240.     }
  241.     @Test
  242.     public void extractLine() throws ParseException {
  243.         LineString lineString = (LineString)wktReader.read("LINESTRING (250 230, 360 320, 526 256, 525 256)");
  244.         LocationIndexedLine locationIndexedLine = new LocationIndexedLine(lineString);
  245.         LinearLocation start = locationIndexedLine.indexOf(new Coordinate(280, 280));
  246.         LinearLocation end = locationIndexedLine.indexOf(new Coordinate(430, 310));
  247.         Geometry geometry = locationIndexedLine.extractLine(start, end);
  248.         log.info("geometry={}", geometry);
  249.     }
  250.     @Test
  251.     public void extractPoint() throws ParseException {
  252.         LineString lineString = (LineString)wktReader.read("LINESTRING (350 226, 374 219, 395 223)");
  253.         LocationIndexedLine locationIndexedLine = new LocationIndexedLine(lineString);
  254.         LinearLocation linearLocation = LengthLocationMap.getLocation(lineString, 20);
  255.         Coordinate result = locationIndexedLine.extractPoint(linearLocation);
  256.         log.info("result={}", result);
  257.     }
  258. }
复制代码
JTSCase.java2.6、TestBuilder 使用

JTS 提供了图形界面工具,可以直观的查看图形,并可进行计算测试;下载地址:https://github.com/locationtech/jts/releases

 
 
参考:
https://blog.csdn.net/abu935009066/article/details/115304685
https://github.com/locationtech/jts

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4