Skip to content

Commit

Permalink
Merge branch 'develop' into feature/#82-geopackage_wps_export
Browse files Browse the repository at this point in the history
  • Loading branch information
emxsys committed Aug 18, 2017
2 parents ceb6994 + 1d728ba commit e4e0918
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,7 @@ public void create(TileEntry entry) throws IOException {
close(st);
}
//create the tile table itself
st = cx.prepareStatement(format("CREATE TABLE %s (" +
st = cx.prepareStatement(format("CREATE TABLE '%s' (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT," +
"zoom_level INTEGER NOT NULL DEFAULT 0," +
"tile_column INTEGER NOT NULL DEFAULT 0," +
Expand All @@ -1174,7 +1174,7 @@ public void create(TileEntry entry) throws IOException {

//create an index on the tile
st = cx.prepareStatement(format(
"create index %s_zyx_idx on %s(zoom_level, tile_column, tile_row);",
"create index '%s_zyx_idx' on '%s' (zoom_level, tile_column, tile_row);",
e.getTableName(), e.getTableName()));
try {
st.execute();
Expand Down Expand Up @@ -1205,7 +1205,7 @@ public void add(TileEntry entry, Tile tile) throws IOException {
try {
Connection cx = connPool.getConnection();
try {
PreparedStatement ps = prepare(cx, format("INSERT INTO %s (zoom_level, tile_column,"
PreparedStatement ps = prepare(cx, format("INSERT INTO '%s' (zoom_level, tile_column,"
+ " tile_row, tile_data) VALUES (?,?,?,?)", entry.getTableName()))
.set(tile.getZoom()).set(tile.getColumn()).set(tile.getRow()).set(tile.getData())
.log(Level.FINE).statement();
Expand Down Expand Up @@ -1259,7 +1259,8 @@ public TileReader reader(TileEntry entry, Integer lowZoom, Integer highZoom,
q.add("tile_row <= " + highRow);
}

StringBuffer sql = new StringBuffer("SELECT * FROM ").append(entry.getTableName());
// StringBuffer sql = new StringBuffer("SELECT * FROM ").append(entry.getTableName());
StringBuffer sql = new StringBuffer(format("SELECT * FROM '%s'", entry.getTableName()));
if (!q.isEmpty()) {
sql.append(" WHERE ");
for (String s : q) {
Expand Down Expand Up @@ -1345,7 +1346,7 @@ public Set<Identifier> searchSpatialIndex(FeatureEntry entry, Double minX, Doubl
q.add("maxy <= " + maxY);
}

StringBuffer sql = new StringBuffer("SELECT id FROM ").append(getSpatialIndexName(entry));
StringBuffer sql = new StringBuffer(format("SELECT id FROM '%s'", getSpatialIndexName(entry)));
if (!q.isEmpty()) {
sql.append(" WHERE ");
for (String s : q) {
Expand Down Expand Up @@ -1402,7 +1403,7 @@ public int getTileBound(TileEntry entry, int zoom, boolean isMax, boolean isRow)
int tileBounds = -1;

StringBuffer sql = new StringBuffer("SELECT " + (isMax? "MAX" : "MIN") + "( " + (isRow? "tile_row" : "tile_column") + ") FROM ");
sql.append(entry.getTableName());
sql.append(format("'%s'", entry.getTableName()));
sql.append(" WHERE zoom_level == ");
sql.append(zoom);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
CREATE VIRTUAL TABLE rtree_${t}_${c} USING rtree(id, minx, maxx, miny, maxy);
CREATE VIRTUAL TABLE 'rtree_${t}_${c}' USING rtree(id, minx, maxx, miny, maxy);

INSERT OR REPLACE INTO rtree_${t}_${c}
SELECT ${i}, ST_MinX(${c}), ST_MaxX(${c}), ST_MinY(${c}), ST_MaxY(${c}) FROM ${t}
INSERT OR REPLACE INTO 'rtree_${t}_${c}'
SELECT ${i}, ST_MinX(${c}), ST_MaxX(${c}), ST_MinY(${c}), ST_MaxY(${c}) FROM '${t}'
WHERE NOT ST_IsEmpty(${c});

-- Conditions: Insertion of non-empty geometry
-- Actions : Insert record into rtree
CREATE TRIGGER rtree_${t}_${c}_insert AFTER INSERT ON ${t}
CREATE TRIGGER 'rtree_${t}_${c}_insert' AFTER INSERT ON '${t}'
WHEN (new.${c} NOT NULL AND NOT ST_IsEmpty(NEW.${c}))
BEGIN
INSERT OR REPLACE INTO rtree_${t}_${c} VALUES (
INSERT OR REPLACE INTO 'rtree_${t}_${c}' VALUES (
NEW.${i},
ST_MinX(NEW.${c}), ST_MaxX(NEW.${c}),
ST_MinY(NEW.${c}), ST_MaxY(NEW.${c})
Expand All @@ -19,11 +19,11 @@ END;
-- Conditions: Update of geometry column to non-empty geometry
-- No row ID change
-- Actions : Update record in rtree
CREATE TRIGGER rtree_${t}_${c}_update1 AFTER UPDATE OF ${c} ON ${t}
CREATE TRIGGER 'rtree_${t}_${c}_update1' AFTER UPDATE OF ${c} ON '${t}'
WHEN OLD.${i} = NEW.${i} AND
(NEW.${c} NOTNULL AND NOT ST_IsEmpty(NEW.${c}))
BEGIN
INSERT OR REPLACE INTO rtree_${t}_${c} VALUES (
INSERT OR REPLACE INTO 'rtree_${t}_${c}' VALUES (
NEW.${i},
ST_MinX(NEW.${c}), ST_MaxX(NEW.${c}),
ST_MinY(NEW.${c}), ST_MaxY(NEW.${c})
Expand All @@ -33,24 +33,24 @@ END;
-- Conditions: Update of geometry column to empty geometry
-- No row ID change
-- Actions : Remove record from rtree
CREATE TRIGGER rtree_${t}_${c}_update2 AFTER UPDATE OF ${c} ON ${t}
CREATE TRIGGER 'rtree_${t}_${c}_update2' AFTER UPDATE OF ${c} ON '${t}'
WHEN OLD.${i} = NEW.${i} AND
(NEW.${c} ISNULL OR ST_IsEmpty(NEW.${c}))
BEGIN
DELETE FROM rtree_${t}_${c} WHERE id = OLD.${i};
DELETE FROM 'rtree_${t}_${c}' WHERE id = OLD.${i};
END;

-- Conditions: Update of any column
-- Row ID change
-- Non-empty geometry
-- Actions : Remove record from rtree for old ${i}
-- Insert record into rtree for new ${i}
CREATE TRIGGER rtree_${t}_${c}_update3 AFTER UPDATE OF ${c} ON ${t}
CREATE TRIGGER 'rtree_${t}_${c}_update3' AFTER UPDATE OF ${c} ON '${t}'
WHEN OLD.${i} != NEW.${i} AND
(NEW.${c} NOTNULL AND NOT ST_IsEmpty(NEW.${c}))
BEGIN
DELETE FROM rtree_${t}_${c} WHERE id = OLD.${i};
INSERT OR REPLACE INTO rtree_${t}_${c} VALUES (
DELETE FROM 'rtree_${t}_${c}' WHERE id = OLD.${i};
INSERT OR REPLACE INTO 'rtree_${t}_${c}' VALUES (
NEW.${i},
ST_MinX(NEW.${c}), ST_MaxX(NEW.${c}),
ST_MinY(NEW.${c}), ST_MaxY(NEW.${c})
Expand All @@ -61,17 +61,17 @@ END;
-- Row ID change
-- Empty geometry
-- Actions : Remove record from rtree for old and new ${i}
CREATE TRIGGER rtree_${t}_${c}_update4 AFTER UPDATE ON ${t}
CREATE TRIGGER 'rtree_${t}_${c}_update4' AFTER UPDATE ON '${t}'
WHEN OLD.${i} != NEW.${i} AND
(NEW.${c} ISNULL OR ST_IsEmpty(NEW.${c}))
BEGIN
DELETE FROM rtree_${t}_${c} WHERE id IN (OLD.${i}, NEW.${i});
DELETE FROM 'rtree_${t}_${c}' WHERE id IN (OLD.${i}, NEW.${i});
END;

-- Conditions: Row deleted
-- Actions : Remove record from rtree for old ${i}
CREATE TRIGGER rtree_${t}_${c}_delete AFTER DELETE ON ${t}
CREATE TRIGGER 'rtree_${t}_${c}_delete' AFTER DELETE ON '${t}'
WHEN old.${c} NOT NULL
BEGIN
DELETE FROM rtree_${t}_${c} WHERE id = OLD.${i};
DELETE FROM 'rtree_${t}_${c}' WHERE id = OLD.${i};
END;
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void assertTableExists(String table) throws Exception {
Connection cx = geopkg.getDataSource().getConnection();
Statement st = cx.createStatement();
try {
st.execute(String.format("SELECT count(*) FROM %s;", table));
st.execute(String.format("SELECT count(*) FROM '%s';", table));
}
catch(Exception e) {
fail(e.getMessage());
Expand Down Expand Up @@ -314,6 +314,36 @@ public void testCreateFeatureEntry() throws Exception {
ra.close();
}

/**
* Identical to {@link GeoPackageTest#testCreateFeatureEntry()} but uses a
* pattern which requires quotes in SQLite.
*
* @throws Exception
*/
@Test
public void testCreateFeatureEntryKeywordConflict() throws Exception {
ShapefileDataStore shp = new ShapefileDataStore(setUpShapefileKeywordConflict());

FeatureEntry entry = new FeatureEntry();
geopkg.add(entry, shp.getFeatureSource(), null);

assertTableExists("bug-sites");

//check metadata contents
assertFeatureEntry(entry);

SimpleFeatureReader re = Features.simple(shp.getFeatureReader());
SimpleFeatureReader ra = geopkg.reader(entry, null, null);

while(re.hasNext()) {
assertTrue(ra.hasNext());
assertSimilar(re.next(), ra.next());
}

re.close();
ra.close();
}

@Test
public void test3DGeometry() throws Exception {
//create feature with 3d geometry
Expand Down Expand Up @@ -499,6 +529,35 @@ public void testSpatialIndexReading() throws Exception {
assertFalse(sfr.hasNext());
}
}

/**
* Identical to {@link GeoPackageTest#testSpatialIndexReading()} but uses a
* pattern which requires quotes in SQLite.
*
* @throws Exception
*/
@Test
public void testSpatialIndexReadingKeywordConflict() throws Exception {
FilterFactory ff = CommonFactoryFinder.getFilterFactory();

ShapefileDataStore shp = new ShapefileDataStore(setUpShapefileKeywordConflict());

FeatureEntry entry = new FeatureEntry();
geopkg.add(entry, shp.getFeatureSource(), null);

assertFalse(geopkg.hasSpatialIndex(entry));

geopkg.createSpatialIndex(entry);

assertTrue(geopkg.hasSpatialIndex(entry));

Set ids = geopkg.searchSpatialIndex(entry, 590230.0, 4915038.0, 590234.0, 4915040.0);
try(SimpleFeatureReader sfr = geopkg.reader(entry, ff.id(ids), null)) {
assertTrue(sfr.hasNext());
assertEquals("bug-sites.1", sfr.next().getID().toString());
assertFalse(sfr.hasNext());
}
}

@Test
public void testCreateTileEntry() throws Exception {
Expand Down Expand Up @@ -527,6 +586,39 @@ public void testCreateTileEntry() throws Exception {
}
}

/**
* Identical to {@link GeoPackageTest#testCreateTileEntry()} but uses a
* pattern which requires quotes in SQLite.
*
* @throws Exception
*/
@Test
public void testCreateTileEntryKeywordConflict() throws Exception {
TileEntry e = new TileEntry();
e.setTableName("foo-table");
e.setBounds(new ReferencedEnvelope(-180,180,-90,90,DefaultGeographicCRS.WGS84));
e.getTileMatricies().add(new TileMatrix(0, 1, 1, 256, 256, 0.1, 0.1));
e.getTileMatricies().add(new TileMatrix(1, 2, 2, 256, 256, 0.1, 0.1));

geopkg.create(e);
assertTileEntry(e);

List<Tile> tiles = new ArrayList();
tiles.add(new Tile(0,0,0,new byte[]{0}));
tiles.add(new Tile(1,0,0,new byte[]{1}));
tiles.add(new Tile(1,0,1,new byte[]{2}));
tiles.add(new Tile(1,1,0,new byte[]{3}));
tiles.add(new Tile(1,1,1,new byte[]{4}));

for (Tile t : tiles) {
geopkg.add(e, t);
}

try(TileReader r = geopkg.reader(e, null, null, null, null, null, null)) {
assertTiles(tiles, r);
}
}

@Test
public void testIndependentTileMatrix() throws Exception {
TileEntry e = new TileEntry();
Expand Down Expand Up @@ -579,7 +671,7 @@ public void testListEntries() throws Exception {
assertEquals("foo", te.getTableName());
assertEquals(2, te.getTileMatricies().size());
}

void assertTiles(List<Tile> tiles, TileReader r) throws IOException {
for (Tile t : tiles) {
assertTrue(r.hasNext());
Expand Down Expand Up @@ -727,6 +819,32 @@ URL setUpShapefile() throws Exception {

return DataUtilities.fileToURL(new File(d, "bugsites.shp"));
}

/**
* A copy of the {@link GeoPackageTest#setUpShapefile()} method but
* substitutes a table name requiring quotes in SQLite.
*
* @return
* @throws Exception
*/
URL setUpShapefileKeywordConflict() throws Exception {
File d = File.createTempFile("bug-sites", "shp", new File("target"));
d.delete();
d.mkdirs();

String[] exts = new String[]{"shp", "shx", "dbf", "prj"};
for (String ext : exts) {
if("prj".equals(ext)) {
String wkt = CRS.decode("EPSG:26713", true).toWKT();
FileUtils.writeStringToFile(new File(d, "bug-sites.prj"), wkt);
} else {
FileUtils.copyURLToFile(TestData.url("shapes/bugsites." + ext),
new File(d, "bug-sites." + ext));
}
}

return DataUtilities.fileToURL(new File(d, "bug-sites.shp"));
}

URL setUpGeoTiff() throws IOException {
File d = File.createTempFile("world", "tiff", new File("target"));
Expand Down

0 comments on commit e4e0918

Please sign in to comment.