Skip to content

Commit

Permalink
Merge pull request #1390 from ebocher/fgb_name
Browse files Browse the repository at this point in the history
Set the file name to the FGB header
  • Loading branch information
ebocher authored Jul 3, 2024
2 parents a85cb0b + 8de1098 commit 2a9d8a5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
2 changes: 1 addition & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
- Add SnapToGrid function
- Write empty geometry with the shapefile driver
- Do not ask for SRID when geometry is empty
- Add the name of the table in the FGB header
- Add the name of the file in the FGB header
- Fix null way id on OSM file-
- Use new sonatype auth
- Add ST_SnapToSelf function
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,11 @@ public String write(ProgressVisitor progress, String tableName, File fileName, b
} else if (fileName.exists()) {
throw new IOException("The geojson file already exist.");
}
fgbWrite(progress, tableName, new FileOutputStream(fileName));
String filePath = fileName.getName();
final int dotIndex = filePath.lastIndexOf('.');
final String fileNameWithoutExt = filePath.substring(0, dotIndex);

fgbWrite(progress, tableName, new FileOutputStream(fileName), fileNameWithoutExt);

return fileName.getAbsolutePath();

Expand All @@ -170,7 +174,7 @@ private static void writeString(String value, ByteBuffer bufferManager) throws I
bufferManager.put(stringBytes);
}

private void fgbWrite(ProgressVisitor progress, String tableName, FileOutputStream outputStream) throws IOException, SQLException {
private void fgbWrite(ProgressVisitor progress, String tableName, FileOutputStream outputStream, String fileNameWithoutExt) throws IOException, SQLException {
try {
DBTypes dbTypes = DBUtils.getDBType(connection);
final TableLocation parse = TableLocation.parse(tableName, dbTypes);
Expand All @@ -183,7 +187,7 @@ private void fgbWrite(ProgressVisitor progress, String tableName, FileOutputStre
String geomCol = geomMetadata.first();
geomMetadata.second();
ResultSet rs = st.executeQuery(String.format("select * from %s", outputTable));
doExport(progress, rs, geomCol, geomMetadata.second().sfs_geometryType, geomMetadata.second().SRID, recordCount, outputStream, tableName);
doExport(progress, rs, geomCol, geomMetadata.second().sfs_geometryType, geomMetadata.second().SRID, recordCount, outputStream, fileNameWithoutExt);
}
}
} finally {
Expand All @@ -197,12 +201,12 @@ private void fgbWrite(ProgressVisitor progress, String tableName, FileOutputStre
}
}

private String doExport(ProgressVisitor progress, ResultSet rs, String geometryColumn, String geometryType, int srid, int recordCount, FileOutputStream outputStream, String name) throws SQLException, IOException {
private String doExport(ProgressVisitor progress, ResultSet rs, String geometryColumn, String geometryType, int srid, int recordCount, FileOutputStream outputStream, String fileName) throws SQLException, IOException {

FlatBufferBuilder bufferBuilder = new FlatBufferBuilder();

//Write the header
HeaderMeta header = writeHeader(outputStream, name, bufferBuilder, recordCount, geometryType, srid, rs.getMetaData());
HeaderMeta header = writeHeader(outputStream, fileName, bufferBuilder, recordCount, geometryType, srid, rs.getMetaData());

long endHeaderPosition = outputStream.getChannel().position();
//Columns numbers
Expand Down Expand Up @@ -350,25 +354,11 @@ private String doExport(ProgressVisitor progress, ResultSet rs, String geometryC
return "";
}

/**
* Export the FlatGeoBuffer from a select query
* @param progress
* @param rs
* @param fileOutputStream
*/
private String fgbWrite(ProgressVisitor progress, ResultSet rs, FileOutputStream fileOutputStream) {




return null;

}

/**
* Write the header
*
* @param outputStream
* @param fileName name of the file
* @param rowCount
* @param geometryType
* @param srid
Expand All @@ -377,7 +367,7 @@ private String fgbWrite(ProgressVisitor progress, ResultSet rs, FileOutputStream
* @throws SQLException
* @throws IOException
*/
private HeaderMeta writeHeader(FileOutputStream outputStream, String name, FlatBufferBuilder bufferBuilder, long rowCount, String geometryType, int srid, ResultSetMetaData metadata) throws SQLException, IOException {
private HeaderMeta writeHeader(FileOutputStream outputStream, String fileName, FlatBufferBuilder bufferBuilder, long rowCount, String geometryType, int srid, ResultSetMetaData metadata) throws SQLException, IOException {
outputStream.write(Constants.MAGIC_BYTES);
//Get the column information
List<ColumnMeta> columns = new ArrayList<>();
Expand All @@ -396,7 +386,7 @@ private HeaderMeta writeHeader(FileOutputStream outputStream, String name, FlatB
columns.add(column);
}
HeaderMeta headerMeta = new HeaderMeta();
headerMeta.name=name;
headerMeta.name=fileName;
headerMeta.featuresCount = rowCount;
headerMeta.geometryType = geometryType(geometryType);
headerMeta.columns = columns;
Expand Down

0 comments on commit 2a9d8a5

Please sign in to comment.