-
-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
7 changed files
with
391 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
87
postgis/src/main/java/org/jdbi/v3/postgis/PostgisCodec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
66
postgis/src/main/java/org/jdbi/v3/postgis/PostgisPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
postgis/src/main/resources/META-INF/services/org.jdbi.v3.core.spi.JdbiPlugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.jdbi.v3.postgis.PostgisPlugin |
Oops, something went wrong.