Skip to content

Commit

Permalink
source-gpkg initial
Browse files Browse the repository at this point in the history
  • Loading branch information
jampukka committed Oct 17, 2023
1 parent eb898bb commit c96d18d
Show file tree
Hide file tree
Showing 34 changed files with 2,037 additions and 15 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@
<artifactId>hakunapi-gpkg</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>fi.nls.hakunapi</groupId>
<artifactId>hakunapi-source-gpkg</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>fi.nls.hakunapi</groupId>
<artifactId>hakunapi-html</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,6 @@ public FeatureType readCollection(Path path, Map<String, SimpleSource> sourcesBy
// Current prefix for properties
String p = "collections." + collectionId + ".";

String title = get(p + "title", collectionId);
String description = get(p + "description", title);
int[] srids = getSRIDs(get(p + "srid", get("default.collections.srid")));
HakunaGeometryDimension dim = getGeometryDims(collectionId, get(p + "geometryDimension"));

Expand All @@ -292,6 +290,9 @@ public FeatureType readCollection(Path path, Map<String, SimpleSource> sourcesBy
}
SimpleFeatureType ft = source.parse(this, path, collectionId, srids);

String title = get(p + "title", ft.getTitle() != null ? ft.getTitle() : collectionId);
String description = get(p + "description", ft.getDescription() != null ? ft.getDescription() : title);

ft.setTitle(title);
ft.setDescription(description);
ft.setGeomDimension(dim);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void endFeatureCollection() throws Exception {
insert.close();
GPKGGeometryColumn geometryColumn = createGeometryColumn(ft.getGeom());
GPKG.insertSpatialRefSys(c, GPKGSpatialRefSystems.get(srid));
GPKGFeaturesTable tableEntry = new GPKGFeaturesTable(tableName, ft.getName(), ft.getTitle(), ft.getDescription(), envelope, srid);
GPKGFeaturesTable tableEntry = new GPKGFeaturesTable(tableName, ft.getTitle(), ft.getDescription(), envelope, srid);
GPKG.insertFeaturesTableEntry(c, tableEntry);
GPKG.insertGeometryColumn(c, geometryColumn);
String idColumnName = isPropertyIntegerType(ft.getId()) ? ft.getId().getName() : FALLBACK_ID_FIELD;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@ public class GPKGFeaturesTable {

private final String tableName;
private final String identifier;
private final String title;
private final String description;
private final double minX;
private final double minY;
private final double maxX;
private final double maxY;
private final int srid;

public GPKGFeaturesTable(String tableName, String identifier, String title, String description, Envelope envelope, int srid) {
public GPKGFeaturesTable(String tableName, String identifier, String description, Envelope envelope, int srid) {
this.tableName = tableName;
this.identifier = identifier;
this.title = title;
this.description = description;
this.minX = envelope.getMinX();
this.minY = envelope.getMinY();
Expand All @@ -36,10 +34,6 @@ public String getIdentifier() {
return identifier;
}

public String getTitle() {
return title;
}

public String getDescription() {
return description;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ public static ByteOrder getByteOrder(byte flags) {
public static int getWKBOffset(int envelopeIndicator) {
switch (envelopeIndicator) {
case NO_ENVELOPE:
return 8 + LENGHT_NO_ENVELOPE;
return LENGHT_NO_ENVELOPE;
case XY_ENVELOPE:
return 8 + LENGHT_XY_ENVELOPE;
return LENGHT_XY_ENVELOPE;
case XYZ_ENVELOPE:
return 8 + LENGHT_XYZ_ENVELOPE;
return LENGHT_XYZ_ENVELOPE;
case XYM_ENVELOPE:
return 8 + LENGHT_XYM_ENVELOPE;
return LENGHT_XYM_ENVELOPE;
case XYZM_ENVELOPE:
return 8 + LENGHT_XYZM_ENVELOPE;
return LENGHT_XYZM_ENVELOPE;
default:
throw new IllegalArgumentException("Invalid value");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package fi.nls.hakunapi.gpkg;

public class GPKGGeometryColumn {

public static final String GPKG_TABLE_GEOMETRY_COLUMNS = "gpkg_geometry_columns";

public enum GeometryTypeName {
GEOMETRY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public void createExtension(Connection c) throws SQLException {
createTriggers(c);
}

public static String getRtreeName(String tableName, String columnName) {
return String.format("rtree_%s_%s", tableName, columnName);
}

private void createVirtualTable(Connection c) throws SQLException {
String sql = String.format("CREATE VIRTUAL TABLE rtree_%s_%s USING rtree(id, minx, maxx, miny, maxy)",
tableName, columnName);
Expand Down
Empty file.
4 changes: 4 additions & 0 deletions src/hakunapi-simple-webapp-javax/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
<groupId>fi.nls.hakunapi</groupId>
<artifactId>hakunapi-gpkg</artifactId>
</dependency>
<dependency>
<groupId>fi.nls.hakunapi</groupId>
<artifactId>hakunapi-source-gpkg</artifactId>
</dependency>
<dependency>
<groupId>fi.nls.hakunapi</groupId>
<artifactId>hakunapi-smile</artifactId>
Expand Down
25 changes: 25 additions & 0 deletions src/hakunapi-source-gpkg/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>fi.nls.hakunapi</groupId>
<artifactId>src</artifactId>
<version>1.2.0-SNAPSHOT</version>
</parent>
<artifactId>hakunapi-source-gpkg</artifactId>

<dependencies>
<dependency>
<groupId>fi.nls.hakunapi</groupId>
<artifactId>hakunapi-gpkg</artifactId>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package fi.nls.hakunapi.source.gpkg;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fi.nls.hakunapi.core.FeatureStream;
import fi.nls.hakunapi.core.ObjectArrayValueContainer;
import fi.nls.hakunapi.core.ValueContainer;
import fi.nls.hakunapi.core.ValueMapper;
import fi.nls.hakunapi.core.ValueProvider;
import fi.nls.hakunapi.core.util.U;

public class BufferedResultSet implements FeatureStream {

private static final Logger LOG = LoggerFactory.getLogger(BufferedResultSet.class);

private final Connection c;
private final PreparedStatement ps;
private final ResultSet rs;
private final ResultSetValueProvider valueProvider;
private final List<ValueMapper> mappers;
private final ValueContainer[] buffer;

private int i;
private int j;
private boolean closed;

public BufferedResultSet(Connection c, PreparedStatement ps, ResultSet rs, int numColsRs, List<ValueMapper> mappers, int bufSize) {
this.c = c;
this.ps = ps;
this.rs = rs;
this.valueProvider = new ResultSetValueProvider(rs, numColsRs);
this.mappers = mappers;
this.buffer = new ValueContainer[bufSize];
}

@Override
public boolean hasNext() {
if (i < j) {
return true;
}
if (closed) {
return false;
}

i = 0;
j = 0;
try {
for (; j < buffer.length; j++) {
if (!rs.next()) {
close();
break;
}
ValueContainer container = buffer[j];
if (container == null) {
buffer[j] = container = new ObjectArrayValueContainer(mappers.size());
}
for (ValueMapper mapper : mappers) {
mapper.accept(valueProvider, container);
}
}
return j > 0;
} catch (Exception e) {
LOG.error("Failed to retrieve more features", e);
close();
return false;
}
}

@Override
public ValueProvider next() {
return buffer[i++];
}

@Override
public void close() {
closed = true;
U.closeSilent(rs);
U.closeSilent(ps);
U.closeSilent(c);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package fi.nls.hakunapi.source.gpkg;

import javax.sql.DataSource;

import fi.nls.hakunapi.core.FeatureProducer;
import fi.nls.hakunapi.core.SimpleFeatureType;
import fi.nls.hakunapi.gpkg.extension.RtreeIndexExtension;

public class GpkgFeatureType extends SimpleFeatureType {

private String table;
private boolean spatialIndex;
private DataSource ds;

public String getTable() {
return table;
}

public void setTable(String table) {
this.table = table;
}

public boolean isSpatialIndex() {
return spatialIndex;
}

public void setSpatialIndex(boolean spatialIndex) {
this.spatialIndex = spatialIndex;
}

public DataSource getDatabase() {
return ds;
}

public void setDatabase(DataSource ds) {
this.ds = ds;
}

@Override
public FeatureProducer getFeatureProducer() {
return new GpkgSimpleFeatureProducer();
}

public String getSpatialIndexName() {
return RtreeIndexExtension.getRtreeName(table, getGeom().getColumn());
}

}
Loading

0 comments on commit c96d18d

Please sign in to comment.