Skip to content

Commit

Permalink
[PostgisRasterExport] add step tests
Browse files Browse the repository at this point in the history
  • Loading branch information
edigonzales committed Jan 21, 2024
1 parent 8c1252e commit ce04dc2
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 29 deletions.
10 changes: 9 additions & 1 deletion docs/user/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1359,8 +1359,15 @@ outputDir | Verzeichnis, in das die Metainfo-Datei gespeichert wird.

### PostgisRasterExport

Exportiert eine PostGIS-Raster-Spalte in eine Raster-Datei mittels SQL-Query. Die SQL-Query darf nur einen Record zurückliefern, d.h. es muss unter Umständen `ST_Union()` verwendet werden. Es angenommen, dass die erste _bytea_-Spalte des Resultsets die Rasterdaten enthält. Weitere _bytea_-Spalten werden ignoriert.
Exportiert eine PostGIS-Raster-Spalte in eine Raster-Datei mittels SQL-Query. Die SQL-Query darf nur einen Record zurückliefern, d.h. es muss unter Umständen `ST_Union()` verwendet werden. Es angenommen, dass die erste _bytea_-Spalte des Resultsets die Rasterdaten enthält. Weitere _bytea_-Spalten werden ignoriert. Das Outputformat und die Formatoptionen müssen in der SQL-Datei (in der Select-Query) angegeben werden, z.B.:

```
SELECT
1::int AS foo, ST_AsGDALRaster((ST_AsRaster(ST_Buffer(ST_Point(2607880,1228287),10),150, 150)), 'AAIGrid', ARRAY[''], 2056) AS raster
;
```

Beispiel:
```
task exportTiff(type: PostgisRasterExport) {
database = [db_uri, db_user, db_pass]
Expand All @@ -1373,6 +1380,7 @@ Parameter | Beschreibung
----------|-------------------
database | Datenbank aus der exportiert werden soll.
sqlFile | Name der SQL-Datei aus das SQL-Statement gelesen und ausgeführt wird.
sqlParameters | Eine Map mit Paaren von Parameter-Name und Parameter-Wert.
dataFile | Name der Rasterdatei, die erstellt werden soll.

### Publisher
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ public PostgisRasterExportStep() {
*
* @param database Database properties to generate database connection.
* @param sqlFile File which contain query.
* @param dataFile File where raster data is written to.
* @param outFile File where raster data is written to.
* @param params Map with key/value pairs. Will be used to replace parameters
* in the sql query.
* @throws Exception If file is missing, no connection to database, could not
* read file or problems while executing sql query.
*/
public void execute(Connector database, File sqlFile, File dataFile, Map<String, String> params) throws Exception {
public void execute(Connector database, File sqlFile, File outFile, Map<String, String> params) throws Exception {
Connection conn = null;

log.lifecycle("Database string: " + database.toString());
log.lifecycle("SQL file:" + sqlFile.getAbsolutePath());
log.lifecycle("Data file: " + dataFile.getAbsolutePath());
log.lifecycle("Output file: " + outFile.getAbsolutePath());

try {
conn = database.connect();
Expand All @@ -63,12 +63,17 @@ public void execute(Connector database, File sqlFile, File dataFile, Map<String,
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();

// Resultat der Query kann viele Attribute beinhalten.
// Es wird das erste bytea[]-Attribut exportiert.
// TODO: Funktioniert ebenfalls mit AAIGrid (Ascii).
// Vielleicht waere es schlauer, man nach einem
// Attributnamen suchen.
for (int i = 1; i < numberOfColumns + 1; i++) {
String columnTypeName = rsmd.getColumnTypeName(i);
if (columnTypeName.equalsIgnoreCase("bytea")) {
rs.next();
InputStream is = rs.getBinaryStream(i);
FileUtils.copyInputStreamToFile(is, dataFile);
FileUtils.copyInputStreamToFile(is, outFile);
break; // If we found a bytea column we can stop.
}
}
Expand All @@ -85,4 +90,9 @@ public void execute(Connector database, File sqlFile, File dataFile, Map<String,
}
}
}

public void execute(Connector database, File sqlFile, File outFile) throws Exception {
execute(database, sqlFile, outFile, null);
}

}
37 changes: 16 additions & 21 deletions gretl/src/main/java/ch/so/agi/gretl/steps/SqlExecutorStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
* within a database based on queries in sql-Scripts
*/
public class SqlExecutorStep {

private GretlLogger log;
private String taskName;
protected GretlLogger log;
protected String taskName;

public SqlExecutorStep() {
this(null);
Expand Down Expand Up @@ -52,33 +51,21 @@ public void execute(Connector trans, List<File> sqlfiles) throws Exception {
}

public void execute(Connector trans, List<File> sqlfiles, Map<String, String> params) throws Exception {


log.lifecycle(taskName + ": Start SqlExecutor");

checkIfConnectorIsNotNull(trans);

assertValidFilePaths(sqlfiles);


log.lifecycle(taskName + ": Given parameters DB-URL: " + trans.connect().getMetaData().getURL() + ", DB-User: "
+ trans.connect().getMetaData().getUserName() + ", Files: " + sqlfiles);

logPathToInputSqlFiles(sqlfiles);

checkSqlFiles(sqlfiles);
try {
Connection db = trans.connect();

checkIfNoExistingFileIsEmpty(sqlfiles);

checkFilesExtensionsForSqlExtension(sqlfiles);

checkFilesForUTF8WithoutBOM(sqlfiles);

readSqlFiles(sqlfiles, db, params);

db.commit();
trans.close();

log.lifecycle(taskName + ": End SqlExecutor (successful)");
} catch (Exception e) {
if (!trans.isClosed()) {
Expand All @@ -92,7 +79,15 @@ public void execute(Connector trans, List<File> sqlfiles, Map<String, String> pa
}
}
}


private void checkSqlFiles(List<File> sqlfiles) throws Exception {
assertValidFilePaths(sqlfiles);
logPathToInputSqlFiles(sqlfiles);
checkIfNoExistingFileIsEmpty(sqlfiles);
checkFilesExtensionsForSqlExtension(sqlfiles);
checkFilesForUTF8WithoutBOM(sqlfiles);
}

/**
*
* @param trans Connector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.gradle.api.tasks.TaskAction;

import java.io.File;
import java.util.Map;

public class PostgisRasterExport extends DefaultTask {
private GretlLogger log;
Expand All @@ -25,7 +26,7 @@ public class PostgisRasterExport extends DefaultTask {

@Input
@Optional
public java.util.Map<String, String> sqlParameters = null;
public Map<String, String> sqlParameters = null;

@OutputFile
public Object dataFile = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package ch.so.agi.gretl.steps;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.testcontainers.containers.PostgisContainerProvider;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.containers.wait.strategy.Wait;

import ch.so.agi.gretl.api.Connector;
import ch.so.agi.gretl.logging.GretlLogger;
import ch.so.agi.gretl.logging.LogEnvironment;
import ch.so.agi.gretl.testutil.TestUtil;
import net.sf.saxon.s9api.SaxonApiException;

public class PostgisRasterExportStepTest {
private GretlLogger log;

static String WAIT_PATTERN = ".*database system is ready to accept connections.*\\s";

@ClassRule
public static PostgreSQLContainer postgres =
(PostgreSQLContainer) new PostgisContainerProvider()
.newInstance().withDatabaseName("gretl")
.withUsername(TestUtil.PG_DDLUSR_USR)
.withInitScript("init_postgresql.sql")
.waitingFor(Wait.forLogMessage(WAIT_PATTERN, 2));

public PostgisRasterExportStepTest() {
LogEnvironment.initStandalone();
this.log = LogEnvironment.getLogger(this.getClass());
}

@Rule
public TemporaryFolder folder = new TemporaryFolder();

@Test
public void export_geotiff_Ok() throws Exception {
// Prepare
Connector sourceDb = new Connector(postgres.getJdbcUrl(), TestUtil.PG_READERUSR_USR, TestUtil.PG_READERUSR_PWD);
File sqlFile = new File("src/test/resources/data/postgisrasterprocessor/prepare_raster_geotiff.sql");
String outFileName = "outfile.tif";
File targetFile = new File("src/test/resources/data/postgisrasterprocessor/target.tif");

File outDirectory = folder.newFolder("build");
//File outDirectory = Paths.get("/Users/stefan/tmp/").toFile();
File outFile = Paths.get(outDirectory.getAbsolutePath(), outFileName).toFile();

// Run: Calculate and export raster file from PostGIS
PostgisRasterExportStep postgisRasterExportStep = new PostgisRasterExportStep();
postgisRasterExportStep.execute(sourceDb, sqlFile, outFile);

// Check result
long targetFileSize = targetFile.length();
long outFileSize = outFile.length();
assertEquals(targetFileSize, outFileSize);
}

@Test
public void export_aaigrid_Ok() throws Exception {
// Prepare
Connector sourceDb = new Connector(postgres.getJdbcUrl(), TestUtil.PG_READERUSR_USR, TestUtil.PG_READERUSR_PWD);
File sqlFile = new File("src/test/resources/data/postgisrasterprocessor/prepare_raster_aaigrid.sql");
String outFileName = "outfile.asc";
File targetFile = new File("src/test/resources/data/postgisrasterprocessor/target.asc");

File outDirectory = folder.newFolder("build");
//File outDirectory = Paths.get("/Users/stefan/tmp/").toFile();
File outFile = Paths.get(outDirectory.getAbsolutePath(), outFileName).toFile();

// Run: Calculate and export raster file from PostGIS
PostgisRasterExportStep postgisRasterExportStep = new PostgisRasterExportStep();
postgisRasterExportStep.execute(sourceDb, sqlFile, outFile);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT
1::int AS foo, ST_AsGDALRaster((ST_AsRaster(ST_Buffer(ST_Point(2607880,1228287),10),150, 150)), 'AAIGrid', ARRAY[''], 2056) AS raster
;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT
1::int AS foo, ST_AsGDALRaster((ST_AsRaster(ST_Buffer(ST_Point(2607880,1228287),10),150, 150)), 'GTiff', ARRAY[''], 2056) AS raster
;
Binary file not shown.
2 changes: 1 addition & 1 deletion gretl/src/test/resources/init_postgresql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ CREATE EXTENSION "uuid-ossp";
GRANT ALL PRIVILEGES ON DATABASE gretl TO ddluser;
CREATE ROLE dmluser WITH LOGIN PASSWORD 'dmluser';
CREATE ROLE readeruser WITH LOGIN PASSWORD 'readeruser';
ALTER DATABASE gretl SET postgis.gdal_enabled_drivers TO 'GTiff PNG JPEG';
ALTER DATABASE gretl SET postgis.gdal_enabled_drivers TO 'ENABLE_ALL';
2 changes: 1 addition & 1 deletion runtimeImage/gretl/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#FROM bellsoft/liberica-openjdk-alpine:11.0.19-7
FROM bellsoft/liberica-openjdk-alpine:8u362-9
FROM bellsoft/liberica-openjdk-alpine:8u392-9

CMD ["gretl"]

Expand Down

0 comments on commit ce04dc2

Please sign in to comment.