Skip to content

Commit e4249a6

Browse files
committed
init jts operations implementations
1 parent 2587af6 commit e4249a6

File tree

7 files changed

+242
-0
lines changed

7 files changed

+242
-0
lines changed

jts/pom.xml

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>parent-modules</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<groupId>com.baeldung.jts</groupId>
13+
<artifactId>jst-example</artifactId>
14+
<version>1.0-SNAPSHOT</version>
15+
<packaging>jar</packaging>
16+
17+
<properties>
18+
<maven.compiler.source>21</maven.compiler.source> <!-- Java version -->
19+
<maven.compiler.target>21</maven.compiler.target>
20+
<slf4j.version>2.0.17</slf4j.version>
21+
<junit.version>4.13.2</junit.version>
22+
<jts.version>1.20.0</jts.version>
23+
<qos.logback.version>1.5.18</qos.logback.version>
24+
</properties>
25+
<dependencies>
26+
<dependency>
27+
<groupId>org.locationtech.jts</groupId>
28+
<artifactId>jts-core</artifactId>
29+
<version>${jts.version}</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.slf4j</groupId>
33+
<artifactId>slf4j-api</artifactId>
34+
<version>${slf4j.version}</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>ch.qos.logback</groupId>
38+
<artifactId>logback-core</artifactId>
39+
<version>${qos.logback.version}</version>
40+
</dependency>
41+
<dependency>
42+
<groupId>ch.qos.logback</groupId>
43+
<artifactId>logback-classic</artifactId>
44+
<version>${qos.logback.version}</version>
45+
</dependency>
46+
<!-- JUnit for unit testing -->
47+
<dependency>
48+
<groupId>junit</groupId>
49+
<artifactId>junit</artifactId>
50+
<version>${junit.version}</version>
51+
<scope>test</scope>
52+
</dependency>
53+
</dependencies>
54+
55+
<build>
56+
<plugins>
57+
<!-- Maven Compiler Plugin -->
58+
<plugin>
59+
<groupId>org.apache.maven.plugins</groupId>
60+
<artifactId>maven-compiler-plugin</artifactId>
61+
<version>3.14.0</version>
62+
<configuration>
63+
<source>${maven.compiler.source}</source>
64+
<target>${maven.compiler.target}</target>
65+
</configuration>
66+
</plugin>
67+
68+
<!-- Maven Shade Plugin for creating an executable JAR -->
69+
<plugin>
70+
<groupId>org.apache.maven.plugins</groupId>
71+
<artifactId>maven-shade-plugin</artifactId>
72+
<version>3.2.4</version>
73+
<executions>
74+
<execution>
75+
<phase>package</phase>
76+
<goals>
77+
<goal>shade</goal>
78+
</goals>
79+
<configuration>
80+
<transformers>
81+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
82+
<mainClass>com.baeldung.jts.JtsApplication</mainClass> <!-- Main class -->
83+
</transformer>
84+
</transformers>
85+
</configuration>
86+
</execution>
87+
</executions>
88+
</plugin>
89+
</plugins>
90+
</build>
91+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.jts;
2+
3+
import com.baeldung.jts.utils.GeometryFactoryUtil;
4+
import org.locationtech.jts.geom.Geometry;
5+
6+
public class JtsApplication {
7+
8+
public static void main(String[] args) throws Exception {
9+
10+
}
11+
12+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.jts.operations;
2+
3+
import org.locationtech.jts.geom.Geometry;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
public class JTSOperationUtils {
8+
private static final Logger log = LoggerFactory.getLogger(JTSOperationUtils.class);
9+
10+
public static boolean isContainment(Geometry point, Geometry polygon) {
11+
boolean isInside = polygon.contains(point);
12+
log.info("Is the point inside polygon? {}", isInside);
13+
return isInside;
14+
}
15+
16+
public static boolean checkIntersect(Geometry rectangle1, Geometry rectangle2) {
17+
boolean intersect = rectangle1.intersects(rectangle2);
18+
Geometry overlap = rectangle1.intersection(rectangle2);
19+
20+
log.info("Do both rectangle intersect? {}", intersect);
21+
log.info("Overlapping Area: {}", overlap);
22+
return intersect;
23+
}
24+
25+
public static Geometry getBuffer(Geometry point, int intBuffer) {
26+
Geometry buffer = point.buffer(intBuffer);
27+
log.info("Buffer Geometry: {}", buffer);
28+
return buffer;
29+
}
30+
31+
public static double getDistance(Geometry point1, Geometry point2) {
32+
double distance = point1.distance(point2);
33+
log.info("Distance: {}",distance);
34+
return distance;
35+
}
36+
37+
public static Geometry getUnion(Geometry geometry1, Geometry geometry2) {
38+
Geometry union = geometry1.union(geometry2);
39+
log.info("Union Result: {}", union);
40+
return union;
41+
}
42+
43+
public static Geometry getDifference(Geometry base, Geometry cut) {
44+
Geometry result = base.difference(cut);
45+
log.info("Resulting Geometry: {}", result);
46+
return result;
47+
}
48+
49+
public static Geometry validateAndRepair(Geometry invalidGeo) throws Exception {
50+
boolean valid = invalidGeo.isValid();
51+
log.info("Is valid Geometry value? {}", valid);
52+
53+
Geometry repaired = invalidGeo.buffer(0);
54+
log.info("Repaired Geometry: {}", repaired);
55+
return repaired;
56+
}
57+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.jts.utils;
2+
3+
import org.locationtech.jts.geom.Geometry;
4+
import org.locationtech.jts.io.WKTReader;
5+
6+
public class GeometryFactoryUtil {
7+
public static Geometry readWKT(String wkt) throws Exception {
8+
WKTReader reader = new WKTReader();
9+
return reader.read(wkt);
10+
}
11+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
spring.application.name=jts
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.baeldung.jts;
2+
3+
import com.baeldung.jts.operations.JTSOperationUtils;
4+
import com.baeldung.jts.utils.GeometryFactoryUtil;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
import org.locationtech.jts.geom.Geometry;
8+
9+
public class JtsApplicationTests {
10+
@Test
11+
public void givenPolygon2D_whenContainPoint_thenContainmentIsTrue() throws Exception {
12+
Geometry point = GeometryFactoryUtil.readWKT("POINT (10 20)");
13+
Geometry polygon = GeometryFactoryUtil.readWKT("POLYGON ((0 0, 0 40, 40 40, 40 0, 0 0))");
14+
Assert.assertTrue(JTSOperationUtils.isContainment(point, polygon));
15+
}
16+
17+
@Test
18+
public void givenRectangle1_whenIntersectWithRectangle2_thenIntersectionIsTrue() throws Exception {
19+
Geometry rectangle1 = GeometryFactoryUtil.readWKT("POLYGON ((10 10, 10 30, 30 30, 30 10, 10 10))");
20+
Geometry rectangle2 = GeometryFactoryUtil.readWKT("POLYGON ((20 20, 20 40, 40 40, 40 20, 20 20))");
21+
Assert.assertTrue(JTSOperationUtils.checkIntersect(rectangle1, rectangle2));
22+
}
23+
24+
@Test
25+
public void givenPoint_whenAddedBuffer_thenPointIsInsideTheBuffer() throws Exception {
26+
Geometry point = GeometryFactoryUtil.readWKT("POINT (10 10)");
27+
Geometry bufferArea = JTSOperationUtils.getBuffer(point, 5);
28+
Assert.assertTrue(JTSOperationUtils.isContainment(point, bufferArea));
29+
}
30+
31+
@Test
32+
public void givenTwoPoints_whenGetDistanceBetween_thenGetTheDistance() throws Exception {
33+
Geometry point1 = GeometryFactoryUtil.readWKT("POINT (10 10)");
34+
Geometry point2 = GeometryFactoryUtil.readWKT("POINT (13 14)");
35+
double distance = JTSOperationUtils.getDistance(point1, point2);
36+
double expectedResult = 5.00;
37+
double delta = 0.00;
38+
Assert.assertEquals(expectedResult, distance, delta);
39+
}
40+
41+
@Test
42+
public void givenTwoGeometries_whenGetUnionOfBoth_thenGetTheUnion() throws Exception {
43+
Geometry geometry1 = GeometryFactoryUtil.readWKT("POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))");
44+
Geometry geometry2 = GeometryFactoryUtil.readWKT("POLYGON ((10 0, 10 10, 20 10, 20 0, 10 0))");
45+
46+
Geometry union = JTSOperationUtils.getUnion(geometry1, geometry2);
47+
Geometry expectedResult = GeometryFactoryUtil.readWKT("POLYGON ((0 0, 0 10, 10 10, 20 10, 20 0, 10 0, 0 0))");
48+
Assert.assertEquals(expectedResult, union);
49+
}
50+
51+
@Test
52+
public void givenBaseRectangle_whenAnotherRectangleOverlapping_GetTheDifferenceRectangle() throws Exception {
53+
Geometry base = GeometryFactoryUtil.readWKT("POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))");
54+
Geometry cut = GeometryFactoryUtil.readWKT("POLYGON ((5 0, 5 10, 10 10, 10 0, 5 0))");
55+
56+
Geometry result = JTSOperationUtils.getDifference(base, cut);
57+
Geometry expectedResult = GeometryFactoryUtil.readWKT("POLYGON ((0 0, 0 10, 5 10, 5 0, 0 0))");
58+
Assert.assertEquals(expectedResult, result);
59+
}
60+
61+
@Test
62+
public void givenInvalidGeometryValue_whenValidated_thenGiveFixedResult() throws Exception {
63+
Geometry invalidGeo = GeometryFactoryUtil.readWKT("POLYGON ((0 0, 5 5, 5 0, 0 5, 0 0))");
64+
Geometry result = JTSOperationUtils.validateAndRepair(invalidGeo);
65+
66+
Geometry expectedResult = GeometryFactoryUtil.readWKT("POLYGON ((2.5 2.5, 5 5, 5 0, 2.5 2.5))");
67+
Assert.assertEquals(expectedResult, result);
68+
}
69+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@
571571
<module>spring-di-2</module>
572572
<module>spring-security-modules/spring-security-legacy-oidc</module>
573573
<module>spring-reactive-modules/spring-reactive-kafka-stream-binder</module>
574+
<module>jts</module>
574575
</modules>
575576

576577
<properties>

0 commit comments

Comments
 (0)