Skip to content

Commit 012dbe9

Browse files
Merge pull request #18721 from oscarramadhan/BAEL-7919
Bael 7919
2 parents 81cdf6e + 617c2e6 commit 012dbe9

File tree

7 files changed

+244
-0
lines changed

7 files changed

+244
-0
lines changed

jts/pom.xml

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
<maven.compiler.plugin.version>3.14.0</maven.compiler.plugin.version>
25+
<maven.shade.plugin.version>3.2.4</maven.shade.plugin.version>
26+
</properties>
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.locationtech.jts</groupId>
30+
<artifactId>jts-core</artifactId>
31+
<version>${jts.version}</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.slf4j</groupId>
35+
<artifactId>slf4j-api</artifactId>
36+
<version>${slf4j.version}</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>ch.qos.logback</groupId>
40+
<artifactId>logback-core</artifactId>
41+
<version>${qos.logback.version}</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>ch.qos.logback</groupId>
45+
<artifactId>logback-classic</artifactId>
46+
<version>${qos.logback.version}</version>
47+
</dependency>
48+
<!-- JUnit for unit testing -->
49+
<dependency>
50+
<groupId>junit</groupId>
51+
<artifactId>junit</artifactId>
52+
<version>${junit.version}</version>
53+
<scope>test</scope>
54+
</dependency>
55+
</dependencies>
56+
57+
<build>
58+
<plugins>
59+
<!-- Maven Compiler Plugin -->
60+
<plugin>
61+
<groupId>org.apache.maven.plugins</groupId>
62+
<artifactId>maven-compiler-plugin</artifactId>
63+
<version>${maven.compiler.plugin.version}</version>
64+
<configuration>
65+
<source>${maven.compiler.source}</source>
66+
<target>${maven.compiler.target}</target>
67+
</configuration>
68+
</plugin>
69+
70+
<!-- Maven Shade Plugin for creating an executable JAR -->
71+
<plugin>
72+
<groupId>org.apache.maven.plugins</groupId>
73+
<artifactId>maven-shade-plugin</artifactId>
74+
<version>${maven.shade.plugin.version}</version>
75+
<executions>
76+
<execution>
77+
<phase>package</phase>
78+
<goals>
79+
<goal>shade</goal>
80+
</goals>
81+
<configuration>
82+
<transformers>
83+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
84+
<mainClass>com.baeldung.jts.JtsApplication</mainClass> <!-- Main class -->
85+
</transformer>
86+
</transformers>
87+
</configuration>
88+
</execution>
89+
</executions>
90+
</plugin>
91+
</plugins>
92+
</build>
93+
</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 checkContainment(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 JtsApplicationUnitTest {
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.checkContainment(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.checkContainment(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_thenGetTheDifferenceRectangle() 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
@@ -681,6 +681,7 @@
681681
<module>jmonkeyengine</module>
682682
<module>json-modules</module>
683683
<module>jsoup</module>
684+
<module>jts</module>
684685
<module>jws</module>
685686
<module>ksqldb</module>
686687
<module>kubernetes-modules</module>

0 commit comments

Comments
 (0)