Skip to content

Commit

Permalink
Add JTS PostGIS data type support
Browse files Browse the repository at this point in the history
Support for Postgres PostGIS with the Eclipse LocationTech JTS Topology
Suite. Supports mapping of all LocationTech data types onto PostGIS and
back.

Co-Authored-By: Bertil Chapuis <bchapuis@gmail.com>
  • Loading branch information
hgschmie and bchapuis committed Jul 25, 2022
1 parent 109d2d4 commit 85d34d7
Show file tree
Hide file tree
Showing 7 changed files with 391 additions and 0 deletions.
7 changes: 7 additions & 0 deletions internal/build/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<dep.jdbi-policy.version>3.31.1-SNAPSHOT</dep.jdbi-policy.version>
<dep.jetbrainsAnnotations.version>21.0.1</dep.jetbrainsAnnotations.version>
<dep.joda-time.version>2.9.9</dep.joda-time.version>
<dep.jts.version>1.19.0</dep.jts.version>
<dep.junit5.version>5.8.2</dep.junit5.version>
<dep.kotlin.version>1.7.10</dep.kotlin.version>
<dep.mockito.version>4.2.0</dep.mockito.version>
Expand Down Expand Up @@ -226,6 +227,12 @@
</exclusions>
</dependency>

<dependency>
<groupId>org.locationtech.jts</groupId>
<artifactId>jts-core</artifactId>
<version>${dep.jts.version}</version>
</dependency>

<dependency>
<groupId>com.opentable.components</groupId>
<artifactId>otj-pg-embedded</artifactId>
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<module>jpa</module>
<module>json</module>
<module>postgres</module>
<module>postgis</module>
<module>spring5</module>
<module>docs</module>
<module>kotlin</module>
Expand Down Expand Up @@ -157,6 +158,11 @@
<version>${project.version}</version>
<classifier>tests</classifier>
</dependency>
<dependency>
<groupId>org.jdbi</groupId>
<artifactId>jdbi3-postgis</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jdbi</groupId>
<artifactId>jdbi3-spring5</artifactId>
Expand Down
91 changes: 91 additions & 0 deletions postgis/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<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>
<artifactId>jdbi3-parent</artifactId>
<groupId>org.jdbi</groupId>
<version>3.31.1-SNAPSHOT</version>
</parent>

<artifactId>jdbi3-postgis</artifactId>
<name>jdbi3 PostGIS</name>
<description>jdbi PostgreSQL PostGIS support</description>

<licenses>
<license>
<name>Apache License 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.html</url>
<distribution>repo</distribution>
</license>
</licenses>

<properties>
<moduleName>org.jdbi.v3.postgis</moduleName>
</properties>

<dependencies>
<dependency>
<groupId>org.jdbi</groupId>
<artifactId>jdbi3-core</artifactId>
</dependency>

<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
</dependency>

<dependency>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-annotations</artifactId>
</dependency>

<dependency>
<groupId>org.locationtech.jts</groupId>
<artifactId>jts-core</artifactId>
</dependency>

<dependency>
<groupId>org.immutables</groupId>
<artifactId>value</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jdbi</groupId>
<artifactId>jdbi3-sqlobject</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jdbi</groupId>
<artifactId>jdbi3-postgres</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jdbi</groupId>
<artifactId>jdbi3-testing</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.opentable.components</groupId>
<artifactId>otj-pg-embedded</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
87 changes: 87 additions & 0 deletions postgis/src/main/java/org/jdbi/v3/postgis/PostgisCodec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jdbi.v3.postgis;

import java.util.function.Function;

import org.jdbi.v3.core.argument.Argument;
import org.jdbi.v3.core.codec.Codec;
import org.jdbi.v3.core.mapper.ColumnMapper;
import org.jdbi.v3.meta.Alpha;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKBReader;
import org.locationtech.jts.io.WKBWriter;

import static org.locationtech.jts.io.WKBConstants.wkbNDR;

@Alpha
final class PostgisCodec implements Codec<Geometry> {

@Override
public ColumnMapper<Geometry> getColumnMapper() {
return (resultSet, index, context) -> {
byte[] bytes = hexStringToByteArray(resultSet.getString(index));
return (Geometry) deserialize(bytes);
};
}

@Override
public Function<Geometry, Argument> getArgumentFunction() {
return data -> (position, statement, context) -> statement.setBytes(position, serialize(data));
}

/**
* Serializes a geometry in the WKB format.
*
* @param geometry An {@link Geometry} object or one of its subclasses. May be null.
* @return A byte array representing the Geometry object in WKB format or null if the object was null.
*/
private static byte[] serialize(Geometry geometry) {
if (geometry == null) {
return null;
}
WKBWriter writer = new WKBWriter(2, wkbNDR, true);
return writer.write(geometry);
}

/**
* Deserializes a geometry in the WKB format.
*
* @param bytes A byte array representing a {@link Geometry} in WKB format or null.
* @return The deserialized object or null if the byte array was null.
*/
private static Geometry deserialize(byte[] bytes) {
if (bytes == null) {
return null;
}
try {
WKBReader reader = new WKBReader(new GeometryFactory());
return reader.read(bytes);
} catch (ParseException e) {
throw new IllegalArgumentException(e);
}
}

private static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] =
(byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
}
return data;
}
}
66 changes: 66 additions & 0 deletions postgis/src/main/java/org/jdbi/v3/postgis/PostgisPlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jdbi.v3.postgis;

import org.jdbi.v3.core.Jdbi;
import org.jdbi.v3.core.codec.Codec;
import org.jdbi.v3.core.codec.CodecFactory;
import org.jdbi.v3.core.spi.JdbiPlugin;
import org.jdbi.v3.meta.Alpha;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryCollection;
import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.geom.LinearRing;
import org.locationtech.jts.geom.MultiLineString;
import org.locationtech.jts.geom.MultiPoint;
import org.locationtech.jts.geom.MultiPolygon;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;

/**
* Postgis plugin. Adds support for binding and mapping the following data types:
*
* <ul>
* <li>{@link org.locationtech.jts.geom.Point}</li>
* <li>{@link org.locationtech.jts.geom.LineString}</li>
* <li>{@link org.locationtech.jts.geom.LinearRing}</li>
* <li>{@link org.locationtech.jts.geom.Polygon}</li>
* <li>{@link org.locationtech.jts.geom.MultiPoint}</li>
* <li>{@link org.locationtech.jts.geom.MultiLineString}</li>
* <li>{@link org.locationtech.jts.geom.MultiPolygon}</li>
* <li>{@link org.locationtech.jts.geom.GeometryCollection}</li>
* <li>{@link org.locationtech.jts.geom.Geometry}</li>
* </ul>
*/
@Alpha
public class PostgisPlugin extends JdbiPlugin.Singleton {

@Override
public void customizeJdbi(Jdbi jdbi) {
final Codec<Geometry> codec = new PostgisCodec();

jdbi.registerCodecFactory(CodecFactory.builder()
.addCodec(Geometry.class, codec)
.addCodec(GeometryCollection.class, codec)
.addCodec(LinearRing.class, codec)
.addCodec(LineString.class, codec)
.addCodec(MultiLineString.class, codec)
.addCodec(MultiPoint.class, codec)
.addCodec(MultiPolygon.class, codec)
.addCodec(Point.class, codec)
.addCodec(Polygon.class, codec)
.build());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.jdbi.v3.postgis.PostgisPlugin

0 comments on commit 85d34d7

Please sign in to comment.