From e6340a6be82e1cf7ba2519f15dde8e14a1ad6128 Mon Sep 17 00:00:00 2001 From: ebocher Date: Thu, 4 Jun 2020 16:04:40 +0200 Subject: [PATCH 01/10] Align all drivers to support the same syntax --- .../java/org/h2gis/api/DriverFunction.java | 62 +++++- .../functions/io/asc/AscDriverFunction.java | 25 ++- .../functions/io/csv/CSVDriverFunction.java | 116 +++++----- .../functions/io/dbf/DBFDriverFunction.java | 88 ++++---- .../org/h2gis/functions/io/dbf/DBFRead.java | 16 +- .../org/h2gis/functions/io/dbf/DBFWrite.java | 17 +- .../io/dbf/internal/DbaseFileHeader.java | 2 +- .../io/geojson/GeoJsonDriverFunction.java | 64 +++--- .../functions/io/geojson/GeoJsonRead.java | 29 ++- .../io/geojson/GeoJsonReaderDriver.java | 33 ++- .../functions/io/geojson/GeoJsonWrite.java | 15 +- .../io/geojson/GeoJsonWriteDriver.java | 18 +- .../functions/io/gpx/GPXDriverFunction.java | 44 ++-- .../org/h2gis/functions/io/gpx/GPXRead.java | 49 +++-- .../gpx/model/AbstractGpxParserDefault.java | 200 ++++++++++-------- .../io/gpx/model/GPXTablesFactory.java | 9 +- .../functions/io/gpx/model/GpxParser.java | 6 +- .../functions/io/gpx/model/GpxPreparser.java | 8 +- .../functions/io/json/JsonDriverFunction.java | 33 ++- .../h2gis/functions/io/json/JsonWrite.java | 36 +++- .../functions/io/json/JsonWriteDriver.java | 8 +- .../functions/io/kml/KMLDriverFunction.java | 34 ++- .../org/h2gis/functions/io/kml/KMLWrite.java | 18 +- .../functions/io/kml/KMLWriterDriver.java | 117 ++++++---- .../functions/io/osm/OSMDriverFunction.java | 41 ++-- .../org/h2gis/functions/io/osm/OSMParser.java | 54 +++-- .../org/h2gis/functions/io/osm/OSMRead.java | 48 +++-- .../functions/io/shp/SHPDriverFunction.java | 143 +++++++------ .../org/h2gis/functions/io/shp/SHPRead.java | 43 +++- .../org/h2gis/functions/io/shp/SHPWrite.java | 20 +- .../functions/io/tsv/TSVDriverFunction.java | 108 ++++++---- .../org/h2gis/functions/io/tsv/TSVRead.java | 45 +++- .../org/h2gis/functions/io/tsv/TSVWrite.java | 25 ++- .../functions/io/shp/SHPImportExportTest.java | 4 +- .../org/h2gis/utilities/JDBCUtilities.java | 1 + 35 files changed, 1039 insertions(+), 540 deletions(-) diff --git a/h2gis-api/src/main/java/org/h2gis/api/DriverFunction.java b/h2gis-api/src/main/java/org/h2gis/api/DriverFunction.java index 55ac90057d..b1968f3c07 100644 --- a/h2gis-api/src/main/java/org/h2gis/api/DriverFunction.java +++ b/h2gis-api/src/main/java/org/h2gis/api/DriverFunction.java @@ -92,7 +92,37 @@ enum IMPORT_DRIVER_TYPE {LINK, COPY} */ void exportTable(Connection connection, String tableReference, File fileName, ProgressVisitor progress) throws SQLException, IOException; + + /** + * Export the specified table from the specified connection into the specified file. + * + * @param connection Active connection, do not close this connection. + * @param tableReference [[catalog.]schema.]table reference. + * @param fileName File path to write, if exists it may be replaced. + * @param deleteFiles True to delete the files if exist + * @param progress Progress visitor following the execution. + * @throws SQLException Table read error. + * @throws IOException File write error. + */ + void exportTable(Connection connection, String tableReference, File fileName, boolean deleteFiles, ProgressVisitor progress) + throws SQLException, IOException; + /** + * Export the specified table from the specified connection into the specified file. + * + * @param connection Active connection, do not close this connection. + * @param tableReference [[catalog.]schema.]table reference. + * @param fileName File path to write, if exists it may be replaced. + * @param options Options to use for the export like encoding, separator ... + * The options are different from a format to another. + * @param deleteFiles True to delete the files if exist + * @param progress Progress visitor following the execution. + * @throws SQLException Table read error. + * @throws IOException File write error. + */ + void exportTable(Connection connection, String tableReference, File fileName, + String options,boolean deleteFiles,ProgressVisitor progress) throws SQLException, IOException; + /** * Export the specified table from the specified connection into the specified file. * @@ -105,8 +135,8 @@ void exportTable(Connection connection, String tableReference, File fileName, Pr * @throws SQLException Table read error. * @throws IOException File write error. */ - void exportTable(Connection connection, String tableReference, File fileName, ProgressVisitor progress, - String options) throws SQLException, IOException; + void exportTable(Connection connection, String tableReference, File fileName, + String options,ProgressVisitor progress) throws SQLException, IOException; /** * Import the specified file into the specified table in the specified connection. @@ -127,14 +157,14 @@ void importFile(Connection connection, String tableReference, File fileName, Pro * @param connection Active connection, do not close this connection. * @param tableReference [[catalog.]schema.]table reference. * @param fileName File path to read. - * @param progress Progress visitor following the execution. * @param options Options to use for the export like encoding, separator ... * The options are different from a format to another. + * @param progress Progress visitor following the execution. * @throws SQLException Table write error. * @throws IOException File read error. */ - void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress, - String options) throws SQLException, IOException; + void importFile(Connection connection, String tableReference, File fileName, String options, ProgressVisitor progress) + throws SQLException, IOException; /** * Import the specified file into the specified table in the specified connection. @@ -142,11 +172,29 @@ void importFile(Connection connection, String tableReference, File fileName, Pro * @param connection Active connection, do not close this connection. * @param tableReference [[catalog.]schema.]table reference. * @param fileName File path to read. + * @param deleteTables True if the existing table used for the import should be deleted, false otherwise. * @param progress Progress visitor following the execution. + * @throws SQLException Table write error. + * @throws IOException File read error. + */ + void importFile(Connection connection, String tableReference, File fileName, boolean deleteTables, ProgressVisitor progress + ) throws SQLException, IOException; + + /** + * Import the specified file into the specified table in the specified connection. + * + * @param connection Active connection, do not close this connection. + * @param tableReference [[catalog.]schema.]table reference. + * @param fileName File path to read. + * @param options Options to use for the export like encoding, separator ... + * The options are different from a format to another. * @param deleteTables True if the existing table used for the import should be deleted, false otherwise. + * @param progress Progress visitor following the execution. * @throws SQLException Table write error. * @throws IOException File read error. */ - void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress, - boolean deleteTables) throws SQLException, IOException; + void importFile(Connection connection, String tableReference, File fileName, String options, boolean deleteTables, ProgressVisitor progress + ) throws SQLException, IOException; + + } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/asc/AscDriverFunction.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/asc/AscDriverFunction.java index 44d4e50474..173cb40056 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/asc/AscDriverFunction.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/asc/AscDriverFunction.java @@ -75,7 +75,17 @@ public void exportTable(Connection connection, String tableReference, File fileN } @Override - public void exportTable(Connection connection, String tableReference, File fileName, ProgressVisitor progress, String encoding) throws SQLException, IOException{ + public void exportTable(Connection connection, String tableReference, File fileName, boolean deleteFiles, ProgressVisitor progress) throws SQLException, IOException { + + } + + @Override + public void exportTable(Connection connection, String tableReference, File fileName, String options, boolean deleteFiles, ProgressVisitor progress) throws SQLException, IOException { + + } + + @Override + public void exportTable(Connection connection, String tableReference, File fileName, String encoding,ProgressVisitor progress) throws SQLException, IOException{ // Import only driver } @@ -102,14 +112,14 @@ public void importFile(Connection connection, String tableReference, File fileNa } @Override - public void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress, - String options) throws SQLException, IOException { + public void importFile(Connection connection, String tableReference, File fileName, String options, ProgressVisitor progress + ) throws SQLException, IOException { importFile(connection, tableReference, fileName, progress); } @Override - public void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress, - boolean deleteTables) throws SQLException, IOException { + public void importFile(Connection connection, String tableReference, File fileName, boolean deleteTables, ProgressVisitor progress + ) throws SQLException, IOException { if(deleteTables) { final boolean isH2 = JDBCUtilities.isH2DataBase(connection); @@ -121,4 +131,9 @@ public void importFile(Connection connection, String tableReference, File fileNa importFile(connection, tableReference, fileName, progress); } + + @Override + public void importFile(Connection connection, String tableReference, File fileName, String options, boolean deleteTables, ProgressVisitor progress) throws SQLException, IOException { + + } } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/csv/CSVDriverFunction.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/csv/CSVDriverFunction.java index 3352106350..7f351313c1 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/csv/CSVDriverFunction.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/csv/CSVDriverFunction.java @@ -29,6 +29,7 @@ import java.io.*; import java.nio.channels.FileChannel; +import java.nio.file.Files; import java.sql.*; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -42,7 +43,7 @@ public class CSVDriverFunction implements DriverFunction{ public static String DESCRIPTION = "CSV file (Comma Separated Values)"; - private static final int BATCH_MAX_SIZE = 100; + private static final int BATCH_MAX_SIZE = 200; private static final int AVERAGE_NODE_SIZE = 500; @Override @@ -77,22 +78,22 @@ public boolean isSpatialFormat(String extension) { @Override public void exportTable(Connection connection, String tableReference, File fileName, ProgressVisitor progress) throws SQLException, IOException { - exportTable(connection, tableReference, fileName, progress, null); + exportTable( connection, tableReference, fileName, null, false, progress); } - /** - * Export a table or a query to a CSV file - * - * @param connection Active connection, do not close this connection. - * @param tableReference [[catalog.]schema.]table reference - * @param fileName File path to read - * @param progress - * @param csvOptions the CSV options ie "charset=UTF-8 fieldSeparator=| fieldDelimiter=," - * @throws SQLException - * @throws IOException - */ @Override - public void exportTable(Connection connection, String tableReference, File fileName, ProgressVisitor progress, String csvOptions) throws SQLException, IOException { + public void exportTable(Connection connection, String tableReference, File fileName, boolean deleteFiles, ProgressVisitor progress) throws SQLException, IOException { + exportTable( connection, tableReference, fileName, null, deleteFiles, progress); + } + + @Override + public void exportTable(Connection connection, String tableReference, File fileName, String csvOptions, boolean deleteFiles, ProgressVisitor progress) throws SQLException, IOException { + if (!FileUtil.isExtensionWellFormated(fileName, "csv")) { + throw new SQLException("Only .csv extension is supported"); + } + if(deleteFiles){ + Files.deleteIfExists(fileName.toPath()); + } String regex = ".*(?i)\\b(select|from)\\b.*"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(tableReference); @@ -111,27 +112,39 @@ public void exportTable(Connection connection, String tableReference, File fileN } } else { - if (FileUtil.isExtensionWellFormated(fileName, "csv")) { - final boolean isH2 = JDBCUtilities.isH2DataBase(connection); - TableLocation location = TableLocation.parse(tableReference, isH2); - try (Statement st = connection.createStatement()) { - JDBCUtilities.attachCancelResultSet(st, progress); - Csv csv = new Csv(); - if (csvOptions != null && csvOptions.indexOf('=') >= 0) { - csv.setOptions(csvOptions); - } - csv.write(fileName.getPath(), st.executeQuery("SELECT * FROM " + location.toString()), null); + final boolean isH2 = JDBCUtilities.isH2DataBase(connection); + TableLocation location = TableLocation.parse(tableReference, isH2); + try (Statement st = connection.createStatement()) { + JDBCUtilities.attachCancelResultSet(st, progress); + Csv csv = new Csv(); + if (csvOptions != null && csvOptions.indexOf('=') >= 0) { + csv.setOptions(csvOptions); } - } else { - throw new SQLException("Only .csv extension is supported"); + csv.write(fileName.getPath(), st.executeQuery("SELECT * FROM " + location.toString()), null); } } } + + /** + * Export a table or a query to a CSV file + * + * @param connection Active connection, do not close this connection. + * @param tableReference [[catalog.]schema.]table reference + * @param fileName File path to read + * @param csvOptions the CSV options ie "charset=UTF-8 fieldSeparator=| fieldDelimiter=," + * @param progress + * @throws SQLException + * @throws IOException + */ + @Override + public void exportTable(Connection connection, String tableReference, File fileName, String csvOptions, ProgressVisitor progress) throws SQLException, IOException { + exportTable( connection, tableReference, fileName, csvOptions, false, progress); + } @Override public void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress) throws SQLException, IOException { - importFile(connection, tableReference, fileName, progress, null); + importFile(connection, tableReference, fileName, null, false,progress); } /** @@ -139,15 +152,34 @@ public void importFile(Connection connection, String tableReference, File fileNa * @param connection Active connection, do not close this connection. * @param tableReference [[catalog.]schema.]table reference * @param fileName File path to read - * @param progress * @param csvOptions the CSV options ie "charset=UTF-8 fieldSeparator=| fieldDelimiter=," + * @param progress * @throws SQLException * @throws IOException */ @Override - public void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress, - String csvOptions) throws SQLException, IOException { - if (FileUtil.isFileImportable(fileName, "csv")) { + public void importFile(Connection connection, String tableReference, File fileName, + String csvOptions, ProgressVisitor progress) throws SQLException, IOException { + importFile(connection, tableReference, fileName, csvOptions, false,progress); + } + + @Override + public void importFile(Connection connection, String tableReference, File fileName, + boolean deleteTables,ProgressVisitor progress) throws SQLException, IOException { + importFile(connection, tableReference, fileName, null, false,progress); + } + + @Override + public void importFile(Connection connection, String tableReference, File fileName, String csvOptions, boolean deleteTables, ProgressVisitor progress) throws SQLException, IOException { + if (!FileUtil.isFileImportable(fileName, "csv")) { + if(deleteTables) { + final boolean isH2 = JDBCUtilities.isH2DataBase(connection); + TableLocation requestedTable = TableLocation.parse(tableReference, isH2); + String table = requestedTable.getTable(); + Statement stmt = connection.createStatement(); + stmt.execute("DROP TABLE IF EXISTS " + table); + stmt.close(); + } final boolean isH2 = JDBCUtilities.isH2DataBase(connection); TableLocation requestedTable = TableLocation.parse(tableReference, isH2); String table = requestedTable.getTable(); @@ -156,13 +188,13 @@ public void importFile(Connection connection, String tableReference, File fileNa long fileSize = fc.size(); // Given the file size and an average node file size. // Skip how many nodes in order to update progression at a step of 1% - long readFileSizeEachNode = Math.max(1, (fileSize / AVERAGE_NODE_SIZE) / 100); + long readFileSizeEachNode = Math.max(1, (fileSize / AVERAGE_NODE_SIZE) / 100); int average_row_size = 0; connection.setAutoCommit(false); Csv csv = new Csv(); if (csvOptions != null && csvOptions.indexOf('=') >= 0) { csv.setOptions(csvOptions); - } + } ResultSet reader = csv.read(new BufferedReader(new InputStreamReader(fis)), null); ResultSetMetaData metadata = reader.getMetaData(); int columnCount = metadata.getColumnCount(); @@ -187,7 +219,6 @@ public void importFile(Connection connection, String tableReference, File fileNa try (Statement stmt = connection.createStatement()) { stmt.execute(createTable.toString()); } - PreparedStatement pst = connection.prepareStatement(insertTable.toString()); long batchSize = 0; try { @@ -223,24 +254,9 @@ public void importFile(Connection connection, String tableReference, File fileNa } } finally { - pst.close(); + pst.close(); connection.setAutoCommit(true); } } } - - @Override - public void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress, - boolean deleteTables) throws SQLException, IOException { - if(deleteTables) { - final boolean isH2 = JDBCUtilities.isH2DataBase(connection); - TableLocation requestedTable = TableLocation.parse(tableReference, isH2); - String table = requestedTable.getTable(); - Statement stmt = connection.createStatement(); - stmt.execute("DROP TABLE IF EXISTS " + table); - stmt.close(); - } - - importFile(connection, tableReference, fileName, progress); - } } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/dbf/DBFDriverFunction.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/dbf/DBFDriverFunction.java index f385156ffc..28ed6f9181 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/dbf/DBFDriverFunction.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/dbf/DBFDriverFunction.java @@ -36,6 +36,7 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Files; import java.sql.*; import java.util.ArrayList; import java.util.List; @@ -50,22 +51,33 @@ public class DBFDriverFunction implements DriverFunction { public static String DESCRIPTION = "dBase III format"; - private static final int BATCH_MAX_SIZE = 100; + private static final int BATCH_MAX_SIZE = 200; @Override public void exportTable(Connection connection, String tableReference, File fileName, ProgressVisitor progress) throws SQLException, IOException { - exportTable(connection, tableReference, fileName, progress, null); + exportTable(connection, tableReference, fileName, null,false,progress); } @Override - public void exportTable(Connection connection, String tableReference, File fileName, ProgressVisitor progress,String encoding) throws SQLException, IOException { + public void exportTable(Connection connection, String tableReference, File fileName, boolean deleteFiles, ProgressVisitor progress) throws SQLException, IOException { + exportTable(connection, tableReference, fileName, null, deleteFiles,progress); + } + + @Override + public void exportTable(Connection connection, String tableReference, File fileName, String options, boolean deleteFiles, ProgressVisitor progress) throws SQLException, IOException { + if (!FileUtil.isExtensionWellFormated(fileName, "dbf")) { + throw new SQLException("Only .dbf extension is supported"); + } + if(deleteFiles){ + Files.deleteIfExists(fileName.toPath()); + } String regex = ".*(?i)\\b(select|from)\\b.*"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(tableReference); if (matcher.find()) { if (tableReference.startsWith("(") && tableReference.endsWith(")")) { - if (FileUtil.isExtensionWellFormated(fileName, "dbf")) { PreparedStatement ps = connection.prepareStatement(tableReference, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); + JDBCUtilities.attachCancelResultSet(ps, progress); ResultSet rs = ps.executeQuery(); int recordCount = 0; rs.last(); @@ -75,8 +87,8 @@ public void exportTable(Connection connection, String tableReference, File fileN ResultSetMetaData resultSetMetaData = rs.getMetaData(); ArrayList columnIndexes = new ArrayList(); DbaseFileHeader header = dBaseHeaderFromMetaData(resultSetMetaData, columnIndexes); - if (encoding != null) { - header.setEncoding(encoding); + if (options != null && !options.isEmpty()) { + header.setEncoding(options); } header.setNumRecords(recordCount); DBFDriver dbfDriver = new DBFDriver(); @@ -93,19 +105,18 @@ public void exportTable(Connection connection, String tableReference, File fileN } } dbfDriver.close(); - } } else { throw new SQLException("The select query must be enclosed in parenthesis: '(SELECT * FROM ORDERS)'."); } } else { - if (FileUtil.isExtensionWellFormated(fileName, "dbf")) { - int recordCount = JDBCUtilities.getRowCount(connection, tableReference); + int recordCount = JDBCUtilities.getRowCount(connection, tableReference); final boolean isH2 = JDBCUtilities.isH2DataBase(connection); String tableName = TableLocation.parse(tableReference, isH2).toString(isH2); // Read table content Statement st = connection.createStatement(); + JDBCUtilities.attachCancelResultSet(st, progress); ProgressVisitor lineProgress = null; if (!(progress instanceof EmptyProgressVisitor)) { try (ResultSet rs = st.executeQuery(String.format("select count(*) from %s", tableName))) { @@ -119,8 +130,8 @@ public void exportTable(Connection connection, String tableReference, File fileN ResultSetMetaData resultSetMetaData = rs.getMetaData(); ArrayList columnIndexes = new ArrayList(); DbaseFileHeader header = dBaseHeaderFromMetaData(resultSetMetaData, columnIndexes); - if (encoding != null) { - header.setEncoding(encoding); + if (options != null&& !options.isEmpty()) { + header.setEncoding(options); } header.setNumRecords(recordCount); DBFDriver dbfDriver = new DBFDriver(); @@ -141,11 +152,12 @@ public void exportTable(Connection connection, String tableReference, File fileN } finally { st.close(); } - } else { - throw new SQLException("Only .dbf extension is supported"); - } } + } + @Override + public void exportTable(Connection connection, String tableReference, File fileName,String encoding, ProgressVisitor progress) throws SQLException, IOException { + exportTable(connection, tableReference, fileName, encoding, false,progress); } @Override @@ -179,23 +191,42 @@ public boolean isSpatialFormat(String extension) { @Override public void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress) throws SQLException, IOException { - importFile(connection, tableReference, fileName, progress, null); + importFile(connection, tableReference, fileName, null, progress); } /** * @param connection Active connection, do not close this connection. * @param tableReference [[catalog.]schema.]table reference * @param fileName File path to read - * @param progress monitor * @param forceFileEncoding File encoding to use, null will use the provided file encoding in file header. + * @param progress monitor * @throws SQLException Table write error * @throws IOException File read error */ @Override - public void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress,String forceFileEncoding) throws SQLException, IOException { - if (FileUtil.isFileImportable(fileName, "dbf")) { + public void importFile(Connection connection, String tableReference, File fileName,String forceFileEncoding, ProgressVisitor progress) throws SQLException, IOException { + importFile(connection, tableReference, fileName,forceFileEncoding, false, progress); + } + + @Override + public void importFile(Connection connection, String tableReference, File fileName, + boolean deleteTables,ProgressVisitor progress) throws SQLException, IOException { + importFile(connection, tableReference, fileName,null, false, progress); + } + + @Override + public void importFile(Connection connection, String tableReference, File fileName, String options, boolean deleteTables, ProgressVisitor progress) throws SQLException, IOException { + if (!FileUtil.isFileImportable(fileName, "dbf")) { + if (deleteTables) { + final boolean isH2 = JDBCUtilities.isH2DataBase(connection); + TableLocation requestedTable = TableLocation.parse(tableReference, isH2); + String table = requestedTable.getTable(); + Statement stmt = connection.createStatement(); + stmt.execute("DROP TABLE IF EXISTS " + table); + stmt.close(); + } DBFDriver dbfDriver = new DBFDriver(); - dbfDriver.initDriverFromFile(fileName, forceFileEncoding); + dbfDriver.initDriverFromFile(fileName, options); final boolean isH2 = JDBCUtilities.isH2DataBase(connection); String parsedTable = TableLocation.parse(tableReference, isH2).toString(isH2); DbaseFileHeader dbfHeader = dbfDriver.getDbaseFileHeader(); @@ -205,7 +236,7 @@ public void importFile(Connection connection, String tableReference, File fileNa } else { try { try ( // Build CREATE TABLE sql request - Statement st = connection.createStatement()) { + Statement st = connection.createStatement()) { List otherCols = new ArrayList(dbfHeader.getNumFields() + 1); for (int idColumn = 0; idColumn < dbfHeader.getNumFields(); idColumn++) { otherCols.add(new Column(dbfHeader.getFieldName(idColumn), 0)); @@ -219,6 +250,7 @@ public void importFile(Connection connection, String tableReference, File fileNa try (PreparedStatement preparedStatement = connection.prepareStatement( String.format("INSERT INTO %s VALUES ( %s )", parsedTable, getQuestionMark(dbfHeader.getNumFields() + 1)))) { + JDBCUtilities.attachCancelResultSet(preparedStatement, progress); long batchSize = 0; for (int rowId = 0; rowId < dbfDriver.getRowCount(); rowId++) { preparedStatement.setObject(1, rowId + 1); @@ -255,22 +287,6 @@ public void importFile(Connection connection, String tableReference, File fileNa } } - @Override - public void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress, - boolean deleteTables) throws SQLException, IOException { - - if(deleteTables) { - final boolean isH2 = JDBCUtilities.isH2DataBase(connection); - TableLocation requestedTable = TableLocation.parse(tableReference, isH2); - String table = requestedTable.getTable(); - Statement stmt = connection.createStatement(); - stmt.execute("DROP TABLE IF EXISTS " + table); - stmt.close(); - } - - importFile(connection, tableReference, fileName, progress); - } - private static class DBFType { char type; diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/dbf/DBFRead.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/dbf/DBFRead.java index 979ec3bdbf..3b5c7f5244 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/dbf/DBFRead.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/dbf/DBFRead.java @@ -51,6 +51,15 @@ public static void read(Connection connection, String fileName) throws IOExcepti throw new SQLException("The file name contains unsupported characters"); } } + public static void read(Connection connection, String fileName, boolean deleteTable) throws IOException, SQLException { + final String name = URIUtilities.fileFromString(fileName).getName(); + String tableName = name.substring(0, name.lastIndexOf(".")).toUpperCase(); + if (tableName.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) { + read(connection, fileName, tableName, null, deleteTable); + } else { + throw new SQLException("The file name contains unsupported characters"); + } + } public static void read(Connection connection, String fileName, String tableReference) throws IOException, SQLException { DBFDriverFunction dbfDriverFunction = new DBFDriverFunction(); @@ -59,6 +68,11 @@ public static void read(Connection connection, String fileName, String tableRefe public static void read(Connection connection, String fileName, String tableReference, String fileEncoding) throws IOException, SQLException { DBFDriverFunction dbfDriverFunction = new DBFDriverFunction(); - dbfDriverFunction.importFile(connection, tableReference, URIUtilities.fileFromString(fileName), new EmptyProgressVisitor(), fileEncoding); + dbfDriverFunction.importFile(connection, tableReference, URIUtilities.fileFromString(fileName), fileEncoding, new EmptyProgressVisitor()); + } + + public static void read(Connection connection, String fileName, String tableReference, String fileEncoding, boolean deleteTable) throws IOException, SQLException { + DBFDriverFunction dbfDriverFunction = new DBFDriverFunction(); + dbfDriverFunction.importFile(connection, tableReference, URIUtilities.fileFromString(fileName), fileEncoding,deleteTable, new EmptyProgressVisitor()); } } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/dbf/DBFWrite.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/dbf/DBFWrite.java index d0739f5769..5e22e55a65 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/dbf/DBFWrite.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/dbf/DBFWrite.java @@ -32,26 +32,37 @@ /** * @author Nicolas Fortin + * @author Erwan Bocher, CNRS */ public class DBFWrite extends AbstractFunction implements ScalarFunction { public DBFWrite() { addProperty(PROP_REMARKS, "Transfer the content of a table into a DBF\n" + - "CALL DBFWRITE('FILENAME', 'TABLE'[,'ENCODING']) or CALL DBFWRITE('FILENAME', '(SELECT * FROM TABLE)'[,'ENCODING'])"); + "CALL DBFWRITE('FILENAME', 'TABLE'[,'ENCODING', true \n(to delete output file if exists)]) or " + + "CALL DBFWRITE('FILENAME', '(SELECT * FROM TABLE)'[,'ENCODING', true \n(to delete output file if exists)])"); } @Override public String getJavaStaticMethod() { - return "exportTable"; //To change body of implemented methods use File | Settings | File Templates. + return "exportTable"; } public static void exportTable(Connection connection, String fileName, String tableReference) throws IOException, SQLException { DBFDriverFunction driverFunction = new DBFDriverFunction(); driverFunction.exportTable(connection, tableReference, URIUtilities.fileFromString(fileName), new EmptyProgressVisitor()); } + public static void exportTable(Connection connection, String fileName, String tableReference, boolean deleteFile) throws IOException, SQLException { + DBFDriverFunction driverFunction = new DBFDriverFunction(); + driverFunction.exportTable(connection, tableReference, URIUtilities.fileFromString(fileName),null, deleteFile, new EmptyProgressVisitor()); + } public static void exportTable(Connection connection, String fileName, String tableReference,String encoding) throws IOException, SQLException { DBFDriverFunction driverFunction = new DBFDriverFunction(); - driverFunction.exportTable(connection, tableReference, new File(fileName), new EmptyProgressVisitor(), encoding); + driverFunction.exportTable(connection, tableReference, new File(fileName), encoding, new EmptyProgressVisitor()); + } + + public static void exportTable(Connection connection, String fileName, String tableReference,String encoding, boolean deleteFile) throws IOException, SQLException { + DBFDriverFunction driverFunction = new DBFDriverFunction(); + driverFunction.exportTable(connection, tableReference, new File(fileName), encoding, deleteFile,new EmptyProgressVisitor()); } } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/dbf/internal/DbaseFileHeader.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/dbf/internal/DbaseFileHeader.java index 9955096a19..b33489677b 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/dbf/internal/DbaseFileHeader.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/dbf/internal/DbaseFileHeader.java @@ -457,7 +457,7 @@ public int getHeaderLength() { * If errors occur while reading. */ public void readHeader(FileChannel channel,String forceEncoding) throws IOException { - if(forceEncoding != null) { + if(forceEncoding != null && !forceEncoding.isEmpty()) { fileEncoding = forceEncoding; } // we'll read in chunks of 1K diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonDriverFunction.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonDriverFunction.java index ace77a23d5..ee0b7b359e 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonDriverFunction.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonDriverFunction.java @@ -22,6 +22,7 @@ import org.h2gis.api.DriverFunction; import org.h2gis.api.ProgressVisitor; +import org.h2gis.functions.io.utility.FileUtil; import org.h2gis.utilities.JDBCUtilities; import org.h2gis.utilities.TableLocation; @@ -71,52 +72,49 @@ public boolean isSpatialFormat(String extension) { @Override public void exportTable(Connection connection, String tableReference, File fileName, ProgressVisitor progress) throws SQLException, IOException{ - exportTable(connection,tableReference, fileName,progress, null); + exportTable(connection,tableReference, fileName, null, false, progress); } - - /** - * Export a table or a query to a geojson file - * - * @param connection - * @param tableReference - * @param fileName - * @param progress - * @param encoding - * @throws SQLException - * @throws IOException - */ + @Override - public void exportTable(Connection connection, String tableReference, File fileName, ProgressVisitor progress, String encoding) throws SQLException, IOException{ + public void exportTable(Connection connection, String tableReference, File fileName, boolean deleteFiles, ProgressVisitor progress) throws SQLException, IOException { + exportTable(connection,tableReference, fileName, null, deleteFiles, progress); + } + + @Override + public void exportTable(Connection connection, String tableReference, File fileName, String encoding, boolean deleteFiles, ProgressVisitor progress) throws SQLException, IOException { GeoJsonWriteDriver geoJsonDriver = new GeoJsonWriteDriver(connection); - geoJsonDriver.write(progress,tableReference, fileName, encoding); + geoJsonDriver.write(progress,tableReference, fileName, encoding, deleteFiles); } + @Override - public void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress) - throws SQLException, IOException { - GeoJsonReaderDriver geoJsonReaderDriver = new GeoJsonReaderDriver(connection, fileName); + public void exportTable(Connection connection, String tableReference, File fileName, String encoding, ProgressVisitor progress) throws SQLException, IOException{ + exportTable(connection,tableReference, fileName, encoding, false, progress); + } + + @Override + public void importFile(Connection connection, String tableReference, File fileName, String options, boolean deleteTables, ProgressVisitor progress) throws SQLException, IOException { + GeoJsonReaderDriver geoJsonReaderDriver = new GeoJsonReaderDriver(connection, fileName, options, deleteTables); geoJsonReaderDriver.read(progress, tableReference); } @Override - public void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress, - String options) throws SQLException, IOException { - importFile(connection, tableReference, fileName, progress); + public void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress) + throws SQLException, IOException { + importFile(connection, tableReference, fileName, null, false, progress); } @Override - public void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress, - boolean deleteTables) throws SQLException, IOException { - - if(deleteTables) { - final boolean isH2 = JDBCUtilities.isH2DataBase(connection); - TableLocation requestedTable = TableLocation.parse(tableReference, isH2); - String table = requestedTable.getTable(); - Statement stmt = connection.createStatement(); - stmt.execute("DROP TABLE IF EXISTS " + table); - stmt.close(); - } + public void importFile(Connection connection, String tableReference, File fileName, String options,ProgressVisitor progress + ) throws SQLException, IOException { + importFile(connection, tableReference, fileName, options, false, progress); + } - importFile(connection, tableReference, fileName, progress); + @Override + public void importFile(Connection connection, String tableReference, File fileName, + boolean deleteTables, ProgressVisitor progress) throws SQLException, IOException { + importFile(connection, tableReference, fileName, null, deleteTables, progress); } + + } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonRead.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonRead.java index bf561a3ec8..ea34bb07e9 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonRead.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonRead.java @@ -57,7 +57,24 @@ public static void readGeoJson(Connection connection, String fileName) throws IO final String name = URIUtilities.fileFromString(fileName).getName(); String tableName = name.substring(0, name.lastIndexOf(".")).toUpperCase(); if (tableName.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) { - readGeoJson(connection, fileName, tableName); + readGeoJson(connection, fileName, tableName,null, false); + } else { + throw new SQLException("The file name contains unsupported characters"); + } + } + + /** + * + * @param connection + * @param fileName + * @throws IOException + * @throws SQLException + */ + public static void readGeoJson(Connection connection, String fileName, boolean deleteTable) throws IOException, SQLException { + final String name = URIUtilities.fileFromString(fileName).getName(); + String tableName = name.substring(0, name.lastIndexOf(".")).toUpperCase(); + if (tableName.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) { + readGeoJson(connection, fileName, tableName,null, deleteTable); } else { throw new SQLException("The file name contains unsupported characters"); } @@ -73,7 +90,15 @@ public static void readGeoJson(Connection connection, String fileName) throws IO * @throws SQLException */ public static void readGeoJson(Connection connection, String fileName, String tableReference) throws IOException, SQLException { + readGeoJson(connection,fileName, tableReference, null, false); + } + + public static void readGeoJson(Connection connection, String fileName, String tableReference,String encoding) throws IOException, SQLException { + readGeoJson(connection,fileName, tableReference, encoding, false); + } + + public static void readGeoJson(Connection connection, String fileName, String tableReference,String encoding, boolean deleteTable ) throws IOException, SQLException { GeoJsonDriverFunction gjdf = new GeoJsonDriverFunction(); - gjdf.importFile(connection, tableReference, URIUtilities.fileFromString(fileName), new EmptyProgressVisitor()); + gjdf.importFile(connection, tableReference, URIUtilities.fileFromString(fileName), encoding, deleteTable, new EmptyProgressVisitor()); } } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonReaderDriver.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonReaderDriver.java index 4bd1e197d0..357d99f789 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonReaderDriver.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonReaderDriver.java @@ -19,6 +19,7 @@ */ package org.h2gis.functions.io.geojson; +import com.fasterxml.jackson.core.JsonEncoding; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonToken; @@ -31,10 +32,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; +import java.io.*; import java.nio.channels.FileChannel; import java.sql.*; import java.util.*; @@ -58,6 +56,8 @@ public class GeoJsonReaderDriver { private final File fileName; private final Connection connection; private static GeometryFactory GF; + private final String encoding; + private final boolean deleteTable; private PreparedStatement preparedStatement = null; private JsonFactory jsFactory; private int featureCounter = 1; @@ -88,6 +88,7 @@ public class GeoJsonReaderDriver { } private Set finalGeometryTypes; + private JsonEncoding jsonEncoding; /** * Driver to import a GeoJSON file into a spatial table. @@ -95,9 +96,11 @@ public class GeoJsonReaderDriver { * @param connection * @param fileName */ - public GeoJsonReaderDriver(Connection connection, File fileName) { + public GeoJsonReaderDriver(Connection connection, File fileName, String encoding, boolean deleteTable) { this.connection = connection; this.fileName = fileName; + this.encoding=encoding; + this.deleteTable=deleteTable; } /** @@ -112,6 +115,11 @@ public void read(ProgressVisitor progress, String tableReference) throws SQLExce if (FileUtil.isFileImportable(fileName, "geojson")) { this.isH2 = JDBCUtilities.isH2DataBase(connection); this.tableLocation = TableLocation.parse(tableReference, isH2); + if(deleteTable){ + Statement stmt = connection.createStatement(); + stmt.execute("DROP TABLE IF EXISTS " + tableLocation); + stmt.close(); + } if (fileName.length() > 0) { parseGeoJson(progress); } else { @@ -179,8 +187,7 @@ private boolean parseMetadata() throws SQLException, IOException { nodeCountProgress = 0; cachedColumnNames = new LinkedHashMap<>(); finalGeometryTypes = new HashSet(); - - try (JsonParser jp = jsFactory.createParser(fis)) { + try (JsonParser jp = jsFactory.createParser( new InputStreamReader(fis, jsonEncoding.getJavaName()))) { jp.nextToken();//START_OBJECT jp.nextToken(); // field_name (type) jp.nextToken(); // value_string (FeatureCollection) @@ -735,7 +742,15 @@ private void parsePropertiesMetadata(JsonParser jp) throws IOException, SQLExcep /** * Creates the JsonFactory. */ - private void init() { + private void init() throws SQLException { + jsonEncoding = JsonEncoding.UTF8; + if (encoding != null && !encoding.isEmpty()) { + try { + jsonEncoding = JsonEncoding.valueOf(encoding); + } catch (IllegalArgumentException ex) { + throw new SQLException("Only UTF-8, UTF-16BE, UTF-16LE, UTF-32BE, UTF-32LE encoding is supported"); + } + } jsFactory = new JsonFactory(); jsFactory.configure(JsonParser.Feature.ALLOW_COMMENTS, true); jsFactory.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); @@ -1262,7 +1277,7 @@ private void parseData() throws IOException, SQLException { FileInputStream fis = null; try { fis = new FileInputStream(fileName); - try (JsonParser jp = jsFactory.createParser(fis)) { + try (JsonParser jp = jsFactory.createParser(new InputStreamReader(fis, jsonEncoding.getJavaName()))) { jp.nextToken();//START_OBJECT jp.nextToken(); // field_name (type) jp.nextToken(); // value_string (FeatureCollection) diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonWrite.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonWrite.java index 2a91fe6e15..8c83b784e2 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonWrite.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonWrite.java @@ -38,7 +38,8 @@ public class GeoJsonWrite extends AbstractFunction implements ScalarFunction { public GeoJsonWrite(){ - addProperty(PROP_REMARKS, "Export a spatial table to a GeoJSON 1.0 file.\n As optional argument an encoding value is supported."); + addProperty(PROP_REMARKS, "Export a spatial table to a GeoJSON 1.0 file.\n " + + "As optional arguments encoding value is supported and delete output file."); } @Override @@ -57,8 +58,12 @@ public String getJavaStaticMethod() { * @throws SQLException */ public static void writeGeoJson(Connection connection, String fileName, String tableReference, String encoding) throws IOException, SQLException { + writeGeoJson(connection,tableReference, fileName, encoding,false); + } + + public static void writeGeoJson(Connection connection, String fileName, String tableReference, String encoding, boolean deleteFile) throws IOException, SQLException { GeoJsonDriverFunction geoJsonDriver = new GeoJsonDriverFunction(); - geoJsonDriver.exportTable(connection,tableReference, URIUtilities.fileFromString(fileName), new EmptyProgressVisitor(),encoding); + geoJsonDriver.exportTable(connection,tableReference, URIUtilities.fileFromString(fileName), encoding,deleteFile,new EmptyProgressVisitor()); } /** @@ -71,6 +76,10 @@ public static void writeGeoJson(Connection connection, String fileName, String t * @throws SQLException */ public static void writeGeoJson(Connection connection, String fileName, String tableReference) throws IOException, SQLException { - writeGeoJson(connection, fileName, tableReference, null); + writeGeoJson(connection, fileName, tableReference, null, false); + } + + public static void writeGeoJson(Connection connection, String fileName, String tableReference, boolean deleteTable) throws IOException, SQLException { + writeGeoJson(connection, fileName, tableReference, null, deleteTable); } } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonWriteDriver.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonWriteDriver.java index 6ebc883da6..13797ff569 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonWriteDriver.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonWriteDriver.java @@ -30,6 +30,7 @@ import org.locationtech.jts.geom.*; import java.io.*; +import java.nio.file.Files; import java.sql.*; import java.util.LinkedHashMap; import java.util.Map; @@ -82,7 +83,7 @@ public GeoJsonWriteDriver(Connection connection) { * @throws IOException */ public void write(ProgressVisitor progress, ResultSet resultSet, File file) throws SQLException, IOException { - write(progress, resultSet, file, null); + write(progress, resultSet, file, null, false); } @@ -93,14 +94,18 @@ public void write(ProgressVisitor progress, ResultSet resultSet, File file) thro * @param rs input resulset * @param fileName the output file * @param encoding + * @param deleteFile * @throws SQLException * @throws java.io.IOException */ - public void write(ProgressVisitor progress, ResultSet rs, File fileName, String encoding) throws SQLException, IOException { + public void write(ProgressVisitor progress, ResultSet rs, File fileName, String encoding, boolean deleteFile) throws SQLException, IOException { if (FileUtil.isExtensionWellFormated(fileName, "geojson")) { + if(deleteFile){ + Files.deleteIfExists(fileName.toPath()); + } FileOutputStream fos = null; JsonEncoding jsonEncoding = JsonEncoding.UTF8; - if (encoding != null) { + if (encoding != null && !encoding.isEmpty()) { try { jsonEncoding = JsonEncoding.valueOf(encoding); } catch (IllegalArgumentException ex) { @@ -170,7 +175,7 @@ public void write(ProgressVisitor progress, ResultSet rs, File fileName, String * @throws SQLException * @throws java.io.IOException */ - public void write(ProgressVisitor progress, String tableName, File fileName, String encoding) throws SQLException, IOException { + public void write(ProgressVisitor progress, String tableName, File fileName, String encoding, boolean deleteFile) throws SQLException, IOException { String regex = ".*(?i)\\b(select|from)\\b.*"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(tableName); @@ -178,12 +183,15 @@ public void write(ProgressVisitor progress, String tableName, File fileName, Str if (tableName.startsWith("(") && tableName.endsWith(")")) { PreparedStatement ps = connection.prepareStatement(tableName, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet resultSet = ps.executeQuery(); - write(progress, resultSet, fileName, encoding); + write(progress, resultSet, fileName, encoding, deleteFile); } else { throw new SQLException("The select query must be enclosed in parenthesis: '(SELECT * FROM ORDERS)'."); } } else { if (FileUtil.isExtensionWellFormated(fileName, "geojson")) { + if(deleteFile){ + Files.deleteIfExists(fileName.toPath()); + } JsonEncoding jsonEncoding = JsonEncoding.UTF8; if (encoding != null) { try { diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/gpx/GPXDriverFunction.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/gpx/GPXDriverFunction.java index 1dfb7816e3..86c4e48557 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/gpx/GPXDriverFunction.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/gpx/GPXDriverFunction.java @@ -78,21 +78,38 @@ public void exportTable(Connection connection, String tableReference, File fileN } @Override - public void exportTable(Connection connection, String tableReference, File fileName, ProgressVisitor progress, - String options) throws SQLException, IOException { + public void exportTable(Connection connection, String tableReference, File fileName, boolean deleteFiles, ProgressVisitor progress) throws SQLException, IOException { throw new UnsupportedOperationException("Not supported yet."); } + @Override + public void exportTable(Connection connection, String tableReference, File fileName, String options, boolean deleteFiles, ProgressVisitor progress) throws SQLException, IOException { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void exportTable(Connection connection, String tableReference, File fileName, String options,ProgressVisitor progress + ) throws SQLException, IOException { + throw new UnsupportedOperationException("Not supported yet."); + } + + + @Override + public void importFile(Connection connection, String tableReference, File fileName, String encoding, boolean deleteTables, ProgressVisitor progress) throws SQLException, IOException { + GpxParser gpd = new GpxParser(connection, fileName, encoding, deleteTables); + gpd.read(tableReference, progress); + } + @Override public void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress) throws SQLException, IOException { - importFile(connection, tableReference, fileName, progress, false); + importFile( connection, tableReference, fileName, null, false, progress); } @Override - public void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress, - String options) throws SQLException, IOException { - importFile(connection, tableReference, fileName, progress); + public void importFile(Connection connection, String tableReference, File fileName, String options,ProgressVisitor progress + ) throws SQLException, IOException { + importFile( connection, tableReference, fileName, options, false, progress); } /** @@ -106,17 +123,8 @@ public void importFile(Connection connection, String tableReference, File fileNa * @throws IOException File read error */ @Override - public void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress, - boolean deleteTables) throws SQLException, IOException { - boolean isH2 = JDBCUtilities.isH2DataBase(connection); - if (fileName.length() == 0) { - JDBCUtilities.createEmptyTable(connection, TableLocation.parse(tableReference, isH2).toString()); - } else { - if (deleteTables) { - GPXTablesFactory.dropOSMTables(connection, isH2, tableReference); - } - GpxParser gpd = new GpxParser(); - gpd.read(fileName, tableReference, connection); - } + public void importFile(Connection connection, String tableReference, File fileName, boolean deleteTables,ProgressVisitor progress + ) throws SQLException, IOException { + importFile( connection, tableReference, fileName, null, deleteTables, progress); } } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/gpx/GPXRead.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/gpx/GPXRead.java index 90cf1c7009..c5132dece5 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/gpx/GPXRead.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/gpx/GPXRead.java @@ -50,24 +50,10 @@ public String getJavaStaticMethod() { } - /** - * Copy data from GPX File into a new table in specified connection. - * - * @param connection Active connection - * @param tableReference [[catalog.]schema.]table reference - * @param fileName File path of the SHP file - * @param deleteTables true to delete the existing tables - * @throws java.io.IOException - * @throws java.sql.SQLException - */ + public static void readGPX(Connection connection, String fileName, String tableReference, boolean deleteTables) throws IOException, SQLException { - File file = URIUtilities.fileFromString(fileName); - if (FileUtil.isFileImportable(file, "gpx")) { - GPXDriverFunction gpxdf = new GPXDriverFunction(); - gpxdf.importFile(connection, tableReference, URIUtilities.fileFromString(fileName), new EmptyProgressVisitor(), deleteTables); - } + readGPX( connection, fileName, tableReference, null, deleteTables); } - /** * Copy data from GPX File into a new table in specified connection. @@ -75,11 +61,28 @@ public static void readGPX(Connection connection, String fileName, String tableR * @param connection Active connection * @param tableReference [[catalog.]schema.]table reference * @param fileName File path of the SHP file + * @param encoding charset encoding + * @param deleteTables true to delete the existing tables * @throws java.io.IOException * @throws java.sql.SQLException */ + public static void readGPX(Connection connection, String fileName, String tableReference, String encoding, boolean deleteTables) throws IOException, SQLException { + GPXDriverFunction gpxdf = new GPXDriverFunction(); + gpxdf.importFile(connection, tableReference, URIUtilities.fileFromString(fileName), encoding, deleteTables, new EmptyProgressVisitor()); + } + + + /** + * Copy data from GPX File into a new table in specified connection. + * + * @param connection Active connection + * @param tableReference [[catalog.]schema.]table reference + * @param fileName File path of the SHP file + * @throws java.io.IOException + * @throws java.sql.SQLException + */ public static void readGPX(Connection connection, String fileName, String tableReference) throws IOException, SQLException { - readGPX(connection, fileName, tableReference, false); + readGPX( connection, fileName, tableReference, null, false); } /** @@ -95,7 +98,17 @@ public static void readGPX(Connection connection, String fileName) throws IOExce final String name = URIUtilities.fileFromString(fileName).getName(); String tableName = name.substring(0, name.lastIndexOf(".")).toUpperCase(); if (tableName.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) { - readGPX(connection, fileName, tableName); + readGPX( connection, fileName, tableName, null, false); + } else { + throw new SQLException("The file name contains unsupported characters"); + } + } + + public static void readGPX(Connection connection, String fileName, boolean deleteTable) throws IOException, SQLException { + final String name = URIUtilities.fileFromString(fileName).getName(); + String tableName = name.substring(0, name.lastIndexOf(".")).toUpperCase(); + if (tableName.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) { + readGPX( connection, fileName, tableName, null, deleteTable); } else { throw new SQLException("The file name contains unsupported characters"); } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/gpx/model/AbstractGpxParserDefault.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/gpx/model/AbstractGpxParserDefault.java index 38ef8d571a..e3474707eb 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/gpx/model/AbstractGpxParserDefault.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/gpx/model/AbstractGpxParserDefault.java @@ -20,9 +20,12 @@ package org.h2gis.functions.io.gpx.model; +import org.h2gis.api.ProgressVisitor; +import org.h2gis.functions.io.utility.FileUtil; import org.h2gis.utilities.JDBCUtilities; import org.h2gis.utilities.TableLocation; import org.h2gis.utilities.TableUtilities; +import org.locationtech.jts.geom.GeometryFactory; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; @@ -30,6 +33,7 @@ import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Connection; import java.sql.SQLException; @@ -40,7 +44,11 @@ * @author Erwan Bocher */ public abstract class AbstractGpxParserDefault extends AbstractGpxParser { - + + private final Connection connection; + private final File fileName; + private final String encoding; + private final boolean deleteTable; // Specific parsers private AbstractGpxParserWpt wptParser; private AbstractGpxParserRte rteParser; @@ -75,6 +83,14 @@ public abstract class AbstractGpxParserDefault extends AbstractGpxParser { // The max size of the StringStack public static final int STRINGSTACK_SIZE = 50; + public AbstractGpxParserDefault(Connection connection, File fileName, String encoding, boolean deleteTable) { + super(); + this.connection=connection; + this.fileName=fileName; + this.encoding=encoding; + this.deleteTable=deleteTable; + } + /** * Initialisation of all the indicators used to read the document. */ @@ -114,105 +130,111 @@ public void clear() { * Reads the document and parses it. The other methods are called * automatically when corresponding markup is found. * - * @param inputFile a File representing the gpx file to read * @param tableName the table used to create all tables - * @param connection the connection to the database * @return a boolean value if the parser ends successfully or not * @throws SQLException if the creation of the tables failed */ - public boolean read(File inputFile, String tableName, Connection connection) throws SQLException { - // Initialisation - final boolean isH2 = JDBCUtilities.isH2DataBase(connection); + public boolean read(String tableName, ProgressVisitor progress) throws SQLException, FileNotFoundException { boolean success = false; - TableLocation requestedTable = TableLocation.parse(tableName, isH2); - String table = requestedTable.getTable(); - - clear(); - - GpxPreparser gpxPreparser = new GpxPreparser(); - try { - gpxPreparser.read(inputFile); - } catch (SAXException ex) { - throw new SQLException(ex); - } catch (IOException ex) { - throw new SQLException(ex); - } - - StringBuilder tableNames = new StringBuilder(); - - // We create the tables to store all gpx data in the database - if (gpxPreparser.getTotalWpt() > 0) { - String wptTableName = TableUtilities.caseIdentifier(requestedTable, table + GPXTablesFactory.WAYPOINT, isH2); - if (JDBCUtilities.tableExists(connection, TableLocation.parse(wptTableName,isH2))) { - throw new SQLException("The table " + wptTableName + " already exists."); - } - setWptPreparedStmt(GPXTablesFactory.createWayPointsTable(connection, wptTableName, isH2)); - tableNames.append(wptTableName).append(","); - } - if (gpxPreparser.getTotalRte() > 0 && gpxPreparser.getTotalRtept() > 0) { - String routeTableName = TableUtilities.caseIdentifier(requestedTable, table + GPXTablesFactory.ROUTE, isH2); - if (JDBCUtilities.tableExists(connection, TableLocation.parse(routeTableName, isH2))) { - throw new SQLException("The table " + routeTableName + " already exists."); - } - String routePointsTableName = TableUtilities.caseIdentifier(requestedTable, table + GPXTablesFactory.ROUTEPOINT, isH2); - if (JDBCUtilities.tableExists(connection, TableLocation.parse(routePointsTableName, isH2))) { - throw new SQLException("The table " + routePointsTableName + " already exists."); - } - setRtePreparedStmt(GPXTablesFactory.createRouteTable(connection, routeTableName, isH2)); - setRteptPreparedStmt(GPXTablesFactory.createRoutePointsTable(connection, routePointsTableName, isH2)); - tableNames.append(routeTableName).append(",").append(routePointsTableName).append(","); - } - - if (gpxPreparser.getTotalTrk() > 0 && gpxPreparser.getTotalTrkseg() > 0 - && gpxPreparser.getTotalTrkpt() > 0) { - String trackTableName = TableUtilities.caseIdentifier(requestedTable, table + GPXTablesFactory.TRACK, isH2); - if (JDBCUtilities.tableExists(connection, TableLocation.parse(trackTableName, isH2))) { - throw new SQLException("The table " + trackTableName + " already exists."); - } + if (FileUtil.isFileImportable(fileName, "gpx")) { + // Initialisation + final boolean isH2 = JDBCUtilities.isH2DataBase(connection); - String trackSegmentsTableName = TableUtilities.caseIdentifier(requestedTable, table + GPXTablesFactory.TRACKSEGMENT, isH2); - if (JDBCUtilities.tableExists(connection, TableLocation.parse(trackSegmentsTableName, isH2))) { - throw new SQLException("The table " + trackSegmentsTableName + " already exists."); - } - - String trackPointsTableName = TableUtilities.caseIdentifier(requestedTable, table + GPXTablesFactory.TRACKPOINT, isH2); - if (JDBCUtilities.tableExists(connection, TableLocation.parse(trackPointsTableName,isH2))) { - throw new SQLException("The table " + trackPointsTableName + " already exists."); - } - setTrkPreparedStmt(GPXTablesFactory.createTrackTable(connection, trackTableName, isH2)); - setTrkSegmentsPreparedStmt(GPXTablesFactory.createTrackSegmentsTable(connection, trackSegmentsTableName, isH2)); - setTrkPointsPreparedStmt(GPXTablesFactory.createTrackPointsTable(connection, trackPointsTableName, isH2)); - tableNames.append(trackTableName).append(",").append(trackSegmentsTableName).append(",").append(trackPointsTableName).append(","); - } - - // Initialisation of the contentHandler by default - try { - setReader(XMLReaderFactory.createXMLReader()); - getReader().setErrorHandler(this); - getReader().setContentHandler(this); - getReader().parse(new InputSource(new FileInputStream(inputFile))); - success = true; - } catch (SAXException ex) { - throw new SQLException(ex); - } catch (IOException ex) { - throw new SQLException("Cannot parse the file " + inputFile.getAbsolutePath(), ex); - } finally { - // When the reading ends, close() method has to be called - if (getWptPreparedStmt() != null) { - getWptPreparedStmt().close(); + TableLocation requestedTable = TableLocation.parse(tableName, isH2); + if (deleteTable) { + GPXTablesFactory.dropOSMTables(connection, isH2, requestedTable); } - if (getRteptPreparedStmt() != null) { - getRtePreparedStmt().close(); - getRteptPreparedStmt().close(); + if (fileName.length() == 0) { + JDBCUtilities.createEmptyTable(connection, requestedTable.toString()); } - if (getTrkPointsPreparedStmt() != null) { - getTrkPreparedStmt().close(); - getTrkSegmentsPreparedStmt().close(); - getTrkPointsPreparedStmt().close(); + else { + String table = requestedTable.getTable(); + clear(); + GpxPreparser gpxPreparser = new GpxPreparser(); + try { + gpxPreparser.read(fileName, encoding); + } catch (SAXException ex) { + throw new SQLException(ex); + } catch (IOException ex) { + throw new SQLException(ex); + } + StringBuilder tableNames = new StringBuilder(); + + // We create the tables to store all gpx data in the database + if (gpxPreparser.getTotalWpt() > 0) { + String wptTableName = TableUtilities.caseIdentifier(requestedTable, table + GPXTablesFactory.WAYPOINT, isH2); + if (JDBCUtilities.tableExists(connection, TableLocation.parse(wptTableName, isH2))) { + throw new SQLException("The table " + wptTableName + " already exists."); + } + setWptPreparedStmt(GPXTablesFactory.createWayPointsTable(connection, wptTableName, isH2)); + tableNames.append(wptTableName).append(","); + } + if (gpxPreparser.getTotalRte() > 0 && gpxPreparser.getTotalRtept() > 0) { + String routeTableName = TableUtilities.caseIdentifier(requestedTable, table + GPXTablesFactory.ROUTE, isH2); + if (JDBCUtilities.tableExists(connection, TableLocation.parse(routeTableName, isH2))) { + throw new SQLException("The table " + routeTableName + " already exists."); + } + String routePointsTableName = TableUtilities.caseIdentifier(requestedTable, table + GPXTablesFactory.ROUTEPOINT, isH2); + if (JDBCUtilities.tableExists(connection, TableLocation.parse(routePointsTableName, isH2))) { + throw new SQLException("The table " + routePointsTableName + " already exists."); + } + setRtePreparedStmt(GPXTablesFactory.createRouteTable(connection, routeTableName, isH2)); + setRteptPreparedStmt(GPXTablesFactory.createRoutePointsTable(connection, routePointsTableName, isH2)); + tableNames.append(routeTableName).append(",").append(routePointsTableName).append(","); + } + + if (gpxPreparser.getTotalTrk() > 0 && gpxPreparser.getTotalTrkseg() > 0 + && gpxPreparser.getTotalTrkpt() > 0) { + String trackTableName = TableUtilities.caseIdentifier(requestedTable, table + GPXTablesFactory.TRACK, isH2); + if (JDBCUtilities.tableExists(connection, TableLocation.parse(trackTableName, isH2))) { + throw new SQLException("The table " + trackTableName + " already exists."); + } + + String trackSegmentsTableName = TableUtilities.caseIdentifier(requestedTable, table + GPXTablesFactory.TRACKSEGMENT, isH2); + if (JDBCUtilities.tableExists(connection, TableLocation.parse(trackSegmentsTableName, isH2))) { + throw new SQLException("The table " + trackSegmentsTableName + " already exists."); + } + + String trackPointsTableName = TableUtilities.caseIdentifier(requestedTable, table + GPXTablesFactory.TRACKPOINT, isH2); + if (JDBCUtilities.tableExists(connection, TableLocation.parse(trackPointsTableName, isH2))) { + throw new SQLException("The table " + trackPointsTableName + " already exists."); + } + setTrkPreparedStmt(GPXTablesFactory.createTrackTable(connection, trackTableName, isH2)); + setTrkSegmentsPreparedStmt(GPXTablesFactory.createTrackSegmentsTable(connection, trackSegmentsTableName, isH2)); + setTrkPointsPreparedStmt(GPXTablesFactory.createTrackPointsTable(connection, trackPointsTableName, isH2)); + tableNames.append(trackTableName).append(",").append(trackSegmentsTableName).append(",").append(trackPointsTableName).append(","); + } + + // Initialisation of the contentHandler by default + try { + setReader(XMLReaderFactory.createXMLReader()); + getReader().setErrorHandler(this); + getReader().setContentHandler(this); + getReader().parse(new InputSource(new FileInputStream(fileName))); + success = true; + } catch (SAXException ex) { + throw new SQLException(ex); + } catch (IOException ex) { + throw new SQLException("Cannot parse the file " + fileName.getAbsolutePath(), ex); + } finally { + // When the reading ends, close() method has to be called + if (getWptPreparedStmt() != null) { + getWptPreparedStmt().close(); + } + if (getRteptPreparedStmt() != null) { + getRtePreparedStmt().close(); + getRteptPreparedStmt().close(); + } + if (getTrkPointsPreparedStmt() != null) { + getTrkPreparedStmt().close(); + getTrkSegmentsPreparedStmt().close(); + getTrkPointsPreparedStmt().close(); + } + } } } + return success; - return success; } /** diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/gpx/model/GPXTablesFactory.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/gpx/model/GPXTablesFactory.java index 1d981f5c1e..75836083db 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/gpx/model/GPXTablesFactory.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/gpx/model/GPXTablesFactory.java @@ -307,17 +307,16 @@ public static PreparedStatement createTrackPointsTable(Connection connection, St * @param tablePrefix * @throws SQLException */ - public static void dropOSMTables(Connection connection, boolean isH2, String tablePrefix) throws SQLException { - TableLocation requestedTable = TableLocation.parse(tablePrefix, isH2); - String gpxTableName = requestedTable.getTable(); + public static void dropOSMTables(Connection connection, boolean isH2, TableLocation tablePrefix) throws SQLException { + String gpxTableName = tablePrefix.toString(); String[] gpxTables = new String[]{WAYPOINT,ROUTE,ROUTEPOINT, TRACK, TRACKPOINT, TRACKSEGMENT}; StringBuilder sb = new StringBuilder("drop table if exists "); String gpxTableSuffix = gpxTables[0]; - String gpxTable = TableUtilities.caseIdentifier(requestedTable, gpxTableName + gpxTableSuffix, isH2); + String gpxTable = TableUtilities.caseIdentifier(tablePrefix, gpxTableName + gpxTableSuffix, isH2); sb.append(gpxTable); for (int i = 1; i < gpxTables.length; i++) { gpxTableSuffix = gpxTables[i]; - gpxTable = TableUtilities.caseIdentifier(requestedTable, gpxTableName + gpxTableSuffix, isH2); + gpxTable = TableUtilities.caseIdentifier(tablePrefix, gpxTableName + gpxTableSuffix, isH2); sb.append(",").append(gpxTable); } try (Statement stmt = connection.createStatement()) { diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/gpx/model/GpxParser.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/gpx/model/GpxParser.java index 7d1cc49ce9..d512225432 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/gpx/model/GpxParser.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/gpx/model/GpxParser.java @@ -25,6 +25,9 @@ import org.xml.sax.Attributes; import org.xml.sax.SAXException; +import java.io.File; +import java.sql.Connection; + /** * Default parser. This class parses GPX 1.1 files and saves them in a * file. It set a contentHandler by default which is able to save general @@ -53,7 +56,8 @@ public class GpxParser extends AbstractGpxParserDefault { * Create a new GPX parser and specify what kind of data must be parsed in * the GPX file */ - public GpxParser() { + public GpxParser(Connection connection, File fileName, String encoding, boolean deleteTable) { + super( connection, fileName, encoding, deleteTable); } /** diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/gpx/model/GpxPreparser.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/gpx/model/GpxPreparser.java index c6094f9048..1c84e5ffaa 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/gpx/model/GpxPreparser.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/gpx/model/GpxPreparser.java @@ -72,13 +72,17 @@ public GpxPreparser() { * @throws SAXException * @throws IOException */ - public boolean read(File inputFile) throws SAXException, IOException { + public boolean read(File inputFile, String encoding) throws SAXException, IOException { boolean success = false; try (FileInputStream fs = new FileInputStream(inputFile)) { XMLReader parser = XMLReaderFactory.createXMLReader(); parser.setErrorHandler(this); parser.setContentHandler(this); - parser.parse(new InputSource(fs)); + InputSource is = new InputSource(fs); + if(encoding!=null && !encoding.isEmpty()) { + is.setEncoding(encoding); + } + parser.parse(is); success = true; } return success; diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/json/JsonDriverFunction.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/json/JsonDriverFunction.java index 5dd4223ced..99910f36b2 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/json/JsonDriverFunction.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/json/JsonDriverFunction.java @@ -65,9 +65,20 @@ public boolean isSpatialFormat(String extension) { @Override public void exportTable(Connection connection, String tableReference, File fileName, ProgressVisitor progress) throws SQLException, IOException { - exportTable(connection,tableReference, fileName, progress,null); + exportTable( connection, tableReference, fileName, null, false,progress); } - + + @Override + public void exportTable(Connection connection, String tableReference, File fileName, boolean deleteFiles, ProgressVisitor progress) throws SQLException, IOException { + exportTable( connection, tableReference, fileName, null, deleteFiles,progress); + } + + @Override + public void exportTable(Connection connection, String tableReference, File fileName, String options, boolean deleteFiles, ProgressVisitor progress) throws SQLException, IOException { + JsonWriteDriver jsonDriver = new JsonWriteDriver(connection, deleteFiles); + jsonDriver.write(progress,tableReference, fileName, options); + } + /** * Save a table or a query to JSON file * @param connection @@ -79,9 +90,8 @@ public void exportTable(Connection connection, String tableReference, File fileN * @throws IOException */ @Override - public void exportTable(Connection connection, String tableReference, File fileName, ProgressVisitor progress, String encoding) throws SQLException, IOException { - JsonWriteDriver jsonDriver = new JsonWriteDriver(connection); - jsonDriver.write(progress,tableReference, fileName, encoding); + public void exportTable(Connection connection, String tableReference, File fileName, String encoding,ProgressVisitor progress) throws SQLException, IOException { + exportTable( connection, tableReference, fileName, encoding, false,progress); } @Override @@ -91,14 +101,19 @@ public void importFile(Connection connection, String tableReference, File fileNa } @Override - public void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress, - String options) throws SQLException, IOException { + public void importFile(Connection connection, String tableReference, File fileName, String options,ProgressVisitor progress + ) throws SQLException, IOException { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void importFile(Connection connection, String tableReference, File fileName, boolean deleteTables,ProgressVisitor progress + ) throws SQLException, IOException { throw new UnsupportedOperationException("Not supported yet."); } @Override - public void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress, - boolean deleteTables) throws SQLException, IOException { + public void importFile(Connection connection, String tableReference, File fileName, String options, boolean deleteTables, ProgressVisitor progress) throws SQLException, IOException { throw new UnsupportedOperationException("Not supported yet."); } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/json/JsonWrite.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/json/JsonWrite.java index e36b6681ca..07591ba7e4 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/json/JsonWrite.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/json/JsonWrite.java @@ -36,7 +36,8 @@ public class JsonWrite extends AbstractFunction implements ScalarFunction{ public JsonWrite(){ - addProperty(PROP_REMARKS, "Export a table to a JSON file.\n As optional argument an encoding value is supported."); + addProperty(PROP_REMARKS, "Export a table to a JSON file." + + "\n As optional arguments encoding value is supported and delete output file."); } @Override @@ -55,8 +56,22 @@ public String getJavaStaticMethod() { * @throws SQLException */ public static void writeGeoJson(Connection connection, String fileName, String tableReference, String encoding) throws IOException, SQLException { - JsonDriverFunction jsonDriver = new JsonDriverFunction(); - jsonDriver.exportTable(connection,tableReference, URIUtilities.fileFromString(fileName), new EmptyProgressVisitor(),encoding); + writeGeoJson( connection, fileName, tableReference, encoding, false); + } + + /** + * + * @param connection + * @param fileName + * @param tableReference + * @param encoding + * @param deleteFile + * @throws IOException + * @throws SQLException + */ + public static void writeGeoJson(Connection connection, String fileName, String tableReference, String encoding, boolean deleteFile) throws IOException, SQLException { + JsonDriverFunction jsonDriver = new JsonDriverFunction(); + jsonDriver.exportTable(connection,tableReference, URIUtilities.fileFromString(fileName),encoding, deleteFile, new EmptyProgressVisitor()); } /** @@ -69,7 +84,20 @@ public static void writeGeoJson(Connection connection, String fileName, String t * @throws SQLException */ public static void writeGeoJson(Connection connection, String fileName, String tableReference) throws IOException, SQLException { - writeGeoJson(connection, fileName, tableReference,null); + writeGeoJson( connection, fileName, tableReference, null, false); + } + + /** + * + * @param connection + * @param fileName + * @param tableReference + * @param deleteFile + * @throws IOException + * @throws SQLException + */ + public static void writeGeoJson(Connection connection, String fileName, String tableReference, boolean deleteFile) throws IOException, SQLException { + writeGeoJson( connection, fileName, tableReference, null, deleteFile); } } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/json/JsonWriteDriver.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/json/JsonWriteDriver.java index 247f4be097..ccc222e107 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/json/JsonWriteDriver.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/json/JsonWriteDriver.java @@ -27,6 +27,7 @@ import org.h2gis.utilities.JDBCUtilities; import java.io.*; +import java.nio.file.Files; import java.sql.*; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -39,14 +40,16 @@ public class JsonWriteDriver { private final Connection connection; + private final boolean deleteFile; /** * A JSON driver to write a table to a JSON file. * * @param connection */ - public JsonWriteDriver(Connection connection) { + public JsonWriteDriver(Connection connection, boolean deleteFile) { this.connection = connection; + this.deleteFile=deleteFile; } /** @@ -261,6 +264,9 @@ public void write(ProgressVisitor progress,String tableName, File fileName, Stri } } else { if (FileUtil.isExtensionWellFormated(fileName, "json")) { + if(deleteFile){ + Files.deleteIfExists(fileName.toPath()); + } FileOutputStream fos = null; try { JsonEncoding jsonEncoding = JsonEncoding.UTF8; diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/kml/KMLDriverFunction.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/kml/KMLDriverFunction.java index 6785ccbead..a4384a2a24 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/kml/KMLDriverFunction.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/kml/KMLDriverFunction.java @@ -65,15 +65,24 @@ public boolean isSpatialFormat(String extension) { @Override public void exportTable(Connection connection, String tableReference, File fileName, ProgressVisitor progress) throws SQLException, IOException { - KMLWriterDriver kMLWriter = new KMLWriterDriver(connection); - kMLWriter.write(progress,tableReference, fileName,null); + exportTable( connection, tableReference, fileName, null, false, progress); } @Override - public void exportTable(Connection connection, String tableReference, File fileName, ProgressVisitor progress, - String encoding) throws SQLException, IOException { - KMLWriterDriver kMLWriter = new KMLWriterDriver(connection); - kMLWriter.write(progress, tableReference, fileName, encoding); + public void exportTable(Connection connection, String tableReference, File fileName, boolean deleteFiles, ProgressVisitor progress) throws SQLException, IOException { + exportTable( connection, tableReference, fileName, null, deleteFiles, progress); + } + + @Override + public void exportTable(Connection connection, String tableReference, File fileName, String options, boolean deleteFiles, ProgressVisitor progress) throws SQLException, IOException { + KMLWriterDriver kMLWriter = new KMLWriterDriver(connection, fileName, options, deleteFiles); + kMLWriter.write(tableReference, progress); + } + + @Override + public void exportTable(Connection connection, String tableReference, File fileName, String encoding, ProgressVisitor progress + ) throws SQLException, IOException { + exportTable( connection, tableReference, fileName, encoding, false, progress); } @Override @@ -83,17 +92,22 @@ public void importFile(Connection connection, String tableReference, File fileNa } @Override - public void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress, - String options) throws SQLException, IOException { + public void importFile(Connection connection, String tableReference, File fileName, String options,ProgressVisitor progress + ) throws SQLException, IOException { throw new UnsupportedOperationException("Not supported yet."); } @Override - public void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress, - boolean deleteTables) throws SQLException, IOException { + public void importFile(Connection connection, String tableReference, File fileName,boolean deleteTables, ProgressVisitor progress + ) throws SQLException, IOException { throw new UnsupportedOperationException("Not supported yet."); } + @Override + public void importFile(Connection connection, String tableReference, File fileName, String options, boolean deleteTables, ProgressVisitor progress) throws SQLException, IOException { + + } + @Override public IMPORT_DRIVER_TYPE getImportDriverType() { return IMPORT_DRIVER_TYPE.COPY; diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/kml/KMLWrite.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/kml/KMLWrite.java index 7eec1ff500..3f7723c8ab 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/kml/KMLWrite.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/kml/KMLWrite.java @@ -32,12 +32,13 @@ /** * SQL Function to export a spatial table to a KML file. * - * @author Erwan Bocher + * @author Erwan Bocher, CNRS */ public class KMLWrite extends AbstractFunction implements ScalarFunction { public KMLWrite() { - addProperty(PROP_REMARKS, "Export a spatial table to a KML or KMZ file."); + addProperty(PROP_REMARKS, "Export a spatial table to a KML or KMZ file.\n" + + "As optional arguments encoding value is supported and delete output file."); } @Override @@ -54,7 +55,18 @@ public String getJavaStaticMethod() { * @throws IOException */ public static void writeKML(Connection connection, String fileName, String tableReference) throws SQLException, IOException { + writeKML( connection, fileName, tableReference, null, false); + } + + public static void writeKML(Connection connection, String fileName, String tableReference, String encoding, boolean deleteFile) throws SQLException, IOException { KMLDriverFunction kMLDriverFunction = new KMLDriverFunction(); - kMLDriverFunction.exportTable(connection, tableReference, URIUtilities.fileFromString(fileName), new EmptyProgressVisitor()); + kMLDriverFunction.exportTable(connection, tableReference, URIUtilities.fileFromString(fileName),encoding,deleteFile, new EmptyProgressVisitor()); + } + public static void writeKML(Connection connection, String fileName, String tableReference,String encoding) throws SQLException, IOException { + writeKML( connection, fileName, tableReference, encoding, false); + } + + public static void writeKML(Connection connection, String fileName, String tableReference,boolean deleteFile) throws SQLException, IOException { + writeKML( connection, fileName, tableReference, null, deleteFile); } } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/kml/KMLWriterDriver.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/kml/KMLWriterDriver.java index 7c9bd51f31..d91f669f00 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/kml/KMLWriterDriver.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/kml/KMLWriterDriver.java @@ -3,7 +3,7 @@ * . H2GIS is developed by CNRS * . * - * This code is part of the H2GIS project. H2GIS is free software; + * This code is part of the H2GIS project. H2GIS is free software; * you can redistribute it and/or modify it under the terms of the GNU * Lesser General Public License as published by the Free Software Foundation; * version 3.0 of the License. @@ -28,6 +28,7 @@ import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; import java.io.*; +import java.nio.file.Files; import java.sql.*; import java.util.HashMap; import java.util.Map; @@ -40,48 +41,77 @@ /** * KML writer - * + * * @author Erwan Bocher */ public class KMLWriterDriver { - + private final Connection connection; + private final File fileName; + private final String encoding; + private final boolean deleteFile; private HashMap kmlFields; private int columnCount = -1; private String tableName; - public KMLWriterDriver(Connection connection) { + public KMLWriterDriver(Connection connection, File fileName, String encoding, boolean deleteFile) { this.connection = connection; + this.fileName = fileName; + this.encoding=encoding; + this.deleteFile=deleteFile; } /** * Write spatial table or sql query to kml or kmz file format. * + * @param tableName the name of table or a select query * @param progress progress monitor - * @param tableName the name of table or a select query - * @param fileName - * @param encoding * @throws SQLException * @throws java.io.IOException */ - public void write(ProgressVisitor progress, String tableName, File fileName, String encoding) throws SQLException, IOException { + public void write( String tableName, ProgressVisitor progress) throws SQLException, IOException { String regex = ".*(?i)\\b(select|from)\\b.*"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(tableName); if (matcher.find()) { if (tableName.startsWith("(") && tableName.endsWith(")")) { - PreparedStatement ps = connection.prepareStatement(tableName, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); - ResultSet resultSet = ps.executeQuery(); - Tuple spatialFieldName = GeometryTableUtilities.getFirstGeometryColumnNameAndIndex(resultSet); - int rowCount = 0; - int type = resultSet.getType(); - if (type == ResultSet.TYPE_SCROLL_INSENSITIVE || type == ResultSet.TYPE_SCROLL_SENSITIVE) { - resultSet.last(); - rowCount = resultSet.getRow(); - resultSet.beforeFirst(); - } - this.tableName = "QUERY_"+System.currentTimeMillis(); - write(progress.subProcess(rowCount), resultSet, spatialFieldName.first(), fileName, encoding); + if (FileUtil.isExtensionWellFormated(fileName, "kml")) { + if(deleteFile){ + Files.deleteIfExists(fileName.toPath()); + } + PreparedStatement ps = connection.prepareStatement(tableName, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); + ResultSet resultSet = ps.executeQuery(); + Tuple spatialFieldName = GeometryTableUtilities.getFirstGeometryColumnNameAndIndex(resultSet); + int rowCount = 0; + int type = resultSet.getType(); + if (type == ResultSet.TYPE_SCROLL_INSENSITIVE || type == ResultSet.TYPE_SCROLL_SENSITIVE) { + resultSet.last(); + rowCount = resultSet.getRow(); + resultSet.beforeFirst(); + } + this.tableName = "QUERY_"+System.currentTimeMillis(); + writeKML(progress.subProcess(rowCount), fileName,resultSet, spatialFieldName.first(), encoding); + }else if (FileUtil.isExtensionWellFormated(fileName, "kmz")) { + if(deleteFile){ + Files.deleteIfExists(fileName.toPath()); + } + PreparedStatement ps = connection.prepareStatement(tableName, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); + ResultSet resultSet = ps.executeQuery(); + Tuple spatialFieldName = GeometryTableUtilities.getFirstGeometryColumnNameAndIndex(resultSet); + int rowCount = 0; + int type = resultSet.getType(); + if (type == ResultSet.TYPE_SCROLL_INSENSITIVE || type == ResultSet.TYPE_SCROLL_SENSITIVE) { + resultSet.last(); + rowCount = resultSet.getRow(); + resultSet.beforeFirst(); + } + this.tableName = "QUERY_"+System.currentTimeMillis(); + String name = fileName.getName(); + int pos = name.lastIndexOf("."); + writeKMZ(progress.subProcess(rowCount), fileName, name.substring(0, pos) + ".kmz", resultSet, spatialFieldName.first(),encoding); + } else { + throw new SQLException("Please use the extensions .kml or kmz."); + } } else { throw new SQLException("The select query must be enclosed in parenthesis: '(SELECT * FROM ORDERS)'."); } @@ -92,33 +122,26 @@ public void write(ProgressVisitor progress, String tableName, File fileName, Str // Read Geometry Index and type Tuple spatialFieldName = GeometryTableUtilities.getFirstGeometryColumnNameAndIndex(resultSet); this.tableName=tableName; - write(progress, resultSet,spatialFieldName.first(), fileName, encoding); - } - } - - /** - * Write a resulset to a kml file - * - * @param progress - * @param rs input resulset - * @param geomField - * @param fileName the output file - * @param encoding - * @throws SQLException - * @throws java.io.IOException - */ - public void write(ProgressVisitor progress, ResultSet rs, String geomField, File fileName, String encoding) throws SQLException, IOException { - if (FileUtil.isExtensionWellFormated(fileName, "kml")) { - writeKML(progress, fileName,rs, geomField, encoding); - } else if (FileUtil.isExtensionWellFormated(fileName, "kmz")) { - String name = fileName.getName(); - int pos = name.lastIndexOf("."); - writeKMZ(progress, fileName, name.substring(0, pos) + ".kml", rs, geomField,encoding); - } else { - throw new SQLException("Please use the extensions .kml or kmz."); + if (FileUtil.isExtensionWellFormated(fileName, "kml")) { + if(deleteFile){ + Files.deleteIfExists(fileName.toPath()); + } + writeKML(progress, fileName,resultSet, spatialFieldName.first(), encoding); + }else if (FileUtil.isExtensionWellFormated(fileName, "kmz")) { + if(deleteFile){ + Files.deleteIfExists(fileName.toPath()); + } + String name = fileName.getName(); + int pos = name.lastIndexOf("."); + writeKMZ(progress, fileName, name.substring(0, pos) + ".kmz", resultSet, spatialFieldName.first(),encoding); + }else { + throw new SQLException("The select query must be enclosed in parenthesis: '(SELECT * FROM ORDERS)'."); + } + } } + /** * Write the spatial table to a KML format * @@ -190,7 +213,7 @@ private void writeKMZ(ProgressVisitor progress,File fileName, String fileNameWit * @param outputStream * @throws SQLException */ - private void writeKMLDocument(ProgressVisitor progress, OutputStream outputStream, ResultSet rs, String geomField, String encoding) throws SQLException { + private void writeKMLDocument(ProgressVisitor progress, OutputStream outputStream, ResultSet rs, String geomField, String encoding) throws SQLException { try { final XMLOutputFactory streamWriterFactory = XMLOutputFactory.newFactory(); streamWriterFactory.setProperty("escapeCharacters", false); @@ -211,7 +234,7 @@ private void writeKMLDocument(ProgressVisitor progress, OutputStream outputStrea xmlOut.writeStartElement("Document"); try { - ResultSetMetaData resultSetMetaData = rs.getMetaData(); + ResultSetMetaData resultSetMetaData = rs.getMetaData(); writeSchema(xmlOut, resultSetMetaData); xmlOut.writeStartElement("Folder"); xmlOut.writeStartElement("name"); @@ -250,7 +273,7 @@ private void writeKMLDocument(ProgressVisitor progress, OutputStream outputStrea * * * @param xmlOut - * @param tableName + * @param metaData */ private void writeSchema(XMLStreamWriter xmlOut, ResultSetMetaData metaData) throws XMLStreamException, SQLException { columnCount = metaData.getColumnCount(); diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/osm/OSMDriverFunction.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/osm/OSMDriverFunction.java index 1fc978948f..3e17d3df20 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/osm/OSMDriverFunction.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/osm/OSMDriverFunction.java @@ -78,21 +78,31 @@ public void exportTable(Connection connection, String tableReference, File fileN } @Override - public void exportTable(Connection connection, String tableReference, File fileName, ProgressVisitor progress, - String options) throws SQLException, IOException { + public void exportTable(Connection connection, String tableReference, File fileName, boolean deleteFiles, ProgressVisitor progress) throws SQLException, IOException { + + } + + @Override + public void exportTable(Connection connection, String tableReference, File fileName, String options, boolean deleteFiles, ProgressVisitor progress) throws SQLException, IOException { + + } + + @Override + public void exportTable(Connection connection, String tableReference, File fileName, + String options, ProgressVisitor progress) throws SQLException, IOException { throw new UnsupportedOperationException("Not supported yet."); } @Override public void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress) throws SQLException, IOException { - importFile(connection, tableReference, fileName, progress, false); + importFile(connection, tableReference, fileName, false, progress); } @Override - public void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress, - String options) throws SQLException, IOException { - importFile(connection, tableReference, fileName, progress, false); + public void importFile(Connection connection, String tableReference, File fileName,String options, ProgressVisitor progress + ) throws SQLException, IOException { + importFile(connection, tableReference, fileName,false, progress); } /** @@ -106,16 +116,15 @@ public void importFile(Connection connection, String tableReference, File fileNa * @throws IOException File read error */ @Override - public void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress, boolean deleteTables) throws SQLException, IOException { - if(fileName == null || !(fileName.getName().endsWith(".osm") || fileName.getName().endsWith("osm.gz") || fileName.getName().endsWith("osm.bz2"))) { - throw new IOException(new IllegalArgumentException("This driver handle only .osm, .osm.gz and .osm.bz2 files")); - } - if(deleteTables){ - OSMTablesFactory.dropOSMTables(connection, JDBCUtilities.isH2DataBase(connection), tableReference); - } - OSMParser osmp = new OSMParser(); - osmp.read(connection, tableReference, fileName, progress); - } + public void importFile(Connection connection, String tableReference, File fileName, boolean deleteTables, ProgressVisitor progress) throws SQLException, IOException { + importFile( connection, tableReference, fileName, null, deleteTables, progress); + } + + @Override + public void importFile(Connection connection, String tableReference, File fileName, String options, boolean deleteTables, ProgressVisitor progress) throws SQLException, IOException { + OSMParser osmp = new OSMParser(connection, fileName, options, deleteTables); + osmp.read(tableReference, progress); + } @Override public String[] getImportFormats() { diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/osm/OSMParser.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/osm/OSMParser.java index 0a295085e6..acc7110dd8 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/osm/OSMParser.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/osm/OSMParser.java @@ -60,7 +60,10 @@ */ public class OSMParser extends DefaultHandler { - + + private final File fileName; + private final String encoding; + private final boolean deleteTable; // Set the same batch size as OSMOSIS private static final int BATCH_SIZE = 8000; private PreparedStatement nodePreparedStmt; @@ -100,35 +103,42 @@ public class OSMParser extends DefaultHandler { private static String TAG_DUPLICATE_EXCEPTION = String.valueOf(ErrorCode.DUPLICATE_KEY_1); private Connection connection; - public OSMParser() { - + public OSMParser(Connection connection, File fileName, String encoding, boolean deleteTable) { + this.connection=connection; + this.fileName=fileName; + this.encoding=encoding; + this.deleteTable=deleteTable; } /** * Read the OSM file and create its corresponding tables. * - * @param inputFile * @param tableName - * @param connection * @param progress * @return * @throws SQLException */ - public boolean read(Connection connection, String tableName, File inputFile, ProgressVisitor progress) throws SQLException { + public boolean read(String tableName, ProgressVisitor progress) throws SQLException { + if(fileName == null || !(fileName.getName().endsWith(".osm") || fileName.getName().endsWith("osm.gz") || fileName.getName().endsWith("osm.bz2"))) { + throw new SQLException(new IllegalArgumentException("This driver handle only .osm, .osm.gz and .osm.bz2 files")); + } + this.progress = progress.subProcess(100); - this.connection=connection; // Initialisation final boolean isH2 = JDBCUtilities.isH2DataBase(connection); boolean success = false; connection.setAutoCommit(false); TableLocation requestedTable = TableLocation.parse(tableName, isH2); + if(deleteTable){ + OSMTablesFactory.dropOSMTables(connection, JDBCUtilities.isH2DataBase(connection), requestedTable.toString()); + } String osmTableName = requestedTable.getTable(); checkOSMTables(connection, isH2, requestedTable, osmTableName); createOSMDatabaseModel(connection, isH2, requestedTable, osmTableName); FileInputStream fs = null; try { - fs = new FileInputStream(inputFile); + fs = new FileInputStream(fileName); this.fc = fs.getChannel(); this.fileSize = fc.size(); if (fileSize > 0) { @@ -139,12 +149,24 @@ public boolean read(Connection connection, String tableName, File inputFile, Pro XMLReader parser = XMLReaderFactory.createXMLReader(); parser.setErrorHandler(this); parser.setContentHandler(this); - if (inputFile.getName().endsWith(".osm")) { - parser.parse(new InputSource(fs)); - } else if (inputFile.getName().endsWith(".osm.gz")) { - parser.parse(new InputSource(new GZIPInputStream(fs))); - } else if (inputFile.getName().endsWith(".osm.bz2")) { - parser.parse(new InputSource(new BZip2CompressorInputStream(fs))); + if (fileName.getName().endsWith(".osm")) { + InputSource is = new InputSource(fs); + if(encoding!=null && !encoding.isEmpty()){ + is.setEncoding(encoding); + } + parser.parse(is); + } else if (fileName.getName().endsWith(".osm.gz")) { + InputSource is = new InputSource(new GZIPInputStream(fs)); + if(encoding!=null && !encoding.isEmpty()){ + is.setEncoding(encoding); + } + parser.parse(is); + } else if (fileName.getName().endsWith(".osm.bz2")) { + InputSource is = new InputSource(new BZip2CompressorInputStream(fs)); + if(encoding!=null && !encoding.isEmpty()){ + is.setEncoding(encoding); + } + parser.parse(is); } else { throw new SQLException("Supported formats are .osm, .osm.gz, .osm.bz2"); } @@ -153,14 +175,14 @@ public boolean read(Connection connection, String tableName, File inputFile, Pro } catch (SAXException ex) { throw new SQLException(ex); } catch (IOException ex) { - throw new SQLException("Cannot parse the file " + inputFile.getAbsolutePath(), ex); + throw new SQLException("Cannot parse the file " + fileName.getAbsolutePath(), ex); } finally { try { if (fs != null) { fs.close(); } } catch (IOException ex) { - throw new SQLException("Cannot close the file " + inputFile.getAbsolutePath(), ex); + throw new SQLException("Cannot close the file " + fileName.getAbsolutePath(), ex); } // When the reading ends, close() method has to be called if (nodePreparedStmt != null) { diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/osm/OSMRead.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/osm/OSMRead.java index 23fbe85e0a..4e39b6744c 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/osm/OSMRead.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/osm/OSMRead.java @@ -71,16 +71,7 @@ public String getJavaStaticMethod() { * @throws SQLException */ public static void readOSM(Connection connection, String fileName, String tableReference, boolean deleteTables) throws FileNotFoundException, SQLException, IOException { - if (deleteTables) { - OSMTablesFactory.dropOSMTables(connection, JDBCUtilities.isH2DataBase(connection), tableReference); - } - File file = URIUtilities.fileFromString(fileName); - if (!file.exists()) { - throw new FileNotFoundException("The following file does not exists:\n" + fileName); - } - OSMDriverFunction osmdf = new OSMDriverFunction(); - osmdf.importFile(connection, tableReference, file, new EmptyProgressVisitor(), deleteTables); - + readOSM(connection, fileName, tableReference, null, deleteTables); } /** @@ -92,7 +83,23 @@ public static void readOSM(Connection connection, String fileName, String tableR * @throws SQLException */ public static void readOSM(Connection connection, String fileName, String tableReference) throws FileNotFoundException, SQLException, IOException { - readOSM(connection, fileName, tableReference, false); + readOSM(connection, fileName, tableReference, null, false); + } + + /** + * + * @param connection + * @param fileName + * @param tableReference + * @param encoding + * @param deleteTables + * @throws FileNotFoundException + * @throws SQLException + * @throws IOException + */ + public static void readOSM(Connection connection, String fileName, String tableReference, String encoding, boolean deleteTables) throws FileNotFoundException, SQLException, IOException { + OSMDriverFunction osmdf = new OSMDriverFunction(); + osmdf.importFile(connection, tableReference, URIUtilities.fileFromString(fileName), encoding, deleteTables,new EmptyProgressVisitor()); } /** @@ -106,7 +113,24 @@ public static void readOSM(Connection connection, String fileName) throws FileNo final String name = URIUtilities.fileFromString(fileName).getName(); String tableName = name.substring(0, name.lastIndexOf(".")).toUpperCase(); if (tableName.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) { - readOSM(connection, fileName, tableName); + readOSM(connection, fileName, tableName, null,false); + } else { + throw new SQLException("The file name contains unsupported characters"); + } + } + + /** + * + * @param connection + * @param fileName + * @throws FileNotFoundException + * @throws SQLException + */ + public static void readOSM(Connection connection, String fileName, boolean deleteTable) throws FileNotFoundException, SQLException, IOException { + final String name = URIUtilities.fileFromString(fileName).getName(); + String tableName = name.substring(0, name.lastIndexOf(".")).toUpperCase(); + if (tableName.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) { + readOSM(connection, fileName, tableName, null,deleteTable); } else { throw new SQLException("The file name contains unsupported characters"); } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/shp/SHPDriverFunction.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/shp/SHPDriverFunction.java index 81377ccee6..0fd3cf4fb5 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/shp/SHPDriverFunction.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/shp/SHPDriverFunction.java @@ -40,6 +40,7 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Files; import java.sql.*; import java.util.ArrayList; import java.util.List; @@ -60,29 +61,36 @@ public class SHPDriverFunction implements DriverFunction { @Override public void exportTable(Connection connection, String tableReference, File fileName, ProgressVisitor progress) throws SQLException, IOException { - exportTable(connection, tableReference, fileName, progress, null); + exportTable(connection, tableReference, fileName, null, progress); } - /** - * Save a table or a query to a shpfile - * @param connection Active connection, do not close this connection. - * @param tableReference [[catalog.]schema.]table reference - * @param fileName File path to write, if exists it may be replaced - * @param progress to display the IO progress - * @param encoding File encoding, null will use default encoding - * @throws SQLException - * @throws IOException - */ @Override - public void exportTable(Connection connection, String tableReference, File fileName, ProgressVisitor progress, String encoding) throws SQLException, IOException { - final boolean isH2 = JDBCUtilities.isH2DataBase(connection); - String regex = ".*(?i)\\b(select|from)\\b.*"; - Pattern pattern = Pattern.compile(regex); - Matcher matcher = pattern.matcher(tableReference); - if (matcher.find()) { - if (tableReference.startsWith("(") && tableReference.endsWith(")")) { - if (FileUtil.isExtensionWellFormated(fileName, "shp")) { + public void exportTable(Connection connection, String tableReference, File fileName, boolean deleteFiles, ProgressVisitor progress) throws SQLException, IOException { + exportTable( connection, tableReference, fileName, null, deleteFiles, progress); + } + + @Override + public void exportTable(Connection connection, String tableReference, File fileName, String options, boolean deleteFiles,ProgressVisitor progress) throws SQLException, IOException { + if (!FileUtil.isExtensionWellFormated(fileName, "shp")) { + throw new SQLException("Only .shp extension is supported"); + } + if(deleteFiles){ + //Delete all shapeFile extensions + String path = fileName.getAbsolutePath(); + String nameWithoutExt = path.substring(0, path.lastIndexOf('.')); + Files.deleteIfExists(fileName.toPath()); + Files.deleteIfExists(new File(nameWithoutExt+".dbf").toPath()); + Files.deleteIfExists(new File(nameWithoutExt+".shx").toPath()); + Files.deleteIfExists(new File(nameWithoutExt+".prj").toPath()); + } + final boolean isH2 = JDBCUtilities.isH2DataBase(connection); + String regex = ".*(?i)\\b(select|from)\\b.*"; + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(tableReference); + if (matcher.find()) { + if (tableReference.startsWith("(") && tableReference.endsWith(")")) { PreparedStatement ps = connection.prepareStatement(tableReference, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); + JDBCUtilities.attachCancelResultSet(ps, progress); ResultSet resultSet = ps.executeQuery(); int recordCount = 0; resultSet.last(); @@ -90,36 +98,45 @@ public void exportTable(Connection connection, String tableReference, File fileN resultSet.beforeFirst(); ProgressVisitor copyProgress = progress.subProcess(recordCount); Tuple spatialFieldNameAndIndex = GeometryTableUtilities.getFirstGeometryColumnNameAndIndex(resultSet); - int srid = doExport(spatialFieldNameAndIndex.second(), resultSet, recordCount, fileName, progress, encoding); + int srid = doExport(spatialFieldNameAndIndex.second(), resultSet, recordCount, fileName, progress, options); String path = fileName.getAbsolutePath(); String nameWithoutExt = path.substring(0, path.lastIndexOf('.')); - PRJUtil.writePRJ(connection, srid, new File(nameWithoutExt + ".prj")); + PRJUtil.writePRJ(connection, srid, new File(nameWithoutExt + ".prj")); copyProgress.endOfProgress(); } else { - throw new SQLException("Only .shp extension is supported"); + throw new SQLException("The select query must be enclosed in parenthesis: '(SELECT * FROM ORDERS)'."); } } else { - throw new SQLException("The select query must be enclosed in parenthesis: '(SELECT * FROM ORDERS)'."); - } - } else { - if (FileUtil.isExtensionWellFormated(fileName, "shp")) { TableLocation location = TableLocation.parse(tableReference, isH2); int recordCount = JDBCUtilities.getRowCount(connection, tableReference); ProgressVisitor copyProgress = progress.subProcess(recordCount); // Read Geometry Index and type Tuple spatialFieldNameAndIndex = GeometryTableUtilities.getFirstGeometryColumnNameAndIndex(connection, TableLocation.parse(tableReference, isH2)); Statement st = connection.createStatement(); + JDBCUtilities.attachCancelResultSet(st, progress); ResultSet rs = st.executeQuery(String.format("select * from %s", location.toString())); - doExport(spatialFieldNameAndIndex.second(), rs, recordCount, fileName, copyProgress, encoding); + doExport(spatialFieldNameAndIndex.second(), rs, recordCount, fileName, copyProgress, options); String path = fileName.getAbsolutePath(); String nameWithoutExt = path.substring(0, path.lastIndexOf('.')); PRJUtil.writePRJ(connection, location, spatialFieldNameAndIndex.first(), new File(nameWithoutExt + ".prj")); copyProgress.endOfProgress(); - - } else { - throw new SQLException("Only .shp extension is supported"); } - } + + } + + /** + * Save a table or a query to a shpfile + * @param connection Active connection, do not close this connection. + * @param tableReference [[catalog.]schema.]table reference + * @param fileName File path to write, if exists it may be replaced + * @param encoding File encoding, null will use default encoding + * @param progress to display the IO progress + * @throws SQLException + * @throws IOException + */ + @Override + public void exportTable(Connection connection, String tableReference, File fileName, String encoding,ProgressVisitor progress) throws SQLException, IOException { + exportTable( connection, tableReference, fileName, encoding, false, progress); } /** @@ -137,7 +154,7 @@ private int doExport(Integer spatialFieldIndex, ResultSet rs, int recordCount, F ArrayList columnIndexes = new ArrayList(); DbaseFileHeader header = DBFDriverFunction.dBaseHeaderFromMetaData(resultSetMetaData, columnIndexes); columnIndexes.add(0, spatialFieldIndex); - if (encoding != null) { + if (encoding != null && !encoding.isEmpty()) { header.setEncoding(encoding); } header.setNumRecords(recordCount); @@ -178,11 +195,9 @@ private int doExport(Integer spatialFieldIndex, ResultSet rs, int recordCount, F rs.close(); } return srid; - } - @Override public String getFormatDescription(String format) { if(format.equalsIgnoreCase("shp")) { @@ -214,7 +229,7 @@ public boolean isSpatialFormat(String extension) { @Override public void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress) throws SQLException, IOException { - importFile(connection, tableReference, fileName, progress, null); + importFile(connection, tableReference, fileName, null, progress); } /** @@ -222,16 +237,35 @@ public void importFile(Connection connection, String tableReference, File fileNa * @param connection Active connection, do not close this connection. * @param tableReference [[catalog.]schema.]table reference * @param fileName File path to read - * @param progress * @param forceEncoding If defined use this encoding instead of the one defined in dbf header. + * @param progress * @throws SQLException Table write error * @throws IOException File read error */ @Override - public void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress,String forceEncoding) throws SQLException, IOException { + public void importFile(Connection connection, String tableReference, File fileName, String forceEncoding, ProgressVisitor progress) throws SQLException, IOException { + importFile(connection, tableReference, fileName, null, false, progress); + } + + @Override + public void importFile(Connection connection, String tableReference, File fileName, boolean deleteTables,ProgressVisitor progress + ) throws SQLException, IOException { + importFile(connection, tableReference, fileName, null, deleteTables, progress); + } + + @Override + public void importFile(Connection connection, String tableReference, File fileName, String options, boolean deleteTables,ProgressVisitor progress) throws SQLException, IOException { final boolean isH2 = JDBCUtilities.isH2DataBase(connection); + if (FileUtil.isFileImportable(fileName, "shp")) { + if(deleteTables) { + TableLocation requestedTable = TableLocation.parse(tableReference, isH2); + String table = requestedTable.getTable(); + Statement stmt = connection.createStatement(); + stmt.execute("DROP TABLE IF EXISTS " + table); + stmt.close(); + } SHPDriver shpDriver = new SHPDriver(); - shpDriver.initDriverFromFile(fileName, forceEncoding); + shpDriver.initDriverFromFile(fileName, options); ProgressVisitor copyProgress = progress.subProcess((int)(shpDriver.getRowCount() / BATCH_MAX_SIZE)); // PostGIS does not show sql String lastSql = ""; @@ -240,9 +274,9 @@ public void importFile(Connection connection, String tableReference, File fileNa ShapefileHeader shpHeader = shpDriver.getShapeFileHeader(); final TableLocation parse; int srid; - try ( - // Build CREATE TABLE sql request - Statement st = connection.createStatement()) { + try ( + // Build CREATE TABLE sql request + Statement st = connection.createStatement()) { String types = DBFDriverFunction.getSQLColumnTypes(dbfHeader, isH2); if(!types.isEmpty()) { types = ", " + types; @@ -266,9 +300,9 @@ public void importFile(Connection connection, String tableReference, File fileNa } } try { - lastSql = String.format("INSERT INTO %s VALUES (DEFAULT, %s )", parse, - DBFDriverFunction.getQuestionMark(dbfHeader.getNumFields() + 1)); - connection.setAutoCommit(false); + lastSql = String.format("INSERT INTO %s VALUES (DEFAULT, %s )", parse, + DBFDriverFunction.getQuestionMark(dbfHeader.getNumFields() + 1)); + connection.setAutoCommit(false); try (PreparedStatement preparedStatement = connection.prepareStatement(lastSql)) { long batchSize = 0; for (int rowId = 0; rowId < shpDriver.getRowCount(); rowId++) { @@ -287,13 +321,12 @@ public void importFile(Connection connection, String tableReference, File fileNa } } if(batchSize > 0) { - preparedStatement.executeBatch(); + preparedStatement.executeBatch(); connection.commit(); } - + connection.setAutoCommit(true); - } - //TODO create spatial index on the_geom ? + } } catch (Exception ex) { connection.createStatement().execute("DROP TABLE IF EXISTS " + tableReference); throw new SQLException(ex.getLocalizedMessage(), ex); @@ -304,21 +337,7 @@ public void importFile(Connection connection, String tableReference, File fileNa shpDriver.close(); copyProgress.endOfProgress(); } - } - - @Override - public void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress, - boolean deleteTables) throws SQLException, IOException { - if(deleteTables) { - final boolean isH2 = JDBCUtilities.isH2DataBase(connection); - TableLocation requestedTable = TableLocation.parse(tableReference, isH2); - String table = requestedTable.getTable(); - Statement stmt = connection.createStatement(); - stmt.execute("DROP TABLE IF EXISTS " + table); - stmt.close(); } - - importFile(connection, tableReference, fileName, progress); } /** diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/shp/SHPRead.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/shp/SHPRead.java index ca675d4868..8e96d293b4 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/shp/SHPRead.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/shp/SHPRead.java @@ -35,6 +35,7 @@ /** * SQL Function to copy Shape File data into a Table. * @author Nicolas Fortin + * @author Erwan Bocher, CNRS */ public class SHPRead extends AbstractFunction implements ScalarFunction { public SHPRead() { @@ -50,18 +51,30 @@ public String getJavaStaticMethod() { * Copy data from Shape File into a new table in specified connection. * @param connection Active connection * @param tableReference [[catalog.]schema.]table reference - * @param fileName File path of the SHP file or URI * @param forceEncoding Use this encoding instead of DBF file header encoding property. + * @param fileName File path of the SHP file or URI * @throws java.io.IOException * @throws java.sql.SQLException */ public static void readShape(Connection connection, String fileName, String tableReference,String forceEncoding) throws IOException, SQLException { + readShape(connection, fileName, tableReference, forceEncoding, false); + } + + /** + * Copy data from Shape File into a new table in specified connection. + * @param connection Active connection + * @param tableReference [[catalog.]schema.]table reference + * @param forceEncoding Use this encoding instead of DBF file header encoding property. + * @param fileName File path of the SHP file or URI + * @param deleteTables delete existing tables + * @throws java.io.IOException + * @throws java.sql.SQLException + */ + public static void readShape(Connection connection, String fileName, String tableReference,String forceEncoding, boolean deleteTables) throws IOException, SQLException { File file = URIUtilities.fileFromString(fileName); - if (FileUtil.isFileImportable(file, "shp")) { - SHPDriverFunction shpDriverFunction = new SHPDriverFunction(); - shpDriverFunction.importFile(connection, TableLocation.parse(tableReference, true).toString(true), - file, new EmptyProgressVisitor(), forceEncoding); - } + SHPDriverFunction shpDriverFunction = new SHPDriverFunction(); + shpDriverFunction.importFile(connection, TableLocation.parse(tableReference, true).toString(true), + file, forceEncoding,deleteTables, new EmptyProgressVisitor()); } /** @@ -73,7 +86,21 @@ public static void readShape(Connection connection, String fileName, String tabl * @throws java.sql.SQLException */ public static void readShape(Connection connection, String fileName, String tableReference) throws IOException, SQLException { - readShape(connection, fileName, tableReference, null); + readShape(connection, fileName, tableReference, null, false); + } + + public static void readShape(Connection connection, String fileName, String tableReference, boolean deleteTable) throws IOException, SQLException { + readShape(connection, fileName, tableReference, null, deleteTable); + } + + public static void readShape(Connection connection, String fileName, boolean deleteTable) throws IOException, SQLException { + final String name = URIUtilities.fileFromString(fileName).getName(); + String tableName = name.substring(0, name.lastIndexOf(".")).toUpperCase(); + if (tableName.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) { + readShape(connection, fileName, tableName, null, deleteTable); + } else { + throw new SQLException("The file name contains unsupported characters"); + } } /** @@ -91,7 +118,7 @@ public static void readShape(Connection connection, String fileName) throws IOEx final String name = URIUtilities.fileFromString(fileName).getName(); String tableName = name.substring(0, name.lastIndexOf(".")).toUpperCase(); if (tableName.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) { - readShape(connection, fileName, tableName); + readShape(connection, fileName, tableName, null, false); } else { throw new SQLException("The file name contains unsupported characters"); } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/shp/SHPWrite.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/shp/SHPWrite.java index 9721aa0e2a..d96208173d 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/shp/SHPWrite.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/shp/SHPWrite.java @@ -32,16 +32,18 @@ /** * SQL Function to read a table and write it into a shape file. * @author Nicolas Fortin + * @author Erwan Bocher, CNRS */ public class SHPWrite extends AbstractFunction implements ScalarFunction { public SHPWrite() { - addProperty(PROP_REMARKS, "Transfer the content of a table into a new shape file\nCALL SHPWRITE('FILENAME', 'TABLE'[,'ENCODING'])"); + addProperty(PROP_REMARKS, "Transfer the content of a table into a new shape file\nCALL SHPWRITE('FILENAME', 'TABLE'[,'ENCODING', true " + + "\n(to delete output file if exists)])"); } @Override public String getJavaStaticMethod() { - return "exportTable"; //To change body of implemented methods use File | Settings | File Templates. + return "exportTable"; } /** @@ -53,7 +55,7 @@ public String getJavaStaticMethod() { * @throws SQLException */ public static void exportTable(Connection connection, String fileName, String tableReference) throws IOException, SQLException { - exportTable(connection, fileName, tableReference, null); + exportTable(connection, fileName, tableReference, null, false); } /** @@ -67,8 +69,16 @@ public static void exportTable(Connection connection, String fileName, String ta * @throws SQLException */ public static void exportTable(Connection connection, String fileName, String tableReference, String encoding) throws IOException, SQLException { + exportTable( connection, fileName, tableReference, encoding, false); + } + + public static void exportTable(Connection connection, String fileName, String tableReference, String encoding, boolean deleteFiles) throws IOException, SQLException { SHPDriverFunction shpDriverFunction = new SHPDriverFunction(); - shpDriverFunction.exportTable(connection, tableReference, URIUtilities.fileFromString(fileName), new EmptyProgressVisitor(), encoding); + shpDriverFunction.exportTable(connection, tableReference, URIUtilities.fileFromString(fileName), encoding, deleteFiles, new EmptyProgressVisitor()); } -} + public static void exportTable(Connection connection, String fileName, String tableReference, boolean deleteFiles) throws IOException, SQLException { + exportTable( connection, fileName, tableReference, null, deleteFiles); + } + + } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVDriverFunction.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVDriverFunction.java index 0c9b083a51..45f3b53e92 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVDriverFunction.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVDriverFunction.java @@ -31,6 +31,7 @@ import java.io.*; import java.nio.channels.FileChannel; +import java.nio.file.Files; import java.sql.*; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -58,7 +59,7 @@ public class TSVDriverFunction implements DriverFunction{ public static String DESCRIPTION = "TSV file (Tab Separated Values)"; - private static final int BATCH_MAX_SIZE = 100; + private static final int BATCH_MAX_SIZE = 200; @Override @@ -92,31 +93,28 @@ public boolean isSpatialFormat(String extension) { @Override public void exportTable(Connection connection, String tableReference, File fileName, ProgressVisitor progress) throws SQLException, IOException { - exportTable(connection, tableReference, fileName, progress, null); + exportTable( connection, tableReference, fileName, null, false, progress); } - - /** - * Export a table or a query to as TSV file - * - * @param connection Active connection, do not close this connection. - * @param tableReference [[catalog.]schema.]table reference - * @param fileName File path to read - * @param progress - * @param encoding - * @throws SQLException - * @throws IOException - */ + @Override - public void exportTable(Connection connection, String tableReference, File fileName, ProgressVisitor progress, String encoding) throws SQLException, IOException { + public void exportTable(Connection connection, String tableReference, File fileName, boolean deleteFiles, ProgressVisitor progress) throws SQLException, IOException { + exportTable( connection, tableReference, fileName, null, deleteFiles, progress); + } + + @Override + public void exportTable(Connection connection, String tableReference, File fileName, String encoding, boolean deleteFiles, ProgressVisitor progress) throws SQLException, IOException { String regex = ".*(?i)\\b(select|from)\\b.*"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(tableReference); if (matcher.find()) { if (tableReference.startsWith("(") && tableReference.endsWith(")")) { if (FileUtil.isExtensionWellFormated(fileName, "tsv")) { + if(deleteFiles){ + Files.deleteIfExists(fileName.toPath()); + } try (Statement st = connection.createStatement()) { JDBCUtilities.attachCancelResultSet(st, progress); - exportFromResultSet(connection, st.executeQuery(tableReference), fileName, new EmptyProgressVisitor(), encoding); + exportFromResultSet(connection, st.executeQuery(tableReference), fileName, encoding, new EmptyProgressVisitor()); } } else { throw new SQLException("Only .tsv extension is supported"); @@ -127,16 +125,35 @@ public void exportTable(Connection connection, String tableReference, File fileN } } else { if (FileUtil.isExtensionWellFormated(fileName, "tsv")) { + if(deleteFiles){ + Files.deleteIfExists(fileName.toPath()); + } final boolean isH2 = JDBCUtilities.isH2DataBase(connection); TableLocation location = TableLocation.parse(tableReference, isH2); try (Statement st = connection.createStatement()) { JDBCUtilities.attachCancelResultSet(st, progress); - exportFromResultSet(connection, st.executeQuery("SELECT * FROM " + location.toString()), fileName,new EmptyProgressVisitor(),encoding); + exportFromResultSet(connection, st.executeQuery("SELECT * FROM " + location.toString()), fileName,encoding,new EmptyProgressVisitor()); } } else { throw new SQLException("Only .tsv extension is supported"); } - } + } + } + + /** + * Export a table or a query to as TSV file + * + * @param connection Active connection, do not close this connection. + * @param tableReference [[catalog.]schema.]table reference + * @param fileName File path to read + * @param progress + * @param encoding + * @throws SQLException + * @throws IOException + */ + @Override + public void exportTable(Connection connection, String tableReference, File fileName, String encoding, ProgressVisitor progress) throws SQLException, IOException { + exportTable( connection, tableReference, fileName, encoding, false, progress); } /** @@ -149,7 +166,7 @@ public void exportTable(Connection connection, String tableReference, File fileN * @param encoding * @throws java.sql.SQLException */ - public void exportFromResultSet(Connection connection, ResultSet res, File fileName, ProgressVisitor progress, String encoding) throws SQLException { + public void exportFromResultSet(Connection connection, ResultSet res, File fileName, String encoding, ProgressVisitor progress) throws SQLException { Csv csv = new Csv(); String csvOptions = "charset=UTF-8 fieldSeparator=\t fieldDelimiter=\t"; if (encoding != null) { @@ -161,20 +178,42 @@ public void exportFromResultSet(Connection connection, ResultSet res, File fileN @Override public void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress) throws SQLException, IOException { + importFile( connection, tableReference, fileName, null, false, progress); + } + + @Override + public void importFile(Connection connection, String tableReference, File fileName, + String options, ProgressVisitor progress) throws SQLException, IOException { + importFile( connection, tableReference, fileName, options, false, progress); + } + + @Override + public void importFile(Connection connection, String tableReference, File fileName, + boolean deleteTables, ProgressVisitor progress) throws SQLException, IOException { + importFile( connection, tableReference, fileName, null, deleteTables, progress); + } + + @Override + public void importFile(Connection connection, String tableReference, File fileName, String options, boolean deleteTables, ProgressVisitor progress) throws SQLException, IOException { if (FileUtil.isFileImportable(fileName, "tsv")) { final boolean isH2 = JDBCUtilities.isH2DataBase(connection); TableLocation requestedTable = TableLocation.parse(tableReference, isH2); + if(deleteTables){ + Statement stmt = connection.createStatement(); + stmt.execute("DROP TABLE IF EXISTS " + requestedTable); + stmt.close(); + } String table = requestedTable.getTable(); - + int AVERAGE_NODE_SIZE = 500; FileInputStream fis = new FileInputStream(fileName); FileChannel fc = fis.getChannel(); long fileSize = fc.size(); // Given the file size and an average node file size. // Skip how many nodes in order to update progression at a step of 1% - long readFileSizeEachNode = Math.max(1, (fileSize / AVERAGE_NODE_SIZE) / 100); + long readFileSizeEachNode = Math.max(1, (fileSize / AVERAGE_NODE_SIZE) / 100); int average_row_size = 0; - + Csv csv = new Csv(); csv.setFieldDelimiter('\t'); csv.setFieldSeparatorRead('\t'); @@ -209,7 +248,7 @@ public void importFile(Connection connection, String tableReference, File fileNa if (progress.isCanceled()) { throw new SQLException("Canceled by user"); } - + for (int i = 0; i < columnCount; i++) { pst.setString(i + 1, reader.getString(i + 1)); } @@ -220,7 +259,7 @@ public void importFile(Connection connection, String tableReference, File fileNa pst.clearBatch(); batchSize = 0; } - + if (average_row_size++ % readFileSizeEachNode == 0) { // Update Progress try { @@ -238,25 +277,4 @@ public void importFile(Connection connection, String tableReference, File fileNa } } } - - @Override - public void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress, - String options) throws SQLException, IOException { - importFile(connection, tableReference, fileName, progress); - } - - @Override - public void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress, - boolean deleteTables) throws SQLException, IOException { - if(deleteTables) { - final boolean isH2 = JDBCUtilities.isH2DataBase(connection); - TableLocation requestedTable = TableLocation.parse(tableReference, isH2); - String table = requestedTable.getTable(); - Statement stmt = connection.createStatement(); - stmt.execute("DROP TABLE IF EXISTS " + table); - stmt.close(); - } - - importFile(connection, tableReference, fileName, progress); - } } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVRead.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVRead.java index bf581970cf..ab637ac205 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVRead.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVRead.java @@ -58,11 +58,26 @@ public String getJavaStaticMethod() { * @throws IOException */ public static void readTSV(Connection connection, String fileName, String tableReference) throws SQLException, FileNotFoundException, IOException { - File file = URIUtilities.fileFromString(fileName); - if (FileUtil.isFileImportable(file, "tsv")) { - TSVDriverFunction tsvDriver = new TSVDriverFunction(); - tsvDriver.importFile(connection, tableReference, file, new EmptyProgressVisitor()); - } + readTSV( connection, fileName, tableReference,null, false); + } + + public static void readTSV(Connection connection, String fileName, String tableReference, String encoding) throws SQLException, FileNotFoundException, IOException { + readTSV( connection, fileName, tableReference,encoding, false); + } + /** + * + * @param connection + * @param fileName + * @param tableReference + * @param encoding + * @param deleteTable + * @throws SQLException + * @throws FileNotFoundException + * @throws IOException + */ + public static void readTSV(Connection connection, String fileName, String tableReference, String encoding, boolean deleteTable) throws SQLException, FileNotFoundException, IOException { + TSVDriverFunction tsvDriver = new TSVDriverFunction(); + tsvDriver.importFile(connection, tableReference, URIUtilities.fileFromString(fileName),encoding,deleteTable, new EmptyProgressVisitor()); } /** @@ -76,7 +91,25 @@ public static void readTSV(Connection connection, String fileName) throws IOExce final String name = URIUtilities.fileFromString(fileName).getName(); String tableName = name.substring(0, name.lastIndexOf(".")).toUpperCase(); if (tableName.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) { - readTSV(connection, fileName, tableName); + readTSV(connection, fileName, tableName, null, false); + } else { + throw new SQLException("The file name contains unsupported characters"); + } + } + + /** + * + * @param connection + * @param fileName + * @param deleteTable + * @throws IOException + * @throws SQLException + */ + public static void readTSV(Connection connection, String fileName, boolean deleteTable) throws IOException, SQLException { + final String name = URIUtilities.fileFromString(fileName).getName(); + String tableName = name.substring(0, name.lastIndexOf(".")).toUpperCase(); + if (tableName.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) { + readTSV(connection, fileName, tableName, null, deleteTable); } else { throw new SQLException("The file name contains unsupported characters"); } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVWrite.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVWrite.java index 2055f06e9e..b73a6733a2 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVWrite.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVWrite.java @@ -55,9 +55,13 @@ public String getJavaStaticMethod() { * @throws IOException */ public static void writeTSV(Connection connection, String fileName, String tableReference) throws SQLException, IOException { - writeTSV(connection, fileName, tableReference, null); + writeTSV( connection, fileName, tableReference, null, false); } - + + public static void writeTSV(Connection connection, String fileName, String tableReference, boolean deleteFile) throws SQLException, IOException { + writeTSV( connection, fileName, tableReference, null, deleteFile); + } + /** * Export a table into a Tab-separated values file * @@ -68,9 +72,20 @@ public static void writeTSV(Connection connection, String fileName, String table * @throws SQLException * @throws IOException */ - public static void writeTSV(Connection connection, String fileName, String tableReference, String encoding) throws SQLException, IOException { - TSVDriverFunction tSVDriverFunction = new TSVDriverFunction(); - tSVDriverFunction.exportTable(connection, tableReference, URIUtilities.fileFromString(fileName), new EmptyProgressVisitor(), encoding); + public static void writeTSV(Connection connection, String fileName, String tableReference, String encoding) throws SQLException, IOException { + writeTSV( connection, fileName, tableReference, encoding, false); } + /** + * @param connection + * @param fileName + * @param tableReference + * @param encoding + * @throws SQLException + * @throws IOException + */ + public static void writeTSV(Connection connection, String fileName, String tableReference, String encoding, boolean deleteFile) throws SQLException, IOException { + TSVDriverFunction tSVDriverFunction = new TSVDriverFunction(); + tSVDriverFunction.exportTable(connection, tableReference, URIUtilities.fileFromString(fileName), encoding, deleteFile, new EmptyProgressVisitor()); + } } diff --git a/h2gis-functions/src/test/java/org/h2gis/functions/io/shp/SHPImportExportTest.java b/h2gis-functions/src/test/java/org/h2gis/functions/io/shp/SHPImportExportTest.java index f528622434..25b423898d 100644 --- a/h2gis-functions/src/test/java/org/h2gis/functions/io/shp/SHPImportExportTest.java +++ b/h2gis-functions/src/test/java/org/h2gis/functions/io/shp/SHPImportExportTest.java @@ -757,7 +757,6 @@ public void testSelectWriteRead() throws Exception { } } - @Disabled @Test public void exportImportPointPostGIS() throws SQLException, IOException { @@ -780,8 +779,7 @@ public void exportImportPointPostGIS() throws SQLException, IOException { // Read this shape file to check values assertTrue(shpFile.exists()); stat.execute("DROP TABLE IF EXISTS IMPORT_PUNCTUAL;"); - driver.importFile(con, "IMPORT_PUNCTUAL", shpFile, new EmptyProgressVisitor(), - true); + driver.importFile(con, "IMPORT_PUNCTUAL", shpFile, true, new EmptyProgressVisitor()); ResultSet res = stat.executeQuery("SELECT THE_GEOM FROM IMPORT_PUNCTUAL;"); res.next(); Geometry geom = (Geometry) res.getObject(1); diff --git a/h2gis-utilities/src/main/java/org/h2gis/utilities/JDBCUtilities.java b/h2gis-utilities/src/main/java/org/h2gis/utilities/JDBCUtilities.java index cfb929e68d..f925ed0778 100644 --- a/h2gis-utilities/src/main/java/org/h2gis/utilities/JDBCUtilities.java +++ b/h2gis-utilities/src/main/java/org/h2gis/utilities/JDBCUtilities.java @@ -588,6 +588,7 @@ public static Connection wrapConnection(Connection connection) { } } + /** * * @param st Statement to cancel From 96d77a955fe6e755df70b38077edb77e6c880580 Mon Sep 17 00:00:00 2001 From: ebocher Date: Mon, 8 Jun 2020 11:21:12 +0200 Subject: [PATCH 02/10] Add supports to zip files Align all io functions documentation Add tests --- .../org/h2gis/functions/io/DriverManager.java | 29 +- .../functions/io/asc/AscDriverFunction.java | 22 +- .../org/h2gis/functions/io/asc/AscRead.java | 147 +++++- .../functions/io/asc/AscReaderDriver.java | 201 ++++++-- .../functions/io/csv/CSVDriverFunction.java | 7 +- .../functions/io/dbf/DBFDriverFunction.java | 12 +- .../org/h2gis/functions/io/dbf/DBFRead.java | 59 ++- .../org/h2gis/functions/io/dbf/DBFWrite.java | 55 ++- .../functions/io/geojson/GeoJsonRead.java | 70 +-- .../io/geojson/GeoJsonReaderDriver.java | 98 ++-- .../functions/io/geojson/GeoJsonWrite.java | 64 ++- .../io/geojson/GeoJsonWriteDriver.java | 341 +++++++++----- .../org/h2gis/functions/io/gpx/GPXRead.java | 76 +-- .../h2gis/functions/io/json/JsonWrite.java | 56 ++- .../functions/io/json/JsonWriteDriver.java | 443 +++++++++++------- .../org/h2gis/functions/io/kml/KMLWrite.java | 46 +- .../org/h2gis/functions/io/osm/OSMRead.java | 79 ++-- .../org/h2gis/functions/io/shp/SHPRead.java | 66 ++- .../org/h2gis/functions/io/shp/SHPWrite.java | 44 +- .../functions/io/tsv/TSVDriverFunction.java | 171 ++++++- .../org/h2gis/functions/io/tsv/TSVRead.java | 69 +-- .../org/h2gis/functions/io/tsv/TSVWrite.java | 51 +- .../functions/io/asc/AscReaderDriverTest.java | 111 +++-- .../h2gis/functions/io/cvs/CSVDriverTest.java | 10 +- .../io/geojson/GeojsonImportExportTest.java | 44 +- .../io/json/JsonImportExportTest.java | 25 +- .../functions/io/shp/SHPImportExportTest.java | 22 + .../h2gis/functions/io/tsv/TSVDriverTest.java | 16 + .../functions/io/asc/precip30min.asc.aux.xml | 10 + .../io/asc/precip30min_center.asc.aux.xml | 13 + 30 files changed, 1736 insertions(+), 721 deletions(-) create mode 100644 h2gis-functions/src/test/resources/org/h2gis/functions/io/asc/precip30min.asc.aux.xml create mode 100644 h2gis-functions/src/test/resources/org/h2gis/functions/io/asc/precip30min_center.asc.aux.xml diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/DriverManager.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/DriverManager.java index 0c98fc2b31..eeb3ddae48 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/DriverManager.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/DriverManager.java @@ -136,8 +136,20 @@ public void exportTable(Connection connection, String tableReference, File fileN } @Override - public void exportTable(Connection connection, String tableReference, File fileName, ProgressVisitor progress, - String options) throws SQLException, IOException { + public void exportTable(Connection connection, String tableReference, File fileName, boolean deleteFiles, ProgressVisitor progress) throws SQLException, IOException { + + throw new SQLFeatureNotSupportedException("Work in progress.."); + } + + @Override + public void exportTable(Connection connection, String tableReference, File fileName, String options, boolean deleteFiles, ProgressVisitor progress) throws SQLException, IOException { + + throw new SQLFeatureNotSupportedException("Work in progress.."); + } + + @Override + public void exportTable(Connection connection, String tableReference, File fileName, String options, ProgressVisitor progress + ) throws SQLException, IOException { throw new SQLFeatureNotSupportedException("Work in progress.."); } @@ -167,14 +179,19 @@ public void importFile(Connection connection, String tableReference, File fileNa } @Override - public void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress, - String options) throws SQLException, IOException { + public void importFile(Connection connection, String tableReference, File fileName, String options, ProgressVisitor progress + ) throws SQLException, IOException { + openFile(connection, fileName.getAbsolutePath(), tableReference); + } + + @Override + public void importFile(Connection connection, String tableReference, File fileName,boolean deleteTables, ProgressVisitor progress + ) throws SQLException, IOException { openFile(connection, fileName.getAbsolutePath(), tableReference); } @Override - public void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress, - boolean deleteTables) throws SQLException, IOException { + public void importFile(Connection connection, String tableReference, File fileName, String options, boolean deleteTables, ProgressVisitor progress) throws SQLException, IOException { openFile(connection, fileName.getAbsolutePath(), tableReference); } } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/asc/AscDriverFunction.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/asc/AscDriverFunction.java index 173cb40056..b40e0d9fe6 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/asc/AscDriverFunction.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/asc/AscDriverFunction.java @@ -71,22 +71,22 @@ public boolean isSpatialFormat(String extension) { @Override public void exportTable(Connection connection, String tableReference, File fileName, ProgressVisitor progress) throws SQLException, IOException{ - // Import only driver + throw new UnsupportedOperationException("Not supported yet."); } @Override public void exportTable(Connection connection, String tableReference, File fileName, boolean deleteFiles, ProgressVisitor progress) throws SQLException, IOException { - + throw new UnsupportedOperationException("Not supported yet."); } @Override public void exportTable(Connection connection, String tableReference, File fileName, String options, boolean deleteFiles, ProgressVisitor progress) throws SQLException, IOException { - + throw new UnsupportedOperationException("Not supported yet."); } @Override public void exportTable(Connection connection, String tableReference, File fileName, String encoding,ProgressVisitor progress) throws SQLException, IOException{ - // Import only driver + throw new UnsupportedOperationException("Not supported yet."); } @Override @@ -106,9 +106,8 @@ public void importFile(Connection connection, String tableReference, File fileNa if(prjFile.exists()) { srid = PRJUtil.getSRID(prjFile); } - try(FileInputStream fos = new FileInputStream(fileName)) { - ascReaderDriver.read(connection, fos, progress, tableReference, srid); - } + ascReaderDriver.read(connection, fileName, progress, tableReference, srid); + } @Override @@ -120,7 +119,6 @@ public void importFile(Connection connection, String tableReference, File fileNa @Override public void importFile(Connection connection, String tableReference, File fileName, boolean deleteTables, ProgressVisitor progress ) throws SQLException, IOException { - if(deleteTables) { final boolean isH2 = JDBCUtilities.isH2DataBase(connection); TableLocation requestedTable = TableLocation.parse(tableReference, isH2); @@ -128,12 +126,14 @@ public void importFile(Connection connection, String tableReference, File fileNa stmt.execute("DROP TABLE IF EXISTS " + requestedTable); stmt.close(); } - importFile(connection, tableReference, fileName, progress); } @Override - public void importFile(Connection connection, String tableReference, File fileName, String options, boolean deleteTables, ProgressVisitor progress) throws SQLException, IOException { - + public void importFile(Connection connection, String tableReference, File fileName, String encoding, boolean deleteTables, ProgressVisitor progress) throws SQLException, IOException { + AscReaderDriver ascReaderDriver = new AscReaderDriver(); + ascReaderDriver.setDeleteTable(deleteTables); + ascReaderDriver.setEncoding(encoding); + importFile(connection, tableReference, fileName, progress, ascReaderDriver); } } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/asc/AscRead.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/asc/AscRead.java index ac7c57ac1f..f41bbe7ff0 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/asc/AscRead.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/asc/AscRead.java @@ -21,6 +21,7 @@ package org.h2gis.functions.io.asc; +import org.h2.value.*; import org.h2gis.api.AbstractFunction; import org.h2gis.api.EmptyProgressVisitor; import org.h2gis.api.ScalarFunction; @@ -43,11 +44,16 @@ public AscRead() { addProperty(PROP_REMARKS, "Import ESRI ASCII Raster file as point geometries\n" + "Pixels are converted into PointZ with Z as the pixel value\n"+ "CALL ASCREAD('dem.asc');\n" + + "CALL ASCREAD('dem.asc',TYPE);\n" + + "TYPE of z data 1 for integer, 2 for double (default 2)\n"+ "CALL ASCREAD('dem.asc', 'MYTABLE');\n" + + "CALL ASCREAD('dem.asc', 'MYTABLE', TYPE);\n" + + "TYPE of z data 1 for integer, 2 for double (default 2)"+ "CALL ASCREAD('dem.asc', 'MYTABLE', GEOM_FILTER, DOWNSCALE_INT, AS_POINTS);\n" + "GEOM_FILTER - Extract only pixels that intersects the provided geometry envelope, null to disable filter\n" + "DOWNSCALE_INT - Coefficient used for exporting less cells (1 all cells, 2 for size / 2)\n" + - "AS_POLYGONS - If true pixels are converted to polygons. (default false)"); + "AS_POINTS - If true pixels are converted to polygons. (default false return points)\n" + + "CALL ASCREAD('dem.asc', 'MYTABLE', GEOM_FILTER, DOWNSCALE_INT, AS_POINTS);\n"); } @Override @@ -56,7 +62,8 @@ public String getJavaStaticMethod() { } /** - * + * Read the ASCII file. + * * @param connection * @param fileName * @throws IOException @@ -66,25 +73,95 @@ public static void readAscii(Connection connection, String fileName) throws IOEx final String name = URIUtilities.fileFromString(fileName).getName(); String tableName = name.substring(0, name.lastIndexOf(".")).toUpperCase(); if (tableName.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) { - readAscii(connection, fileName, tableName); + readAscii(connection, fileName, ValueVarchar.get(tableName)); } else { throw new SQLException("The file name contains unsupported characters"); } } /** - * Read the GeoJSON file. + * Read the ASCII file. * * @param connection * @param fileName - * @param tableReference + * @param option * @throws IOException * @throws SQLException */ - public static void readAscii(Connection connection, String fileName, String tableReference) throws IOException, SQLException { + public static void readAscii(Connection connection, String fileName, Value option) throws IOException, SQLException { + int zType =2; + String tableReference = null; + boolean deletTable = false; + Geometry envelope=null; + if(option instanceof ValueInteger){ + zType = option.getInt(); + if(zType!=1 || zType!=2){ + throw new SQLException("Please use 1 for integer or 2 for double conversion"); + } + }else if (option instanceof ValueVarchar){ + tableReference = option.getString(); + } else if (option instanceof ValueBoolean){ + deletTable = option.getBoolean(); + }else if (option instanceof ValueGeometry){ + envelope = ((ValueGeometry) option).getGeometry(); + } + else if (!(option instanceof ValueNull)){ + throw new SQLException("Supported optional parameter is integer for z type or varchar for table name"); + } + if(tableReference==null){ + final String name = URIUtilities.fileFromString(fileName).getName(); + String tableName = name.substring(0, name.lastIndexOf(".")).toUpperCase(); + if (tableName.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) { + tableReference = tableName; + } else { + throw new SQLException("The file name contains unsupported characters"); + }} + AscDriverFunction ascReaderFunction = new AscDriverFunction(); + AscReaderDriver ascReaderDriver = new AscReaderDriver(); + if(envelope != null && !envelope.isEmpty()) { + ascReaderDriver.setExtractEnvelope(envelope.getEnvelopeInternal()); + } + + ascReaderDriver.setZType(zType); + ascReaderDriver.setDeleteTable(deletTable); + ascReaderFunction.importFile(connection, tableReference, URIUtilities.fileFromString(fileName), new EmptyProgressVisitor(),ascReaderDriver); + } + + /** + * Read the ASCII file. + * + * @param connection + * @param fileName + * @param tableReference + * @param option + * @throws IOException + * @throws SQLException + */ + public static void readAscii(Connection connection, String fileName, String tableReference, Value option) throws IOException, SQLException { + int zType =2; + boolean deletTable = false; + Geometry envelope=null; + if(option instanceof ValueInteger){ + zType = option.getInt(); + if(zType!=1 || zType!=2){ + throw new SQLException("Please use 1 for integer or 2 for double conversion"); + } + } else if (option instanceof ValueBoolean){ + deletTable = option.getBoolean(); + }else if (option instanceof ValueGeometry){ + envelope = ((ValueGeometry) option).getGeometry(); + } + else if (!(option instanceof ValueNull)){ + throw new SQLException("Supported optional parameter is integer for z type or varchar for table name"); + } AscDriverFunction ascReaderFunction = new AscDriverFunction(); AscReaderDriver ascReaderDriver = new AscReaderDriver(); + if(envelope != null && !envelope.isEmpty()) { + ascReaderDriver.setExtractEnvelope(envelope.getEnvelopeInternal()); + } ascReaderDriver.setAs3DPoint(true); + ascReaderDriver.setZType(zType); + ascReaderDriver.setDeleteTable(deletTable); ascReaderFunction.importFile(connection, tableReference, URIUtilities.fileFromString(fileName), new EmptyProgressVisitor(),ascReaderDriver); } @@ -113,4 +190,62 @@ public static void readAscii(Connection connection, String fileName, String tabl } ascReaderFunction.importFile(connection, tableReference, URIUtilities.fileFromString(fileName), new EmptyProgressVisitor(), ascReaderDriver); } + + /** + * Import a small subset of ASC file. + * @param connection + * @param fileName + * @param tableReference + * @param envelope Extract only pixels that intersects the provided geometry envelope, null to disable filter + * @param downScale Coefficient used for exporting less cells (1 all cells, 2 for size / 2) + * @param extractAsPolygons If true pixels are converted to polygon. (default false) + * @throws IOException + * @throws SQLException + */ + public static void readAscii(Connection connection, String fileName, String tableReference, Geometry envelope, int downScale, boolean extractAsPolygons, boolean deleteTable) throws IOException, SQLException { + AscDriverFunction ascReaderFunction = new AscDriverFunction(); + AscReaderDriver ascReaderDriver = new AscReaderDriver(); + if(envelope != null && !envelope.isEmpty()) { + ascReaderDriver.setExtractEnvelope(envelope.getEnvelopeInternal()); + } + if(downScale > 1) { + ascReaderDriver.setDownScale(downScale); + } + if(!extractAsPolygons){ + ascReaderDriver.setAs3DPoint(extractAsPolygons); + } + ascReaderDriver.setDeleteTable(deleteTable); + + ascReaderFunction.importFile(connection, tableReference, URIUtilities.fileFromString(fileName), new EmptyProgressVisitor(), ascReaderDriver); + } + + /** + * Import a small subset of ASC file. + * @param connection + * @param fileName + * @param tableReference + * @param envelope Extract only pixels that intersects the provided geometry envelope, null to disable filter + * @param downScale Coefficient used for exporting less cells (1 all cells, 2 for size / 2) + * @param extractAsPolygons If true pixels are converted to polygon. (default false) + * @throws IOException + * @throws SQLException + */ + public static void readAscii(Connection connection, String fileName, String tableReference, Geometry envelope, int downScale, boolean extractAsPolygons, boolean deleteTable, String encoding, int zType) throws IOException, SQLException { + AscDriverFunction ascReaderFunction = new AscDriverFunction(); + AscReaderDriver ascReaderDriver = new AscReaderDriver(); + if(envelope != null && !envelope.isEmpty()) { + ascReaderDriver.setExtractEnvelope(envelope.getEnvelopeInternal()); + } + if(downScale > 1) { + ascReaderDriver.setDownScale(downScale); + } + if(!extractAsPolygons){ + ascReaderDriver.setAs3DPoint(extractAsPolygons); + } + ascReaderDriver.setEncoding(encoding); + ascReaderDriver.setZType(zType); + ascReaderDriver.setDeleteTable(deleteTable); + + ascReaderFunction.importFile(connection, tableReference, URIUtilities.fileFromString(fileName), new EmptyProgressVisitor(), ascReaderDriver); + } } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/asc/AscReaderDriver.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/asc/AscReaderDriver.java index ec0393924f..7aeb3078ad 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/asc/AscReaderDriver.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/asc/AscReaderDriver.java @@ -22,15 +22,16 @@ import org.h2gis.api.EmptyProgressVisitor; import org.h2gis.api.ProgressVisitor; +import org.h2gis.functions.io.utility.FileUtil; +import org.h2gis.utilities.JDBCUtilities; +import org.h2gis.utilities.TableLocation; import org.locationtech.jts.geom.Coordinate; import org.locationtech.jts.geom.Envelope; import org.locationtech.jts.geom.GeometryFactory; import org.locationtech.jts.geom.Point; import org.locationtech.jts.geom.Polygon; -import java.io.BufferedInputStream; -import java.io.IOException; -import java.io.InputStream; +import java.io.*; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; @@ -38,16 +39,36 @@ import java.sql.Types; import java.util.NoSuchElementException; import java.util.Scanner; +import java.util.zip.GZIPInputStream; /** * Driver to import ESRI ASCII Raster file as polygons * + * This class is written to directly access the ESRI ascii grid format. + * + * The ASCII grid data file format comprises a few lines of header data followed + * by lists of cell values. The header data includes the following keywords and + * values: + * + * ncols : number of columns in the data set. + * + * nrows : number of rows in the data set. + * + * xllcorner : x-coordinate of the west border of the LowerLeft corner. + * + * yllcorner : y-coordinate of the south border of the LowerLeft corner. + * + * cellsize : size of the square cell of the data set. + * + * NODATA_value : arbitrary value assigned to unknown cells. + * * @author Nicolas Fortin (Université Gustave Eiffel 2020) + * @author Erwan Bocher, CNRS, 2020 */ public class AscReaderDriver { private static final int BATCH_MAX_SIZE = 100; private static final int BUFFER_SIZE = 16384; - private boolean as3DPoint = false; + private boolean as3DPoint = true; private Envelope extractEnvelope = null; private int downScale = 1; private String lastWord = ""; @@ -58,7 +79,11 @@ public class AscReaderDriver { private double yValue; private double xValue; private boolean readFirst; - private int noData; + private double noData; + private int zType = 1; + private boolean deleteTable = false; + private String encoding ="UTF-8"; + private boolean importNodata=false; /** * @return If true ASC is imported as 3D points cloud, Raster is imported in pixel polygons otherwise. @@ -170,38 +195,94 @@ private void readHeader(Scanner scanner) throws IOException { readFirst = true; // XXX lastWord = scanner.next(); - noData = Integer.parseInt(lastWord); + noData= Double.parseDouble(lastWord); + } } /** - * Read asc stream + * Read asc file * * @param connection - * @param inputStream + * @param fileName * @param progress * @param tableReference * @param srid the espg code of the input file * @throws SQLException * @throws IOException */ - public void read(Connection connection, InputStream inputStream, ProgressVisitor progress, String tableReference, + public void read(Connection connection, File fileName, ProgressVisitor progress, String tableReference, int srid) throws SQLException, IOException { - BufferedInputStream bof = new BufferedInputStream(inputStream, BUFFER_SIZE); + if (fileName!=null && fileName.getName().toLowerCase().endsWith(".asc")) { + if (!fileName.exists()) { + throw new SQLException("The file " + tableReference + " doesn't exist "); + } + boolean isH2 = JDBCUtilities.isH2DataBase(connection); + TableLocation requestedTable = TableLocation.parse(tableReference, isH2); + if (deleteTable) { + Statement stmt = connection.createStatement(); + stmt.execute("DROP TABLE IF EXISTS " + requestedTable.toString(isH2)); + stmt.close(); + } + try(FileInputStream inputStream = new FileInputStream(fileName)) { + readAsc(connection, inputStream, progress, requestedTable, isH2, srid); + } + } + else if (fileName!=null && fileName.getName().toLowerCase().endsWith(".gz")) { + if (!fileName.exists()) { + throw new SQLException("The file " + tableReference + " doesn't exist "); + } + boolean isH2 = JDBCUtilities.isH2DataBase(connection); + TableLocation requestedTable = TableLocation.parse(tableReference, isH2); + if (deleteTable) { + Statement stmt = connection.createStatement(); + stmt.execute("DROP TABLE IF EXISTS " + requestedTable.toString(isH2)); + stmt.close(); + } + FileInputStream fis = new FileInputStream(fileName); + readAsc(connection, new GZIPInputStream(fis), progress, requestedTable, isH2, srid); + } + else{ + throw new SQLException("The asc read driver supports only asc or gz extensions"); + } + } + + /** + * Read the ascii file from inpustream + * @param connection + * @param inputStream + * @param progress + * @param requestedTable + * @param isH2 + * @param srid + * @throws UnsupportedEncodingException + * @throws SQLException + */ + private void readAsc(Connection connection, InputStream inputStream, ProgressVisitor progress, TableLocation requestedTable,boolean isH2, + int srid) throws UnsupportedEncodingException, SQLException { + BufferedReader reader = new BufferedReader(new InputStreamReader(new BufferedInputStream(inputStream,BUFFER_SIZE), encoding)); try { - Scanner scanner = new Scanner(bof); + Scanner scanner = new Scanner(reader); // Read HEADER readHeader(scanner); - // Read values Statement st = connection.createStatement(); PreparedStatement preparedStatement; - if(as3DPoint) { - st.execute("CREATE TABLE " + tableReference + "(PK SERIAL NOT NULL, THE_GEOM GEOMETRY(POINTZ, "+srid+"), " + " CONSTRAINT ASC_PK PRIMARY KEY (PK))"); - preparedStatement = connection.prepareStatement("INSERT INTO " + tableReference + - "(the_geom) VALUES (?)"); + if (as3DPoint) { + if (zType == 1) { + st.execute("CREATE TABLE " + requestedTable.toString(isH2)+"(PK SERIAL NOT NULL, THE_GEOM GEOMETRY(POINTZ, " + srid + "), Z integer," + " CONSTRAINT ASC_PK PRIMARY KEY (PK))"); + } else { + st.execute("CREATE TABLE " + requestedTable.toString(isH2) + "(PK SERIAL NOT NULL, THE_GEOM GEOMETRY(POINTZ, " + srid + "), Z double precision," + " CONSTRAINT ASC_PK PRIMARY KEY (PK))"); + } + preparedStatement = connection.prepareStatement("INSERT INTO " + requestedTable.toString(isH2) + + "(the_geom, Z) VALUES (?, ?)"); } else { - st.execute("CREATE TABLE " + tableReference + "(PK SERIAL NOT NULL, THE_GEOM GEOMETRY(POLYGON, "+srid+"),Z int, " + " CONSTRAINT ASC_PK PRIMARY KEY (PK))"); - preparedStatement = connection.prepareStatement("INSERT INTO " + tableReference + + if (zType == 1) { + st.execute("CREATE TABLE " + requestedTable.toString(isH2) + "(PK SERIAL NOT NULL, THE_GEOM GEOMETRY(POLYGONZ, " + srid + "),Z integer, " + " CONSTRAINT ASC_PK PRIMARY KEY (PK))"); + + } else { + st.execute("CREATE TABLE " + requestedTable.toString(isH2) + "(PK SERIAL NOT NULL, THE_GEOM GEOMETRY(POLYGONZ, " + srid + "),Z double precision, " + " CONSTRAINT ASC_PK PRIMARY KEY (PK))"); + } + preparedStatement = connection.prepareStatement("INSERT INTO " + requestedTable.toString(isH2) + "(the_geom, Z) VALUES (?, ?)"); } // Read data @@ -212,11 +293,11 @@ public void read(Connection connection, InputStream inputStream, ProgressVisitor int lastRow = nrows; int lastCol = ncols; // Compute envelope - if(extractEnvelope != null) { - firstCol = (int)Math.floor((extractEnvelope.getMinX() - xValue) / cellSize); - lastCol = (int)Math.ceil((extractEnvelope.getMaxX() - xValue) / cellSize); - firstRow = nrows - (int)Math.ceil((extractEnvelope.getMaxY() - (yValue - cellSize * nrows)) / cellSize); - lastRow = nrows - (int)Math.ceil((extractEnvelope.getMinY() - (yValue - cellSize * nrows)) / cellSize); + if (extractEnvelope != null) { + firstCol = (int) Math.floor((extractEnvelope.getMinX() - xValue) / cellSize); + lastCol = (int) Math.ceil((extractEnvelope.getMaxX() - xValue) / cellSize); + firstRow = nrows - (int) Math.ceil((extractEnvelope.getMaxY() - (yValue - cellSize * nrows)) / cellSize); + lastRow = nrows - (int) Math.ceil((extractEnvelope.getMinY() - (yValue - cellSize * nrows)) / cellSize); } ProgressVisitor cellProgress = new EmptyProgressVisitor(); if (progress != null) { @@ -229,29 +310,39 @@ public void read(Connection connection, InputStream inputStream, ProgressVisitor } else { readFirst = true; } - if((downScale == 1 || (i % downScale == 0 && j % downScale == 0)) && (extractEnvelope == null || (i >= firstRow && i <= lastRow && j >= firstCol && j <= lastCol))) { - int data = Integer.parseInt(lastWord); + + if ((downScale == 1 || (i % downScale == 0 && j % downScale == 0)) && (extractEnvelope == null || (i >= firstRow && i <= lastRow && j >= firstCol && j <= lastCol))) { + double z = Double.parseDouble(lastWord); double x = xValue + j * cellSize; double y = yValue - i * cellSize; if (as3DPoint) { - if (data != noData) { - Point cell = factory.createPoint(new Coordinate(new Coordinate(x + cellSize / 2, y - cellSize / 2, data))); - cell.setSRID(srid); + Point cell = factory.createPoint(new Coordinate(x + cellSize / 2, y - cellSize / 2, z)); + cell.setSRID(srid); + if (Math.abs(noData - z)!=0) { + preparedStatement.setObject(1, cell); + preparedStatement.setObject(2, z); + preparedStatement.addBatch(); + batchSize++; + } else if(importNodata) { preparedStatement.setObject(1, cell); + preparedStatement.setObject(2, noData); preparedStatement.addBatch(); batchSize++; } } else { - Polygon cell = factory.createPolygon(new Coordinate[]{new Coordinate(x, y), new Coordinate(x, y - cellSize * downScale), new Coordinate(x + cellSize * downScale, y - cellSize * downScale), new Coordinate(x + cellSize * downScale, y), new Coordinate(x, y)}); + Polygon cell = factory.createPolygon(new Coordinate[]{new Coordinate(x, y, z), new Coordinate(x, y - cellSize * downScale, z), new Coordinate(x + cellSize * downScale, y - cellSize * downScale, z), new Coordinate(x + cellSize * downScale, y, z), new Coordinate(x, y, z)}); cell.setSRID(srid); - preparedStatement.setObject(1, cell); - if (data != noData) { - preparedStatement.setObject(2, data); - } else { - preparedStatement.setNull(2, Types.INTEGER); + if (Math.abs(noData - z)!=0) { + preparedStatement.setObject(1, cell); + preparedStatement.setObject(2, z); + preparedStatement.addBatch(); + batchSize++; + } else if(importNodata){ + preparedStatement.setObject(1, cell); + preparedStatement.setObject(2, noData); + preparedStatement.addBatch(); + batchSize++; } - preparedStatement.addBatch(); - batchSize++; } if (batchSize >= BATCH_MAX_SIZE) { preparedStatement.executeBatch(); @@ -261,16 +352,50 @@ public void read(Connection connection, InputStream inputStream, ProgressVisitor } } cellProgress.endStep(); - if(i > lastRow) { + if (i > lastRow) { break; } } if (batchSize > 0) { preparedStatement.executeBatch(); } - } catch (NoSuchElementException | NumberFormatException ex) { + } catch (NoSuchElementException | NumberFormatException | IOException | SQLException ex) { throw new SQLException("Unexpected word " + lastWord, ex); } } + /** + * Use to set the z conversion type + * 1 = integer + * 2 = double + * @param zType + */ + public void setZType(int zType) { + this.zType =zType; + } + + /** + * Set true to delete the input table if exists + * @param deleteTable + */ + public void setDeleteTable(boolean deleteTable) { + this.deleteTable = deleteTable; + } + + /** + * Set encoding + * @param encoding + */ + public void setEncoding(String encoding) { + this.encoding=encoding; + } + + /** + * Set to true if nodata must be imported. + * Default is false + * @param importNodata + */ + public void setImportNodata(boolean importNodata) { + this.importNodata=importNodata; + } } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/csv/CSVDriverFunction.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/csv/CSVDriverFunction.java index 7f351313c1..0198fc97b4 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/csv/CSVDriverFunction.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/csv/CSVDriverFunction.java @@ -166,18 +166,17 @@ public void importFile(Connection connection, String tableReference, File fileNa @Override public void importFile(Connection connection, String tableReference, File fileName, boolean deleteTables,ProgressVisitor progress) throws SQLException, IOException { - importFile(connection, tableReference, fileName, null, false,progress); + importFile(connection, tableReference, fileName, null, deleteTables,progress); } @Override public void importFile(Connection connection, String tableReference, File fileName, String csvOptions, boolean deleteTables, ProgressVisitor progress) throws SQLException, IOException { - if (!FileUtil.isFileImportable(fileName, "csv")) { + if (FileUtil.isFileImportable(fileName, "csv")) { if(deleteTables) { final boolean isH2 = JDBCUtilities.isH2DataBase(connection); TableLocation requestedTable = TableLocation.parse(tableReference, isH2); - String table = requestedTable.getTable(); Statement stmt = connection.createStatement(); - stmt.execute("DROP TABLE IF EXISTS " + table); + stmt.execute("DROP TABLE IF EXISTS " + requestedTable); stmt.close(); } final boolean isH2 = JDBCUtilities.isH2DataBase(connection); diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/dbf/DBFDriverFunction.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/dbf/DBFDriverFunction.java index 28ed6f9181..d21977fca4 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/dbf/DBFDriverFunction.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/dbf/DBFDriverFunction.java @@ -216,19 +216,17 @@ public void importFile(Connection connection, String tableReference, File fileNa @Override public void importFile(Connection connection, String tableReference, File fileName, String options, boolean deleteTables, ProgressVisitor progress) throws SQLException, IOException { - if (!FileUtil.isFileImportable(fileName, "dbf")) { + if (FileUtil.isFileImportable(fileName, "dbf")) { + final boolean isH2 = JDBCUtilities.isH2DataBase(connection); + TableLocation requestedTable = TableLocation.parse(tableReference, isH2); if (deleteTables) { - final boolean isH2 = JDBCUtilities.isH2DataBase(connection); - TableLocation requestedTable = TableLocation.parse(tableReference, isH2); - String table = requestedTable.getTable(); Statement stmt = connection.createStatement(); - stmt.execute("DROP TABLE IF EXISTS " + table); + stmt.execute("DROP TABLE IF EXISTS " + requestedTable.toString(isH2)); stmt.close(); } DBFDriver dbfDriver = new DBFDriver(); dbfDriver.initDriverFromFile(fileName, options); - final boolean isH2 = JDBCUtilities.isH2DataBase(connection); - String parsedTable = TableLocation.parse(tableReference, isH2).toString(isH2); + String parsedTable = requestedTable.toString(isH2); DbaseFileHeader dbfHeader = dbfDriver.getDbaseFileHeader(); ProgressVisitor copyProgress = progress.subProcess((int) (dbfDriver.getRowCount() / BATCH_MAX_SIZE)); if (dbfHeader.getNumFields() == 0) { diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/dbf/DBFRead.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/dbf/DBFRead.java index 3b5c7f5244..22388c9042 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/dbf/DBFRead.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/dbf/DBFRead.java @@ -20,6 +20,10 @@ package org.h2gis.functions.io.dbf; +import org.h2.value.Value; +import org.h2.value.ValueBoolean; +import org.h2.value.ValueNull; +import org.h2.value.ValueVarchar; import org.h2gis.api.AbstractFunction; import org.h2gis.api.EmptyProgressVisitor; import org.h2gis.api.ScalarFunction; @@ -31,47 +35,66 @@ /** * @author Nicolas Fortin + * @author Erwan Bocher, CNRS, 2020 */ public class DBFRead extends AbstractFunction implements ScalarFunction { public DBFRead() { - addProperty(PROP_REMARKS, "Read a DBase III file and copy the content into a new table in the database"); + addProperty(PROP_REMARKS, "Read a DBase III file and copy the content into a new table in the database"+ + "\n DBFRead(..."+ + "\n Supported arguments :" + + "\n path of the file" + + "\n path of the file, table name"+ + "\n path of the file, true for delete the table with the same file name"+ + "\n path of the file, table name, true to delete the table name"+ + "\n path of the file, table name, true to delete the table name"+ + "\n path of the file, table name, encoding chartset"+ + "\n path of the file, table name, encoding chartset, true to delete the table name"); } @Override public String getJavaStaticMethod() { - return "read"; + return "importTable"; } - public static void read(Connection connection, String fileName) throws IOException, SQLException { + public static void importTable(Connection connection, String fileName) throws IOException, SQLException { final String name = URIUtilities.fileFromString(fileName).getName(); String tableName = name.substring(0, name.lastIndexOf(".")).toUpperCase(); if (tableName.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) { - read(connection, fileName, tableName); - } else { - throw new SQLException("The file name contains unsupported characters"); - } - } - public static void read(Connection connection, String fileName, boolean deleteTable) throws IOException, SQLException { - final String name = URIUtilities.fileFromString(fileName).getName(); - String tableName = name.substring(0, name.lastIndexOf(".")).toUpperCase(); - if (tableName.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) { - read(connection, fileName, tableName, null, deleteTable); + importTable(connection, fileName, tableName, null, false); } else { throw new SQLException("The file name contains unsupported characters"); } } - public static void read(Connection connection, String fileName, String tableReference) throws IOException, SQLException { + public static void importTable(Connection connection, String fileName, Value option) throws IOException, SQLException { + String tableReference =null; + boolean deleteTable = false; + if(option instanceof ValueBoolean){ + deleteTable = option.getBoolean(); + }else if (option instanceof ValueVarchar){ + tableReference = option.getString(); + }else if (!(option instanceof ValueNull)){ + throw new SQLException("Supported optional parameter is boolean or varchar"); + } DBFDriverFunction dbfDriverFunction = new DBFDriverFunction(); - dbfDriverFunction.importFile(connection, tableReference, URIUtilities.fileFromString(fileName), new EmptyProgressVisitor()); + dbfDriverFunction.importFile(connection, tableReference, URIUtilities.fileFromString(fileName),null, deleteTable, new EmptyProgressVisitor()); } - public static void read(Connection connection, String fileName, String tableReference, String fileEncoding) throws IOException, SQLException { + public static void importTable(Connection connection, String fileName, String tableReference, Value option) throws IOException, SQLException { + String encoding =null; + boolean deleteTable = false; + if(option instanceof ValueBoolean){ + deleteTable = option.getBoolean(); + }else if (option instanceof ValueVarchar){ + encoding = option.getString(); + }else if (!(option instanceof ValueNull)){ + throw new SQLException("Supported optional parameter is boolean or varchar"); + } DBFDriverFunction dbfDriverFunction = new DBFDriverFunction(); - dbfDriverFunction.importFile(connection, tableReference, URIUtilities.fileFromString(fileName), fileEncoding, new EmptyProgressVisitor()); + dbfDriverFunction.importFile(connection, tableReference, URIUtilities.fileFromString(fileName), encoding,deleteTable, new EmptyProgressVisitor()); } - public static void read(Connection connection, String fileName, String tableReference, String fileEncoding, boolean deleteTable) throws IOException, SQLException { + public static void importTable(Connection connection, String fileName, String tableReference, String fileEncoding, boolean deleteTable) throws IOException, SQLException { DBFDriverFunction dbfDriverFunction = new DBFDriverFunction(); dbfDriverFunction.importFile(connection, tableReference, URIUtilities.fileFromString(fileName), fileEncoding,deleteTable, new EmptyProgressVisitor()); } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/dbf/DBFWrite.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/dbf/DBFWrite.java index 5e22e55a65..75e4d87fbb 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/dbf/DBFWrite.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/dbf/DBFWrite.java @@ -20,6 +20,10 @@ package org.h2gis.functions.io.dbf; +import org.h2.value.Value; +import org.h2.value.ValueBoolean; +import org.h2.value.ValueNull; +import org.h2.value.ValueVarchar; import org.h2gis.api.AbstractFunction; import org.h2gis.api.EmptyProgressVisitor; import org.h2gis.api.ScalarFunction; @@ -38,8 +42,12 @@ public class DBFWrite extends AbstractFunction implements ScalarFunction { public DBFWrite() { addProperty(PROP_REMARKS, "Transfer the content of a table into a DBF\n" + - "CALL DBFWRITE('FILENAME', 'TABLE'[,'ENCODING', true \n(to delete output file if exists)]) or " + - "CALL DBFWRITE('FILENAME', '(SELECT * FROM TABLE)'[,'ENCODING', true \n(to delete output file if exists)])"); + "\n DBFWrite(..."+ + "\n Supported arguments :" + + "\n path of the file, table name"+ + "\n path of the file, table name, true to delete the file if exists"+ + "\n path of the file, table name, encoding chartset"+ + "\n path of the file, table name, encoding chartset, true to delete the file if exists"); } @Override @@ -51,18 +59,43 @@ public static void exportTable(Connection connection, String fileName, String ta DBFDriverFunction driverFunction = new DBFDriverFunction(); driverFunction.exportTable(connection, tableReference, URIUtilities.fileFromString(fileName), new EmptyProgressVisitor()); } - public static void exportTable(Connection connection, String fileName, String tableReference, boolean deleteFile) throws IOException, SQLException { - DBFDriverFunction driverFunction = new DBFDriverFunction(); - driverFunction.exportTable(connection, tableReference, URIUtilities.fileFromString(fileName),null, deleteFile, new EmptyProgressVisitor()); - } - - public static void exportTable(Connection connection, String fileName, String tableReference,String encoding) throws IOException, SQLException { - DBFDriverFunction driverFunction = new DBFDriverFunction(); - driverFunction.exportTable(connection, tableReference, new File(fileName), encoding, new EmptyProgressVisitor()); - } + /** + * Read a table and write it into a dbf file. + * @param connection Active connection + * @param fileName Shape file name or URI + * @param tableReference Table name or select query + * Note : The select query must be enclosed in parenthesis + * @param encoding charset encoding + * @param deleteFile true to delete output file + * @throws IOException + * @throws SQLException + */ public static void exportTable(Connection connection, String fileName, String tableReference,String encoding, boolean deleteFile) throws IOException, SQLException { DBFDriverFunction driverFunction = new DBFDriverFunction(); driverFunction.exportTable(connection, tableReference, new File(fileName), encoding, deleteFile,new EmptyProgressVisitor()); } + + /** + * Read a table and write it into a dbf file. + * @param connection Active connection + * @param fileName Shape file name or URI + * @param tableReference Table name or select query + * Note : The select query must be enclosed in parenthesis + * @param option Could be string file encoding charset or boolean value to delete the existing file + * @throws IOException + * @throws SQLException + */ + public static void exportTable(Connection connection, String fileName, String tableReference, Value option) throws IOException, SQLException { + String encoding = null; + boolean deleteFiles = false; + if(option instanceof ValueBoolean){ + deleteFiles = option.getBoolean(); + }else if (option instanceof ValueVarchar){ + encoding = option.getString(); + }else if (!(option instanceof ValueNull)){ + throw new SQLException("Supported optional parameter is boolean or varchar"); + } + exportTable( connection, fileName, tableReference, encoding, deleteFiles); + } } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonRead.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonRead.java index ea34bb07e9..2fc71f5c6a 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonRead.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonRead.java @@ -20,6 +20,10 @@ package org.h2gis.functions.io.geojson; +import org.h2.value.Value; +import org.h2.value.ValueBoolean; +import org.h2.value.ValueNull; +import org.h2.value.ValueVarchar; import org.h2gis.api.AbstractFunction; import org.h2gis.api.EmptyProgressVisitor; import org.h2gis.api.ScalarFunction; @@ -33,17 +37,26 @@ * SQL function to read a GeoJSON file an creates the corresponding spatial * table. * - * @author Erwan Bocher + * @author Erwan Bocher, CNRS, 2020 */ public class GeoJsonRead extends AbstractFunction implements ScalarFunction { public GeoJsonRead() { - addProperty(PROP_REMARKS, "Import a GeoJSON 1.0 file."); + addProperty(PROP_REMARKS, "Import a GeoJSON 1.0 file."+ + "\n GeoJsonRead(..."+ + "\n Supported arguments :" + + "\n path of the file" + + "\n path of the file, table name"+ + "\n path of the file, true for delete the table with the same file name"+ + "\n path of the file, table name, true to delete the table name"+ + "\n path of the file, table name, true to delete the table name"+ + "\n path of the file, table name, encoding chartset"+ + "\n path of the file, table name, encoding chartset, true to delete the table name"); } @Override public String getJavaStaticMethod() { - return "readGeoJson"; + return "importTable"; } /** @@ -53,28 +66,11 @@ public String getJavaStaticMethod() { * @throws IOException * @throws SQLException */ - public static void readGeoJson(Connection connection, String fileName) throws IOException, SQLException { + public static void importTable(Connection connection, String fileName) throws IOException, SQLException { final String name = URIUtilities.fileFromString(fileName).getName(); String tableName = name.substring(0, name.lastIndexOf(".")).toUpperCase(); if (tableName.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) { - readGeoJson(connection, fileName, tableName,null, false); - } else { - throw new SQLException("The file name contains unsupported characters"); - } - } - - /** - * - * @param connection - * @param fileName - * @throws IOException - * @throws SQLException - */ - public static void readGeoJson(Connection connection, String fileName, boolean deleteTable) throws IOException, SQLException { - final String name = URIUtilities.fileFromString(fileName).getName(); - String tableName = name.substring(0, name.lastIndexOf(".")).toUpperCase(); - if (tableName.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) { - readGeoJson(connection, fileName, tableName,null, deleteTable); + importTable(connection, fileName, tableName,null, false); } else { throw new SQLException("The file name contains unsupported characters"); } @@ -85,19 +81,37 @@ public static void readGeoJson(Connection connection, String fileName, boolean d * * @param connection * @param fileName - * @param tableReference + * @param option * @throws IOException * @throws SQLException */ - public static void readGeoJson(Connection connection, String fileName, String tableReference) throws IOException, SQLException { - readGeoJson(connection,fileName, tableReference, null, false); + public static void importTable(Connection connection, String fileName, Value option) throws IOException, SQLException { + String tableReference =null; + boolean deleteTable = false; + if(option instanceof ValueBoolean){ + deleteTable = option.getBoolean(); + }else if (option instanceof ValueVarchar){ + tableReference = option.getString(); + }else if (!(option instanceof ValueNull)){ + throw new SQLException("Supported optional parameter is boolean or varchar"); + } + importTable(connection,fileName, tableReference, null, deleteTable); } - public static void readGeoJson(Connection connection, String fileName, String tableReference,String encoding) throws IOException, SQLException { - readGeoJson(connection,fileName, tableReference, encoding, false); + public static void importTable(Connection connection, String fileName, String tableReference, Value option) throws IOException, SQLException { + String encoding =null; + boolean deleteTable = false; + if(option instanceof ValueBoolean){ + deleteTable = option.getBoolean(); + }else if (option instanceof ValueVarchar){ + encoding = option.getString(); + }else if (!(option instanceof ValueNull)){ + throw new SQLException("Supported optional parameter is boolean or varchar"); + } + importTable(connection,fileName, tableReference, encoding, deleteTable); } - public static void readGeoJson(Connection connection, String fileName, String tableReference,String encoding, boolean deleteTable ) throws IOException, SQLException { + public static void importTable(Connection connection, String fileName, String tableReference,String encoding, boolean deleteTable ) throws IOException, SQLException { GeoJsonDriverFunction gjdf = new GeoJsonDriverFunction(); gjdf.importFile(connection, tableReference, URIUtilities.fileFromString(fileName), encoding, deleteTable, new EmptyProgressVisitor()); } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonReaderDriver.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonReaderDriver.java index 357d99f789..9b25c48585 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonReaderDriver.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonReaderDriver.java @@ -34,8 +34,11 @@ import java.io.*; import java.nio.channels.FileChannel; +import java.nio.file.FileSystems; import java.sql.*; import java.util.*; +import java.util.zip.GZIPInputStream; +import java.util.zip.ZipInputStream; /** * Driver to import a GeoJSON file into a spatial table. @@ -60,12 +63,9 @@ public class GeoJsonReaderDriver { private final boolean deleteTable; private PreparedStatement preparedStatement = null; private JsonFactory jsFactory; + private int nbFeature = 1; private int featureCounter = 1; private ProgressVisitor progress = new EmptyProgressVisitor(); - private FileChannel fc; - private long fileSize = 0; - private long readFileSizeEachNode = 1; - private long nodeCountProgress = 0; // For progression information return private static final int AVERAGE_NODE_SIZE = 500; boolean hasGeometryField = false; @@ -112,7 +112,10 @@ public GeoJsonReaderDriver(Connection connection, File fileName, String encoding * @throws java.io.IOException */ public void read(ProgressVisitor progress, String tableReference) throws SQLException, IOException { - if (FileUtil.isFileImportable(fileName, "geojson")) { + if (fileName!=null && fileName.getName().toLowerCase().endsWith(".geojson")) { + if(!fileName.exists()){ + throw new SQLException("The file " + tableLocation + " doesn't exist "); + } this.isH2 = JDBCUtilities.isH2DataBase(connection); this.tableLocation = TableLocation.parse(tableReference, isH2); if(deleteTable){ @@ -126,6 +129,38 @@ public void read(ProgressVisitor progress, String tableReference) throws SQLExce JDBCUtilities.createEmptyTable(connection, tableLocation.toString()); } } + else if(fileName!=null && fileName.getName().toLowerCase().endsWith(".gz")){ + if(!fileName.exists()){ + throw new SQLException("The file " + tableLocation + " doesn't exist "); + } + this.isH2 = JDBCUtilities.isH2DataBase(connection); + this.tableLocation = TableLocation.parse(tableReference, isH2); + if(deleteTable){ + Statement stmt = connection.createStatement(); + stmt.execute("DROP TABLE IF EXISTS " + tableLocation); + stmt.close(); + } + + if (fileName.length() > 0) { + this.progress = progress.subProcess(100); + init(); + FileInputStream fis = new FileInputStream(fileName); + if (parseMetadata(new GZIPInputStream(fis))) { + connection.setAutoCommit(false); + GF = new GeometryFactory(new PrecisionModel(), parsedSRID); + fis = new FileInputStream(fileName); + parseData(new GZIPInputStream(fis)); + setGeometryTypeConstraints(); + connection.setAutoCommit(true); + } else { + throw new SQLException("Cannot create the table " + tableLocation + " to import the GeoJSON data"); + } + } else { + JDBCUtilities.createEmptyTable(connection, tableLocation.toString()); + } + }else { + throw new SQLException("The geojson read driver supports only geojson or gz extensions"); + } } /** @@ -155,11 +190,11 @@ public void read(ProgressVisitor progress, String tableReference) throws SQLExce */ private void parseGeoJson(ProgressVisitor progress) throws SQLException, IOException { this.progress = progress.subProcess(100); - init(); - if (parseMetadata()) { + init(); ; + if (parseMetadata(new FileInputStream(fileName))) { connection.setAutoCommit(false); GF = new GeometryFactory(new PrecisionModel(), parsedSRID); - parseData(); + parseData(new FileInputStream(fileName)); setGeometryTypeConstraints(); connection.setAutoCommit(true); @@ -174,20 +209,11 @@ private void parseGeoJson(ProgressVisitor progress) throws SQLException, IOExcep * @throws SQLException * @throws IOException */ - private boolean parseMetadata() throws SQLException, IOException { - FileInputStream fis = null; + private boolean parseMetadata(InputStream is) throws SQLException, IOException { try { - fis = new FileInputStream(fileName); - this.fc = fis.getChannel(); - this.fileSize = fc.size(); - - // Given the file size and an average node file size. - // Skip how many nodes in order to update progression at a step of 1% - readFileSizeEachNode = Math.max(1, (this.fileSize / AVERAGE_NODE_SIZE) / 100); - nodeCountProgress = 0; cachedColumnNames = new LinkedHashMap<>(); finalGeometryTypes = new HashSet(); - try (JsonParser jp = jsFactory.createParser( new InputStreamReader(fis, jsonEncoding.getJavaName()))) { + try (JsonParser jp = jsFactory.createParser( new InputStreamReader(is, jsonEncoding.getJavaName()))) { jp.nextToken();//START_OBJECT jp.nextToken(); // field_name (type) jp.nextToken(); // value_string (FeatureCollection) @@ -205,8 +231,8 @@ private boolean parseMetadata() throws SQLException, IOException { } finally { try { - if (fis != null) { - fis.close(); + if (is != null) { + is.close(); } } catch (IOException ex) { throw new IOException(ex); @@ -281,16 +307,7 @@ private void parseFeaturesMetadata(JsonParser jp) throws IOException, SQLExcepti } parseFeatureMetadata(jp); token = jp.nextToken(); //START_OBJECT new feature - featureCounter++; - - if (nodeCountProgress++ % readFileSizeEachNode == 0) { - // Update Progress - try { - progress.setStep((int) (((double) fc.position() / fileSize) * 100)); - } catch (IOException ex) { - // Ignore - } - } + nbFeature++; } else { throw new SQLException("Malformed GeoJSON file. Expected 'Feature', found '" + geomType + "'"); } @@ -954,14 +971,7 @@ private void parseFeatures(JsonParser jp) throws IOException, SQLException { token = jp.nextToken(); //START_OBJECT new feature featureCounter++; - if (nodeCountProgress++ % readFileSizeEachNode == 0) { - // Update Progress - try { - progress.setStep((int) (((double) fc.position() / fileSize) * 100)); - } catch (IOException ex) { - // Ignore - } - } + progress.setStep((int) ((featureCounter / nbFeature) * 100)); if (batchSize > 0) { preparedStatement.executeBatch(); connection.commit(); @@ -1273,11 +1283,9 @@ private Coordinate parseCoordinate(JsonParser jp) throws IOException { * @throws IOException * @throws SQLException */ - private void parseData() throws IOException, SQLException { - FileInputStream fis = null; + private void parseData(InputStream is) throws IOException, SQLException { try { - fis = new FileInputStream(fileName); - try (JsonParser jp = jsFactory.createParser(new InputStreamReader(fis, jsonEncoding.getJavaName()))) { + try (JsonParser jp = jsFactory.createParser(new InputStreamReader(is, jsonEncoding.getJavaName()))) { jp.nextToken();//START_OBJECT jp.nextToken(); // field_name (type) jp.nextToken(); // value_string (FeatureCollection) @@ -1293,8 +1301,8 @@ private void parseData() throws IOException, SQLException { } finally { try { - if (fis != null) { - fis.close(); + if (is != null) { + is.close(); } } catch (IOException ex) { throw new SQLException(ex); diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonWrite.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonWrite.java index 8c83b784e2..ae22ef3011 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonWrite.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonWrite.java @@ -20,6 +20,10 @@ package org.h2gis.functions.io.geojson; +import org.h2.value.Value; +import org.h2.value.ValueBoolean; +import org.h2.value.ValueNull; +import org.h2.value.ValueVarchar; import org.h2gis.api.AbstractFunction; import org.h2gis.api.EmptyProgressVisitor; import org.h2gis.api.ScalarFunction; @@ -38,30 +42,33 @@ public class GeoJsonWrite extends AbstractFunction implements ScalarFunction { public GeoJsonWrite(){ - addProperty(PROP_REMARKS, "Export a spatial table to a GeoJSON 1.0 file.\n " + - "As optional arguments encoding value is supported and delete output file."); + addProperty(PROP_REMARKS, "Export a spatial table to a GeoJSON 1.0 file.\n "+ + "\nGeoJsonWrite(..."+ + "\n Supported arguments :" + + "\n path of the file, table name"+ + "\n path of the file, table name, true to delete the file if exists"+ + "\n path of the file, table name, encoding chartset"+ + "\n path of the file, table name, encoding chartset, true to delete the file if exists"); } @Override public String getJavaStaticMethod() { - return "writeGeoJson"; + return "exportTable"; } - + + /** - * Write the GeoJSON file. - * - * @param connection - * @param fileName - * @param tableReference - * @param encoding + * Read a table and write it into a GEOJSON file. + * @param connection Active connection + * @param fileName Shape file name or URI + * @param tableReference Table name or select query + * Note : The select query must be enclosed in parenthesis + * @param encoding charset encoding + * @param deleteFile true to delete output file * @throws IOException * @throws SQLException */ - public static void writeGeoJson(Connection connection, String fileName, String tableReference, String encoding) throws IOException, SQLException { - writeGeoJson(connection,tableReference, fileName, encoding,false); - } - - public static void writeGeoJson(Connection connection, String fileName, String tableReference, String encoding, boolean deleteFile) throws IOException, SQLException { + public static void exportTable(Connection connection, String fileName, String tableReference, String encoding, boolean deleteFile) throws IOException, SQLException { GeoJsonDriverFunction geoJsonDriver = new GeoJsonDriverFunction(); geoJsonDriver.exportTable(connection,tableReference, URIUtilities.fileFromString(fileName), encoding,deleteFile,new EmptyProgressVisitor()); } @@ -75,11 +82,30 @@ public static void writeGeoJson(Connection connection, String fileName, String t * @throws IOException * @throws SQLException */ - public static void writeGeoJson(Connection connection, String fileName, String tableReference) throws IOException, SQLException { - writeGeoJson(connection, fileName, tableReference, null, false); + public static void exportTable(Connection connection, String fileName, String tableReference) throws IOException, SQLException { + exportTable(connection, fileName, tableReference, null, false); } - public static void writeGeoJson(Connection connection, String fileName, String tableReference, boolean deleteTable) throws IOException, SQLException { - writeGeoJson(connection, fileName, tableReference, null, deleteTable); + /** + * Read a table and write it into a geojson file. + * @param connection Active connection + * @param fileName Shape file name or URI + * @param tableReference Table name or select query + * Note : The select query must be enclosed in parenthesis + * @param option Could be string file encoding charset or boolean value to delete the existing file + * @throws IOException + * @throws SQLException + */ + public static void exportTable(Connection connection, String fileName, String tableReference, Value option) throws IOException, SQLException { + String encoding = null; + boolean deleteFiles = false; + if(option instanceof ValueBoolean){ + deleteFiles = option.getBoolean(); + }else if (option instanceof ValueVarchar){ + encoding = option.getString(); + }else if (!(option instanceof ValueNull)){ + throw new SQLException("Supported optional parameter is boolean or varchar"); + } + exportTable( connection, fileName, tableReference, encoding, deleteFiles); } } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonWriteDriver.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonWriteDriver.java index 13797ff569..22f4dcff23 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonWriteDriver.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonWriteDriver.java @@ -36,6 +36,9 @@ import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.zip.GZIPOutputStream; +import java.util.zip.ZipOutputStream; + import org.h2gis.utilities.GeometryTableUtilities; import org.h2gis.utilities.Tuple; @@ -82,6 +85,19 @@ public GeoJsonWriteDriver(Connection connection) { * @throws SQLException * @throws IOException */ + public void write(ProgressVisitor progress, ResultSet resultSet, File file, String encoding) throws SQLException, IOException { + write(progress, resultSet, file, encoding, false); + } + + /** + * Write a resulset to a geojson file + * + * @param progress + * @param resultSet + * @param file + * @throws SQLException + * @throws IOException + */ public void write(ProgressVisitor progress, ResultSet resultSet, File file) throws SQLException, IOException { write(progress, resultSet, file, null, false); } @@ -100,70 +116,196 @@ public void write(ProgressVisitor progress, ResultSet resultSet, File file) thro */ public void write(ProgressVisitor progress, ResultSet rs, File fileName, String encoding, boolean deleteFile) throws SQLException, IOException { if (FileUtil.isExtensionWellFormated(fileName, "geojson")) { - if(deleteFile){ + if (deleteFile) { Files.deleteIfExists(fileName.toPath()); } - FileOutputStream fos = null; - JsonEncoding jsonEncoding = JsonEncoding.UTF8; - if (encoding != null && !encoding.isEmpty()) { + geojsonWriter(progress, rs, new FileOutputStream(fileName), encoding); + }else if (FileUtil.isExtensionWellFormated(fileName, "gz")) { + if (deleteFile) { + Files.deleteIfExists(fileName.toPath()); + } + GZIPOutputStream gzos = null; + try{ + FileOutputStream fos = new FileOutputStream(fileName); + gzos = new GZIPOutputStream(fos) ; + geojsonWriter(progress, rs, gzos, encoding); + } finally { try { - jsonEncoding = JsonEncoding.valueOf(encoding); - } catch (IllegalArgumentException ex) { - throw new SQLException("Only UTF-8, UTF-16BE, UTF-16LE, UTF-32BE, UTF-32LE encoding is supported"); + if (gzos != null) { + gzos.close(); + } + } catch (IOException ex) { + throw new SQLException(ex); } } - try { - fos = new FileOutputStream(fileName); - int rowCount = 0; - int type = rs.getType(); - if (type == ResultSet.TYPE_SCROLL_INSENSITIVE || type == ResultSet.TYPE_SCROLL_SENSITIVE) { - rs.last(); - rowCount = rs.getRow(); - rs.beforeFirst(); - } - ProgressVisitor copyProgress = progress.subProcess(rowCount); - Tuple geometryInfo = GeometryTableUtilities.getFirstGeometryColumnNameAndIndex(rs.getMetaData()); - JsonFactory jsonFactory = new JsonFactory(); - JsonGenerator jsonGenerator = jsonFactory.createGenerator(new BufferedOutputStream(fos), jsonEncoding); - - // header of the GeoJSON file - jsonGenerator.writeStartObject(); - jsonGenerator.writeStringField("type", "FeatureCollection"); - jsonGenerator.writeArrayFieldStart("features"); + } else if (FileUtil.isExtensionWellFormated(fileName, "zip")) { + if (deleteFile) { + Files.deleteIfExists(fileName.toPath()); + } + ZipOutputStream zip = null; + try{ + FileOutputStream fos = new FileOutputStream(fileName); + zip = new ZipOutputStream(fos) ; + geojsonWriter(progress, rs, zip, encoding); + } finally { try { - ResultSetMetaData resultSetMetaData = rs.getMetaData(); - cacheMetadata(resultSetMetaData); - while (rs.next()) { - writeFeature(jsonGenerator, rs, geometryInfo.second()); - copyProgress.endStep(); + if (zip != null) { + zip.close(); } - copyProgress.endOfProgress(); - // footer - jsonGenerator.writeEndArray(); - jsonGenerator.writeEndObject(); - jsonGenerator.flush(); - jsonGenerator.close(); - } finally { - rs.close(); + } catch (IOException ex) { + throw new SQLException(ex); + } + } + }else { + throw new SQLException("Only .geojson , .gz or .zip extensions are supported"); + } + } + + /** + * Method to write a resulset to a geojson file + * @param progress + * @param rs + * @param fos + * @param encoding + * @throws SQLException + * @throws IOException + */ + private void geojsonWriter(ProgressVisitor progress, ResultSet rs, OutputStream fos , String encoding) throws SQLException, IOException { + JsonEncoding jsonEncoding = JsonEncoding.UTF8; + if (encoding != null && !encoding.isEmpty()) { + try { + jsonEncoding = JsonEncoding.valueOf(encoding); + } catch (IllegalArgumentException ex) { + throw new SQLException("Only UTF-8, UTF-16BE, UTF-16LE, UTF-32BE, UTF-32LE encoding is supported"); + } + } + try { + int rowCount = 0; + int type = rs.getType(); + if (type == ResultSet.TYPE_SCROLL_INSENSITIVE || type == ResultSet.TYPE_SCROLL_SENSITIVE) { + rs.last(); + rowCount = rs.getRow(); + rs.beforeFirst(); + } + ProgressVisitor copyProgress = progress.subProcess(rowCount); + Tuple geometryInfo = GeometryTableUtilities.getFirstGeometryColumnNameAndIndex(rs.getMetaData()); + JsonFactory jsonFactory = new JsonFactory(); + JsonGenerator jsonGenerator = jsonFactory.createGenerator(new BufferedOutputStream(fos), jsonEncoding); + + // header of the GeoJSON file + jsonGenerator.writeStartObject(); + jsonGenerator.writeStringField("type", "FeatureCollection"); + jsonGenerator.writeArrayFieldStart("features"); + try { + ResultSetMetaData resultSetMetaData = rs.getMetaData(); + cacheMetadata(resultSetMetaData); + while (rs.next()) { + writeFeature(jsonGenerator, rs, geometryInfo.second()); + copyProgress.endStep(); } + copyProgress.endOfProgress(); + // footer + jsonGenerator.writeEndArray(); + jsonGenerator.writeEndObject(); + jsonGenerator.flush(); + jsonGenerator.close(); + } finally { + rs.close(); + } + + } catch (FileNotFoundException ex) { + throw new SQLException(ex); - } catch (FileNotFoundException ex) { + } finally { + try { + if (fos != null) { + fos.close(); + } + } catch (IOException ex) { throw new SQLException(ex); + } + } + } - } finally { - try { - if (fos != null) { - fos.close(); + /** + * Method to write a table to a geojson file + * + * @param progress + * @param tableName + * @param fos + * @param encoding + * @throws SQLException + * @throws IOException + */ + private void geojsonWriter(ProgressVisitor progress, String tableName, OutputStream fos , String encoding) throws SQLException, IOException { + JsonEncoding jsonEncoding = JsonEncoding.UTF8; + if (encoding != null) { + try { + jsonEncoding = JsonEncoding.valueOf(encoding); + } catch (IllegalArgumentException ex) { + throw new SQLException("Only UTF-8, UTF-16BE, UTF-16LE, UTF-32BE, UTF-32LE encoding is supported"); + } + } + try { + final TableLocation parse = TableLocation.parse(tableName, JDBCUtilities.isH2DataBase(connection)); + int recordCount = JDBCUtilities.getRowCount(connection, parse); + if (recordCount > 0) { + ProgressVisitor copyProgress = progress.subProcess(recordCount); + // Read Geometry Index and type + Tuple geometryTableInfo = GeometryTableUtilities.getFirstGeometryColumnNameAndIndex(connection, parse); + + try ( // Read table content + Statement st = connection.createStatement()) { + JsonFactory jsonFactory = new JsonFactory(); + JsonGenerator jsonGenerator = jsonFactory.createGenerator(new BufferedOutputStream(fos), jsonEncoding); + + // header of the GeoJSON file + jsonGenerator.writeStartObject(); + jsonGenerator.writeStringField("type", "FeatureCollection"); + writeCRS(jsonGenerator, GeometryTableUtilities.getAuthorityAndSRID(connection, parse, geometryTableInfo.first())); + jsonGenerator.writeArrayFieldStart("features"); + + ResultSet rs = st.executeQuery(String.format("select * from %s", tableName)); + + try { + ResultSetMetaData resultSetMetaData = rs.getMetaData(); + cacheMetadata(resultSetMetaData); + while (rs.next()) { + writeFeature(jsonGenerator, rs, geometryTableInfo.second()); + copyProgress.endStep(); + } + copyProgress.endOfProgress(); + // footer + jsonGenerator.writeEndArray(); + jsonGenerator.writeEndObject(); + jsonGenerator.flush(); + jsonGenerator.close(); + + } finally { + rs.close(); } - } catch (IOException ex) { - throw new SQLException(ex); } } - } else { - throw new SQLException("Only .geojson extension is supported"); + } finally { + try { + if (fos != null) { + fos.close(); + } + } catch (IOException ex) { + throw new SQLException(ex); + } } } - + public void write(ProgressVisitor progress, String tableName, File fileName, String encoding) throws SQLException, IOException { + write( progress, tableName, fileName, encoding, false); + } + + + + + public void write(ProgressVisitor progress, String tableName, File fileName, boolean deleteFile) throws SQLException, IOException { + write( progress, tableName, fileName, null, deleteFile); + } /** * Write the spatial table to GeoJSON format. @@ -189,75 +331,50 @@ public void write(ProgressVisitor progress, String tableName, File fileName, Str } } else { if (FileUtil.isExtensionWellFormated(fileName, "geojson")) { - if(deleteFile){ + if (deleteFile) { Files.deleteIfExists(fileName.toPath()); } - JsonEncoding jsonEncoding = JsonEncoding.UTF8; - if (encoding != null) { - try { - jsonEncoding = JsonEncoding.valueOf(encoding); - } catch (IllegalArgumentException ex) { - throw new SQLException("Only UTF-8, UTF-16BE, UTF-16LE, UTF-32BE, UTF-32LE encoding is supported"); + geojsonWriter(progress, tableName, new FileOutputStream(fileName), encoding); + } + else if (FileUtil.isExtensionWellFormated(fileName, "gz")) { + if (deleteFile) { + Files.deleteIfExists(fileName.toPath()); } - } - - FileOutputStream fos = null; - try { - fos = new FileOutputStream(fileName); - final TableLocation parse = TableLocation.parse(tableName, JDBCUtilities.isH2DataBase(connection)); - int recordCount = JDBCUtilities.getRowCount(connection, parse); - if (recordCount > 0) { - ProgressVisitor copyProgress = progress.subProcess(recordCount); - // Read Geometry Index and type - Tuple geometryTableInfo = GeometryTableUtilities.getFirstGeometryColumnNameAndIndex(connection, parse); - - try ( // Read table content - Statement st = connection.createStatement()) { - JsonFactory jsonFactory = new JsonFactory(); - JsonGenerator jsonGenerator = jsonFactory.createGenerator(new BufferedOutputStream(fos), jsonEncoding); - - // header of the GeoJSON file - jsonGenerator.writeStartObject(); - jsonGenerator.writeStringField("type", "FeatureCollection"); - writeCRS(jsonGenerator, GeometryTableUtilities.getAuthorityAndSRID(connection, parse, geometryTableInfo.first())); - jsonGenerator.writeArrayFieldStart("features"); - - ResultSet rs = st.executeQuery(String.format("select * from %s", tableName)); - - try { - ResultSetMetaData resultSetMetaData = rs.getMetaData(); - cacheMetadata(resultSetMetaData); - while (rs.next()) { - writeFeature(jsonGenerator, rs, geometryTableInfo.second()); - copyProgress.endStep(); - } - copyProgress.endOfProgress(); - // footer - jsonGenerator.writeEndArray(); - jsonGenerator.writeEndObject(); - jsonGenerator.flush(); - jsonGenerator.close(); - - } finally { - rs.close(); + GZIPOutputStream gzos = null; + try{ + FileOutputStream fos = new FileOutputStream(fileName); + gzos = new GZIPOutputStream(fos) ; + geojsonWriter(progress, tableName, gzos, encoding); + } finally { + try { + if (gzos != null) { + gzos.close(); } + } catch (IOException ex) { + throw new SQLException(ex); } } - } catch (FileNotFoundException ex) { - throw new SQLException(ex); - - } finally { - try { - if (fos != null) { - fos.close(); + } else if (FileUtil.isExtensionWellFormated(fileName, "zip")) { + if (deleteFile) { + Files.deleteIfExists(fileName.toPath()); + } + ZipOutputStream zip = null; + try{ + FileOutputStream fos = new FileOutputStream(fileName); + zip = new ZipOutputStream(fos) ; + geojsonWriter(progress, tableName, zip, encoding); + } finally { + try { + if (zip != null) { + zip.close(); + } + } catch (IOException ex) { + throw new SQLException(ex); } - } catch (IOException ex) { - throw new SQLException(ex); } + }else { + throw new SQLException("Only .geojson , .gz or .zip extensions are supported"); } - } else { - throw new SQLException("Only .geojson extension is supported"); - } } } @@ -279,8 +396,8 @@ public void write(ProgressVisitor progress, String tableName, File fileName, Str * { "type": "Feature", "geometry":{"type": "Point", "coordinates": [102.0, * 0.5]}, "properties": {"prop0": "value0"} } * - * @param writer - * @param resultSetMetaData + * @param jsonGenerator + * @param rs * @param geoFieldIndex */ private void writeFeature(JsonGenerator jsonGenerator, ResultSet rs, int geoFieldIndex) throws IOException, SQLException { @@ -320,8 +437,8 @@ && isSupportedPropertyType(resultSetMetaData.getColumnType(i), fieldTypeName)) { * * "geometry":{"type": "Point", "coordinates": [102.0, 0.5]} * - * @param jsonGenerator - * @param geometry + * @param geom + * @param gen */ private void writeGeometry(Geometry geom, JsonGenerator gen) throws IOException { if (geom != null) { diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/gpx/GPXRead.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/gpx/GPXRead.java index c5132dece5..16694f2bd8 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/gpx/GPXRead.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/gpx/GPXRead.java @@ -20,6 +20,10 @@ package org.h2gis.functions.io.gpx; +import org.h2.value.Value; +import org.h2.value.ValueBoolean; +import org.h2.value.ValueNull; +import org.h2.value.ValueVarchar; import org.h2gis.api.AbstractFunction; import org.h2gis.api.EmptyProgressVisitor; import org.h2gis.api.ScalarFunction; @@ -27,6 +31,7 @@ import org.h2gis.utilities.URIUtilities; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Connection; import java.sql.SQLException; @@ -41,18 +46,49 @@ public class GPXRead extends AbstractFunction implements ScalarFunction { public GPXRead() { addProperty(PROP_REMARKS, "Read a GPX file and copy the content in the specified tables." + "\nThe user can set a prefix name for all GPX tables and specify if the existing GPX\n" - + " tables must be dropped."); + + " tables must be dropped."+ + "\n GPXRead(..."+ + "\n Supported arguments :" + + "\n path of the file" + + "\n path of the file, table name"+ + "\n path of the file, true for delete the table with the same file name"+ + "\n path of the file, table name, true to delete the table name"+ + "\n path of the file, table name, true to delete the table name"+ + "\n path of the file, table name, encoding chartset"+ + "\n path of the file, table name, encoding chartset, true to delete the table name"); } @Override public String getJavaStaticMethod() { - return "readGPX"; + return "importTable"; } - - - public static void readGPX(Connection connection, String fileName, String tableReference, boolean deleteTables) throws IOException, SQLException { - readGPX( connection, fileName, tableReference, null, deleteTables); + + + public static void importTable(Connection connection, String fileName, Value option) throws SQLException, FileNotFoundException, IOException { + String tableReference =null; + boolean deleteTable = false; + if(option instanceof ValueBoolean){ + deleteTable = option.getBoolean(); + }else if (option instanceof ValueVarchar){ + tableReference = option.getString(); + }else if (!(option instanceof ValueNull)){ + throw new SQLException("Supported optional parameter is boolean or varchar"); + } + importTable( connection, fileName, tableReference,null, deleteTable); + } + + public static void importTable(Connection connection, String fileName, String tableReference, Value option) throws SQLException, FileNotFoundException, IOException { + String encoding =null; + boolean deleteTable = false; + if(option instanceof ValueBoolean){ + deleteTable = option.getBoolean(); + }else if (option instanceof ValueVarchar){ + encoding = option.getString(); + }else if (!(option instanceof ValueNull)){ + throw new SQLException("Supported optional parameter is boolean or varchar"); + } + importTable(connection, fileName, tableReference,encoding, deleteTable); } /** @@ -66,24 +102,12 @@ public static void readGPX(Connection connection, String fileName, String tableR * @throws java.io.IOException * @throws java.sql.SQLException */ - public static void readGPX(Connection connection, String fileName, String tableReference, String encoding, boolean deleteTables) throws IOException, SQLException { + public static void importTable(Connection connection, String fileName, String tableReference, String encoding, boolean deleteTables) throws IOException, SQLException { GPXDriverFunction gpxdf = new GPXDriverFunction(); gpxdf.importFile(connection, tableReference, URIUtilities.fileFromString(fileName), encoding, deleteTables, new EmptyProgressVisitor()); } - /** - * Copy data from GPX File into a new table in specified connection. - * - * @param connection Active connection - * @param tableReference [[catalog.]schema.]table reference - * @param fileName File path of the SHP file - * @throws java.io.IOException - * @throws java.sql.SQLException - */ - public static void readGPX(Connection connection, String fileName, String tableReference) throws IOException, SQLException { - readGPX( connection, fileName, tableReference, null, false); - } /** * Copy data from GPX File into a new table in specified connection. @@ -94,21 +118,11 @@ public static void readGPX(Connection connection, String fileName, String tableR * @throws IOException * @throws SQLException */ - public static void readGPX(Connection connection, String fileName) throws IOException, SQLException { - final String name = URIUtilities.fileFromString(fileName).getName(); - String tableName = name.substring(0, name.lastIndexOf(".")).toUpperCase(); - if (tableName.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) { - readGPX( connection, fileName, tableName, null, false); - } else { - throw new SQLException("The file name contains unsupported characters"); - } - } - - public static void readGPX(Connection connection, String fileName, boolean deleteTable) throws IOException, SQLException { + public static void importTable(Connection connection, String fileName) throws IOException, SQLException { final String name = URIUtilities.fileFromString(fileName).getName(); String tableName = name.substring(0, name.lastIndexOf(".")).toUpperCase(); if (tableName.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) { - readGPX( connection, fileName, tableName, null, deleteTable); + importTable( connection, fileName, tableName, null, false); } else { throw new SQLException("The file name contains unsupported characters"); } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/json/JsonWrite.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/json/JsonWrite.java index 07591ba7e4..a227e31985 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/json/JsonWrite.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/json/JsonWrite.java @@ -19,6 +19,10 @@ */ package org.h2gis.functions.io.json; +import org.h2.value.Value; +import org.h2.value.ValueBoolean; +import org.h2.value.ValueNull; +import org.h2.value.ValueVarchar; import org.h2gis.api.AbstractFunction; import org.h2gis.api.EmptyProgressVisitor; import org.h2gis.api.ScalarFunction; @@ -37,27 +41,19 @@ public class JsonWrite extends AbstractFunction implements ScalarFunction{ public JsonWrite(){ addProperty(PROP_REMARKS, "Export a table to a JSON file." + - "\n As optional arguments encoding value is supported and delete output file."); + "\nJsonWrite(..."+ + "\n Supported arguments :" + + "\n path of the file, table name"+ + "\n path of the file, table name, true to delete the file if exists"+ + "\n path of the file, table name, encoding chartset"+ + "\n path of the file, table name, encoding chartset, true to delete the file if exists"); } @Override public String getJavaStaticMethod() { - return "writeGeoJson"; + return "exportTable"; } - /** - * Write the JSON file. - * - * @param connection - * @param fileName - * @param tableReference - * @param encoding - * @throws IOException - * @throws SQLException - */ - public static void writeGeoJson(Connection connection, String fileName, String tableReference, String encoding) throws IOException, SQLException { - writeGeoJson( connection, fileName, tableReference, encoding, false); - } /** * @@ -69,7 +65,7 @@ public static void writeGeoJson(Connection connection, String fileName, String t * @throws IOException * @throws SQLException */ - public static void writeGeoJson(Connection connection, String fileName, String tableReference, String encoding, boolean deleteFile) throws IOException, SQLException { + public static void exportTable(Connection connection, String fileName, String tableReference, String encoding, boolean deleteFile) throws IOException, SQLException { JsonDriverFunction jsonDriver = new JsonDriverFunction(); jsonDriver.exportTable(connection,tableReference, URIUtilities.fileFromString(fileName),encoding, deleteFile, new EmptyProgressVisitor()); } @@ -83,21 +79,31 @@ public static void writeGeoJson(Connection connection, String fileName, String t * @throws IOException * @throws SQLException */ - public static void writeGeoJson(Connection connection, String fileName, String tableReference) throws IOException, SQLException { - writeGeoJson( connection, fileName, tableReference, null, false); + public static void exportTable(Connection connection, String fileName, String tableReference) throws IOException, SQLException { + exportTable( connection, fileName, tableReference, null, false); } /** - * - * @param connection - * @param fileName - * @param tableReference - * @param deleteFile + * Read a table and write it into a json file. + * @param connection Active connection + * @param fileName Shape file name or URI + * @param tableReference Table name or select query + * Note : The select query must be enclosed in parenthesis + * @param option Could be string file encoding charset or boolean value to delete the existing file * @throws IOException * @throws SQLException */ - public static void writeGeoJson(Connection connection, String fileName, String tableReference, boolean deleteFile) throws IOException, SQLException { - writeGeoJson( connection, fileName, tableReference, null, deleteFile); + public static void exportTable(Connection connection, String fileName, String tableReference, Value option) throws IOException, SQLException { + String encoding = null; + boolean deleteFiles = false; + if(option instanceof ValueBoolean){ + deleteFiles = option.getBoolean(); + }else if (option instanceof ValueVarchar){ + encoding = option.getString(); + }else if (!(option instanceof ValueNull)){ + throw new SQLException("Supported optional parameter is boolean or varchar"); + } + exportTable( connection, fileName, tableReference, encoding, deleteFiles); } } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/json/JsonWriteDriver.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/json/JsonWriteDriver.java index ccc222e107..6ae84da543 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/json/JsonWriteDriver.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/json/JsonWriteDriver.java @@ -31,6 +31,8 @@ import java.sql.*; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.zip.GZIPOutputStream; +import java.util.zip.ZipOutputStream; /** * JSON class to write a table or a resultset to a file @@ -76,167 +78,44 @@ public void write(ProgressVisitor progress, ResultSet resultSet, File file) thro */ public void write(ProgressVisitor progress, ResultSet rs, File fileName, String encoding) throws SQLException, IOException { if (FileUtil.isExtensionWellFormated(fileName, "json")) { - JsonEncoding jsonEncoding = JsonEncoding.UTF8; - if (encoding != null) { + jsonWrite(progress, rs, new FileOutputStream(fileName), encoding); + } else if (FileUtil.isExtensionWellFormated(fileName, "gz")) { + if (deleteFile) { + Files.deleteIfExists(fileName.toPath()); + } + GZIPOutputStream gzos = null; + try{ + FileOutputStream fos = new FileOutputStream(fileName); + gzos = new GZIPOutputStream(fos) ; + jsonWrite(progress, rs, gzos, encoding); + } finally { try { - jsonEncoding = JsonEncoding.valueOf(encoding); - } catch (IllegalArgumentException ex) { - throw new SQLException("Only UTF-8, UTF-16BE, UTF-16LE, UTF-32BE, UTF-32LE encoding is supported"); + if (gzos != null) { + gzos.close(); + } + } catch (IOException ex) { + throw new SQLException(ex); } } - FileOutputStream fos = null; + } else if (FileUtil.isExtensionWellFormated(fileName, "zip")) { + if (deleteFile) { + Files.deleteIfExists(fileName.toPath()); + } + ZipOutputStream zip = null; try { - fos = new FileOutputStream(fileName); - int rowCount = 0; - int type = rs.getType(); - if (type == ResultSet.TYPE_SCROLL_INSENSITIVE || type == ResultSet.TYPE_SCROLL_SENSITIVE) { - rs.last(); - rowCount = rs.getRow(); - rs.beforeFirst(); - } - ProgressVisitor copyProgress = progress.subProcess(rowCount); - try ( // Read table content - Statement st = connection.createStatement()) { - JsonFactory jsonFactory = new JsonFactory(); - JsonGenerator jsonGenerator = jsonFactory.createGenerator(new BufferedOutputStream(fos), jsonEncoding); - try { - ResultSetMetaData rsmd = rs.getMetaData(); - int numColumns = rsmd.getColumnCount(); - while (rs.next()) { - jsonGenerator.writeStartObject(); - for (int i = 1; i < numColumns + 1; i++) { - String column_name = rsmd.getColumnName(i); - switch (rsmd.getColumnType(i)) { - case java.sql.Types.ARRAY: - Object[] values = (Object[]) rs.getArray(i).getArray(); - if(values !=null){ - jsonGenerator.writeArrayFieldStart(column_name); - for (Object value : values) { - jsonGenerator.writeObject(value); - } - jsonGenerator.writeEndArray(); - } - break; - case java.sql.Types.BIGINT: - jsonGenerator.writeObjectField(column_name, rs.getLong(i)); - break; - case java.sql.Types.REAL: - jsonGenerator.writeObjectField(column_name, rs.getFloat(i)); - break; - case java.sql.Types.BOOLEAN: - jsonGenerator.writeObjectField(column_name, rs.getBoolean(i)); - break; - case java.sql.Types.BLOB: - jsonGenerator.writeObjectField(column_name, rs.getBlob(i)); - break; - case java.sql.Types.DOUBLE: - jsonGenerator.writeObjectField(column_name, rs.getDouble(i)); - break; - case java.sql.Types.FLOAT: - jsonGenerator.writeObjectField(column_name, rs.getDouble(i)); - break; - case java.sql.Types.INTEGER: - jsonGenerator.writeObjectField(column_name, rs.getInt(i)); - break; - case java.sql.Types.NVARCHAR: - jsonGenerator.writeObjectField(column_name, rs.getNString(i)); - break; - case java.sql.Types.VARCHAR: - jsonGenerator.writeObjectField(column_name, rs.getString(i)); - break; - case java.sql.Types.CHAR: - jsonGenerator.writeObjectField(column_name, rs.getString(i)); - break; - case java.sql.Types.NCHAR: - jsonGenerator.writeObjectField(column_name, rs.getNString(i)); - break; - case java.sql.Types.LONGNVARCHAR: - jsonGenerator.writeObjectField(column_name, rs.getNString(i)); - break; - case java.sql.Types.LONGVARCHAR: - jsonGenerator.writeObjectField(column_name, rs.getString(i)); - break; - case java.sql.Types.TINYINT: - jsonGenerator.writeObjectField(column_name, rs.getByte(i)); - break; - case java.sql.Types.SMALLINT: - jsonGenerator.writeObjectField(column_name, rs.getShort(i)); - break; - case java.sql.Types.DATE: - jsonGenerator.writeObjectField(column_name, rs.getDate(i)); - break; - case java.sql.Types.TIME: - jsonGenerator.writeObjectField(column_name, rs.getTime(i)); - break; - case java.sql.Types.TIMESTAMP: - jsonGenerator.writeObjectField(column_name, rs.getTimestamp(i)); - break; - case java.sql.Types.BINARY: - jsonGenerator.writeObjectField(column_name, rs.getBytes(i)); - break; - case java.sql.Types.VARBINARY: - jsonGenerator.writeObjectField(column_name, rs.getBytes(i)); - break; - case java.sql.Types.LONGVARBINARY: - jsonGenerator.writeObjectField(column_name, rs.getBinaryStream(i)); - break; - case java.sql.Types.BIT: - jsonGenerator.writeObjectField(column_name, rs.getBoolean(i)); - break; - case java.sql.Types.CLOB: - jsonGenerator.writeObjectField(column_name, rs.getClob(i)); - break; - case java.sql.Types.NUMERIC: - jsonGenerator.writeObjectField(column_name, rs.getBigDecimal(i)); - break; - case java.sql.Types.DECIMAL: - jsonGenerator.writeObjectField(column_name, rs.getBigDecimal(i)); - break; - case java.sql.Types.DATALINK: - jsonGenerator.writeObjectField(column_name, rs.getURL(i)); - break; - case java.sql.Types.REF: - jsonGenerator.writeObjectField(column_name, rs.getRef(i)); - break; - case java.sql.Types.STRUCT: - jsonGenerator.writeObjectField(column_name, rs.getObject(i)); - break; - case java.sql.Types.DISTINCT: - jsonGenerator.writeObjectField(column_name, rs.getObject(i)); - break; - case java.sql.Types.JAVA_OBJECT: - jsonGenerator.writeObjectField(column_name, rs.getObject(i)); - break; - default: - jsonGenerator.writeObjectField(column_name, rs.getString(i)); - break; - } - } - jsonGenerator.writeEndObject(); - - copyProgress.endStep(); - } - copyProgress.endOfProgress(); - jsonGenerator.flush(); - jsonGenerator.close(); - } finally { - rs.close(); + FileOutputStream fos = new FileOutputStream(fileName); + zip = new ZipOutputStream(fos); + jsonWrite(progress, rs, zip, encoding); + } finally { + try { + if (zip != null) { + zip.close(); } - } - - } catch (FileNotFoundException ex) { - throw new SQLException(ex); - - } finally { - try { - if (fos != null) { - fos.close(); - } } catch (IOException ex) { throw new SQLException(ex); } } - } else { + }else { throw new SQLException("Only .json extension is supported"); } } @@ -267,21 +146,240 @@ public void write(ProgressVisitor progress,String tableName, File fileName, Stri if(deleteFile){ Files.deleteIfExists(fileName.toPath()); } - FileOutputStream fos = null; + jsonWrite(progress, tableName, new FileOutputStream(fileName),encoding); + + } else if (FileUtil.isExtensionWellFormated(fileName, "gz")) { + if (deleteFile) { + Files.deleteIfExists(fileName.toPath()); + } + GZIPOutputStream gzos = null; + try{ + FileOutputStream fos = new FileOutputStream(fileName); + gzos = new GZIPOutputStream(fos) ; + jsonWrite(progress, tableName, gzos, encoding); + } finally { + try { + if (gzos != null) { + gzos.close(); + } + } catch (IOException ex) { + throw new SQLException(ex); + } + } + } else if (FileUtil.isExtensionWellFormated(fileName, "zip")) { + if (deleteFile) { + Files.deleteIfExists(fileName.toPath()); + } + ZipOutputStream zip = null; + try{ + FileOutputStream fos = new FileOutputStream(fileName); + zip = new ZipOutputStream(fos) ; + jsonWrite(progress, tableName, zip, encoding); + } finally { + try { + if (zip != null) { + zip.close(); + } + } catch (IOException ex) { + throw new SQLException(ex); + } + } + }else { + throw new SQLException("Only .json, .gz and .zip extensions are supported"); + } + } + } + + /** + * Write a json resulset + * @param progress + * @param rs + * @param fos + * @param encoding + * @throws SQLException + * @throws IOException + */ + private void jsonWrite(ProgressVisitor progress, ResultSet rs, OutputStream fos , String encoding) throws SQLException, IOException { + JsonEncoding jsonEncoding = JsonEncoding.UTF8; + if (encoding != null) { + try { + jsonEncoding = JsonEncoding.valueOf(encoding); + } catch (IllegalArgumentException ex) { + throw new SQLException("Only UTF-8, UTF-16BE, UTF-16LE, UTF-32BE, UTF-32LE encoding is supported"); + } + } try { - JsonEncoding jsonEncoding = JsonEncoding.UTF8; - if (encoding != null) { + int rowCount = 0; + int type = rs.getType(); + if (type == ResultSet.TYPE_SCROLL_INSENSITIVE || type == ResultSet.TYPE_SCROLL_SENSITIVE) { + rs.last(); + rowCount = rs.getRow(); + rs.beforeFirst(); + } + ProgressVisitor copyProgress = progress.subProcess(rowCount); + try ( // Read table content + Statement st = connection.createStatement()) { + JsonFactory jsonFactory = new JsonFactory(); + JsonGenerator jsonGenerator = jsonFactory.createGenerator(new BufferedOutputStream(fos), jsonEncoding); try { - jsonEncoding = JsonEncoding.valueOf(encoding); - } catch (IllegalArgumentException ex) { - throw new SQLException("Only UTF-8, UTF-16BE, UTF-16LE, UTF-32BE, UTF-32LE encoding is supported"); + ResultSetMetaData rsmd = rs.getMetaData(); + int numColumns = rsmd.getColumnCount(); + while (rs.next()) { + jsonGenerator.writeStartObject(); + for (int i = 1; i < numColumns + 1; i++) { + String column_name = rsmd.getColumnName(i); + switch (rsmd.getColumnType(i)) { + case java.sql.Types.ARRAY: + Object[] values = (Object[]) rs.getArray(i).getArray(); + if(values !=null){ + jsonGenerator.writeArrayFieldStart(column_name); + for (Object value : values) { + jsonGenerator.writeObject(value); + } + jsonGenerator.writeEndArray(); + } + break; + case java.sql.Types.BIGINT: + jsonGenerator.writeObjectField(column_name, rs.getLong(i)); + break; + case java.sql.Types.REAL: + jsonGenerator.writeObjectField(column_name, rs.getFloat(i)); + break; + case java.sql.Types.BOOLEAN: + jsonGenerator.writeObjectField(column_name, rs.getBoolean(i)); + break; + case java.sql.Types.BLOB: + jsonGenerator.writeObjectField(column_name, rs.getBlob(i)); + break; + case java.sql.Types.DOUBLE: + jsonGenerator.writeObjectField(column_name, rs.getDouble(i)); + break; + case java.sql.Types.FLOAT: + jsonGenerator.writeObjectField(column_name, rs.getDouble(i)); + break; + case java.sql.Types.INTEGER: + jsonGenerator.writeObjectField(column_name, rs.getInt(i)); + break; + case java.sql.Types.NVARCHAR: + jsonGenerator.writeObjectField(column_name, rs.getNString(i)); + break; + case java.sql.Types.VARCHAR: + jsonGenerator.writeObjectField(column_name, rs.getString(i)); + break; + case java.sql.Types.CHAR: + jsonGenerator.writeObjectField(column_name, rs.getString(i)); + break; + case java.sql.Types.NCHAR: + jsonGenerator.writeObjectField(column_name, rs.getNString(i)); + break; + case java.sql.Types.LONGNVARCHAR: + jsonGenerator.writeObjectField(column_name, rs.getNString(i)); + break; + case java.sql.Types.LONGVARCHAR: + jsonGenerator.writeObjectField(column_name, rs.getString(i)); + break; + case java.sql.Types.TINYINT: + jsonGenerator.writeObjectField(column_name, rs.getByte(i)); + break; + case java.sql.Types.SMALLINT: + jsonGenerator.writeObjectField(column_name, rs.getShort(i)); + break; + case java.sql.Types.DATE: + jsonGenerator.writeObjectField(column_name, rs.getDate(i)); + break; + case java.sql.Types.TIME: + jsonGenerator.writeObjectField(column_name, rs.getTime(i)); + break; + case java.sql.Types.TIMESTAMP: + jsonGenerator.writeObjectField(column_name, rs.getTimestamp(i)); + break; + case java.sql.Types.BINARY: + jsonGenerator.writeObjectField(column_name, rs.getBytes(i)); + break; + case java.sql.Types.VARBINARY: + jsonGenerator.writeObjectField(column_name, rs.getBytes(i)); + break; + case java.sql.Types.LONGVARBINARY: + jsonGenerator.writeObjectField(column_name, rs.getBinaryStream(i)); + break; + case java.sql.Types.BIT: + jsonGenerator.writeObjectField(column_name, rs.getBoolean(i)); + break; + case java.sql.Types.CLOB: + jsonGenerator.writeObjectField(column_name, rs.getClob(i)); + break; + case java.sql.Types.NUMERIC: + jsonGenerator.writeObjectField(column_name, rs.getBigDecimal(i)); + break; + case java.sql.Types.DECIMAL: + jsonGenerator.writeObjectField(column_name, rs.getBigDecimal(i)); + break; + case java.sql.Types.DATALINK: + jsonGenerator.writeObjectField(column_name, rs.getURL(i)); + break; + case java.sql.Types.REF: + jsonGenerator.writeObjectField(column_name, rs.getRef(i)); + break; + case java.sql.Types.STRUCT: + jsonGenerator.writeObjectField(column_name, rs.getObject(i)); + break; + case java.sql.Types.DISTINCT: + jsonGenerator.writeObjectField(column_name, rs.getObject(i)); + break; + case java.sql.Types.JAVA_OBJECT: + jsonGenerator.writeObjectField(column_name, rs.getObject(i)); + break; + default: + jsonGenerator.writeObjectField(column_name, rs.getString(i)); + break; + } + } + jsonGenerator.writeEndObject(); + + copyProgress.endStep(); + } + copyProgress.endOfProgress(); + jsonGenerator.flush(); + jsonGenerator.close(); + } finally { + rs.close(); } } - fos = new FileOutputStream(fileName); + + } finally { + try { + if (fos != null) { + fos.close(); + } + } catch (IOException ex) { + throw new SQLException(ex); + } + } + } + + + /** + * Write a json table + * @param progress + * @param tableName + * @param fos + * @param encoding + * @throws SQLException + * @throws IOException + */ + private void jsonWrite(ProgressVisitor progress, String tableName, OutputStream fos , String encoding) throws SQLException, IOException { + JsonEncoding jsonEncoding = JsonEncoding.UTF8; + if (encoding != null) { + try { + jsonEncoding = JsonEncoding.valueOf(encoding); + } catch (IllegalArgumentException ex) { + throw new SQLException("Only UTF-8, UTF-16BE, UTF-16LE, UTF-32BE, UTF-32LE encoding is supported"); + } + } int recordCount = JDBCUtilities.getRowCount(connection, tableName); if (recordCount > 0) { try ( // Read table content - Statement st = connection.createStatement()) { + Statement st = connection.createStatement()) { JsonFactory jsonFactory = new JsonFactory(); JsonGenerator jsonGenerator = jsonFactory.createGenerator(new BufferedOutputStream(fos), jsonEncoding); ResultSet rs = st.executeQuery(String.format("select * from %s", tableName)); @@ -296,7 +394,7 @@ public void write(ProgressVisitor progress,String tableName, File fileName, Stri switch (rsmd.getColumnType(i)) { case java.sql.Types.ARRAY: Object[] values = (Object[]) rs.getArray(i).getArray(); - if(values !=null){ + if (values != null) { jsonGenerator.writeArrayFieldStart(column_name); for (Object value : values) { jsonGenerator.writeObject(value); @@ -409,23 +507,16 @@ public void write(ProgressVisitor progress,String tableName, File fileName, Stri } finally { rs.close(); } - } - } - } catch (FileNotFoundException ex) { - throw new SQLException(ex); - } finally { - try { - if (fos != null) { - fos.close(); - } - } catch (IOException ex) { - throw new SQLException(ex); + } finally { + try { + if (fos != null) { + fos.close(); + } + } catch (IOException ex) { + throw new SQLException(ex); + } } } - } else { - throw new SQLException("Only .json extension is supported"); - } } - } } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/kml/KMLWrite.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/kml/KMLWrite.java index 3f7723c8ab..992db48045 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/kml/KMLWrite.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/kml/KMLWrite.java @@ -20,6 +20,10 @@ package org.h2gis.functions.io.kml; +import org.h2.value.Value; +import org.h2.value.ValueBoolean; +import org.h2.value.ValueNull; +import org.h2.value.ValueVarchar; import org.h2gis.api.AbstractFunction; import org.h2gis.api.EmptyProgressVisitor; import org.h2gis.api.ScalarFunction; @@ -38,12 +42,17 @@ public class KMLWrite extends AbstractFunction implements ScalarFunction { public KMLWrite() { addProperty(PROP_REMARKS, "Export a spatial table to a KML or KMZ file.\n" + - "As optional arguments encoding value is supported and delete output file."); + "\nKMLWrite(..."+ + "\n Supported arguments :" + + "\n path of the file, table name"+ + "\n path of the file, table name, true to delete the file if exists"+ + "\n path of the file, table name, encoding chartset"+ + "\n path of the file, table name, encoding chartset, true to delete the file if exists"); } @Override public String getJavaStaticMethod() { - return "writeKML"; + return "exportTable"; } /** @@ -54,19 +63,34 @@ public String getJavaStaticMethod() { * @throws SQLException * @throws IOException */ - public static void writeKML(Connection connection, String fileName, String tableReference) throws SQLException, IOException { - writeKML( connection, fileName, tableReference, null, false); + public static void exportTable(Connection connection, String fileName, String tableReference) throws SQLException, IOException { + exportTable( connection, fileName, tableReference, null, false); } - public static void writeKML(Connection connection, String fileName, String tableReference, String encoding, boolean deleteFile) throws SQLException, IOException { + public static void exportTable(Connection connection, String fileName, String tableReference, String encoding, boolean deleteFile) throws SQLException, IOException { KMLDriverFunction kMLDriverFunction = new KMLDriverFunction(); kMLDriverFunction.exportTable(connection, tableReference, URIUtilities.fileFromString(fileName),encoding,deleteFile, new EmptyProgressVisitor()); } - public static void writeKML(Connection connection, String fileName, String tableReference,String encoding) throws SQLException, IOException { - writeKML( connection, fileName, tableReference, encoding, false); - } - - public static void writeKML(Connection connection, String fileName, String tableReference,boolean deleteFile) throws SQLException, IOException { - writeKML( connection, fileName, tableReference, null, deleteFile); + /** + * Read a table and write it into a kml file. + * @param connection Active connection + * @param fileName Shape file name or URI + * @param tableReference Table name or select query + * Note : The select query must be enclosed in parenthesis + * @param option Could be string file encoding charset or boolean value to delete the existing file + * @throws IOException + * @throws SQLException + */ + public static void exportTable(Connection connection, String fileName, String tableReference, Value option) throws IOException, SQLException { + String encoding = null; + boolean deleteFiles = false; + if(option instanceof ValueBoolean){ + deleteFiles = option.getBoolean(); + }else if (option instanceof ValueVarchar){ + encoding = option.getString(); + }else if (!(option instanceof ValueNull)){ + throw new SQLException("Supported optional parameter is boolean or varchar"); + } + exportTable( connection, fileName, tableReference, encoding, deleteFiles); } } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/osm/OSMRead.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/osm/OSMRead.java index 4e39b6744c..2e0262bd47 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/osm/OSMRead.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/osm/OSMRead.java @@ -20,6 +20,10 @@ package org.h2gis.functions.io.osm; +import org.h2.value.Value; +import org.h2.value.ValueBoolean; +import org.h2.value.ValueNull; +import org.h2.value.ValueVarchar; import org.h2gis.api.AbstractFunction; import org.h2gis.api.EmptyProgressVisitor; import org.h2gis.api.ScalarFunction; @@ -43,22 +47,20 @@ public OSMRead() { addProperty(PROP_REMARKS, "Read a OSM file and copy the content in the specified tables.\n" + "The user can set a prefix name for all OSM tables and specify if the existing OSM\n" + " tables must be dropped." + - "\nHere a sample in order to extract buildings polygons using way nodes:\n" + - "create index on MAP_WAY_NODE(ID_WAY,ID_NODE);\n" + - "drop table if exists MAP_BUILDINGS,MAP_WAY_GEOM;\n" + - "create table MAP_BUILDINGS(ID_WAY bigint primary key) as SELECT DISTINCT ID_WAY FROM MAP_WAY_TAG WT," + - " MAP_TAG T WHERE WT.ID_TAG = T.ID_TAG AND T.TAG_KEY IN ('building');\n" + - "create table MAP_WAY_GEOM(ID_WAY BIGINT PRIMARY KEY, THE_GEOM POLYGON) AS SELECT ID_WAY, " + - "ST_MAKEPOLYGON(ST_MAKELINE(THE_GEOM)) THE_GEOM FROM (SELECT (SELECT ST_ACCUM(THE_GEOM) THE_GEOM FROM" + - " (SELECT N.ID_NODE, N.THE_GEOM,WN.ID_WAY IDWAY FROM MAP_NODE N,MAP_WAY_NODE WN WHERE N.ID_NODE = WN" + - ".ID_NODE ORDER BY WN.NODE_ORDER) WHERE IDWAY = W.ID_WAY) THE_GEOM ,W.ID_WAY FROM MAP_WAY W," + - "MAP_BUILDINGS B WHERE W.ID_WAY = B.ID_WAY) GEOM_TABLE WHERE ST_GEOMETRYN(THE_GEOM," + - "1) = ST_GEOMETRYN(THE_GEOM, ST_NUMGEOMETRIES(THE_GEOM)) AND ST_NUMGEOMETRIES(THE_GEOM) > 2;"); + "\n OSMRead(..."+ + "\n Supported arguments :" + + "\n path of the file" + + "\n path of the file, table name"+ + "\n path of the file, true for delete the table with the same file name"+ + "\n path of the file, table name, true to delete the table name"+ + "\n path of the file, table name, true to delete the table name"+ + "\n path of the file, table name, encoding chartset"+ + "\n path of the file, table name, encoding chartset, true to delete the table name"); } @Override public String getJavaStaticMethod() { - return "readOSM"; + return "importTable"; } /** @@ -66,24 +68,42 @@ public String getJavaStaticMethod() { * @param connection * @param fileName * @param tableReference - * @param deleteTables true to delete the existing tables + * @param option true to delete the existing tables or set a chartset encoding * @throws FileNotFoundException * @throws SQLException */ - public static void readOSM(Connection connection, String fileName, String tableReference, boolean deleteTables) throws FileNotFoundException, SQLException, IOException { - readOSM(connection, fileName, tableReference, null, deleteTables); + public static void importTable(Connection connection, String fileName, String tableReference, Value option) throws FileNotFoundException, SQLException, IOException { + String encoding =null; + boolean deleteTable = false; + if(option instanceof ValueBoolean){ + deleteTable = option.getBoolean(); + }else if (option instanceof ValueVarchar){ + encoding = option.getString(); + }else if (!(option instanceof ValueNull)){ + throw new SQLException("Supported optional parameter is boolean or varchar"); + } + importTable(connection, fileName, tableReference, encoding, deleteTable); } /** * * @param connection * @param fileName - * @param tableReference + * @param option * @throws FileNotFoundException * @throws SQLException */ - public static void readOSM(Connection connection, String fileName, String tableReference) throws FileNotFoundException, SQLException, IOException { - readOSM(connection, fileName, tableReference, null, false); + public static void importTable(Connection connection, String fileName, Value option) throws FileNotFoundException, SQLException, IOException { + String tableReference =null; + boolean deleteTable = false; + if(option instanceof ValueBoolean){ + deleteTable = option.getBoolean(); + }else if (option instanceof ValueVarchar){ + tableReference = option.getString(); + }else if (!(option instanceof ValueNull)){ + throw new SQLException("Supported optional parameter is boolean or varchar"); + } + importTable(connection, fileName, tableReference,null, deleteTable); } /** @@ -97,7 +117,7 @@ public static void readOSM(Connection connection, String fileName, String tableR * @throws SQLException * @throws IOException */ - public static void readOSM(Connection connection, String fileName, String tableReference, String encoding, boolean deleteTables) throws FileNotFoundException, SQLException, IOException { + public static void importTable(Connection connection, String fileName, String tableReference, String encoding, boolean deleteTables) throws FileNotFoundException, SQLException, IOException { OSMDriverFunction osmdf = new OSMDriverFunction(); osmdf.importFile(connection, tableReference, URIUtilities.fileFromString(fileName), encoding, deleteTables,new EmptyProgressVisitor()); } @@ -109,28 +129,11 @@ public static void readOSM(Connection connection, String fileName, String tableR * @throws FileNotFoundException * @throws SQLException */ - public static void readOSM(Connection connection, String fileName) throws FileNotFoundException, SQLException, IOException { - final String name = URIUtilities.fileFromString(fileName).getName(); - String tableName = name.substring(0, name.lastIndexOf(".")).toUpperCase(); - if (tableName.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) { - readOSM(connection, fileName, tableName, null,false); - } else { - throw new SQLException("The file name contains unsupported characters"); - } - } - - /** - * - * @param connection - * @param fileName - * @throws FileNotFoundException - * @throws SQLException - */ - public static void readOSM(Connection connection, String fileName, boolean deleteTable) throws FileNotFoundException, SQLException, IOException { + public static void importTable(Connection connection, String fileName) throws FileNotFoundException, SQLException, IOException { final String name = URIUtilities.fileFromString(fileName).getName(); String tableName = name.substring(0, name.lastIndexOf(".")).toUpperCase(); if (tableName.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) { - readOSM(connection, fileName, tableName, null,deleteTable); + importTable(connection, fileName, tableName, null,false); } else { throw new SQLException("The file name contains unsupported characters"); } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/shp/SHPRead.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/shp/SHPRead.java index 8e96d293b4..54c3ebccb3 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/shp/SHPRead.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/shp/SHPRead.java @@ -20,6 +20,10 @@ package org.h2gis.functions.io.shp; +import org.h2.value.Value; +import org.h2.value.ValueBoolean; +import org.h2.value.ValueNull; +import org.h2.value.ValueVarchar; import org.h2gis.api.AbstractFunction; import org.h2gis.api.EmptyProgressVisitor; import org.h2gis.api.ScalarFunction; @@ -39,25 +43,43 @@ */ public class SHPRead extends AbstractFunction implements ScalarFunction { public SHPRead() { - addProperty(PROP_REMARKS, "Read a shape file and copy the content in the specified table."); + addProperty(PROP_REMARKS, "Read a shape file and copy the content in the specified table."+ + "\n SHPRead(..."+ + "\n Supported arguments :" + + "\n path of the file" + + "\n path of the file, table name"+ + "\n path of the file, true for delete the table with the same file name"+ + "\n path of the file, table name, true to delete the table name"+ + "\n path of the file, table name, true to delete the table name"+ + "\n path of the file, table name, encoding chartset"+ + "\n path of the file, table name, encoding chartset, true to delete the table name"); } @Override public String getJavaStaticMethod() { - return "readShape"; + return "importTable"; } /** * Copy data from Shape File into a new table in specified connection. * @param connection Active connection * @param tableReference [[catalog.]schema.]table reference - * @param forceEncoding Use this encoding instead of DBF file header encoding property. + * @param option Could be string file encoding charset or boolean value to delete the existing table * @param fileName File path of the SHP file or URI * @throws java.io.IOException * @throws java.sql.SQLException */ - public static void readShape(Connection connection, String fileName, String tableReference,String forceEncoding) throws IOException, SQLException { - readShape(connection, fileName, tableReference, forceEncoding, false); + public static void importTable(Connection connection, String fileName, String tableReference, Value option) throws IOException, SQLException { + String encoding =null; + boolean deleteTable = false; + if(option instanceof ValueBoolean){ + deleteTable = option.getBoolean(); + }else if (option instanceof ValueVarchar){ + encoding = option.getString(); + }else if (!(option instanceof ValueNull)){ + throw new SQLException("Supported optional parameter is boolean or varchar"); + } + importTable(connection, fileName, tableReference, encoding, deleteTable); } /** @@ -70,37 +92,33 @@ public static void readShape(Connection connection, String fileName, String tabl * @throws java.io.IOException * @throws java.sql.SQLException */ - public static void readShape(Connection connection, String fileName, String tableReference,String forceEncoding, boolean deleteTables) throws IOException, SQLException { + public static void importTable(Connection connection, String fileName, String tableReference,String forceEncoding, boolean deleteTables) throws IOException, SQLException { File file = URIUtilities.fileFromString(fileName); SHPDriverFunction shpDriverFunction = new SHPDriverFunction(); shpDriverFunction.importFile(connection, TableLocation.parse(tableReference, true).toString(true), file, forceEncoding,deleteTables, new EmptyProgressVisitor()); } + /** * Copy data from Shape File into a new table in specified connection. * @param connection Active connection - * @param tableReference [[catalog.]schema.]table reference * @param fileName File path of the SHP file or URI + * @param option [[catalog.]schema.]table reference * @throws java.io.IOException * @throws java.sql.SQLException */ - public static void readShape(Connection connection, String fileName, String tableReference) throws IOException, SQLException { - readShape(connection, fileName, tableReference, null, false); - } - - public static void readShape(Connection connection, String fileName, String tableReference, boolean deleteTable) throws IOException, SQLException { - readShape(connection, fileName, tableReference, null, deleteTable); - } - - public static void readShape(Connection connection, String fileName, boolean deleteTable) throws IOException, SQLException { - final String name = URIUtilities.fileFromString(fileName).getName(); - String tableName = name.substring(0, name.lastIndexOf(".")).toUpperCase(); - if (tableName.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) { - readShape(connection, fileName, tableName, null, deleteTable); - } else { - throw new SQLException("The file name contains unsupported characters"); + public static void importTable(Connection connection, String fileName, Value option) throws IOException, SQLException { + String tableReference =null; + boolean deleteTable = false; + if(option instanceof ValueBoolean){ + deleteTable = option.getBoolean(); + }else if (option instanceof ValueVarchar){ + tableReference = option.getString(); + }else if (!(option instanceof ValueNull)){ + throw new SQLException("Supported optional parameter is boolean or varchar"); } + importTable(connection, fileName, tableReference, null, false); } /** @@ -114,11 +132,11 @@ public static void readShape(Connection connection, String fileName, boolean del * @throws java.io.IOException * @throws java.sql.SQLException */ - public static void readShape(Connection connection, String fileName) throws IOException, SQLException { + public static void importTable(Connection connection, String fileName) throws IOException, SQLException { final String name = URIUtilities.fileFromString(fileName).getName(); String tableName = name.substring(0, name.lastIndexOf(".")).toUpperCase(); if (tableName.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) { - readShape(connection, fileName, tableName, null, false); + importTable(connection, fileName, tableName, null, false); } else { throw new SQLException("The file name contains unsupported characters"); } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/shp/SHPWrite.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/shp/SHPWrite.java index d96208173d..cde2f27961 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/shp/SHPWrite.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/shp/SHPWrite.java @@ -20,6 +20,10 @@ package org.h2gis.functions.io.shp; +import org.h2.value.Value; +import org.h2.value.ValueBoolean; +import org.h2.value.ValueNull; +import org.h2.value.ValueVarchar; import org.h2gis.api.AbstractFunction; import org.h2gis.api.EmptyProgressVisitor; import org.h2gis.api.ScalarFunction; @@ -37,8 +41,13 @@ public class SHPWrite extends AbstractFunction implements ScalarFunction { public SHPWrite() { - addProperty(PROP_REMARKS, "Transfer the content of a table into a new shape file\nCALL SHPWRITE('FILENAME', 'TABLE'[,'ENCODING', true " + - "\n(to delete output file if exists)])"); + addProperty(PROP_REMARKS, "Transfer the content of a table into a new shape file)"+ + "\nSHPWrite(..."+ + "\n Supported arguments :" + + "\n path of the file, table name"+ + "\n path of the file, table name, true to delete the file if exists"+ + "\n path of the file, table name, encoding chartset"+ + "\n path of the file, table name, encoding chartset, true to delete the file if exists"); } @Override @@ -64,21 +73,36 @@ public static void exportTable(Connection connection, String fileName, String ta * @param fileName Shape file name or URI * @param tableReference Table name or select query * Note : The select query must be enclosed in parenthesis - * @param encoding File encoding + * @param option Could be string file encoding charset or boolean value to delete the existing file * @throws IOException * @throws SQLException */ - public static void exportTable(Connection connection, String fileName, String tableReference, String encoding) throws IOException, SQLException { - exportTable( connection, fileName, tableReference, encoding, false); + public static void exportTable(Connection connection, String fileName, String tableReference, Value option) throws IOException, SQLException { + String encoding = null; + boolean deleteFiles = false; + if(option instanceof ValueBoolean){ + deleteFiles = option.getBoolean(); + }else if (option instanceof ValueVarchar){ + encoding = option.getString(); + }else if (!(option instanceof ValueNull)){ + throw new SQLException("Supported optional parameter is boolean or varchar"); + } + exportTable( connection, fileName, tableReference, encoding, deleteFiles); } + /** + * Read a table and write it into a shape file. + * @param connection Active connection + * @param fileName Shape file name or URI + * @param tableReference Table name or select query + * Note : The select query must be enclosed in parenthesis + * @param encoding charset encoding + * @param deleteFiles true to delete output file + * @throws IOException + * @throws SQLException + */ public static void exportTable(Connection connection, String fileName, String tableReference, String encoding, boolean deleteFiles) throws IOException, SQLException { SHPDriverFunction shpDriverFunction = new SHPDriverFunction(); shpDriverFunction.exportTable(connection, tableReference, URIUtilities.fileFromString(fileName), encoding, deleteFiles, new EmptyProgressVisitor()); } - - public static void exportTable(Connection connection, String fileName, String tableReference, boolean deleteFiles) throws IOException, SQLException { - exportTable( connection, fileName, tableReference, null, deleteFiles); - } - } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVDriverFunction.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVDriverFunction.java index 45f3b53e92..f867103963 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVDriverFunction.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVDriverFunction.java @@ -35,6 +35,9 @@ import java.sql.*; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; +import java.util.zip.ZipOutputStream; /** * This driver allow to import and export the Tab Separated Values (TSV): a @@ -109,16 +112,45 @@ public void exportTable(Connection connection, String tableReference, File fileN if (matcher.find()) { if (tableReference.startsWith("(") && tableReference.endsWith(")")) { if (FileUtil.isExtensionWellFormated(fileName, "tsv")) { - if(deleteFiles){ + if (deleteFiles) { Files.deleteIfExists(fileName.toPath()); } - try (Statement st = connection.createStatement()) { - JDBCUtilities.attachCancelResultSet(st, progress); - exportFromResultSet(connection, st.executeQuery(tableReference), fileName, encoding, new EmptyProgressVisitor()); + try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName)))) { + try (Statement st = connection.createStatement()) { + JDBCUtilities.attachCancelResultSet(st, progress); + exportFromResultSet(connection, st.executeQuery(tableReference), bw, encoding, new EmptyProgressVisitor()); + } + } + }else if(FileUtil.isExtensionWellFormated(fileName, "gz")) { + if(deleteFiles){ + Files.deleteIfExists(fileName.toPath()); + } + final boolean isH2 = JDBCUtilities.isH2DataBase(connection); + TableLocation location = TableLocation.parse(tableReference, isH2); + try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( + new GZIPOutputStream(new FileOutputStream(fileName))))) { + try (Statement st = connection.createStatement()) { + JDBCUtilities.attachCancelResultSet(st, progress); + exportFromResultSet(connection, st.executeQuery(tableReference), bw,encoding,new EmptyProgressVisitor()); + } + } + }else if(FileUtil.isExtensionWellFormated(fileName, "zip")) { + if(deleteFiles){ + Files.deleteIfExists(fileName.toPath()); + } + final boolean isH2 = JDBCUtilities.isH2DataBase(connection); + TableLocation location = TableLocation.parse(tableReference, isH2); + try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( + new ZipOutputStream(new FileOutputStream(fileName))))) { + try (Statement st = connection.createStatement()) { + JDBCUtilities.attachCancelResultSet(st, progress); + exportFromResultSet(connection, st.executeQuery(tableReference), bw,encoding,new EmptyProgressVisitor()); + } + } + } + else { + throw new SQLException("Only .tsv, .gz or .zip extensions are supported"); } - } else { - throw new SQLException("Only .tsv extension is supported"); - } } else { throw new SQLException("The select query must be enclosed in parenthesis: '(SELECT * FROM ORDERS)'."); @@ -130,12 +162,42 @@ public void exportTable(Connection connection, String tableReference, File fileN } final boolean isH2 = JDBCUtilities.isH2DataBase(connection); TableLocation location = TableLocation.parse(tableReference, isH2); - try (Statement st = connection.createStatement()) { - JDBCUtilities.attachCancelResultSet(st, progress); - exportFromResultSet(connection, st.executeQuery("SELECT * FROM " + location.toString()), fileName,encoding,new EmptyProgressVisitor()); + try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName)))) { + try (Statement st = connection.createStatement()) { + JDBCUtilities.attachCancelResultSet(st, progress); + exportFromResultSet(connection, st.executeQuery("SELECT * FROM " + location.toString()), bw, encoding, new EmptyProgressVisitor()); + } + } + } + else if(FileUtil.isExtensionWellFormated(fileName, "gz")) { + if(deleteFiles){ + Files.deleteIfExists(fileName.toPath()); + } + final boolean isH2 = JDBCUtilities.isH2DataBase(connection); + TableLocation location = TableLocation.parse(tableReference, isH2); + try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( + new GZIPOutputStream(new FileOutputStream(fileName))))) { + try (Statement st = connection.createStatement()) { + JDBCUtilities.attachCancelResultSet(st, progress); + exportFromResultSet(connection, st.executeQuery("SELECT * FROM " + location.toString()), bw,encoding,new EmptyProgressVisitor()); + } + } + }else if(FileUtil.isExtensionWellFormated(fileName, "zip")) { + if(deleteFiles){ + Files.deleteIfExists(fileName.toPath()); + } + final boolean isH2 = JDBCUtilities.isH2DataBase(connection); + TableLocation location = TableLocation.parse(tableReference, isH2); + try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( + new ZipOutputStream(new FileOutputStream(fileName))))) { + try (Statement st = connection.createStatement()) { + JDBCUtilities.attachCancelResultSet(st, progress); + exportFromResultSet(connection, st.executeQuery("SELECT * FROM " + location.toString()), bw,encoding,new EmptyProgressVisitor()); + } } - } else { - throw new SQLException("Only .tsv extension is supported"); + } + else { + throw new SQLException("Only .tsv, .gz or .zip extensions are supported"); } } } @@ -161,19 +223,19 @@ public void exportTable(Connection connection, String tableReference, File fileN * * @param connection * @param res - * @param fileName + * @param writer * @param progress * @param encoding * @throws java.sql.SQLException */ - public void exportFromResultSet(Connection connection, ResultSet res, File fileName, String encoding, ProgressVisitor progress) throws SQLException { + public void exportFromResultSet(Connection connection, ResultSet res, Writer writer, String encoding, ProgressVisitor progress) throws SQLException { Csv csv = new Csv(); String csvOptions = "charset=UTF-8 fieldSeparator=\t fieldDelimiter=\t"; if (encoding != null) { csvOptions = String.format("charset=%s fieldSeparator=\t fieldDelimiter=\t", encoding); } csv.setOptions(csvOptions); - csv.write(fileName.getPath(), res, null); + csv.write(writer, res); } @Override @@ -195,15 +257,18 @@ public void importFile(Connection connection, String tableReference, File fileNa @Override public void importFile(Connection connection, String tableReference, File fileName, String options, boolean deleteTables, ProgressVisitor progress) throws SQLException, IOException { - if (FileUtil.isFileImportable(fileName, "tsv")) { - final boolean isH2 = JDBCUtilities.isH2DataBase(connection); - TableLocation requestedTable = TableLocation.parse(tableReference, isH2); + final boolean isH2 = JDBCUtilities.isH2DataBase(connection); + TableLocation requestedTable = TableLocation.parse(tableReference, isH2); + if (fileName!=null && fileName.getName().toLowerCase().endsWith(".tsv")) { + if(!fileName.exists()){ + throw new SQLException("The file " + requestedTable + " doesn't exist "); + } if(deleteTables){ Statement stmt = connection.createStatement(); stmt.execute("DROP TABLE IF EXISTS " + requestedTable); stmt.close(); } - String table = requestedTable.getTable(); + String table = requestedTable.toString(isH2); int AVERAGE_NODE_SIZE = 500; FileInputStream fis = new FileInputStream(fileName); @@ -276,5 +341,73 @@ public void importFile(Connection connection, String tableReference, File fileNa pst.close(); } } + else if (fileName!=null && fileName.getName().toLowerCase().endsWith(".gz")) { + if(!fileName.exists()){ + throw new SQLException("The file " + requestedTable + " doesn't exist "); + } + if(deleteTables){ + Statement stmt = connection.createStatement(); + stmt.execute("DROP TABLE IF EXISTS " + requestedTable); + stmt.close(); + } + try (BufferedReader br = new BufferedReader(new InputStreamReader( + new GZIPInputStream(new FileInputStream(fileName))))) { + String table = requestedTable.toString(isH2); + Csv csv = new Csv(); + csv.setFieldDelimiter('\t'); + csv.setFieldSeparatorRead('\t'); + ResultSet reader = csv.read(br, null); + ResultSetMetaData metadata = reader.getMetaData(); + int columnCount = metadata.getColumnCount(); + StringBuilder createTable = new StringBuilder("CREATE TABLE "); + createTable.append(table).append("("); + + StringBuilder insertTable = new StringBuilder("INSERT INTO "); + insertTable.append(table).append(" VALUES("); + for (int i = 0; i < columnCount; i++) { + if (i > 0) { + createTable.append(","); + insertTable.append(","); + } + createTable.append(metadata.getColumnName(i + 1)).append(" VARCHAR"); + insertTable.append("?"); + } + createTable.append(")"); + insertTable.append(")"); + + try (Statement stmt = connection.createStatement()) { + stmt.execute(createTable.toString()); + } + + PreparedStatement pst = connection.prepareStatement(insertTable.toString()); + long batchSize = 0; + try { + while (reader.next()) { + if (progress.isCanceled()) { + throw new SQLException("Canceled by user"); + } + + for (int i = 0; i < columnCount; i++) { + pst.setString(i + 1, reader.getString(i + 1)); + } + pst.addBatch(); + batchSize++; + if (batchSize >= BATCH_MAX_SIZE) { + pst.executeBatch(); + pst.clearBatch(); + batchSize = 0; + } + } + if (batchSize > 0) { + pst.executeBatch(); + } + } finally { + pst.close(); + } + } + } + else{ + throw new SQLException("The TSV read driver supports only tsv or gz extensions"); + } } } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVRead.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVRead.java index ab637ac205..2659621bb5 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVRead.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVRead.java @@ -21,6 +21,10 @@ package org.h2gis.functions.io.tsv; +import org.h2.value.Value; +import org.h2.value.ValueBoolean; +import org.h2.value.ValueNull; +import org.h2.value.ValueVarchar; import org.h2gis.api.AbstractFunction; import org.h2gis.api.EmptyProgressVisitor; import org.h2gis.api.ScalarFunction; @@ -40,29 +44,56 @@ public class TSVRead extends AbstractFunction implements ScalarFunction{ public TSVRead() { - addProperty(PROP_REMARKS, "Read a Tab-separated values file."); + addProperty(PROP_REMARKS, "Read a Tab-separated values file." + + "\n TSVRead(..."+ + "\n Supported arguments :" + + "\n path of the file" + + "\n path of the file, table name"+ + "\n path of the file, true for delete the table with the same file name"+ + "\n path of the file, table name, true to delete the table name"+ + "\n path of the file, table name, true to delete the table name"+ + "\n path of the file, table name, encoding chartset"+ + "\n path of the file, table name, encoding chartset, true to delete the table name"); } @Override public String getJavaStaticMethod() { - return "readTSV"; + return "importTable"; } /** * Copy data from TSV File into a new table in specified connection. * @param connection * @param fileName - * @param tableReference + * @param option table name or true to delete it * @throws SQLException * @throws FileNotFoundException * @throws IOException */ - public static void readTSV(Connection connection, String fileName, String tableReference) throws SQLException, FileNotFoundException, IOException { - readTSV( connection, fileName, tableReference,null, false); + public static void importTable(Connection connection, String fileName, Value option) throws SQLException, FileNotFoundException, IOException { + String tableReference =null; + boolean deleteTable = false; + if(option instanceof ValueBoolean){ + deleteTable = option.getBoolean(); + }else if (option instanceof ValueVarchar){ + tableReference = option.getString(); + }else if (!(option instanceof ValueNull)){ + throw new SQLException("Supported optional parameter is boolean or varchar"); + } + importTable( connection, fileName, tableReference,null, deleteTable); } - public static void readTSV(Connection connection, String fileName, String tableReference, String encoding) throws SQLException, FileNotFoundException, IOException { - readTSV( connection, fileName, tableReference,encoding, false); + public static void importTable(Connection connection, String fileName, String tableReference, Value option) throws SQLException, FileNotFoundException, IOException { + String encoding =null; + boolean deleteTable = false; + if(option instanceof ValueBoolean){ + deleteTable = option.getBoolean(); + }else if (option instanceof ValueVarchar){ + encoding = option.getString(); + }else if (!(option instanceof ValueNull)){ + throw new SQLException("Supported optional parameter is boolean or varchar"); + } + importTable(connection, fileName, tableReference,encoding, deleteTable); } /** * @@ -75,7 +106,7 @@ public static void readTSV(Connection connection, String fileName, String tableR * @throws FileNotFoundException * @throws IOException */ - public static void readTSV(Connection connection, String fileName, String tableReference, String encoding, boolean deleteTable) throws SQLException, FileNotFoundException, IOException { + public static void importTable(Connection connection, String fileName, String tableReference, String encoding, boolean deleteTable) throws SQLException, FileNotFoundException, IOException { TSVDriverFunction tsvDriver = new TSVDriverFunction(); tsvDriver.importFile(connection, tableReference, URIUtilities.fileFromString(fileName),encoding,deleteTable, new EmptyProgressVisitor()); } @@ -87,29 +118,11 @@ public static void readTSV(Connection connection, String fileName, String tableR * @throws IOException * @throws SQLException */ - public static void readTSV(Connection connection, String fileName) throws IOException, SQLException { - final String name = URIUtilities.fileFromString(fileName).getName(); - String tableName = name.substring(0, name.lastIndexOf(".")).toUpperCase(); - if (tableName.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) { - readTSV(connection, fileName, tableName, null, false); - } else { - throw new SQLException("The file name contains unsupported characters"); - } - } - - /** - * - * @param connection - * @param fileName - * @param deleteTable - * @throws IOException - * @throws SQLException - */ - public static void readTSV(Connection connection, String fileName, boolean deleteTable) throws IOException, SQLException { + public static void importTable(Connection connection, String fileName) throws IOException, SQLException { final String name = URIUtilities.fileFromString(fileName).getName(); String tableName = name.substring(0, name.lastIndexOf(".")).toUpperCase(); if (tableName.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) { - readTSV(connection, fileName, tableName, null, deleteTable); + importTable(connection, fileName, tableName, null, false); } else { throw new SQLException("The file name contains unsupported characters"); } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVWrite.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVWrite.java index b73a6733a2..bd6cc340fa 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVWrite.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVWrite.java @@ -21,6 +21,10 @@ package org.h2gis.functions.io.tsv; +import org.h2.value.Value; +import org.h2.value.ValueBoolean; +import org.h2.value.ValueNull; +import org.h2.value.ValueVarchar; import org.h2gis.api.AbstractFunction; import org.h2gis.api.EmptyProgressVisitor; import org.h2gis.api.ScalarFunction; @@ -37,12 +41,18 @@ public class TSVWrite extends AbstractFunction implements ScalarFunction { public TSVWrite() { - addProperty(PROP_REMARKS, "Write a Tab-separated values file."); + addProperty(PROP_REMARKS, "Write a Tab-separated values file."+ + "\n TSVWrite(..."+ + "\n Supported arguments :" + + "\n path of the file, table name"+ + "\n path of the file, table name, true to delete the file if exists"+ + "\n path of the file, table name, encoding chartset"+ + "\n path of the file, table name, encoding chartset, true to delete the file if exists"); } @Override public String getJavaStaticMethod() { - return "writeTSV"; + return "exportTable"; } /** @@ -54,26 +64,33 @@ public String getJavaStaticMethod() { * @throws SQLException * @throws IOException */ - public static void writeTSV(Connection connection, String fileName, String tableReference) throws SQLException, IOException { - writeTSV( connection, fileName, tableReference, null, false); + public static void exportTable(Connection connection, String fileName, String tableReference) throws SQLException, IOException { + exportTable( connection, fileName, tableReference, null, false); } - public static void writeTSV(Connection connection, String fileName, String tableReference, boolean deleteFile) throws SQLException, IOException { - writeTSV( connection, fileName, tableReference, null, deleteFile); - } + /** - * Export a table into a Tab-separated values file - * - * @param connection - * @param fileName - * @param tableReference - * @param encoding - * @throws SQLException + * Read a table and write it into a tsv file. + * @param connection Active connection + * @param fileName Shape file name or URI + * @param tableReference Table name or select query + * Note : The select query must be enclosed in parenthesis + * @param option Could be string file encoding charset or boolean value to delete the existing file * @throws IOException + * @throws SQLException */ - public static void writeTSV(Connection connection, String fileName, String tableReference, String encoding) throws SQLException, IOException { - writeTSV( connection, fileName, tableReference, encoding, false); + public static void exportTable(Connection connection, String fileName, String tableReference, Value option) throws IOException, SQLException { + String encoding = null; + boolean deleteFiles = false; + if(option instanceof ValueBoolean){ + deleteFiles = option.getBoolean(); + }else if (option instanceof ValueVarchar){ + encoding = option.toString(); + }else if (!(option instanceof ValueNull)){ + throw new SQLException("Supported optional parameter is boolean or varchar"); + } + exportTable( connection, fileName, tableReference, encoding, deleteFiles); } /** @@ -84,7 +101,7 @@ public static void writeTSV(Connection connection, String fileName, String table * @throws SQLException * @throws IOException */ - public static void writeTSV(Connection connection, String fileName, String tableReference, String encoding, boolean deleteFile) throws SQLException, IOException { + public static void exportTable(Connection connection, String fileName, String tableReference, String encoding, boolean deleteFile) throws SQLException, IOException { TSVDriverFunction tSVDriverFunction = new TSVDriverFunction(); tSVDriverFunction.exportTable(connection, tableReference, URIUtilities.fileFromString(fileName), encoding, deleteFile, new EmptyProgressVisitor()); } diff --git a/h2gis-functions/src/test/java/org/h2gis/functions/io/asc/AscReaderDriverTest.java b/h2gis-functions/src/test/java/org/h2gis/functions/io/asc/AscReaderDriverTest.java index 5e4acda022..4bd3ad93e2 100644 --- a/h2gis-functions/src/test/java/org/h2gis/functions/io/asc/AscReaderDriverTest.java +++ b/h2gis-functions/src/test/java/org/h2gis/functions/io/asc/AscReaderDriverTest.java @@ -29,6 +29,7 @@ import org.locationtech.jts.geom.Geometry; import org.locationtech.jts.geom.GeometryFactory; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; @@ -43,10 +44,10 @@ public class AscReaderDriverTest { private Connection connection; - + private static final String DB_NAME = "ASCRead_db"; @BeforeEach public void tearUp() throws Exception { - connection = JDBCUtilities.wrapConnection(H2GISDBFactory.createSpatialDataBase(AscReaderDriverTest.class.getSimpleName(), true, "")); + connection = JDBCUtilities.wrapConnection(H2GISDBFactory.createSpatialDataBase("/tmp/dbgis;AUTO_SERVER=TRUE", true, "")); } @AfterEach @@ -58,74 +59,127 @@ public void tearDown() throws Exception { @Test public void testReadPrecip() throws IOException, SQLException { + Statement st = connection.createStatement(); + st.execute("DROP TABLE IF EXISTS PRECIP30MIN"); AscReaderDriver reader = new AscReaderDriver(); - try(InputStream inputStream = AscReaderDriverTest.class.getResourceAsStream("precip30min.asc")) { - reader.read(connection, inputStream, new EmptyProgressVisitor(), "PRECIP30MIN", 4326); + reader.read(connection, new File(AscReaderDriverTest.class.getResource("precip30min.asc").getPath()), new EmptyProgressVisitor(), "PRECIP30MIN", 4326); + + // Check first read cell + try(ResultSet rs = st.executeQuery("SELECT * FROM PRECIP30MIN WHERE ST_INTERSECTS(THE_GEOM, ST_SETSRID(ST_MAKEPOINT(-179.75,-80.25), 4326))")) { + assertTrue(rs.next()); + assertEquals(234, rs.getInt("Z")); } - // Check database content + // Check last read cell + st = connection.createStatement(); + try(ResultSet rs = st.executeQuery("SELECT * FROM PRECIP30MIN WHERE ST_INTERSECTS(THE_GEOM, ST_SETSRID(ST_MAKEPOINT(-172.75, -89.75), 4326))")) { + assertTrue(rs.next()); + assertEquals(114, rs.getInt("Z")); + } + // Check nodata cell + st = connection.createStatement(); + try(ResultSet rs = st.executeQuery("SELECT * FROM PRECIP30MIN WHERE ST_INTERSECTS(THE_GEOM, ST_SETSRID(ST_MAKEPOINT(-177.25, -84.25), 4326))")) { + assertFalse(rs.next()); + } + } + + @Test + public void testReadPrecipDouble() throws IOException, SQLException { + Statement st = connection.createStatement(); + st.execute("DROP TABLE IF EXISTS PRECIP30MIN"); + AscReaderDriver reader = new AscReaderDriver(); + reader.setZType(2); + reader.read(connection, new File(AscReaderDriverTest.class.getResource("precip30min.asc").getPath()), new EmptyProgressVisitor(), "PRECIP30MIN", 4326); + + // Check first read cell + try(ResultSet rs = st.executeQuery("SELECT * FROM PRECIP30MIN WHERE ST_INTERSECTS(THE_GEOM, ST_SETSRID(ST_MAKEPOINT(-179.75,-80.25), 4326))")) { + assertTrue(rs.next()); + assertEquals(234.0, rs.getDouble("Z"), 0.00001); + } + + // Check last read cell + st = connection.createStatement(); + try(ResultSet rs = st.executeQuery("SELECT * FROM PRECIP30MIN WHERE ST_INTERSECTS(THE_GEOM, ST_SETSRID(ST_MAKEPOINT(-172.75, -89.75), 4326))")) { + assertTrue(rs.next()); + assertEquals(114, rs.getDouble("Z"), 0.00001); + } + // Check nodata cell + st = connection.createStatement(); + try(ResultSet rs = st.executeQuery("SELECT * FROM PRECIP30MIN WHERE ST_INTERSECTS(THE_GEOM, ST_SETSRID(ST_MAKEPOINT(-177.25, -84.25), 4326))")) { + assertFalse(rs.next()); + } + } + + @Test + public void testReadPrecipCenterNodata() throws IOException, SQLException { + AscReaderDriver reader = new AscReaderDriver(); + reader.setDeleteTable(true); + reader.setImportNodata(true); + reader.read(connection, new File(AscReaderDriverTest.class.getResource("precip30min_center.asc").getPath()), new EmptyProgressVisitor(), "PRECIP30MIN", 4326); + // Check database content // Check first read cell Statement st = connection.createStatement(); - try(ResultSet rs = st.executeQuery("SELECT * FROM PRECIP30MIN WHERE ST_INTERSECTS(THE_GEOM, ST_SETSRID(ST_MAKEPOINT(-179.74,-80.18), 4326))")) { + + try(ResultSet rs = st.executeQuery("SELECT * FROM PRECIP30MIN WHERE ST_INTERSECTS(THE_GEOM, ST_SETSRID(ST_MAKEPOINT(-180, -80.50), 4326))")) { assertTrue(rs.next()); assertEquals(234, rs.getInt("Z")); } // Check last read cell st = connection.createStatement(); - try(ResultSet rs = st.executeQuery("SELECT * FROM PRECIP30MIN WHERE ST_INTERSECTS(THE_GEOM, ST_SETSRID(ST_MAKEPOINT(-172.604,-89.867), 4326))")) { + try(ResultSet rs = st.executeQuery("SELECT * FROM PRECIP30MIN WHERE ST_INTERSECTS(THE_GEOM, ST_SETSRID(ST_MAKEPOINT(-173, -90), 4326))")) { assertTrue(rs.next()); assertEquals(114, rs.getInt("Z")); } + st.execute("CALL SHPWRITE('/tmp/grid.shp', 'PRECIP30MIN')"); + //st.execute("CALL SHPWRITE('/tmp/grid_nodata.shp', '(SELECT * FROM PRECIP30MIN WHERE ST_INTERSECTS(THE_GEOM, st_buffer(ST_SETSRID(ST_MAKEPOINT(-179.5,-80.25), 4326), 0.1)))')"); + // Check nodata cell st = connection.createStatement(); - try(ResultSet rs = st.executeQuery("SELECT * FROM PRECIP30MIN WHERE ST_INTERSECTS(THE_GEOM, ST_SETSRID(ST_MAKEPOINT(-177.438, -84.077), 4326))")) { + try(ResultSet rs = st.executeQuery("SELECT * FROM PRECIP30MIN WHERE ST_INTERSECTS(THE_GEOM, ST_SETSRID(ST_MAKEPOINT(-177.50, -84.5), 4326))")) { assertTrue(rs.next()); - assertNull(rs.getObject("Z")); + assertEquals(-9999, rs.getInt("Z")); } } - - @Test public void testReadPrecipCenter() throws IOException, SQLException { AscReaderDriver reader = new AscReaderDriver(); - try(InputStream inputStream = AscReaderDriverTest.class.getResourceAsStream("precip30min_center.asc")) { - reader.read(connection, inputStream, new EmptyProgressVisitor(), "PRECIP30MIN", 4326); - } + reader.setDeleteTable(true); + reader.read(connection, new File(AscReaderDriverTest.class.getResource("precip30min_center.asc").getPath()), new EmptyProgressVisitor(), "PRECIP30MIN", 4326); // Check database content // Check first read cell Statement st = connection.createStatement(); - try(ResultSet rs = st.executeQuery("SELECT * FROM PRECIP30MIN WHERE ST_INTERSECTS(THE_GEOM, ST_SETSRID(ST_MAKEPOINT(-180.1454, -80.303), 4326))")) { + + try(ResultSet rs = st.executeQuery("SELECT * FROM PRECIP30MIN WHERE ST_INTERSECTS(THE_GEOM, ST_SETSRID(ST_MAKEPOINT(-180, -80.50), 4326))")) { assertTrue(rs.next()); assertEquals(234, rs.getInt("Z")); } // Check last read cell st = connection.createStatement(); - try(ResultSet rs = st.executeQuery("SELECT * FROM PRECIP30MIN WHERE ST_INTERSECTS(THE_GEOM, ST_SETSRID(ST_MAKEPOINT(-173.213, -89.771), 4326))")) { + try(ResultSet rs = st.executeQuery("SELECT * FROM PRECIP30MIN WHERE ST_INTERSECTS(THE_GEOM, ST_SETSRID(ST_MAKEPOINT(-173, -90), 4326))")) { assertTrue(rs.next()); assertEquals(114, rs.getInt("Z")); } // Check nodata cell st = connection.createStatement(); - try(ResultSet rs = st.executeQuery("SELECT * FROM PRECIP30MIN WHERE ST_INTERSECTS(THE_GEOM, ST_SETSRID(ST_MAKEPOINT(-177.3831, -84.5793), 4326))")) { - assertTrue(rs.next()); - assertNull(rs.getObject("Z")); + try(ResultSet rs = st.executeQuery("SELECT * FROM PRECIP30MIN WHERE ST_INTERSECTS(THE_GEOM, ST_SETSRID(ST_MAKEPOINT(-177.50, -84.5), 4326))")) { + assertFalse(rs.next()); } } @Test public void testReadPrecipPoint() throws IOException, SQLException { AscReaderDriver reader = new AscReaderDriver(); reader.setAs3DPoint(true); - try(InputStream inputStream = AscReaderDriverTest.class.getResourceAsStream("precip30min.asc")) { - reader.read(connection, inputStream, new EmptyProgressVisitor(), "PRECIP30MIN", 4326); - } + reader.setDeleteTable(true); + reader.read(connection, new File(AscReaderDriverTest.class.getResource("precip30min.asc").getPath()), new EmptyProgressVisitor(), "PRECIP30MIN", 4326); + // Check database content @@ -154,12 +208,10 @@ public void testReadPrecipPoint() throws IOException, SQLException { @Test public void testReadPrecipEnvelope() throws IOException, SQLException { AscReaderDriver reader = new AscReaderDriver(); - reader.setExtractEnvelope(new Envelope(-178.242, -174.775, -89.707, -85.205)); + reader.setDeleteTable(true); + reader.read(connection, new File(AscReaderDriverTest.class.getResource("precip30min.asc").getPath()), new EmptyProgressVisitor(), "PRECIP30MIN", 4326); - try(InputStream inputStream = AscReaderDriverTest.class.getResourceAsStream("precip30min.asc")) { - reader.read(connection, inputStream, new EmptyProgressVisitor(), "PRECIP30MIN", 4326); - } // Check database content @@ -176,10 +228,9 @@ public void testReadPrecipEnvelope() throws IOException, SQLException { public void testReadPrecipDownscale() throws IOException, SQLException { AscReaderDriver reader = new AscReaderDriver(); reader.setDownScale(5); + reader.setDeleteTable(true); + reader.read(connection, new File(AscReaderDriverTest.class.getResource("precip30min.asc").getPath()), new EmptyProgressVisitor(), "PRECIP30MIN", 4326); - try(InputStream inputStream = AscReaderDriverTest.class.getResourceAsStream("precip30min.asc")) { - reader.read(connection, inputStream, new EmptyProgressVisitor(), "PRECIP30MIN", 4326); - } // Check database content @@ -195,6 +246,7 @@ public void testReadPrecipDownscale() throws IOException, SQLException { @Test public void testASCRead() throws IOException, SQLException { Statement st = connection.createStatement(); + st.execute("DROP TABLE PRECIP30MIN IF EXISTS"); st.execute(String.format("CALL ASCREAD('%s')",AscReaderDriverTest.class.getResource("precip30min.asc").getFile())); // Check number of extracted cells @@ -234,6 +286,7 @@ public void testASCRead() throws IOException, SQLException { @Test public void testASCReadPoints() throws IOException, SQLException { Statement st = connection.createStatement(); + st.execute("DROP TABLE PRECIP30MIN IF EXISTS"); st.execute(String.format("CALL ASCREAD('%s')",AscReaderDriverTest.class.getResource("precip30min.asc").getFile())); try(ResultSet rs = st.executeQuery("SELECT the_geom FROM PRECIP30MIN limit 1")) { assertTrue(rs.next()); diff --git a/h2gis-functions/src/test/java/org/h2gis/functions/io/cvs/CSVDriverTest.java b/h2gis-functions/src/test/java/org/h2gis/functions/io/cvs/CSVDriverTest.java index 7c96767209..677f376eb1 100644 --- a/h2gis-functions/src/test/java/org/h2gis/functions/io/cvs/CSVDriverTest.java +++ b/h2gis-functions/src/test/java/org/h2gis/functions/io/cvs/CSVDriverTest.java @@ -97,9 +97,9 @@ public void testDriverOptions() throws SQLException, IOException { // Export in target with special chars File csvFile = new File("target/csv_options.csv"); CSVDriverFunction exp = new CSVDriverFunction(); - exp.exportTable(connection, "AREA", csvFile,new EmptyProgressVisitor(), "fieldSeparator=| fieldDelimiter=,"); + exp.exportTable(connection, "AREA", csvFile, "fieldSeparator=| fieldDelimiter=,",new EmptyProgressVisitor()); stat.execute("DROP TABLE IF EXISTS mycsv"); - exp.importFile(connection, "MYCSV", csvFile, new EmptyProgressVisitor(), "fieldSeparator=| fieldDelimiter=,"); + exp.importFile(connection, "MYCSV", csvFile, "fieldSeparator=| fieldDelimiter=,",new EmptyProgressVisitor()); try (ResultSet rs = stat.executeQuery("select SUM(idarea::int) from mycsv")) { assertTrue(rs.next()); assertEquals(3,rs.getDouble(1),1e-2); @@ -118,7 +118,7 @@ public void testDriverDeleteTable() throws SQLException, IOException { File csvFile = new File("target/area éxport.csv"); DriverFunction exp = new CSVDriverFunction(); exp.exportTable(connection, "AREA", csvFile,new EmptyProgressVisitor()); - exp.importFile(connection, "MYCSV", csvFile, new EmptyProgressVisitor(), true); + exp.importFile(connection, "MYCSV", csvFile, true,new EmptyProgressVisitor()); ResultSet rs = stat.executeQuery("select SUM(idarea::int) from mycsv"); try { assertTrue(rs.next()); @@ -138,9 +138,9 @@ public void testDriverQueryOptions() throws SQLException, IOException { // Export in target with special chars File csvFile = new File("target/csv_options.csv"); CSVDriverFunction exp = new CSVDriverFunction(); - exp.exportTable(connection, "(SELECT * FROM AREA)", csvFile,new EmptyProgressVisitor(), "fieldSeparator=| fieldDelimiter=,"); + exp.exportTable(connection, "(SELECT * FROM AREA)", csvFile, "fieldSeparator=| fieldDelimiter=,",new EmptyProgressVisitor()); stat.execute("DROP TABLE IF EXISTS mycsv"); - exp.importFile(connection, "MYCSV", csvFile, new EmptyProgressVisitor(), "fieldSeparator=| fieldDelimiter=,"); + exp.importFile(connection, "MYCSV", csvFile, "fieldSeparator=| fieldDelimiter=,",new EmptyProgressVisitor()); try (ResultSet rs = stat.executeQuery("select SUM(idarea::int) from mycsv")) { assertTrue(rs.next()); assertEquals(3,rs.getDouble(1),1e-2); diff --git a/h2gis-functions/src/test/java/org/h2gis/functions/io/geojson/GeojsonImportExportTest.java b/h2gis-functions/src/test/java/org/h2gis/functions/io/geojson/GeojsonImportExportTest.java index 14b7527d58..7954ad75c5 100644 --- a/h2gis-functions/src/test/java/org/h2gis/functions/io/geojson/GeojsonImportExportTest.java +++ b/h2gis-functions/src/test/java/org/h2gis/functions/io/geojson/GeojsonImportExportTest.java @@ -783,9 +783,9 @@ public void exportImportFile() throws SQLException, IOException { // Read this shape file to check values assertTrue(fileOut.exists()); stat.execute("DROP TABLE IF EXISTS IMPORT_LINEAL;"); - stat.execute("CALL GeoJSONRead('target/lineal_export.geojson')"); + stat.execute("CALL GeoJSONRead('target/lineal_export.geojson', 'IMPORT_LINEAL', true)"); - try (ResultSet res = stat.executeQuery("SELECT IDAREA FROM LINEAL_EXPORT;")) { + try (ResultSet res = stat.executeQuery("SELECT IDAREA FROM IMPORT_LINEAL;")) { res.next(); assertEquals(1, res.getInt(1)); } @@ -866,6 +866,46 @@ public void exportQueryImportFile() throws SQLException, IOException { } } + @Test + public void exportQueryImportFileOption() throws SQLException, IOException { + Statement stat = connection.createStatement(); + File fileOut = new File("target/lineal_export.geojson"); + stat.execute("DROP TABLE IF EXISTS LINEAL"); + stat.execute("create table lineal(idarea int primary key, the_geom GEOMETRY(LINESTRING Z))"); + stat.execute("insert into lineal values(1, 'LINESTRING(-10 109 5, 12 2 6)'),(2, 'LINESTRING(-15 109 5, 120 2 6)')"); + // Create a geojson file using a query + stat.execute("CALL GeoJSONWrite('target/lineal_export.geojson', '(SELECT THE_GEOM FROM LINEAL LIMIT 1 )', true)"); + // Read this shape file to check values + assertTrue(fileOut.exists()); + stat.execute("DROP TABLE IF EXISTS IMPORT_LINEAL;"); + stat.execute("CALL GeoJSONRead('target/lineal_export.geojson', 'IMPORT_LINEAL', true)"); + + try (ResultSet res = stat.executeQuery("SELECT COUNT(*) FROM LINEAL_EXPORT;")) { + res.next(); + assertEquals(1, res.getInt(1)); + } + } + + @Test + public void exportQueryImportFileGZOption() throws SQLException, IOException { + Statement stat = connection.createStatement(); + File fileOut = new File("target/lineal_export.geojson"); + stat.execute("DROP TABLE IF EXISTS LINEAL"); + stat.execute("create table lineal(idarea int primary key, the_geom GEOMETRY(LINESTRING Z))"); + stat.execute("insert into lineal values(1, 'LINESTRING(-10 109 5, 12 2 6)'),(2, 'LINESTRING(-15 109 5, 120 2 6)')"); + // Create a geojson file using a query + stat.execute("CALL GeoJSONWrite('target/lineal_export.gz', '(SELECT THE_GEOM FROM LINEAL LIMIT 1 )', true)"); + // Read this shape file to check values + assertTrue(fileOut.exists()); + stat.execute("DROP TABLE IF EXISTS IMPORT_LINEAL;"); + stat.execute("CALL GeoJSONRead('target/lineal_export.gz')"); + + try (ResultSet res = stat.executeQuery("SELECT COUNT(*) FROM LINEAL_EXPORT;")) { + res.next(); + assertEquals(1, res.getInt(1)); + } + } + @Test public void exportResultSetBadEncoding() throws SQLException, IOException { assertThrows(JdbcSQLDataException.class, () -> { diff --git a/h2gis-functions/src/test/java/org/h2gis/functions/io/json/JsonImportExportTest.java b/h2gis-functions/src/test/java/org/h2gis/functions/io/json/JsonImportExportTest.java index 73f9a32fde..4e3967b3de 100644 --- a/h2gis-functions/src/test/java/org/h2gis/functions/io/json/JsonImportExportTest.java +++ b/h2gis-functions/src/test/java/org/h2gis/functions/io/json/JsonImportExportTest.java @@ -26,13 +26,17 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import java.io.BufferedReader; import java.io.File; +import java.io.FileInputStream; +import java.io.InputStreamReader; import java.nio.file.Files; import java.nio.file.Paths; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import java.util.zip.GZIPInputStream; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -80,12 +84,31 @@ public void testWriteResultSetJson() throws Exception { stat.execute("create table TABLE_POINT(idarea int primary key, the_geom GEOMETRY(POINT), codes ARRAY)"); stat.execute("insert into TABLE_POINT values(1, 'POINT(1 2)', (10000, 20000, 30000, 10000))"); ResultSet rs = stat.executeQuery("SELECT * FROM TABLE_POINT"); - JsonWriteDriver jsonWriteDriver = new JsonWriteDriver(connection); + JsonWriteDriver jsonWriteDriver = new JsonWriteDriver(connection, true ); jsonWriteDriver.write(new EmptyProgressVisitor(), rs, new File("target/result.json")); String result = new String( Files.readAllBytes(Paths.get("target/result.json"))); assertEquals("{\"IDAREA\":1,\"THE_GEOM\":\"POINT (1 2)\",\"CODES\":[10000,20000,30000,10000]}",result); } } + + @Test + public void testWriteResultSetJsonGZ() throws Exception { + try (Statement stat = connection.createStatement()) { + stat.execute("DROP TABLE IF EXISTS TABLE_POINT"); + stat.execute("create table TABLE_POINT(idarea int primary key, the_geom GEOMETRY(POINT), codes ARRAY)"); + stat.execute("insert into TABLE_POINT values(1, 'POINT(1 2)', (10000, 20000, 30000, 10000))"); + ResultSet rs = stat.executeQuery("SELECT * FROM TABLE_POINT"); + JsonWriteDriver jsonWriteDriver = new JsonWriteDriver(connection, true ); + jsonWriteDriver.write(new EmptyProgressVisitor(), rs, new File("target/result.gz")); + File outpuFile = new File("target/result.gz"); + assertTrue(outpuFile.exists()); + GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(outpuFile)); + InputStreamReader reader = new InputStreamReader(gzis); + BufferedReader in = new BufferedReader(reader); + assertEquals("{\"IDAREA\":1,\"THE_GEOM\":\"POINT (1 2)\",\"CODES\":[10000,20000,30000,10000]}",in.readLine()); + gzis.close(); + } + } @Test public void testWriteQueryJson() throws Exception { diff --git a/h2gis-functions/src/test/java/org/h2gis/functions/io/shp/SHPImportExportTest.java b/h2gis-functions/src/test/java/org/h2gis/functions/io/shp/SHPImportExportTest.java index 25b423898d..dcd2337c7e 100644 --- a/h2gis-functions/src/test/java/org/h2gis/functions/io/shp/SHPImportExportTest.java +++ b/h2gis-functions/src/test/java/org/h2gis/functions/io/shp/SHPImportExportTest.java @@ -452,6 +452,28 @@ public void exportImportLineStringZ() throws SQLException, IOException { res.close(); } + @Test + public void exportImportOptions() throws SQLException, IOException { + Statement stat = connection.createStatement(); + File shpFile = new File("target/lineal_export.shp"); + stat.execute("DROP TABLE IF EXISTS LINEAL"); + stat.execute("create table lineal(idarea int primary key, the_geom GEOMETRY(LINESTRING Z))"); + stat.execute("insert into lineal values(1, 'LINESTRING(-10 109 5, 12 6 0)')"); + // Create a shape file using table area + stat.execute("CALL SHPWrite('target/lineal_export.shp', 'LINEAL', true)"); + // Read this shape file to check values + assertTrue(shpFile.exists()); + stat.execute("CALL SHPRead('target/lineal_export.shp', 'IMPORT_LINEAL', true)"); + ResultSet res = stat.executeQuery("SELECT THE_GEOM FROM IMPORT_LINEAL;"); + res.next(); + Geometry geom = (Geometry) res.getObject(1); + Coordinate[] coords = geom.getCoordinates(); + assertEquals(coords[0].z, 5, 10E-1); + //Since the 'NaN' DOUBLE values for Z coordinates is invalid in a shapefile, it is converted to '0.0'. + assertEquals(coords[1].z, 0, 10E-1); + res.close(); + } + @Test public void exportTableWithBadExtensionName() throws SQLException, IOException { assertThrows(SQLException.class, ()-> { diff --git a/h2gis-functions/src/test/java/org/h2gis/functions/io/tsv/TSVDriverTest.java b/h2gis-functions/src/test/java/org/h2gis/functions/io/tsv/TSVDriverTest.java index dcd84d10ba..206ff4eb56 100644 --- a/h2gis-functions/src/test/java/org/h2gis/functions/io/tsv/TSVDriverTest.java +++ b/h2gis-functions/src/test/java/org/h2gis/functions/io/tsv/TSVDriverTest.java @@ -104,7 +104,23 @@ public void testWriteRead() throws SQLException, IOException { assertTrue(rs.next()); assertEquals(20000, rs.getDouble(1), 1e-6); } + } + @Test + public void testWriteReadGZ() throws SQLException, IOException { + Statement stat = connection.createStatement(); + File tsvFile = new File("target/mytsv_export.gz"); + stat.execute("DROP TABLE IF EXISTS myTSV"); + stat.execute("create table myTSV(the_geom GEOMETRY, idarea int primary key)"); + stat.execute("insert into myTSV values('POLYGON ((-10 109, 90 109, 90 9, -10 9, -10 109))', 1)"); + stat.execute("insert into myTSV values('POLYGON ((90 109, 190 109, 190 9, 90 9, 90 109))', 2)"); + stat.execute("CALL TSVWrite('target/mytsv_export.gz', 'myTSV')"); + assertTrue(tsvFile.exists()); + stat.execute("CALL TSVRead('target/mytsv_export.gz', 'TSV_IMPORT');"); + try (ResultSet rs = stat.executeQuery("select SUM(ST_AREA(the_geom::GEOMETRY)) from TSV_IMPORT")) { + assertTrue(rs.next()); + assertEquals(20000, rs.getDouble(1), 1e-6); + } } @Test diff --git a/h2gis-functions/src/test/resources/org/h2gis/functions/io/asc/precip30min.asc.aux.xml b/h2gis-functions/src/test/resources/org/h2gis/functions/io/asc/precip30min.asc.aux.xml new file mode 100644 index 0000000000..8e1cb39a10 --- /dev/null +++ b/h2gis-functions/src/test/resources/org/h2gis/functions/io/asc/precip30min.asc.aux.xml @@ -0,0 +1,10 @@ + + + + 239 + 152.47157190635 + 75 + 35.172128500869 + + + diff --git a/h2gis-functions/src/test/resources/org/h2gis/functions/io/asc/precip30min_center.asc.aux.xml b/h2gis-functions/src/test/resources/org/h2gis/functions/io/asc/precip30min_center.asc.aux.xml new file mode 100644 index 0000000000..86d5a1e02d --- /dev/null +++ b/h2gis-functions/src/test/resources/org/h2gis/functions/io/asc/precip30min_center.asc.aux.xml @@ -0,0 +1,13 @@ + + + Point + + + + 239 + 152.47157190635 + 75 + 35.172128500869 + + + From 6dc466e5a49d5566e581e9134176a4e97ac7a94c Mon Sep 17 00:00:00 2001 From: ebocher Date: Mon, 8 Jun 2020 11:31:04 +0200 Subject: [PATCH 03/10] Fix tests --- .../h2gis/functions/io/geojson/GeojsonImportExportTest.java | 4 ++-- .../test/java/org/h2gis/functions/io/tsv/TSVDriverTest.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/h2gis-functions/src/test/java/org/h2gis/functions/io/geojson/GeojsonImportExportTest.java b/h2gis-functions/src/test/java/org/h2gis/functions/io/geojson/GeojsonImportExportTest.java index 7954ad75c5..0adc39ea46 100644 --- a/h2gis-functions/src/test/java/org/h2gis/functions/io/geojson/GeojsonImportExportTest.java +++ b/h2gis-functions/src/test/java/org/h2gis/functions/io/geojson/GeojsonImportExportTest.java @@ -880,7 +880,7 @@ public void exportQueryImportFileOption() throws SQLException, IOException { stat.execute("DROP TABLE IF EXISTS IMPORT_LINEAL;"); stat.execute("CALL GeoJSONRead('target/lineal_export.geojson', 'IMPORT_LINEAL', true)"); - try (ResultSet res = stat.executeQuery("SELECT COUNT(*) FROM LINEAL_EXPORT;")) { + try (ResultSet res = stat.executeQuery("SELECT COUNT(*) FROM IMPORT_LINEAL;")) { res.next(); assertEquals(1, res.getInt(1)); } @@ -889,7 +889,7 @@ public void exportQueryImportFileOption() throws SQLException, IOException { @Test public void exportQueryImportFileGZOption() throws SQLException, IOException { Statement stat = connection.createStatement(); - File fileOut = new File("target/lineal_export.geojson"); + File fileOut = new File("target/lineal_export.gz"); stat.execute("DROP TABLE IF EXISTS LINEAL"); stat.execute("create table lineal(idarea int primary key, the_geom GEOMETRY(LINESTRING Z))"); stat.execute("insert into lineal values(1, 'LINESTRING(-10 109 5, 12 2 6)'),(2, 'LINESTRING(-15 109 5, 120 2 6)')"); diff --git a/h2gis-functions/src/test/java/org/h2gis/functions/io/tsv/TSVDriverTest.java b/h2gis-functions/src/test/java/org/h2gis/functions/io/tsv/TSVDriverTest.java index 206ff4eb56..1efc4f9255 100644 --- a/h2gis-functions/src/test/java/org/h2gis/functions/io/tsv/TSVDriverTest.java +++ b/h2gis-functions/src/test/java/org/h2gis/functions/io/tsv/TSVDriverTest.java @@ -116,7 +116,7 @@ public void testWriteReadGZ() throws SQLException, IOException { stat.execute("insert into myTSV values('POLYGON ((90 109, 190 109, 190 9, 90 9, 90 109))', 2)"); stat.execute("CALL TSVWrite('target/mytsv_export.gz', 'myTSV')"); assertTrue(tsvFile.exists()); - stat.execute("CALL TSVRead('target/mytsv_export.gz', 'TSV_IMPORT');"); + stat.execute("CALL TSVRead('target/mytsv_export.gz', 'TSV_IMPORT', true);"); try (ResultSet rs = stat.executeQuery("select SUM(ST_AREA(the_geom::GEOMETRY)) from TSV_IMPORT")) { assertTrue(rs.next()); assertEquals(20000, rs.getDouble(1), 1e-6); From cdcfb31ebabbaa7e058cc8c1f289aa31374ae9e7 Mon Sep 17 00:00:00 2001 From: Bocher Date: Mon, 8 Jun 2020 12:42:47 +0200 Subject: [PATCH 04/10] Delete precip30min.asc.aux.xml --- .../org/h2gis/functions/io/asc/precip30min.asc.aux.xml | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 h2gis-functions/src/test/resources/org/h2gis/functions/io/asc/precip30min.asc.aux.xml diff --git a/h2gis-functions/src/test/resources/org/h2gis/functions/io/asc/precip30min.asc.aux.xml b/h2gis-functions/src/test/resources/org/h2gis/functions/io/asc/precip30min.asc.aux.xml deleted file mode 100644 index 8e1cb39a10..0000000000 --- a/h2gis-functions/src/test/resources/org/h2gis/functions/io/asc/precip30min.asc.aux.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - 239 - 152.47157190635 - 75 - 35.172128500869 - - - From f168ecd75c84ddaa8eb2024640a60fbab26e86d1 Mon Sep 17 00:00:00 2001 From: Bocher Date: Mon, 8 Jun 2020 12:43:01 +0200 Subject: [PATCH 05/10] Delete precip30min_center.asc.aux.xml --- .../functions/io/asc/precip30min_center.asc.aux.xml | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 h2gis-functions/src/test/resources/org/h2gis/functions/io/asc/precip30min_center.asc.aux.xml diff --git a/h2gis-functions/src/test/resources/org/h2gis/functions/io/asc/precip30min_center.asc.aux.xml b/h2gis-functions/src/test/resources/org/h2gis/functions/io/asc/precip30min_center.asc.aux.xml deleted file mode 100644 index 86d5a1e02d..0000000000 --- a/h2gis-functions/src/test/resources/org/h2gis/functions/io/asc/precip30min_center.asc.aux.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - Point - - - - 239 - 152.47157190635 - 75 - 35.172128500869 - - - From add705d162ebdca4f2bf16df662283f6939db58c Mon Sep 17 00:00:00 2001 From: ebocher Date: Mon, 8 Jun 2020 15:13:27 +0200 Subject: [PATCH 06/10] Fix space --- .../org/h2gis/functions/io/asc/AscRead.java | 168 +++++++++--------- .../functions/io/asc/AscReaderDriver.java | 114 ++++++------ 2 files changed, 147 insertions(+), 135 deletions(-) diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/asc/AscRead.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/asc/AscRead.java index f41bbe7ff0..dbad148263 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/asc/AscRead.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/asc/AscRead.java @@ -3,22 +3,20 @@ * . H2GIS is developed by CNRS * . * - * This code is part of the H2GIS project. H2GIS is free software; - * you can redistribute it and/or modify it under the terms of the GNU - * Lesser General Public License as published by the Free Software Foundation; - * version 3.0 of the License. + * This code is part of the H2GIS project. H2GIS is free software; you can + * redistribute it and/or modify it under the terms of the GNU Lesser General + * Public License as published by the Free Software Foundation; version 3.0 of + * the License. * - * H2GIS is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - * for more details . + * H2GIS is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details . * * * For more information, please consult: * or contact directly: info_at_h2gis.org */ - - package org.h2gis.functions.io.asc; import org.h2.value.*; @@ -33,41 +31,40 @@ import java.sql.SQLException; /** - * SQL function to import ESRI ASCII Raster file as polygons - * table. + * SQL function to import ESRI ASCII Raster file as polygons table. * * @author Nicolas Fortin (Université Gustave Eiffel 2020) */ public class AscRead extends AbstractFunction implements ScalarFunction { public AscRead() { - addProperty(PROP_REMARKS, "Import ESRI ASCII Raster file as point geometries\n" + - "Pixels are converted into PointZ with Z as the pixel value\n"+ - "CALL ASCREAD('dem.asc');\n" + - "CALL ASCREAD('dem.asc',TYPE);\n" + - "TYPE of z data 1 for integer, 2 for double (default 2)\n"+ - "CALL ASCREAD('dem.asc', 'MYTABLE');\n" + - "CALL ASCREAD('dem.asc', 'MYTABLE', TYPE);\n" + - "TYPE of z data 1 for integer, 2 for double (default 2)"+ - "CALL ASCREAD('dem.asc', 'MYTABLE', GEOM_FILTER, DOWNSCALE_INT, AS_POINTS);\n" + - "GEOM_FILTER - Extract only pixels that intersects the provided geometry envelope, null to disable filter\n" + - "DOWNSCALE_INT - Coefficient used for exporting less cells (1 all cells, 2 for size / 2)\n" + - "AS_POINTS - If true pixels are converted to polygons. (default false return points)\n" + - "CALL ASCREAD('dem.asc', 'MYTABLE', GEOM_FILTER, DOWNSCALE_INT, AS_POINTS);\n"); + addProperty(PROP_REMARKS, "Import ESRI ASCII Raster file as point geometries\n" + + "Pixels are converted into PointZ with Z as the pixel value\n" + + "CALL ASCREAD('dem.asc');\n" + + "CALL ASCREAD('dem.asc',TYPE);\n" + + "TYPE of z data 1 for integer, 2 for double (default 2)\n" + + "CALL ASCREAD('dem.asc', 'MYTABLE');\n" + + "CALL ASCREAD('dem.asc', 'MYTABLE', TYPE);\n" + + "TYPE of z data 1 for integer, 2 for double (default 2)" + + "CALL ASCREAD('dem.asc', 'MYTABLE', GEOM_FILTER, DOWNSCALE_INT, AS_POINTS);\n" + + "GEOM_FILTER - Extract only pixels that intersects the provided geometry envelope, null to disable filter\n" + + "DOWNSCALE_INT - Coefficient used for exporting less cells (1 all cells, 2 for size / 2)\n" + + "AS_POINTS - If true pixels are converted to polygons. (default false return points)\n" + + "CALL ASCREAD('dem.asc', 'MYTABLE', GEOM_FILTER, DOWNSCALE_INT, AS_POINTS);\n"); } @Override public String getJavaStaticMethod() { return "readAscii"; } - + /** * Read the ASCII file. * * @param connection * @param fileName * @throws IOException - * @throws SQLException + * @throws SQLException */ public static void readAscii(Connection connection, String fileName) throws IOException, SQLException { final String name = URIUtilities.fileFromString(fileName).getName(); @@ -81,50 +78,50 @@ public static void readAscii(Connection connection, String fileName) throws IOEx /** * Read the ASCII file. - * + * * @param connection * @param fileName * @param option * @throws IOException - * @throws SQLException + * @throws SQLException */ public static void readAscii(Connection connection, String fileName, Value option) throws IOException, SQLException { - int zType =2; - String tableReference = null; - boolean deletTable = false; - Geometry envelope=null; - if(option instanceof ValueInteger){ + int zType = 2; + String tableReference = null; + boolean deletTable = false; + Geometry envelope = null; + if (option instanceof ValueInteger) { zType = option.getInt(); - if(zType!=1 || zType!=2){ + if (zType != 1 || zType != 2) { throw new SQLException("Please use 1 for integer or 2 for double conversion"); } - }else if (option instanceof ValueVarchar){ + } else if (option instanceof ValueVarchar) { tableReference = option.getString(); - } else if (option instanceof ValueBoolean){ + } else if (option instanceof ValueBoolean) { deletTable = option.getBoolean(); - }else if (option instanceof ValueGeometry){ + } else if (option instanceof ValueGeometry) { envelope = ((ValueGeometry) option).getGeometry(); - } - else if (!(option instanceof ValueNull)){ + } else if (!(option instanceof ValueNull)) { throw new SQLException("Supported optional parameter is integer for z type or varchar for table name"); } - if(tableReference==null){ - final String name = URIUtilities.fileFromString(fileName).getName(); - String tableName = name.substring(0, name.lastIndexOf(".")).toUpperCase(); - if (tableName.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) { - tableReference = tableName; - } else { - throw new SQLException("The file name contains unsupported characters"); - }} + if (tableReference == null) { + final String name = URIUtilities.fileFromString(fileName).getName(); + String tableName = name.substring(0, name.lastIndexOf(".")).toUpperCase(); + if (tableName.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) { + tableReference = tableName; + } else { + throw new SQLException("The file name contains unsupported characters"); + } + } AscDriverFunction ascReaderFunction = new AscDriverFunction(); AscReaderDriver ascReaderDriver = new AscReaderDriver(); - if(envelope != null && !envelope.isEmpty()) { + if (envelope != null && !envelope.isEmpty()) { ascReaderDriver.setExtractEnvelope(envelope.getEnvelopeInternal()); } ascReaderDriver.setZType(zType); ascReaderDriver.setDeleteTable(deletTable); - ascReaderFunction.importFile(connection, tableReference, URIUtilities.fileFromString(fileName), new EmptyProgressVisitor(),ascReaderDriver); + ascReaderFunction.importFile(connection, tableReference, URIUtilities.fileFromString(fileName), new EmptyProgressVisitor(), ascReaderDriver); } /** @@ -138,80 +135,87 @@ else if (!(option instanceof ValueNull)){ * @throws SQLException */ public static void readAscii(Connection connection, String fileName, String tableReference, Value option) throws IOException, SQLException { - int zType =2; - boolean deletTable = false; - Geometry envelope=null; - if(option instanceof ValueInteger){ + int zType = 2; + boolean deletTable = false; + Geometry envelope = null; + if (option instanceof ValueInteger) { zType = option.getInt(); - if(zType!=1 || zType!=2){ + if (zType != 1 || zType != 2) { throw new SQLException("Please use 1 for integer or 2 for double conversion"); } - } else if (option instanceof ValueBoolean){ + } else if (option instanceof ValueBoolean) { deletTable = option.getBoolean(); - }else if (option instanceof ValueGeometry){ + } else if (option instanceof ValueGeometry) { envelope = ((ValueGeometry) option).getGeometry(); - } - else if (!(option instanceof ValueNull)){ + } else if (!(option instanceof ValueNull)) { throw new SQLException("Supported optional parameter is integer for z type or varchar for table name"); } AscDriverFunction ascReaderFunction = new AscDriverFunction(); AscReaderDriver ascReaderDriver = new AscReaderDriver(); - if(envelope != null && !envelope.isEmpty()) { + if (envelope != null && !envelope.isEmpty()) { ascReaderDriver.setExtractEnvelope(envelope.getEnvelopeInternal()); } ascReaderDriver.setAs3DPoint(true); ascReaderDriver.setZType(zType); ascReaderDriver.setDeleteTable(deletTable); - ascReaderFunction.importFile(connection, tableReference, URIUtilities.fileFromString(fileName), new EmptyProgressVisitor(),ascReaderDriver); + ascReaderFunction.importFile(connection, tableReference, URIUtilities.fileFromString(fileName), new EmptyProgressVisitor(), ascReaderDriver); } /** * Import a small subset of ASC file. + * * @param connection * @param fileName * @param tableReference - * @param envelope Extract only pixels that intersects the provided geometry envelope, null to disable filter - * @param downScale Coefficient used for exporting less cells (1 all cells, 2 for size / 2) - * @param extractAsPolygons If true pixels are converted to polygon. (default false) + * @param envelope Extract only pixels that intersects the provided geometry + * envelope, null to disable filter + * @param downScale Coefficient used for exporting less cells (1 all cells, + * 2 for size / 2) + * @param extractAsPolygons If true pixels are converted to polygon. + * (default false) * @throws IOException * @throws SQLException */ public static void readAscii(Connection connection, String fileName, String tableReference, Geometry envelope, int downScale, boolean extractAsPolygons) throws IOException, SQLException { AscDriverFunction ascReaderFunction = new AscDriverFunction(); AscReaderDriver ascReaderDriver = new AscReaderDriver(); - if(envelope != null && !envelope.isEmpty()) { + if (envelope != null && !envelope.isEmpty()) { ascReaderDriver.setExtractEnvelope(envelope.getEnvelopeInternal()); } - if(downScale > 1) { + if (downScale > 1) { ascReaderDriver.setDownScale(downScale); } - if(!extractAsPolygons){ - ascReaderDriver.setAs3DPoint(extractAsPolygons); + if (!extractAsPolygons) { + ascReaderDriver.setAs3DPoint(extractAsPolygons); } ascReaderFunction.importFile(connection, tableReference, URIUtilities.fileFromString(fileName), new EmptyProgressVisitor(), ascReaderDriver); } /** * Import a small subset of ASC file. + * * @param connection * @param fileName * @param tableReference - * @param envelope Extract only pixels that intersects the provided geometry envelope, null to disable filter - * @param downScale Coefficient used for exporting less cells (1 all cells, 2 for size / 2) - * @param extractAsPolygons If true pixels are converted to polygon. (default false) + * @param envelope Extract only pixels that intersects the provided geometry + * envelope, null to disable filter + * @param downScale Coefficient used for exporting less cells (1 all cells, + * 2 for size / 2) + * @param extractAsPolygons If true pixels are converted to polygon. + * (default false) * @throws IOException * @throws SQLException */ public static void readAscii(Connection connection, String fileName, String tableReference, Geometry envelope, int downScale, boolean extractAsPolygons, boolean deleteTable) throws IOException, SQLException { AscDriverFunction ascReaderFunction = new AscDriverFunction(); AscReaderDriver ascReaderDriver = new AscReaderDriver(); - if(envelope != null && !envelope.isEmpty()) { + if (envelope != null && !envelope.isEmpty()) { ascReaderDriver.setExtractEnvelope(envelope.getEnvelopeInternal()); } - if(downScale > 1) { + if (downScale > 1) { ascReaderDriver.setDownScale(downScale); } - if(!extractAsPolygons){ + if (!extractAsPolygons) { ascReaderDriver.setAs3DPoint(extractAsPolygons); } ascReaderDriver.setDeleteTable(deleteTable); @@ -221,25 +225,29 @@ public static void readAscii(Connection connection, String fileName, String tabl /** * Import a small subset of ASC file. + * * @param connection * @param fileName * @param tableReference - * @param envelope Extract only pixels that intersects the provided geometry envelope, null to disable filter - * @param downScale Coefficient used for exporting less cells (1 all cells, 2 for size / 2) - * @param extractAsPolygons If true pixels are converted to polygon. (default false) + * @param envelope Extract only pixels that intersects the provided geometry + * envelope, null to disable filter + * @param downScale Coefficient used for exporting less cells (1 all cells, + * 2 for size / 2) + * @param extractAsPolygons If true pixels are converted to polygon. + * (default false) * @throws IOException * @throws SQLException */ public static void readAscii(Connection connection, String fileName, String tableReference, Geometry envelope, int downScale, boolean extractAsPolygons, boolean deleteTable, String encoding, int zType) throws IOException, SQLException { AscDriverFunction ascReaderFunction = new AscDriverFunction(); AscReaderDriver ascReaderDriver = new AscReaderDriver(); - if(envelope != null && !envelope.isEmpty()) { + if (envelope != null && !envelope.isEmpty()) { ascReaderDriver.setExtractEnvelope(envelope.getEnvelopeInternal()); } - if(downScale > 1) { + if (downScale > 1) { ascReaderDriver.setDownScale(downScale); } - if(!extractAsPolygons){ + if (!extractAsPolygons) { ascReaderDriver.setAs3DPoint(extractAsPolygons); } ascReaderDriver.setEncoding(encoding); diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/asc/AscReaderDriver.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/asc/AscReaderDriver.java index 7aeb3078ad..ec46f44a09 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/asc/AscReaderDriver.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/asc/AscReaderDriver.java @@ -3,26 +3,24 @@ * . H2GIS is developed by CNRS * . * - * This code is part of the H2GIS project. H2GIS is free software; - * you can redistribute it and/or modify it under the terms of the GNU - * Lesser General Public License as published by the Free Software Foundation; - * version 3.0 of the License. + * This code is part of the H2GIS project. H2GIS is free software; you can + * redistribute it and/or modify it under the terms of the GNU Lesser General + * Public License as published by the Free Software Foundation; version 3.0 of + * the License. * - * H2GIS is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - * for more details . + * H2GIS is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details . * * * For more information, please consult: * or contact directly: info_at_h2gis.org */ - package org.h2gis.functions.io.asc; import org.h2gis.api.EmptyProgressVisitor; import org.h2gis.api.ProgressVisitor; -import org.h2gis.functions.io.utility.FileUtil; import org.h2gis.utilities.JDBCUtilities; import org.h2gis.utilities.TableLocation; import org.locationtech.jts.geom.Coordinate; @@ -36,7 +34,6 @@ import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Statement; -import java.sql.Types; import java.util.NoSuchElementException; import java.util.Scanner; import java.util.zip.GZIPInputStream; @@ -46,26 +43,27 @@ * * This class is written to directly access the ESRI ascii grid format. * - * The ASCII grid data file format comprises a few lines of header data followed - * by lists of cell values. The header data includes the following keywords and - * values: + * The ASCII grid data file format comprises a few lines of header data followed + * by lists of cell values. The header data includes the following keywords and + * values: * - * ncols : number of columns in the data set. + * ncols : number of columns in the data set. * - * nrows : number of rows in the data set. + * nrows : number of rows in the data set. * - * xllcorner : x-coordinate of the west border of the LowerLeft corner. + * xllcorner : x-coordinate of the west border of the LowerLeft corner. * - * yllcorner : y-coordinate of the south border of the LowerLeft corner. + * yllcorner : y-coordinate of the south border of the LowerLeft corner. * - * cellsize : size of the square cell of the data set. + * cellsize : size of the square cell of the data set. * - * NODATA_value : arbitrary value assigned to unknown cells. + * NODATA_value : arbitrary value assigned to unknown cells. * * @author Nicolas Fortin (Université Gustave Eiffel 2020) * @author Erwan Bocher, CNRS, 2020 */ public class AscReaderDriver { + private static final int BATCH_MAX_SIZE = 100; private static final int BUFFER_SIZE = 16384; private boolean as3DPoint = true; @@ -82,18 +80,20 @@ public class AscReaderDriver { private double noData; private int zType = 1; private boolean deleteTable = false; - private String encoding ="UTF-8"; - private boolean importNodata=false; + private String encoding = "UTF-8"; + private boolean importNodata = false; /** - * @return If true ASC is imported as 3D points cloud, Raster is imported in pixel polygons otherwise. + * @return If true ASC is imported as 3D points cloud, Raster is imported in + * pixel polygons otherwise. */ public boolean isAs3DPoint() { return as3DPoint; } /** - * @param as3DPoint If true ASC is imported as 3D points cloud, Raster is imported in pixel polygons otherwise. + * @param as3DPoint If true ASC is imported as 3D points cloud, Raster is + * imported in pixel polygons otherwise. */ public void setAs3DPoint(boolean as3DPoint) { this.as3DPoint = as3DPoint; @@ -107,21 +107,24 @@ public Envelope getExtractEnvelope() { } /** - * @param extractEnvelope Imported geometries are filtered using this optional envelope. Set Null object for no filtering. + * @param extractEnvelope Imported geometries are filtered using this + * optional envelope. Set Null object for no filtering. */ public void setExtractEnvelope(Envelope extractEnvelope) { this.extractEnvelope = extractEnvelope; } /** - * @return Coefficient used for exporting less cells (1 all cells, 2 for size / 2) + * @return Coefficient used for exporting less cells (1 all cells, 2 for + * size / 2) */ public int getDownScale() { return downScale; } /** - * @param downScale Coefficient used for exporting less cells (1 all cells, 2 for size / 2) + * @param downScale Coefficient used for exporting less cells (1 all cells, + * 2 for size / 2) */ public void setDownScale(int downScale) { this.downScale = downScale; @@ -195,10 +198,11 @@ private void readHeader(Scanner scanner) throws IOException { readFirst = true; // XXX lastWord = scanner.next(); - noData= Double.parseDouble(lastWord); + noData = Double.parseDouble(lastWord); } } + /** * Read asc file * @@ -211,8 +215,8 @@ private void readHeader(Scanner scanner) throws IOException { * @throws IOException */ public void read(Connection connection, File fileName, ProgressVisitor progress, String tableReference, - int srid) throws SQLException, IOException { - if (fileName!=null && fileName.getName().toLowerCase().endsWith(".asc")) { + int srid) throws SQLException, IOException { + if (fileName != null && fileName.getName().toLowerCase().endsWith(".asc")) { if (!fileName.exists()) { throw new SQLException("The file " + tableReference + " doesn't exist "); } @@ -223,11 +227,10 @@ public void read(Connection connection, File fileName, ProgressVisitor progress, stmt.execute("DROP TABLE IF EXISTS " + requestedTable.toString(isH2)); stmt.close(); } - try(FileInputStream inputStream = new FileInputStream(fileName)) { + try (FileInputStream inputStream = new FileInputStream(fileName)) { readAsc(connection, inputStream, progress, requestedTable, isH2, srid); } - } - else if (fileName!=null && fileName.getName().toLowerCase().endsWith(".gz")) { + } else if (fileName != null && fileName.getName().toLowerCase().endsWith(".gz")) { if (!fileName.exists()) { throw new SQLException("The file " + tableReference + " doesn't exist "); } @@ -240,14 +243,14 @@ else if (fileName!=null && fileName.getName().toLowerCase().endsWith(".gz")) { } FileInputStream fis = new FileInputStream(fileName); readAsc(connection, new GZIPInputStream(fis), progress, requestedTable, isH2, srid); - } - else{ + } else { throw new SQLException("The asc read driver supports only asc or gz extensions"); } } /** * Read the ascii file from inpustream + * * @param connection * @param inputStream * @param progress @@ -257,9 +260,9 @@ else if (fileName!=null && fileName.getName().toLowerCase().endsWith(".gz")) { * @throws UnsupportedEncodingException * @throws SQLException */ - private void readAsc(Connection connection, InputStream inputStream, ProgressVisitor progress, TableLocation requestedTable,boolean isH2, - int srid) throws UnsupportedEncodingException, SQLException { - BufferedReader reader = new BufferedReader(new InputStreamReader(new BufferedInputStream(inputStream,BUFFER_SIZE), encoding)); + private void readAsc(Connection connection, InputStream inputStream, ProgressVisitor progress, TableLocation requestedTable, boolean isH2, + int srid) throws UnsupportedEncodingException, SQLException { + BufferedReader reader = new BufferedReader(new InputStreamReader(new BufferedInputStream(inputStream, BUFFER_SIZE), encoding)); try { Scanner scanner = new Scanner(reader); // Read HEADER @@ -269,12 +272,12 @@ private void readAsc(Connection connection, InputStream inputStream, ProgressVis PreparedStatement preparedStatement; if (as3DPoint) { if (zType == 1) { - st.execute("CREATE TABLE " + requestedTable.toString(isH2)+"(PK SERIAL NOT NULL, THE_GEOM GEOMETRY(POINTZ, " + srid + "), Z integer," + " CONSTRAINT ASC_PK PRIMARY KEY (PK))"); + st.execute("CREATE TABLE " + requestedTable.toString(isH2) + "(PK SERIAL NOT NULL, THE_GEOM GEOMETRY(POINTZ, " + srid + "), Z integer," + " CONSTRAINT ASC_PK PRIMARY KEY (PK))"); } else { st.execute("CREATE TABLE " + requestedTable.toString(isH2) + "(PK SERIAL NOT NULL, THE_GEOM GEOMETRY(POINTZ, " + srid + "), Z double precision," + " CONSTRAINT ASC_PK PRIMARY KEY (PK))"); } - preparedStatement = connection.prepareStatement("INSERT INTO " + requestedTable.toString(isH2) + - "(the_geom, Z) VALUES (?, ?)"); + preparedStatement = connection.prepareStatement("INSERT INTO " + requestedTable.toString(isH2) + + "(the_geom, Z) VALUES (?, ?)"); } else { if (zType == 1) { st.execute("CREATE TABLE " + requestedTable.toString(isH2) + "(PK SERIAL NOT NULL, THE_GEOM GEOMETRY(POLYGONZ, " + srid + "),Z integer, " + " CONSTRAINT ASC_PK PRIMARY KEY (PK))"); @@ -282,8 +285,8 @@ private void readAsc(Connection connection, InputStream inputStream, ProgressVis } else { st.execute("CREATE TABLE " + requestedTable.toString(isH2) + "(PK SERIAL NOT NULL, THE_GEOM GEOMETRY(POLYGONZ, " + srid + "),Z double precision, " + " CONSTRAINT ASC_PK PRIMARY KEY (PK))"); } - preparedStatement = connection.prepareStatement("INSERT INTO " + requestedTable.toString(isH2) + - "(the_geom, Z) VALUES (?, ?)"); + preparedStatement = connection.prepareStatement("INSERT INTO " + requestedTable.toString(isH2) + + "(the_geom, Z) VALUES (?, ?)"); } // Read data GeometryFactory factory = new GeometryFactory(); @@ -318,12 +321,12 @@ private void readAsc(Connection connection, InputStream inputStream, ProgressVis if (as3DPoint) { Point cell = factory.createPoint(new Coordinate(x + cellSize / 2, y - cellSize / 2, z)); cell.setSRID(srid); - if (Math.abs(noData - z)!=0) { + if (Math.abs(noData - z) != 0) { preparedStatement.setObject(1, cell); preparedStatement.setObject(2, z); preparedStatement.addBatch(); batchSize++; - } else if(importNodata) { + } else if (importNodata) { preparedStatement.setObject(1, cell); preparedStatement.setObject(2, noData); preparedStatement.addBatch(); @@ -332,12 +335,12 @@ private void readAsc(Connection connection, InputStream inputStream, ProgressVis } else { Polygon cell = factory.createPolygon(new Coordinate[]{new Coordinate(x, y, z), new Coordinate(x, y - cellSize * downScale, z), new Coordinate(x + cellSize * downScale, y - cellSize * downScale, z), new Coordinate(x + cellSize * downScale, y, z), new Coordinate(x, y, z)}); cell.setSRID(srid); - if (Math.abs(noData - z)!=0) { + if (Math.abs(noData - z) != 0) { preparedStatement.setObject(1, cell); preparedStatement.setObject(2, z); preparedStatement.addBatch(); batchSize++; - } else if(importNodata){ + } else if (importNodata) { preparedStatement.setObject(1, cell); preparedStatement.setObject(2, noData); preparedStatement.addBatch(); @@ -365,17 +368,17 @@ private void readAsc(Connection connection, InputStream inputStream, ProgressVis } /** - * Use to set the z conversion type - * 1 = integer - * 2 = double + * Use to set the z conversion type 1 = integer 2 = double + * * @param zType */ public void setZType(int zType) { - this.zType =zType; + this.zType = zType; } /** * Set true to delete the input table if exists + * * @param deleteTable */ public void setDeleteTable(boolean deleteTable) { @@ -384,18 +387,19 @@ public void setDeleteTable(boolean deleteTable) { /** * Set encoding + * * @param encoding */ public void setEncoding(String encoding) { - this.encoding=encoding; + this.encoding = encoding; } /** - * Set to true if nodata must be imported. - * Default is false + * Set to true if nodata must be imported. Default is false + * * @param importNodata */ public void setImportNodata(boolean importNodata) { - this.importNodata=importNodata; + this.importNodata = importNodata; } } From c3fc8682752a7043f95962aa92c7d5bd52e9f7c9 Mon Sep 17 00:00:00 2001 From: ebocher Date: Mon, 8 Jun 2020 15:14:46 +0200 Subject: [PATCH 07/10] Fix spaces --- .../functions/io/asc/AscDriverFunction.java | 38 +++-- .../functions/io/tsv/TSVDriverFunction.java | 134 ++++++++---------- .../org/h2gis/functions/io/tsv/TSVRead.java | 78 +++++----- .../org/h2gis/functions/io/tsv/TSVWrite.java | 53 ++++--- 4 files changed, 146 insertions(+), 157 deletions(-) diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/asc/AscDriverFunction.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/asc/AscDriverFunction.java index b40e0d9fe6..d423b66d32 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/asc/AscDriverFunction.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/asc/AscDriverFunction.java @@ -3,21 +3,20 @@ * . H2GIS is developed by CNRS * . * - * This code is part of the H2GIS project. H2GIS is free software; - * you can redistribute it and/or modify it under the terms of the GNU - * Lesser General Public License as published by the Free Software Foundation; - * version 3.0 of the License. + * This code is part of the H2GIS project. H2GIS is free software; you can + * redistribute it and/or modify it under the terms of the GNU Lesser General + * Public License as published by the Free Software Foundation; version 3.0 of + * the License. * - * H2GIS is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - * for more details . + * H2GIS is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details . * * * For more information, please consult: * or contact directly: info_at_h2gis.org */ - package org.h2gis.functions.io.asc; import org.h2gis.api.DriverFunction; @@ -27,7 +26,6 @@ import org.h2gis.utilities.TableLocation; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.sql.Connection; import java.sql.SQLException; @@ -35,7 +33,7 @@ /** * Asc driver to import ESRI ASCII Raster file as polygons - * + * * @author Nicolas Fortin (Université Gustave Eiffel 2020) */ public class AscDriverFunction implements DriverFunction { @@ -70,7 +68,7 @@ public boolean isSpatialFormat(String extension) { } @Override - public void exportTable(Connection connection, String tableReference, File fileName, ProgressVisitor progress) throws SQLException, IOException{ + public void exportTable(Connection connection, String tableReference, File fileName, ProgressVisitor progress) throws SQLException, IOException { throw new UnsupportedOperationException("Not supported yet."); } @@ -85,7 +83,7 @@ public void exportTable(Connection connection, String tableReference, File fileN } @Override - public void exportTable(Connection connection, String tableReference, File fileName, String encoding,ProgressVisitor progress) throws SQLException, IOException{ + public void exportTable(Connection connection, String tableReference, File fileName, String encoding, ProgressVisitor progress) throws SQLException, IOException { throw new UnsupportedOperationException("Not supported yet."); } @@ -102,8 +100,8 @@ public void importFile(Connection connection, String tableReference, File fileNa String filePath = fileName.getAbsolutePath(); final int dotIndex = filePath.lastIndexOf('.'); final String fileNamePrefix = filePath.substring(0, dotIndex); - File prjFile = new File(fileNamePrefix+".prj"); - if(prjFile.exists()) { + File prjFile = new File(fileNamePrefix + ".prj"); + if (prjFile.exists()) { srid = PRJUtil.getSRID(prjFile); } ascReaderDriver.read(connection, fileName, progress, tableReference, srid); @@ -111,15 +109,15 @@ public void importFile(Connection connection, String tableReference, File fileNa } @Override - public void importFile(Connection connection, String tableReference, File fileName, String options, ProgressVisitor progress - ) throws SQLException, IOException { + public void importFile(Connection connection, String tableReference, File fileName, String options, ProgressVisitor progress + ) throws SQLException, IOException { importFile(connection, tableReference, fileName, progress); } @Override - public void importFile(Connection connection, String tableReference, File fileName, boolean deleteTables, ProgressVisitor progress - ) throws SQLException, IOException { - if(deleteTables) { + public void importFile(Connection connection, String tableReference, File fileName, boolean deleteTables, ProgressVisitor progress + ) throws SQLException, IOException { + if (deleteTables) { final boolean isH2 = JDBCUtilities.isH2DataBase(connection); TableLocation requestedTable = TableLocation.parse(tableReference, isH2); Statement stmt = connection.createStatement(); diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVDriverFunction.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVDriverFunction.java index f867103963..d59381a6b6 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVDriverFunction.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVDriverFunction.java @@ -3,22 +3,20 @@ * . H2GIS is developed by CNRS * . * - * This code is part of the H2GIS project. H2GIS is free software; - * you can redistribute it and/or modify it under the terms of the GNU - * Lesser General Public License as published by the Free Software Foundation; - * version 3.0 of the License. + * This code is part of the H2GIS project. H2GIS is free software; you can + * redistribute it and/or modify it under the terms of the GNU Lesser General + * Public License as published by the Free Software Foundation; version 3.0 of + * the License. * - * H2GIS is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - * for more details . + * H2GIS is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details . * * * For more information, please consult: * or contact directly: info_at_h2gis.org */ - - package org.h2gis.functions.io.tsv; import org.h2.tools.Csv; @@ -59,15 +57,14 @@ * @author Erwan Bocher * @author Sylvain PALOMINOS (UBS 2019) */ -public class TSVDriverFunction implements DriverFunction{ +public class TSVDriverFunction implements DriverFunction { public static String DESCRIPTION = "TSV file (Tab Separated Values)"; private static final int BATCH_MAX_SIZE = 200; - - + @Override public IMPORT_DRIVER_TYPE getImportDriverType() { - return IMPORT_DRIVER_TYPE.COPY; + return IMPORT_DRIVER_TYPE.COPY; } @Override @@ -96,12 +93,12 @@ public boolean isSpatialFormat(String extension) { @Override public void exportTable(Connection connection, String tableReference, File fileName, ProgressVisitor progress) throws SQLException, IOException { - exportTable( connection, tableReference, fileName, null, false, progress); + exportTable(connection, tableReference, fileName, null, false, progress); } @Override public void exportTable(Connection connection, String tableReference, File fileName, boolean deleteFiles, ProgressVisitor progress) throws SQLException, IOException { - exportTable( connection, tableReference, fileName, null, deleteFiles, progress); + exportTable(connection, tableReference, fileName, null, deleteFiles, progress); } @Override @@ -121,43 +118,42 @@ public void exportTable(Connection connection, String tableReference, File fileN exportFromResultSet(connection, st.executeQuery(tableReference), bw, encoding, new EmptyProgressVisitor()); } } - }else if(FileUtil.isExtensionWellFormated(fileName, "gz")) { - if(deleteFiles){ - Files.deleteIfExists(fileName.toPath()); - } - final boolean isH2 = JDBCUtilities.isH2DataBase(connection); - TableLocation location = TableLocation.parse(tableReference, isH2); - try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( - new GZIPOutputStream(new FileOutputStream(fileName))))) { - try (Statement st = connection.createStatement()) { - JDBCUtilities.attachCancelResultSet(st, progress); - exportFromResultSet(connection, st.executeQuery(tableReference), bw,encoding,new EmptyProgressVisitor()); - } - } - }else if(FileUtil.isExtensionWellFormated(fileName, "zip")) { - if(deleteFiles){ - Files.deleteIfExists(fileName.toPath()); - } - final boolean isH2 = JDBCUtilities.isH2DataBase(connection); - TableLocation location = TableLocation.parse(tableReference, isH2); - try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( - new ZipOutputStream(new FileOutputStream(fileName))))) { - try (Statement st = connection.createStatement()) { - JDBCUtilities.attachCancelResultSet(st, progress); - exportFromResultSet(connection, st.executeQuery(tableReference), bw,encoding,new EmptyProgressVisitor()); - } + } else if (FileUtil.isExtensionWellFormated(fileName, "gz")) { + if (deleteFiles) { + Files.deleteIfExists(fileName.toPath()); + } + final boolean isH2 = JDBCUtilities.isH2DataBase(connection); + TableLocation location = TableLocation.parse(tableReference, isH2); + try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( + new GZIPOutputStream(new FileOutputStream(fileName))))) { + try (Statement st = connection.createStatement()) { + JDBCUtilities.attachCancelResultSet(st, progress); + exportFromResultSet(connection, st.executeQuery(tableReference), bw, encoding, new EmptyProgressVisitor()); } } - else { - throw new SQLException("Only .tsv, .gz or .zip extensions are supported"); + } else if (FileUtil.isExtensionWellFormated(fileName, "zip")) { + if (deleteFiles) { + Files.deleteIfExists(fileName.toPath()); + } + final boolean isH2 = JDBCUtilities.isH2DataBase(connection); + TableLocation location = TableLocation.parse(tableReference, isH2); + try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( + new ZipOutputStream(new FileOutputStream(fileName))))) { + try (Statement st = connection.createStatement()) { + JDBCUtilities.attachCancelResultSet(st, progress); + exportFromResultSet(connection, st.executeQuery(tableReference), bw, encoding, new EmptyProgressVisitor()); + } } + } else { + throw new SQLException("Only .tsv, .gz or .zip extensions are supported"); + } } else { throw new SQLException("The select query must be enclosed in parenthesis: '(SELECT * FROM ORDERS)'."); } } else { if (FileUtil.isExtensionWellFormated(fileName, "tsv")) { - if(deleteFiles){ + if (deleteFiles) { Files.deleteIfExists(fileName.toPath()); } final boolean isH2 = JDBCUtilities.isH2DataBase(connection); @@ -168,9 +164,8 @@ public void exportTable(Connection connection, String tableReference, File fileN exportFromResultSet(connection, st.executeQuery("SELECT * FROM " + location.toString()), bw, encoding, new EmptyProgressVisitor()); } } - } - else if(FileUtil.isExtensionWellFormated(fileName, "gz")) { - if(deleteFiles){ + } else if (FileUtil.isExtensionWellFormated(fileName, "gz")) { + if (deleteFiles) { Files.deleteIfExists(fileName.toPath()); } final boolean isH2 = JDBCUtilities.isH2DataBase(connection); @@ -179,11 +174,11 @@ else if(FileUtil.isExtensionWellFormated(fileName, "gz")) { new GZIPOutputStream(new FileOutputStream(fileName))))) { try (Statement st = connection.createStatement()) { JDBCUtilities.attachCancelResultSet(st, progress); - exportFromResultSet(connection, st.executeQuery("SELECT * FROM " + location.toString()), bw,encoding,new EmptyProgressVisitor()); + exportFromResultSet(connection, st.executeQuery("SELECT * FROM " + location.toString()), bw, encoding, new EmptyProgressVisitor()); } } - }else if(FileUtil.isExtensionWellFormated(fileName, "zip")) { - if(deleteFiles){ + } else if (FileUtil.isExtensionWellFormated(fileName, "zip")) { + if (deleteFiles) { Files.deleteIfExists(fileName.toPath()); } final boolean isH2 = JDBCUtilities.isH2DataBase(connection); @@ -192,11 +187,10 @@ else if(FileUtil.isExtensionWellFormated(fileName, "gz")) { new ZipOutputStream(new FileOutputStream(fileName))))) { try (Statement st = connection.createStatement()) { JDBCUtilities.attachCancelResultSet(st, progress); - exportFromResultSet(connection, st.executeQuery("SELECT * FROM " + location.toString()), bw,encoding,new EmptyProgressVisitor()); + exportFromResultSet(connection, st.executeQuery("SELECT * FROM " + location.toString()), bw, encoding, new EmptyProgressVisitor()); } } - } - else { + } else { throw new SQLException("Only .tsv, .gz or .zip extensions are supported"); } } @@ -204,20 +198,20 @@ else if(FileUtil.isExtensionWellFormated(fileName, "gz")) { /** * Export a table or a query to as TSV file - * + * * @param connection Active connection, do not close this connection. * @param tableReference [[catalog.]schema.]table reference * @param fileName File path to read * @param progress * @param encoding * @throws SQLException - * @throws IOException + * @throws IOException */ @Override public void exportTable(Connection connection, String tableReference, File fileName, String encoding, ProgressVisitor progress) throws SQLException, IOException { - exportTable( connection, tableReference, fileName, encoding, false, progress); + exportTable(connection, tableReference, fileName, encoding, false, progress); } - + /** * Export a resultset to a TSV file * @@ -240,30 +234,30 @@ public void exportFromResultSet(Connection connection, ResultSet res, Writer wri @Override public void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress) throws SQLException, IOException { - importFile( connection, tableReference, fileName, null, false, progress); + importFile(connection, tableReference, fileName, null, false, progress); } @Override public void importFile(Connection connection, String tableReference, File fileName, - String options, ProgressVisitor progress) throws SQLException, IOException { - importFile( connection, tableReference, fileName, options, false, progress); + String options, ProgressVisitor progress) throws SQLException, IOException { + importFile(connection, tableReference, fileName, options, false, progress); } @Override public void importFile(Connection connection, String tableReference, File fileName, - boolean deleteTables, ProgressVisitor progress) throws SQLException, IOException { - importFile( connection, tableReference, fileName, null, deleteTables, progress); + boolean deleteTables, ProgressVisitor progress) throws SQLException, IOException { + importFile(connection, tableReference, fileName, null, deleteTables, progress); } @Override public void importFile(Connection connection, String tableReference, File fileName, String options, boolean deleteTables, ProgressVisitor progress) throws SQLException, IOException { final boolean isH2 = JDBCUtilities.isH2DataBase(connection); TableLocation requestedTable = TableLocation.parse(tableReference, isH2); - if (fileName!=null && fileName.getName().toLowerCase().endsWith(".tsv")) { - if(!fileName.exists()){ + if (fileName != null && fileName.getName().toLowerCase().endsWith(".tsv")) { + if (!fileName.exists()) { throw new SQLException("The file " + requestedTable + " doesn't exist "); } - if(deleteTables){ + if (deleteTables) { Statement stmt = connection.createStatement(); stmt.execute("DROP TABLE IF EXISTS " + requestedTable); stmt.close(); @@ -340,12 +334,11 @@ public void importFile(Connection connection, String tableReference, File fileNa } finally { pst.close(); } - } - else if (fileName!=null && fileName.getName().toLowerCase().endsWith(".gz")) { - if(!fileName.exists()){ + } else if (fileName != null && fileName.getName().toLowerCase().endsWith(".gz")) { + if (!fileName.exists()) { throw new SQLException("The file " + requestedTable + " doesn't exist "); } - if(deleteTables){ + if (deleteTables) { Statement stmt = connection.createStatement(); stmt.execute("DROP TABLE IF EXISTS " + requestedTable); stmt.close(); @@ -405,8 +398,7 @@ else if (fileName!=null && fileName.getName().toLowerCase().endsWith(".gz")) { pst.close(); } } - } - else{ + } else { throw new SQLException("The TSV read driver supports only tsv or gz extensions"); } } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVRead.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVRead.java index 2659621bb5..4322ebb10a 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVRead.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVRead.java @@ -3,22 +3,20 @@ * . H2GIS is developed by CNRS * . * - * This code is part of the H2GIS project. H2GIS is free software; - * you can redistribute it and/or modify it under the terms of the GNU - * Lesser General Public License as published by the Free Software Foundation; - * version 3.0 of the License. + * This code is part of the H2GIS project. H2GIS is free software; you can + * redistribute it and/or modify it under the terms of the GNU Lesser General + * Public License as published by the Free Software Foundation; version 3.0 of + * the License. * - * H2GIS is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - * for more details . + * H2GIS is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details . * * * For more information, please consult: * or contact directly: info_at_h2gis.org */ - - package org.h2gis.functions.io.tsv; import org.h2.value.Value; @@ -28,10 +26,8 @@ import org.h2gis.api.AbstractFunction; import org.h2gis.api.EmptyProgressVisitor; import org.h2gis.api.ScalarFunction; -import org.h2gis.functions.io.utility.FileUtil; import org.h2gis.utilities.URIUtilities; -import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Connection; @@ -39,21 +35,22 @@ /** * Read a Tab-separated values file + * * @author Erwan Bocher */ -public class TSVRead extends AbstractFunction implements ScalarFunction{ +public class TSVRead extends AbstractFunction implements ScalarFunction { public TSVRead() { - addProperty(PROP_REMARKS, "Read a Tab-separated values file." + - "\n TSVRead(..."+ - "\n Supported arguments :" + - "\n path of the file" + - "\n path of the file, table name"+ - "\n path of the file, true for delete the table with the same file name"+ - "\n path of the file, table name, true to delete the table name"+ - "\n path of the file, table name, true to delete the table name"+ - "\n path of the file, table name, encoding chartset"+ - "\n path of the file, table name, encoding chartset, true to delete the table name"); + addProperty(PROP_REMARKS, "Read a Tab-separated values file." + + "\n TSVRead(..." + + "\n Supported arguments :" + + "\n path of the file" + + "\n path of the file, table name" + + "\n path of the file, true for delete the table with the same file name" + + "\n path of the file, table name, true to delete the table name" + + "\n path of the file, table name, true to delete the table name" + + "\n path of the file, table name, encoding chartset" + + "\n path of the file, table name, encoding chartset, true to delete the table name"); } @Override @@ -63,38 +60,40 @@ public String getJavaStaticMethod() { /** * Copy data from TSV File into a new table in specified connection. + * * @param connection * @param fileName - * @param option table name or true to delete it + * @param option table name or true to delete it * @throws SQLException * @throws FileNotFoundException - * @throws IOException + * @throws IOException */ public static void importTable(Connection connection, String fileName, Value option) throws SQLException, FileNotFoundException, IOException { - String tableReference =null; - boolean deleteTable = false; - if(option instanceof ValueBoolean){ + String tableReference = null; + boolean deleteTable = false; + if (option instanceof ValueBoolean) { deleteTable = option.getBoolean(); - }else if (option instanceof ValueVarchar){ + } else if (option instanceof ValueVarchar) { tableReference = option.getString(); - }else if (!(option instanceof ValueNull)){ + } else if (!(option instanceof ValueNull)) { throw new SQLException("Supported optional parameter is boolean or varchar"); } - importTable( connection, fileName, tableReference,null, deleteTable); + importTable(connection, fileName, tableReference, null, deleteTable); } public static void importTable(Connection connection, String fileName, String tableReference, Value option) throws SQLException, FileNotFoundException, IOException { - String encoding =null; - boolean deleteTable = false; - if(option instanceof ValueBoolean){ + String encoding = null; + boolean deleteTable = false; + if (option instanceof ValueBoolean) { deleteTable = option.getBoolean(); - }else if (option instanceof ValueVarchar){ + } else if (option instanceof ValueVarchar) { encoding = option.getString(); - }else if (!(option instanceof ValueNull)){ + } else if (!(option instanceof ValueNull)) { throw new SQLException("Supported optional parameter is boolean or varchar"); } - importTable(connection, fileName, tableReference,encoding, deleteTable); + importTable(connection, fileName, tableReference, encoding, deleteTable); } + /** * * @param connection @@ -108,15 +107,16 @@ public static void importTable(Connection connection, String fileName, String ta */ public static void importTable(Connection connection, String fileName, String tableReference, String encoding, boolean deleteTable) throws SQLException, FileNotFoundException, IOException { TSVDriverFunction tsvDriver = new TSVDriverFunction(); - tsvDriver.importFile(connection, tableReference, URIUtilities.fileFromString(fileName),encoding,deleteTable, new EmptyProgressVisitor()); + tsvDriver.importFile(connection, tableReference, URIUtilities.fileFromString(fileName), encoding, deleteTable, new EmptyProgressVisitor()); } /** * Copy data from TSV File into a new table in specified connection. + * * @param connection * @param fileName * @throws IOException - * @throws SQLException + * @throws SQLException */ public static void importTable(Connection connection, String fileName) throws IOException, SQLException { final String name = URIUtilities.fileFromString(fileName).getName(); diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVWrite.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVWrite.java index bd6cc340fa..f913c545c9 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVWrite.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/tsv/TSVWrite.java @@ -3,22 +3,20 @@ * . H2GIS is developed by CNRS * . * - * This code is part of the H2GIS project. H2GIS is free software; - * you can redistribute it and/or modify it under the terms of the GNU - * Lesser General Public License as published by the Free Software Foundation; - * version 3.0 of the License. + * This code is part of the H2GIS project. H2GIS is free software; you can + * redistribute it and/or modify it under the terms of the GNU Lesser General + * Public License as published by the Free Software Foundation; version 3.0 of + * the License. * - * H2GIS is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - * for more details . + * H2GIS is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details . * * * For more information, please consult: * or contact directly: info_at_h2gis.org */ - - package org.h2gis.functions.io.tsv; import org.h2.value.Value; @@ -36,18 +34,19 @@ /** * Write a Tab-separated values file + * * @author Erwan Bocher */ public class TSVWrite extends AbstractFunction implements ScalarFunction { public TSVWrite() { - addProperty(PROP_REMARKS, "Write a Tab-separated values file."+ - "\n TSVWrite(..."+ - "\n Supported arguments :" + - "\n path of the file, table name"+ - "\n path of the file, table name, true to delete the file if exists"+ - "\n path of the file, table name, encoding chartset"+ - "\n path of the file, table name, encoding chartset, true to delete the file if exists"); + addProperty(PROP_REMARKS, "Write a Tab-separated values file." + + "\n TSVWrite(..." + + "\n Supported arguments :" + + "\n path of the file, table name" + + "\n path of the file, table name, true to delete the file if exists" + + "\n path of the file, table name, encoding chartset" + + "\n path of the file, table name, encoding chartset, true to delete the file if exists"); } @Override @@ -65,32 +64,32 @@ public String getJavaStaticMethod() { * @throws IOException */ public static void exportTable(Connection connection, String fileName, String tableReference) throws SQLException, IOException { - exportTable( connection, fileName, tableReference, null, false); + exportTable(connection, fileName, tableReference, null, false); } - - /** * Read a table and write it into a tsv file. + * * @param connection Active connection * @param fileName Shape file name or URI - * @param tableReference Table name or select query - * Note : The select query must be enclosed in parenthesis - * @param option Could be string file encoding charset or boolean value to delete the existing file + * @param tableReference Table name or select query Note : The select query + * must be enclosed in parenthesis + * @param option Could be string file encoding charset or boolean value to + * delete the existing file * @throws IOException * @throws SQLException */ public static void exportTable(Connection connection, String fileName, String tableReference, Value option) throws IOException, SQLException { String encoding = null; boolean deleteFiles = false; - if(option instanceof ValueBoolean){ + if (option instanceof ValueBoolean) { deleteFiles = option.getBoolean(); - }else if (option instanceof ValueVarchar){ + } else if (option instanceof ValueVarchar) { encoding = option.toString(); - }else if (!(option instanceof ValueNull)){ + } else if (!(option instanceof ValueNull)) { throw new SQLException("Supported optional parameter is boolean or varchar"); } - exportTable( connection, fileName, tableReference, encoding, deleteFiles); + exportTable(connection, fileName, tableReference, encoding, deleteFiles); } /** From d1b1fd1a96bfb5c3ace8d3cd78ff79d3bc713db6 Mon Sep 17 00:00:00 2001 From: ebocher Date: Mon, 8 Jun 2020 15:16:35 +0200 Subject: [PATCH 08/10] Fix spaces --- .../functions/io/geojson/GeoJsonRead.java | 75 +++++---- .../io/geojson/GeoJsonReaderDriver.java | 35 ++-- .../functions/io/geojson/GeoJsonWrite.java | 60 +++---- .../io/geojson/GeoJsonWriteDriver.java | 152 +++++++++--------- 4 files changed, 155 insertions(+), 167 deletions(-) diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonRead.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonRead.java index 2fc71f5c6a..eeb4832013 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonRead.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonRead.java @@ -3,21 +3,20 @@ * . H2GIS is developed by CNRS * . * - * This code is part of the H2GIS project. H2GIS is free software; - * you can redistribute it and/or modify it under the terms of the GNU - * Lesser General Public License as published by the Free Software Foundation; - * version 3.0 of the License. + * This code is part of the H2GIS project. H2GIS is free software; you can + * redistribute it and/or modify it under the terms of the GNU Lesser General + * Public License as published by the Free Software Foundation; version 3.0 of + * the License. * - * H2GIS is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - * for more details . + * H2GIS is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details . * * * For more information, please consult: * or contact directly: info_at_h2gis.org */ - package org.h2gis.functions.io.geojson; import org.h2.value.Value; @@ -42,35 +41,35 @@ public class GeoJsonRead extends AbstractFunction implements ScalarFunction { public GeoJsonRead() { - addProperty(PROP_REMARKS, "Import a GeoJSON 1.0 file."+ - "\n GeoJsonRead(..."+ - "\n Supported arguments :" + - "\n path of the file" + - "\n path of the file, table name"+ - "\n path of the file, true for delete the table with the same file name"+ - "\n path of the file, table name, true to delete the table name"+ - "\n path of the file, table name, true to delete the table name"+ - "\n path of the file, table name, encoding chartset"+ - "\n path of the file, table name, encoding chartset, true to delete the table name"); + addProperty(PROP_REMARKS, "Import a GeoJSON 1.0 file." + + "\n GeoJsonRead(..." + + "\n Supported arguments :" + + "\n path of the file" + + "\n path of the file, table name" + + "\n path of the file, true for delete the table with the same file name" + + "\n path of the file, table name, true to delete the table name" + + "\n path of the file, table name, true to delete the table name" + + "\n path of the file, table name, encoding chartset" + + "\n path of the file, table name, encoding chartset, true to delete the table name"); } @Override public String getJavaStaticMethod() { return "importTable"; } - + /** - * + * * @param connection * @param fileName * @throws IOException - * @throws SQLException + * @throws SQLException */ public static void importTable(Connection connection, String fileName) throws IOException, SQLException { final String name = URIUtilities.fileFromString(fileName).getName(); String tableName = name.substring(0, name.lastIndexOf(".")).toUpperCase(); if (tableName.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) { - importTable(connection, fileName, tableName,null, false); + importTable(connection, fileName, tableName, null, false); } else { throw new SQLException("The file name contains unsupported characters"); } @@ -78,40 +77,40 @@ public static void importTable(Connection connection, String fileName) throws IO /** * Read the GeoJSON file. - * + * * @param connection * @param fileName * @param option * @throws IOException - * @throws SQLException + * @throws SQLException */ public static void importTable(Connection connection, String fileName, Value option) throws IOException, SQLException { - String tableReference =null; - boolean deleteTable = false; - if(option instanceof ValueBoolean){ + String tableReference = null; + boolean deleteTable = false; + if (option instanceof ValueBoolean) { deleteTable = option.getBoolean(); - }else if (option instanceof ValueVarchar){ + } else if (option instanceof ValueVarchar) { tableReference = option.getString(); - }else if (!(option instanceof ValueNull)){ + } else if (!(option instanceof ValueNull)) { throw new SQLException("Supported optional parameter is boolean or varchar"); } - importTable(connection,fileName, tableReference, null, deleteTable); + importTable(connection, fileName, tableReference, null, deleteTable); } public static void importTable(Connection connection, String fileName, String tableReference, Value option) throws IOException, SQLException { - String encoding =null; - boolean deleteTable = false; - if(option instanceof ValueBoolean){ + String encoding = null; + boolean deleteTable = false; + if (option instanceof ValueBoolean) { deleteTable = option.getBoolean(); - }else if (option instanceof ValueVarchar){ + } else if (option instanceof ValueVarchar) { encoding = option.getString(); - }else if (!(option instanceof ValueNull)){ + } else if (!(option instanceof ValueNull)) { throw new SQLException("Supported optional parameter is boolean or varchar"); } - importTable(connection,fileName, tableReference, encoding, deleteTable); + importTable(connection, fileName, tableReference, encoding, deleteTable); } - public static void importTable(Connection connection, String fileName, String tableReference,String encoding, boolean deleteTable ) throws IOException, SQLException { + public static void importTable(Connection connection, String fileName, String tableReference, String encoding, boolean deleteTable) throws IOException, SQLException { GeoJsonDriverFunction gjdf = new GeoJsonDriverFunction(); gjdf.importFile(connection, tableReference, URIUtilities.fileFromString(fileName), encoding, deleteTable, new EmptyProgressVisitor()); } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonReaderDriver.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonReaderDriver.java index 9b25c48585..2171057cd8 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonReaderDriver.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonReaderDriver.java @@ -25,7 +25,6 @@ import com.fasterxml.jackson.core.JsonToken; import org.h2gis.api.EmptyProgressVisitor; import org.h2gis.api.ProgressVisitor; -import org.h2gis.functions.io.utility.FileUtil; import org.h2gis.utilities.JDBCUtilities; import org.h2gis.utilities.TableLocation; import org.locationtech.jts.geom.*; @@ -33,12 +32,9 @@ import org.slf4j.LoggerFactory; import java.io.*; -import java.nio.channels.FileChannel; -import java.nio.file.FileSystems; import java.sql.*; import java.util.*; import java.util.zip.GZIPInputStream; -import java.util.zip.ZipInputStream; /** * Driver to import a GeoJSON file into a spatial table. @@ -99,8 +95,8 @@ public class GeoJsonReaderDriver { public GeoJsonReaderDriver(Connection connection, File fileName, String encoding, boolean deleteTable) { this.connection = connection; this.fileName = fileName; - this.encoding=encoding; - this.deleteTable=deleteTable; + this.encoding = encoding; + this.deleteTable = deleteTable; } /** @@ -112,13 +108,13 @@ public GeoJsonReaderDriver(Connection connection, File fileName, String encoding * @throws java.io.IOException */ public void read(ProgressVisitor progress, String tableReference) throws SQLException, IOException { - if (fileName!=null && fileName.getName().toLowerCase().endsWith(".geojson")) { - if(!fileName.exists()){ + if (fileName != null && fileName.getName().toLowerCase().endsWith(".geojson")) { + if (!fileName.exists()) { throw new SQLException("The file " + tableLocation + " doesn't exist "); } this.isH2 = JDBCUtilities.isH2DataBase(connection); this.tableLocation = TableLocation.parse(tableReference, isH2); - if(deleteTable){ + if (deleteTable) { Statement stmt = connection.createStatement(); stmt.execute("DROP TABLE IF EXISTS " + tableLocation); stmt.close(); @@ -128,14 +124,13 @@ public void read(ProgressVisitor progress, String tableReference) throws SQLExce } else { JDBCUtilities.createEmptyTable(connection, tableLocation.toString()); } - } - else if(fileName!=null && fileName.getName().toLowerCase().endsWith(".gz")){ - if(!fileName.exists()){ + } else if (fileName != null && fileName.getName().toLowerCase().endsWith(".gz")) { + if (!fileName.exists()) { throw new SQLException("The file " + tableLocation + " doesn't exist "); } this.isH2 = JDBCUtilities.isH2DataBase(connection); this.tableLocation = TableLocation.parse(tableReference, isH2); - if(deleteTable){ + if (deleteTable) { Statement stmt = connection.createStatement(); stmt.execute("DROP TABLE IF EXISTS " + tableLocation); stmt.close(); @@ -158,7 +153,7 @@ else if(fileName!=null && fileName.getName().toLowerCase().endsWith(".gz")){ } else { JDBCUtilities.createEmptyTable(connection, tableLocation.toString()); } - }else { + } else { throw new SQLException("The geojson read driver supports only geojson or gz extensions"); } } @@ -190,7 +185,7 @@ else if(fileName!=null && fileName.getName().toLowerCase().endsWith(".gz")){ */ private void parseGeoJson(ProgressVisitor progress) throws SQLException, IOException { this.progress = progress.subProcess(100); - init(); ; + init();; if (parseMetadata(new FileInputStream(fileName))) { connection.setAutoCommit(false); GF = new GeometryFactory(new PrecisionModel(), parsedSRID); @@ -213,7 +208,7 @@ private boolean parseMetadata(InputStream is) throws SQLException, IOException { try { cachedColumnNames = new LinkedHashMap<>(); finalGeometryTypes = new HashSet(); - try (JsonParser jp = jsFactory.createParser( new InputStreamReader(is, jsonEncoding.getJavaName()))) { + try (JsonParser jp = jsFactory.createParser(new InputStreamReader(is, jsonEncoding.getJavaName()))) { jp.nextToken();//START_OBJECT jp.nextToken(); // field_name (type) jp.nextToken(); // value_string (FeatureCollection) @@ -706,9 +701,9 @@ private void parsePropertiesMetadata(JsonParser jp) throws IOException, SQLExcep break; case VALUE_TRUE: case VALUE_FALSE: - if (!hasField|| dataType == Types.NULL) { + if (!hasField || dataType == Types.NULL) { cachedColumnNames.put(fieldName, Types.BOOLEAN); - } else if (hasField && dataType != Types.BOOLEAN) { + } else if (hasField && dataType != Types.BOOLEAN) { cachedColumnNames.put(fieldName, Types.VARCHAR); } break; @@ -724,14 +719,14 @@ private void parsePropertiesMetadata(JsonParser jp) throws IOException, SQLExcep } break; case VALUE_NUMBER_INT: - if (!hasField|| dataType == Types.NULL) { + if (!hasField || dataType == Types.NULL) { cachedColumnNames.put(fieldName, Types.BIGINT); } else if (hasField && dataType != Types.BIGINT) { cachedColumnNames.put(fieldName, Types.VARCHAR); } break; case START_ARRAY: - if (!hasField|| dataType == Types.NULL) { + if (!hasField || dataType == Types.NULL) { cachedColumnNames.put(fieldName, Types.ARRAY); } else if (hasField && dataType != Types.ARRAY) { cachedColumnNames.put(fieldName, Types.VARCHAR); diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonWrite.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonWrite.java index ae22ef3011..c1652f6468 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonWrite.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonWrite.java @@ -3,21 +3,20 @@ * . H2GIS is developed by CNRS * . * - * This code is part of the H2GIS project. H2GIS is free software; - * you can redistribute it and/or modify it under the terms of the GNU - * Lesser General Public License as published by the Free Software Foundation; - * version 3.0 of the License. + * This code is part of the H2GIS project. H2GIS is free software; you can + * redistribute it and/or modify it under the terms of the GNU Lesser General + * Public License as published by the Free Software Foundation; version 3.0 of + * the License. * - * H2GIS is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - * for more details . + * H2GIS is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details . * * * For more information, please consult: * or contact directly: info_at_h2gis.org */ - package org.h2gis.functions.io.geojson; import org.h2.value.Value; @@ -40,29 +39,28 @@ */ public class GeoJsonWrite extends AbstractFunction implements ScalarFunction { - - public GeoJsonWrite(){ - addProperty(PROP_REMARKS, "Export a spatial table to a GeoJSON 1.0 file.\n "+ - "\nGeoJsonWrite(..."+ - "\n Supported arguments :" + - "\n path of the file, table name"+ - "\n path of the file, table name, true to delete the file if exists"+ - "\n path of the file, table name, encoding chartset"+ - "\n path of the file, table name, encoding chartset, true to delete the file if exists"); + public GeoJsonWrite() { + addProperty(PROP_REMARKS, "Export a spatial table to a GeoJSON 1.0 file.\n " + + "\nGeoJsonWrite(..." + + "\n Supported arguments :" + + "\n path of the file, table name" + + "\n path of the file, table name, true to delete the file if exists" + + "\n path of the file, table name, encoding chartset" + + "\n path of the file, table name, encoding chartset, true to delete the file if exists"); } - + @Override public String getJavaStaticMethod() { return "exportTable"; } - /** * Read a table and write it into a GEOJSON file. + * * @param connection Active connection * @param fileName Shape file name or URI - * @param tableReference Table name or select query - * Note : The select query must be enclosed in parenthesis + * @param tableReference Table name or select query Note : The select query + * must be enclosed in parenthesis * @param encoding charset encoding * @param deleteFile true to delete output file * @throws IOException @@ -70,7 +68,7 @@ public String getJavaStaticMethod() { */ public static void exportTable(Connection connection, String fileName, String tableReference, String encoding, boolean deleteFile) throws IOException, SQLException { GeoJsonDriverFunction geoJsonDriver = new GeoJsonDriverFunction(); - geoJsonDriver.exportTable(connection,tableReference, URIUtilities.fileFromString(fileName), encoding,deleteFile,new EmptyProgressVisitor()); + geoJsonDriver.exportTable(connection, tableReference, URIUtilities.fileFromString(fileName), encoding, deleteFile, new EmptyProgressVisitor()); } /** @@ -88,24 +86,26 @@ public static void exportTable(Connection connection, String fileName, String ta /** * Read a table and write it into a geojson file. + * * @param connection Active connection * @param fileName Shape file name or URI - * @param tableReference Table name or select query - * Note : The select query must be enclosed in parenthesis - * @param option Could be string file encoding charset or boolean value to delete the existing file + * @param tableReference Table name or select query Note : The select query + * must be enclosed in parenthesis + * @param option Could be string file encoding charset or boolean value to + * delete the existing file * @throws IOException * @throws SQLException */ public static void exportTable(Connection connection, String fileName, String tableReference, Value option) throws IOException, SQLException { String encoding = null; boolean deleteFiles = false; - if(option instanceof ValueBoolean){ + if (option instanceof ValueBoolean) { deleteFiles = option.getBoolean(); - }else if (option instanceof ValueVarchar){ + } else if (option instanceof ValueVarchar) { encoding = option.getString(); - }else if (!(option instanceof ValueNull)){ + } else if (!(option instanceof ValueNull)) { throw new SQLException("Supported optional parameter is boolean or varchar"); } - exportTable( connection, fileName, tableReference, encoding, deleteFiles); + exportTable(connection, fileName, tableReference, encoding, deleteFiles); } } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonWriteDriver.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonWriteDriver.java index 22f4dcff23..5be87a636c 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonWriteDriver.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/geojson/GeoJsonWriteDriver.java @@ -3,21 +3,20 @@ * . H2GIS is developed by CNRS * . * - * This code is part of the H2GIS project. H2GIS is free software; - * you can redistribute it and/or modify it under the terms of the GNU - * Lesser General Public License as published by the Free Software Foundation; - * version 3.0 of the License. + * This code is part of the H2GIS project. H2GIS is free software; you can + * redistribute it and/or modify it under the terms of the GNU Lesser General + * Public License as published by the Free Software Foundation; version 3.0 of + * the License. * - * H2GIS is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - * for more details . + * H2GIS is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details . * * * For more information, please consult: * or contact directly: info_at_h2gis.org */ - package org.h2gis.functions.io.geojson; import com.fasterxml.jackson.core.JsonEncoding; @@ -61,7 +60,6 @@ */ public class GeoJsonWriteDriver { - private final Connection connection; private Map cachedColumnNames; private int columnCountProperties = -1; @@ -73,12 +71,11 @@ public class GeoJsonWriteDriver { */ public GeoJsonWriteDriver(Connection connection) { this.connection = connection; - } - - + } + /** * Write a resulset to a geojson file - * + * * @param progress * @param resultSet * @param file @@ -102,7 +99,6 @@ public void write(ProgressVisitor progress, ResultSet resultSet, File file) thro write(progress, resultSet, file, null, false); } - /** * Write a resulset to a geojson file * @@ -120,14 +116,14 @@ public void write(ProgressVisitor progress, ResultSet rs, File fileName, String Files.deleteIfExists(fileName.toPath()); } geojsonWriter(progress, rs, new FileOutputStream(fileName), encoding); - }else if (FileUtil.isExtensionWellFormated(fileName, "gz")) { + } else if (FileUtil.isExtensionWellFormated(fileName, "gz")) { if (deleteFile) { Files.deleteIfExists(fileName.toPath()); } GZIPOutputStream gzos = null; - try{ + try { FileOutputStream fos = new FileOutputStream(fileName); - gzos = new GZIPOutputStream(fos) ; + gzos = new GZIPOutputStream(fos); geojsonWriter(progress, rs, gzos, encoding); } finally { try { @@ -143,9 +139,9 @@ public void write(ProgressVisitor progress, ResultSet rs, File fileName, String Files.deleteIfExists(fileName.toPath()); } ZipOutputStream zip = null; - try{ + try { FileOutputStream fos = new FileOutputStream(fileName); - zip = new ZipOutputStream(fos) ; + zip = new ZipOutputStream(fos); geojsonWriter(progress, rs, zip, encoding); } finally { try { @@ -156,13 +152,14 @@ public void write(ProgressVisitor progress, ResultSet rs, File fileName, String throw new SQLException(ex); } } - }else { + } else { throw new SQLException("Only .geojson , .gz or .zip extensions are supported"); } } /** - * Method to write a resulset to a geojson file + * Method to write a resulset to a geojson file + * * @param progress * @param rs * @param fos @@ -170,7 +167,7 @@ public void write(ProgressVisitor progress, ResultSet rs, File fileName, String * @throws SQLException * @throws IOException */ - private void geojsonWriter(ProgressVisitor progress, ResultSet rs, OutputStream fos , String encoding) throws SQLException, IOException { + private void geojsonWriter(ProgressVisitor progress, ResultSet rs, OutputStream fos, String encoding) throws SQLException, IOException { JsonEncoding jsonEncoding = JsonEncoding.UTF8; if (encoding != null && !encoding.isEmpty()) { try { @@ -228,7 +225,7 @@ private void geojsonWriter(ProgressVisitor progress, ResultSet rs, OutputStream } /** - * Method to write a table to a geojson file + * Method to write a table to a geojson file * * @param progress * @param tableName @@ -237,8 +234,8 @@ private void geojsonWriter(ProgressVisitor progress, ResultSet rs, OutputStream * @throws SQLException * @throws IOException */ - private void geojsonWriter(ProgressVisitor progress, String tableName, OutputStream fos , String encoding) throws SQLException, IOException { - JsonEncoding jsonEncoding = JsonEncoding.UTF8; + private void geojsonWriter(ProgressVisitor progress, String tableName, OutputStream fos, String encoding) throws SQLException, IOException { + JsonEncoding jsonEncoding = JsonEncoding.UTF8; if (encoding != null) { try { jsonEncoding = JsonEncoding.valueOf(encoding); @@ -255,7 +252,7 @@ private void geojsonWriter(ProgressVisitor progress, String tableName, OutputStr Tuple geometryTableInfo = GeometryTableUtilities.getFirstGeometryColumnNameAndIndex(connection, parse); try ( // Read table content - Statement st = connection.createStatement()) { + Statement st = connection.createStatement()) { JsonFactory jsonFactory = new JsonFactory(); JsonGenerator jsonGenerator = jsonFactory.createGenerator(new BufferedOutputStream(fos), jsonEncoding); @@ -286,7 +283,7 @@ private void geojsonWriter(ProgressVisitor progress, String tableName, OutputStr } } } - } finally { + } finally { try { if (fos != null) { fos.close(); @@ -296,15 +293,13 @@ private void geojsonWriter(ProgressVisitor progress, String tableName, OutputStr } } } + public void write(ProgressVisitor progress, String tableName, File fileName, String encoding) throws SQLException, IOException { - write( progress, tableName, fileName, encoding, false); + write(progress, tableName, fileName, encoding, false); } - - - public void write(ProgressVisitor progress, String tableName, File fileName, boolean deleteFile) throws SQLException, IOException { - write( progress, tableName, fileName, null, deleteFile); + write(progress, tableName, fileName, null, deleteFile); } /** @@ -335,46 +330,45 @@ public void write(ProgressVisitor progress, String tableName, File fileName, Str Files.deleteIfExists(fileName.toPath()); } geojsonWriter(progress, tableName, new FileOutputStream(fileName), encoding); - } - else if (FileUtil.isExtensionWellFormated(fileName, "gz")) { - if (deleteFile) { - Files.deleteIfExists(fileName.toPath()); - } - GZIPOutputStream gzos = null; - try{ - FileOutputStream fos = new FileOutputStream(fileName); - gzos = new GZIPOutputStream(fos) ; - geojsonWriter(progress, tableName, gzos, encoding); - } finally { - try { - if (gzos != null) { - gzos.close(); - } - } catch (IOException ex) { - throw new SQLException(ex); + } else if (FileUtil.isExtensionWellFormated(fileName, "gz")) { + if (deleteFile) { + Files.deleteIfExists(fileName.toPath()); + } + GZIPOutputStream gzos = null; + try { + FileOutputStream fos = new FileOutputStream(fileName); + gzos = new GZIPOutputStream(fos); + geojsonWriter(progress, tableName, gzos, encoding); + } finally { + try { + if (gzos != null) { + gzos.close(); } + } catch (IOException ex) { + throw new SQLException(ex); } - } else if (FileUtil.isExtensionWellFormated(fileName, "zip")) { - if (deleteFile) { - Files.deleteIfExists(fileName.toPath()); - } - ZipOutputStream zip = null; - try{ - FileOutputStream fos = new FileOutputStream(fileName); - zip = new ZipOutputStream(fos) ; - geojsonWriter(progress, tableName, zip, encoding); - } finally { - try { - if (zip != null) { - zip.close(); - } - } catch (IOException ex) { - throw new SQLException(ex); + } + } else if (FileUtil.isExtensionWellFormated(fileName, "zip")) { + if (deleteFile) { + Files.deleteIfExists(fileName.toPath()); + } + ZipOutputStream zip = null; + try { + FileOutputStream fos = new FileOutputStream(fileName); + zip = new ZipOutputStream(fos); + geojsonWriter(progress, tableName, zip, encoding); + } finally { + try { + if (zip != null) { + zip.close(); } + } catch (IOException ex) { + throw new SQLException(ex); } - }else { - throw new SQLException("Only .geojson , .gz or .zip extensions are supported"); } + } else { + throw new SQLException("Only .geojson , .gz or .zip extensions are supported"); + } } } @@ -432,7 +426,7 @@ && isSupportedPropertyType(resultSetMetaData.getColumnType(i), fieldTypeName)) { /** * Write a JTS geometry to its GeoJSON geometry representation. - * + * * Syntax: * * "geometry":{"type": "Point", "coordinates": [102.0, 0.5]} @@ -440,7 +434,7 @@ && isSupportedPropertyType(resultSetMetaData.getColumnType(i), fieldTypeName)) { * @param geom * @param gen */ - private void writeGeometry(Geometry geom, JsonGenerator gen) throws IOException { + private void writeGeometry(Geometry geom, JsonGenerator gen) throws IOException { if (geom != null) { gen.writeObjectFieldStart("geometry"); if (geom instanceof Point) { @@ -464,7 +458,7 @@ private void writeGeometry(Geometry geom, JsonGenerator gen) throws IOException } else { gen.writeNullField("geometry"); } - + } /** @@ -571,7 +565,7 @@ private void write(GeometryCollection coll, JsonGenerator gen) throws IOExceptio } else { throw new RuntimeException("Unsupported Geomery type"); } - gen.writeEndObject(); + gen.writeEndObject(); } gen.writeEndArray(); } @@ -688,7 +682,7 @@ private void writeProperties(JsonGenerator jsonGenerator, ResultSet rs) throws I jsonGenerator.writeArrayFieldStart(string); writeArray(jsonGenerator, array, true); jsonGenerator.writeEndArray(); - } else if (rs.getObject(fieldId) != null && rs.getObject(fieldId).equals("{}")){ + } else if (rs.getObject(fieldId) != null && rs.getObject(fieldId).equals("{}")) { jsonGenerator.writeObjectFieldStart(string); jsonGenerator.writeEndObject(); } else if (rs.getObject(fieldId) == "null") { @@ -725,9 +719,9 @@ public boolean isSupportedPropertyType(int sqlTypeId, String sqlTypeName) throws case Types.ARRAY: case Types.OTHER: case Types.DECIMAL: - case Types.REAL: - case Types.TINYINT: - case Types.NUMERIC: + case Types.REAL: + case Types.TINYINT: + case Types.NUMERIC: case Types.NULL: return true; default: @@ -763,10 +757,10 @@ private void writeCRS(JsonGenerator jsonGenerator, String[] authorityAndSRID) th * @throw IOException */ private void writeArray(JsonGenerator jsonGenerator, Object[] array, boolean firstInHierarchy) throws IOException, SQLException { - if(!firstInHierarchy) { + if (!firstInHierarchy) { jsonGenerator.writeStartArray(); } - for(int i = 0; i < array.length; i++) { + for (int i = 0; i < array.length; i++) { if (array[i] instanceof Integer) { jsonGenerator.writeNumber((int) array[i]); } else if (array[i] instanceof String) { @@ -784,7 +778,7 @@ private void writeArray(JsonGenerator jsonGenerator, Object[] array, boolean fir writeArray(jsonGenerator, (Object[]) array[i], false); } } - if(!firstInHierarchy) { + if (!firstInHierarchy) { jsonGenerator.writeEndArray(); } } From 2f523451d02d71d44d1f47d8f6b53303539a239d Mon Sep 17 00:00:00 2001 From: ebocher Date: Mon, 8 Jun 2020 15:18:11 +0200 Subject: [PATCH 09/10] Fix spaces --- .../org/h2gis/functions/io/kml/KMLWrite.java | 55 ++++++------- .../org/h2gis/functions/io/osm/OSMRead.java | 78 +++++++++---------- 2 files changed, 67 insertions(+), 66 deletions(-) diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/kml/KMLWrite.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/kml/KMLWrite.java index 992db48045..e87ee4b2bb 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/kml/KMLWrite.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/kml/KMLWrite.java @@ -3,21 +3,20 @@ * . H2GIS is developed by CNRS * . * - * This code is part of the H2GIS project. H2GIS is free software; - * you can redistribute it and/or modify it under the terms of the GNU - * Lesser General Public License as published by the Free Software Foundation; - * version 3.0 of the License. + * This code is part of the H2GIS project. H2GIS is free software; you can + * redistribute it and/or modify it under the terms of the GNU Lesser General + * Public License as published by the Free Software Foundation; version 3.0 of + * the License. * - * H2GIS is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - * for more details . + * H2GIS is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details . * * * For more information, please consult: * or contact directly: info_at_h2gis.org */ - package org.h2gis.functions.io.kml; import org.h2.value.Value; @@ -41,13 +40,13 @@ public class KMLWrite extends AbstractFunction implements ScalarFunction { public KMLWrite() { - addProperty(PROP_REMARKS, "Export a spatial table to a KML or KMZ file.\n" + - "\nKMLWrite(..."+ - "\n Supported arguments :" + - "\n path of the file, table name"+ - "\n path of the file, table name, true to delete the file if exists"+ - "\n path of the file, table name, encoding chartset"+ - "\n path of the file, table name, encoding chartset, true to delete the file if exists"); + addProperty(PROP_REMARKS, "Export a spatial table to a KML or KMZ file.\n" + + "\nKMLWrite(..." + + "\n Supported arguments :" + + "\n path of the file, table name" + + "\n path of the file, table name, true to delete the file if exists" + + "\n path of the file, table name, encoding chartset" + + "\n path of the file, table name, encoding chartset, true to delete the file if exists"); } @Override @@ -57,40 +56,44 @@ public String getJavaStaticMethod() { /** * This method is used to write a spatial table into a KML file + * * @param connection * @param fileName * @param tableReference * @throws SQLException - * @throws IOException + * @throws IOException */ public static void exportTable(Connection connection, String fileName, String tableReference) throws SQLException, IOException { - exportTable( connection, fileName, tableReference, null, false); + exportTable(connection, fileName, tableReference, null, false); } public static void exportTable(Connection connection, String fileName, String tableReference, String encoding, boolean deleteFile) throws SQLException, IOException { KMLDriverFunction kMLDriverFunction = new KMLDriverFunction(); - kMLDriverFunction.exportTable(connection, tableReference, URIUtilities.fileFromString(fileName),encoding,deleteFile, new EmptyProgressVisitor()); + kMLDriverFunction.exportTable(connection, tableReference, URIUtilities.fileFromString(fileName), encoding, deleteFile, new EmptyProgressVisitor()); } + /** * Read a table and write it into a kml file. + * * @param connection Active connection * @param fileName Shape file name or URI - * @param tableReference Table name or select query - * Note : The select query must be enclosed in parenthesis - * @param option Could be string file encoding charset or boolean value to delete the existing file + * @param tableReference Table name or select query Note : The select query + * must be enclosed in parenthesis + * @param option Could be string file encoding charset or boolean value to + * delete the existing file * @throws IOException * @throws SQLException */ public static void exportTable(Connection connection, String fileName, String tableReference, Value option) throws IOException, SQLException { String encoding = null; boolean deleteFiles = false; - if(option instanceof ValueBoolean){ + if (option instanceof ValueBoolean) { deleteFiles = option.getBoolean(); - }else if (option instanceof ValueVarchar){ + } else if (option instanceof ValueVarchar) { encoding = option.getString(); - }else if (!(option instanceof ValueNull)){ + } else if (!(option instanceof ValueNull)) { throw new SQLException("Supported optional parameter is boolean or varchar"); } - exportTable( connection, fileName, tableReference, encoding, deleteFiles); + exportTable(connection, fileName, tableReference, encoding, deleteFiles); } } diff --git a/h2gis-functions/src/main/java/org/h2gis/functions/io/osm/OSMRead.java b/h2gis-functions/src/main/java/org/h2gis/functions/io/osm/OSMRead.java index 2e0262bd47..8bc8dc530e 100644 --- a/h2gis-functions/src/main/java/org/h2gis/functions/io/osm/OSMRead.java +++ b/h2gis-functions/src/main/java/org/h2gis/functions/io/osm/OSMRead.java @@ -3,21 +3,20 @@ * . H2GIS is developed by CNRS * . * - * This code is part of the H2GIS project. H2GIS is free software; - * you can redistribute it and/or modify it under the terms of the GNU - * Lesser General Public License as published by the Free Software Foundation; - * version 3.0 of the License. + * This code is part of the H2GIS project. H2GIS is free software; you can + * redistribute it and/or modify it under the terms of the GNU Lesser General + * Public License as published by the Free Software Foundation; version 3.0 of + * the License. * - * H2GIS is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - * for more details . + * H2GIS is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details . * * * For more information, please consult: * or contact directly: info_at_h2gis.org */ - package org.h2gis.functions.io.osm; import org.h2.value.Value; @@ -27,10 +26,8 @@ import org.h2gis.api.AbstractFunction; import org.h2gis.api.EmptyProgressVisitor; import org.h2gis.api.ScalarFunction; -import org.h2gis.utilities.JDBCUtilities; import org.h2gis.utilities.URIUtilities; -import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Connection; @@ -46,64 +43,65 @@ public class OSMRead extends AbstractFunction implements ScalarFunction { public OSMRead() { addProperty(PROP_REMARKS, "Read a OSM file and copy the content in the specified tables.\n" + "The user can set a prefix name for all OSM tables and specify if the existing OSM\n" - + " tables must be dropped." + - "\n OSMRead(..."+ - "\n Supported arguments :" + - "\n path of the file" + - "\n path of the file, table name"+ - "\n path of the file, true for delete the table with the same file name"+ - "\n path of the file, table name, true to delete the table name"+ - "\n path of the file, table name, true to delete the table name"+ - "\n path of the file, table name, encoding chartset"+ - "\n path of the file, table name, encoding chartset, true to delete the table name"); + + " tables must be dropped." + + "\n OSMRead(..." + + "\n Supported arguments :" + + "\n path of the file" + + "\n path of the file, table name" + + "\n path of the file, true for delete the table with the same file name" + + "\n path of the file, table name, true to delete the table name" + + "\n path of the file, table name, true to delete the table name" + + "\n path of the file, table name, encoding chartset" + + "\n path of the file, table name, encoding chartset, true to delete the table name"); } @Override public String getJavaStaticMethod() { return "importTable"; } - + /** - * + * * @param connection * @param fileName * @param tableReference - * @param option true to delete the existing tables or set a chartset encoding + * @param option true to delete the existing tables or set a chartset + * encoding * @throws FileNotFoundException - * @throws SQLException + * @throws SQLException */ public static void importTable(Connection connection, String fileName, String tableReference, Value option) throws FileNotFoundException, SQLException, IOException { - String encoding =null; - boolean deleteTable = false; - if(option instanceof ValueBoolean){ + String encoding = null; + boolean deleteTable = false; + if (option instanceof ValueBoolean) { deleteTable = option.getBoolean(); - }else if (option instanceof ValueVarchar){ + } else if (option instanceof ValueVarchar) { encoding = option.getString(); - }else if (!(option instanceof ValueNull)){ + } else if (!(option instanceof ValueNull)) { throw new SQLException("Supported optional parameter is boolean or varchar"); } importTable(connection, fileName, tableReference, encoding, deleteTable); } /** - * + * * @param connection * @param fileName * @param option * @throws FileNotFoundException - * @throws SQLException + * @throws SQLException */ public static void importTable(Connection connection, String fileName, Value option) throws FileNotFoundException, SQLException, IOException { - String tableReference =null; - boolean deleteTable = false; - if(option instanceof ValueBoolean){ + String tableReference = null; + boolean deleteTable = false; + if (option instanceof ValueBoolean) { deleteTable = option.getBoolean(); - }else if (option instanceof ValueVarchar){ + } else if (option instanceof ValueVarchar) { tableReference = option.getString(); - }else if (!(option instanceof ValueNull)){ + } else if (!(option instanceof ValueNull)) { throw new SQLException("Supported optional parameter is boolean or varchar"); } - importTable(connection, fileName, tableReference,null, deleteTable); + importTable(connection, fileName, tableReference, null, deleteTable); } /** @@ -119,7 +117,7 @@ public static void importTable(Connection connection, String fileName, Value opt */ public static void importTable(Connection connection, String fileName, String tableReference, String encoding, boolean deleteTables) throws FileNotFoundException, SQLException, IOException { OSMDriverFunction osmdf = new OSMDriverFunction(); - osmdf.importFile(connection, tableReference, URIUtilities.fileFromString(fileName), encoding, deleteTables,new EmptyProgressVisitor()); + osmdf.importFile(connection, tableReference, URIUtilities.fileFromString(fileName), encoding, deleteTables, new EmptyProgressVisitor()); } /** @@ -133,7 +131,7 @@ public static void importTable(Connection connection, String fileName) throws Fi final String name = URIUtilities.fileFromString(fileName).getName(); String tableName = name.substring(0, name.lastIndexOf(".")).toUpperCase(); if (tableName.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) { - importTable(connection, fileName, tableName, null,false); + importTable(connection, fileName, tableName, null, false); } else { throw new SQLException("The file name contains unsupported characters"); } From e1d0964fe330b595b995fa8f420816e29bacaf8d Mon Sep 17 00:00:00 2001 From: ebocher Date: Mon, 8 Jun 2020 16:03:18 +0200 Subject: [PATCH 10/10] Fix spaces --- .../java/org/h2gis/api/DriverFunction.java | 165 ++++++++++-------- 1 file changed, 89 insertions(+), 76 deletions(-) diff --git a/h2gis-api/src/main/java/org/h2gis/api/DriverFunction.java b/h2gis-api/src/main/java/org/h2gis/api/DriverFunction.java index b1968f3c07..dbaa04c4a3 100644 --- a/h2gis-api/src/main/java/org/h2gis/api/DriverFunction.java +++ b/h2gis-api/src/main/java/org/h2gis/api/DriverFunction.java @@ -17,7 +17,6 @@ * For more information, please consult: * or contact directly: info_at_h2gis.org */ - package org.h2gis.api; import java.io.File; @@ -26,8 +25,9 @@ import java.sql.SQLException; /** - * This function can import/export a file into/from a table. Connection may be on a remote H2/Postgres database. - * The file can be linked to the database or copied into the database. + * This function can import/export a file into/from a table. Connection may be + * on a remote H2/Postgres database. The file can be linked to the database or + * copied into the database. * * @author Nicolas Fortin * @author Sylvain PALOMINOS (UBS 2018) @@ -35,16 +35,19 @@ public interface DriverFunction { /** - * A linked table is created instantly but work only if the DataBase is local. - * A copy will transfer Data from the File to the remote/local database and the database content will not be - * synced with the file. + * A linked table is created instantly but work only if the DataBase is + * local. A copy will transfer Data from the File to the remote/local + * database and the database content will not be synced with the file. */ - enum IMPORT_DRIVER_TYPE {LINK, COPY} + enum IMPORT_DRIVER_TYPE { + LINK, COPY + } /** * Return the driver import type. * - * @return The driver type. A LINK Driver mean the usage of a TableEngine on H2 DataBase. + * @return The driver type. A LINK Driver mean the usage of a TableEngine on + * H2 DataBase. */ IMPORT_DRIVER_TYPE getImportDriverType(); @@ -65,15 +68,16 @@ enum IMPORT_DRIVER_TYPE {LINK, COPY} /** * Return the description of the specified format. * - * @param format Format given through getImportFormats and/or getExportFormats - * @return The description of this format under the default Locale. - * An empty string if the description is not available. + * @param format Format given through getImportFormats and/or + * getExportFormats + * @return The description of this format under the default Locale. An empty + * string if the description is not available. */ String getFormatDescription(String format); /** - * Returns true if the file extension is from a spatial file, false otherwise or if the driver does not recognize - * it. + * Returns true if the file extension is from a spatial file, false + * otherwise or if the driver does not recognize it. * * @param extension Extension to check. * @return True if the extension is a spatial one, false otherwise. @@ -81,120 +85,129 @@ enum IMPORT_DRIVER_TYPE {LINK, COPY} boolean isSpatialFormat(String extension); /** - * Export the specified table from the specified connection into the specified file. + * Export the specified table from the specified connection into the + * specified file. * - * @param connection Active connection, do not close this connection. + * @param connection Active connection, do not close this connection. * @param tableReference [[catalog.]schema.]table reference. - * @param fileName File path to write, if exists it may be replaced. - * @param progress Progress visitor following the execution. + * @param fileName File path to write, if exists it may be replaced. + * @param progress Progress visitor following the execution. * @throws SQLException Table read error. - * @throws IOException File write error. + * @throws IOException File write error. */ void exportTable(Connection connection, String tableReference, File fileName, ProgressVisitor progress) throws SQLException, IOException; - - /** - * Export the specified table from the specified connection into the specified file. + + /** + * Export the specified table from the specified connection into the + * specified file. * - * @param connection Active connection, do not close this connection. + * @param connection Active connection, do not close this connection. * @param tableReference [[catalog.]schema.]table reference. - * @param fileName File path to write, if exists it may be replaced. - * @param deleteFiles True to delete the files if exist - * @param progress Progress visitor following the execution. + * @param fileName File path to write, if exists it may be replaced. + * @param deleteFiles True to delete the files if exist + * @param progress Progress visitor following the execution. * @throws SQLException Table read error. - * @throws IOException File write error. + * @throws IOException File write error. */ void exportTable(Connection connection, String tableReference, File fileName, boolean deleteFiles, ProgressVisitor progress) throws SQLException, IOException; /** - * Export the specified table from the specified connection into the specified file. + * Export the specified table from the specified connection into the + * specified file. * - * @param connection Active connection, do not close this connection. + * @param connection Active connection, do not close this connection. * @param tableReference [[catalog.]schema.]table reference. - * @param fileName File path to write, if exists it may be replaced. - * @param options Options to use for the export like encoding, separator ... - * The options are different from a format to another. - * @param deleteFiles True to delete the files if exist - * @param progress Progress visitor following the execution. + * @param fileName File path to write, if exists it may be replaced. + * @param options Options to use for the export like encoding, separator ... + * The options are different from a format to another. + * @param deleteFiles True to delete the files if exist + * @param progress Progress visitor following the execution. * @throws SQLException Table read error. - * @throws IOException File write error. + * @throws IOException File write error. */ void exportTable(Connection connection, String tableReference, File fileName, - String options,boolean deleteFiles,ProgressVisitor progress) throws SQLException, IOException; - + String options, boolean deleteFiles, ProgressVisitor progress) throws SQLException, IOException; + /** - * Export the specified table from the specified connection into the specified file. + * Export the specified table from the specified connection into the + * specified file. * - * @param connection Active connection, do not close this connection. + * @param connection Active connection, do not close this connection. * @param tableReference [[catalog.]schema.]table reference. - * @param fileName File path to write, if exists it may be replaced. - * @param progress Progress visitor following the execution. - * @param options Options to use for the export like encoding, separator ... - * The options are different from a format to another. + * @param fileName File path to write, if exists it may be replaced. + * @param progress Progress visitor following the execution. + * @param options Options to use for the export like encoding, separator ... + * The options are different from a format to another. * @throws SQLException Table read error. - * @throws IOException File write error. + * @throws IOException File write error. */ void exportTable(Connection connection, String tableReference, File fileName, - String options,ProgressVisitor progress) throws SQLException, IOException; + String options, ProgressVisitor progress) throws SQLException, IOException; /** - * Import the specified file into the specified table in the specified connection. + * Import the specified file into the specified table in the specified + * connection. * - * @param connection Active connection, do not close this connection. + * @param connection Active connection, do not close this connection. * @param tableReference [[catalog.]schema.]table reference. - * @param fileName File path to read. - * @param progress Progress visitor following the execution. + * @param fileName File path to read. + * @param progress Progress visitor following the execution. * @throws SQLException Table write error. - * @throws IOException File read error. + * @throws IOException File read error. */ void importFile(Connection connection, String tableReference, File fileName, ProgressVisitor progress) throws SQLException, IOException; /** - * Import the specified file into the specified table in the specified connection. + * Import the specified file into the specified table in the specified + * connection. * - * @param connection Active connection, do not close this connection. + * @param connection Active connection, do not close this connection. * @param tableReference [[catalog.]schema.]table reference. - * @param fileName File path to read. - * @param options Options to use for the export like encoding, separator ... - * The options are different from a format to another. - * @param progress Progress visitor following the execution. + * @param fileName File path to read. + * @param options Options to use for the export like encoding, separator ... + * The options are different from a format to another. + * @param progress Progress visitor following the execution. * @throws SQLException Table write error. - * @throws IOException File read error. + * @throws IOException File read error. */ void importFile(Connection connection, String tableReference, File fileName, String options, ProgressVisitor progress) - throws SQLException, IOException; + throws SQLException, IOException; /** - * Import the specified file into the specified table in the specified connection. + * Import the specified file into the specified table in the specified + * connection. * - * @param connection Active connection, do not close this connection. + * @param connection Active connection, do not close this connection. * @param tableReference [[catalog.]schema.]table reference. - * @param fileName File path to read. - * @param deleteTables True if the existing table used for the import should be deleted, false otherwise. - * @param progress Progress visitor following the execution. + * @param fileName File path to read. + * @param deleteTables True if the existing table used for the import should + * be deleted, false otherwise. + * @param progress Progress visitor following the execution. * @throws SQLException Table write error. - * @throws IOException File read error. + * @throws IOException File read error. */ void importFile(Connection connection, String tableReference, File fileName, boolean deleteTables, ProgressVisitor progress - ) throws SQLException, IOException; - + ) throws SQLException, IOException; + /** - * Import the specified file into the specified table in the specified connection. + * Import the specified file into the specified table in the specified + * connection. * - * @param connection Active connection, do not close this connection. + * @param connection Active connection, do not close this connection. * @param tableReference [[catalog.]schema.]table reference. - * @param fileName File path to read. - * @param options Options to use for the export like encoding, separator ... - * The options are different from a format to another. - * @param deleteTables True if the existing table used for the import should be deleted, false otherwise. - * @param progress Progress visitor following the execution. + * @param fileName File path to read. + * @param options Options to use for the export like encoding, separator ... + * The options are different from a format to another. + * @param deleteTables True if the existing table used for the import should + * be deleted, false otherwise. + * @param progress Progress visitor following the execution. * @throws SQLException Table write error. - * @throws IOException File read error. + * @throws IOException File read error. */ void importFile(Connection connection, String tableReference, File fileName, String options, boolean deleteTables, ProgressVisitor progress - ) throws SQLException, IOException; - - + ) throws SQLException, IOException; + }