Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions jts/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<groupId>com.baeldung.jts</groupId>
<artifactId>jst-example</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
<maven.compiler.source>21</maven.compiler.source> <!-- Java version -->
<maven.compiler.target>21</maven.compiler.target>
<slf4j.version>2.0.17</slf4j.version>
<junit.version>4.13.2</junit.version>
<jts.version>1.20.0</jts.version>
<qos.logback.version>1.5.18</qos.logback.version>
<maven.compiler.plugin.version>3.14.0</maven.compiler.plugin.version>
<maven.shade.plugin.version>3.2.4</maven.shade.plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.locationtech.jts</groupId>
<artifactId>jts-core</artifactId>
<version>${jts.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${qos.logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${qos.logback.version}</version>
</dependency>
<!-- JUnit for unit testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<!-- Maven Compiler Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>

<!-- Maven Shade Plugin for creating an executable JAR -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${maven.shade.plugin.version}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.baeldung.jts.JtsApplication</mainClass> <!-- Main class -->
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
12 changes: 12 additions & 0 deletions jts/src/main/java/com/baeldung/jts/JtsApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.baeldung.jts;

import com.baeldung.jts.utils.GeometryFactoryUtil;
import org.locationtech.jts.geom.Geometry;

public class JtsApplication {

public static void main(String[] args) throws Exception {

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.baeldung.jts.operations;

import org.locationtech.jts.geom.Geometry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JTSOperationUtils {
private static final Logger log = LoggerFactory.getLogger(JTSOperationUtils.class);

public static boolean checkContainment(Geometry point, Geometry polygon) {
boolean isInside = polygon.contains(point);
log.info("Is the point inside polygon? {}", isInside);
return isInside;
}

public static boolean checkIntersect(Geometry rectangle1, Geometry rectangle2) {
boolean intersect = rectangle1.intersects(rectangle2);
Geometry overlap = rectangle1.intersection(rectangle2);

log.info("Do both rectangle intersect? {}", intersect);
log.info("Overlapping Area: {}", overlap);
return intersect;
}

public static Geometry getBuffer(Geometry point, int intBuffer) {
Geometry buffer = point.buffer(intBuffer);
log.info("Buffer Geometry: {}", buffer);
return buffer;
}

public static double getDistance(Geometry point1, Geometry point2) {
double distance = point1.distance(point2);
log.info("Distance: {}",distance);
return distance;
}

public static Geometry getUnion(Geometry geometry1, Geometry geometry2) {
Geometry union = geometry1.union(geometry2);
log.info("Union Result: {}", union);
return union;
}

public static Geometry getDifference(Geometry base, Geometry cut) {
Geometry result = base.difference(cut);
log.info("Resulting Geometry: {}", result);
return result;
}

public static Geometry validateAndRepair(Geometry invalidGeo) throws Exception {
boolean valid = invalidGeo.isValid();
log.info("Is valid Geometry value? {}", valid);

Geometry repaired = invalidGeo.buffer(0);
log.info("Repaired Geometry: {}", repaired);
return repaired;
}
}
11 changes: 11 additions & 0 deletions jts/src/main/java/com/baeldung/jts/utils/GeometryFactoryUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.baeldung.jts.utils;

import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.WKTReader;

public class GeometryFactoryUtil {
public static Geometry readWKT(String wkt) throws Exception {
WKTReader reader = new WKTReader();
return reader.read(wkt);
}
}
1 change: 1 addition & 0 deletions jts/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.application.name=jts
69 changes: 69 additions & 0 deletions jts/src/test/java/com/baeldung/jts/JtsApplicationUnitTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.baeldung.jts;

import com.baeldung.jts.operations.JTSOperationUtils;
import com.baeldung.jts.utils.GeometryFactoryUtil;
import org.junit.Assert;
import org.junit.Test;
import org.locationtech.jts.geom.Geometry;

public class JtsApplicationUnitTest {
@Test
public void givenPolygon2D_whenContainPoint_thenContainmentIsTrue() throws Exception {
Geometry point = GeometryFactoryUtil.readWKT("POINT (10 20)");
Geometry polygon = GeometryFactoryUtil.readWKT("POLYGON ((0 0, 0 40, 40 40, 40 0, 0 0))");
Assert.assertTrue(JTSOperationUtils.checkContainment(point, polygon));
}

@Test
public void givenRectangle1_whenIntersectWithRectangle2_thenIntersectionIsTrue() throws Exception {
Geometry rectangle1 = GeometryFactoryUtil.readWKT("POLYGON ((10 10, 10 30, 30 30, 30 10, 10 10))");
Geometry rectangle2 = GeometryFactoryUtil.readWKT("POLYGON ((20 20, 20 40, 40 40, 40 20, 20 20))");
Assert.assertTrue(JTSOperationUtils.checkIntersect(rectangle1, rectangle2));
}

@Test
public void givenPoint_whenAddedBuffer_thenPointIsInsideTheBuffer() throws Exception {
Geometry point = GeometryFactoryUtil.readWKT("POINT (10 10)");
Geometry bufferArea = JTSOperationUtils.getBuffer(point, 5);
Assert.assertTrue(JTSOperationUtils.checkContainment(point, bufferArea));
}

@Test
public void givenTwoPoints_whenGetDistanceBetween_thenGetTheDistance() throws Exception {
Geometry point1 = GeometryFactoryUtil.readWKT("POINT (10 10)");
Geometry point2 = GeometryFactoryUtil.readWKT("POINT (13 14)");
double distance = JTSOperationUtils.getDistance(point1, point2);
double expectedResult = 5.00;
double delta = 0.00;
Assert.assertEquals(expectedResult, distance, delta);
}

@Test
public void givenTwoGeometries_whenGetUnionOfBoth_thenGetTheUnion() throws Exception {
Geometry geometry1 = GeometryFactoryUtil.readWKT("POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))");
Geometry geometry2 = GeometryFactoryUtil.readWKT("POLYGON ((10 0, 10 10, 20 10, 20 0, 10 0))");

Geometry union = JTSOperationUtils.getUnion(geometry1, geometry2);
Geometry expectedResult = GeometryFactoryUtil.readWKT("POLYGON ((0 0, 0 10, 10 10, 20 10, 20 0, 10 0, 0 0))");
Assert.assertEquals(expectedResult, union);
}

@Test
public void givenBaseRectangle_whenAnotherRectangleOverlapping_thenGetTheDifferenceRectangle() throws Exception {
Geometry base = GeometryFactoryUtil.readWKT("POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))");
Geometry cut = GeometryFactoryUtil.readWKT("POLYGON ((5 0, 5 10, 10 10, 10 0, 5 0))");

Geometry result = JTSOperationUtils.getDifference(base, cut);
Geometry expectedResult = GeometryFactoryUtil.readWKT("POLYGON ((0 0, 0 10, 5 10, 5 0, 0 0))");
Assert.assertEquals(expectedResult, result);
}

@Test
public void givenInvalidGeometryValue_whenValidated_thenGiveFixedResult() throws Exception {
Geometry invalidGeo = GeometryFactoryUtil.readWKT("POLYGON ((0 0, 5 5, 5 0, 0 5, 0 0))");
Geometry result = JTSOperationUtils.validateAndRepair(invalidGeo);

Geometry expectedResult = GeometryFactoryUtil.readWKT("POLYGON ((2.5 2.5, 5 5, 5 0, 2.5 2.5))");
Assert.assertEquals(expectedResult, result);
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@
<module>jmonkeyengine</module>
<module>json-modules</module>
<module>jsoup</module>
<module>jts</module>
<module>jws</module>
<module>ksqldb</module>
<module>kubernetes-modules</module>
Expand Down