Skip to content

Commit

Permalink
Merge pull request #1366 from ebocher/json_extension_geojson
Browse files Browse the repository at this point in the history
Json extension geojson
  • Loading branch information
ebocher authored Oct 9, 2023
2 parents 32ad767 + b7172dc commit 87e254f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@
+ Change the `h2gis-dist` main class from `org.h2.tools.Server` to `org.h2.tools.Console`
+ Set scope as test for slf4j-simple
+ Zip and unzip functions with subfolders
+ GeoJson driver must be able to read json extension
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ public IMPORT_DRIVER_TYPE getImportDriverType() {

@Override
public String[] getImportFormats() {
return new String[]{"geojson", "geojson.gz"};
return new String[]{"json", "geojson", "geojson.gz"};
}

@Override
public String[] getExportFormats() {
return new String[]{"geojson", "geojson.gz"};
return new String[]{"json", "geojson", "geojson.gz"};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ public GeoJsonReaderDriver(Connection connection, File fileName, String encoding
* @throws java.io.IOException
*/
public String read(ProgressVisitor progress, String tableReference) throws SQLException, IOException {
if (fileName != null && fileName.getName().toLowerCase().endsWith(".geojson")) {
String fileNameLower = fileName.getName().toLowerCase();
if (fileName != null && (fileNameLower.endsWith(".geojson") || fileNameLower.endsWith(".json"))) {
if (!fileName.exists()) {
throw new SQLException("The file " + tableLocation + " doesn't exist ");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public GeoJsonWriteDriver(Connection connection) {
* @throws java.io.IOException
*/
public void write(ProgressVisitor progress, ResultSet rs, File fileName, String encoding, boolean deleteFile) throws SQLException, IOException {
if (FileUtilities.isExtensionWellFormated(fileName, "geojson")) {
if (FileUtilities.isExtensionWellFormated(fileName, "geojson")|| FileUtilities.isExtensionWellFormated(fileName, "json")) {
if (deleteFile) {
Files.deleteIfExists(fileName.toPath());
} else if (fileName.exists()) {
Expand Down Expand Up @@ -319,7 +319,7 @@ public void write(ProgressVisitor progress, String tableName, File fileName, Str
Matcher matcher = pattern.matcher(tableName);
if (matcher.find()) {
if (tableName.startsWith("(") && tableName.endsWith(")")) {
if (FileUtilities.isExtensionWellFormated(fileName, "geojson")) {
if (FileUtilities.isExtensionWellFormated(fileName, "geojson")|| FileUtilities.isExtensionWellFormated(fileName, "json")) {
if (deleteFile) {
Files.deleteIfExists(fileName.toPath());
} else if (fileName.exists()) {
Expand Down Expand Up @@ -380,7 +380,7 @@ public void write(ProgressVisitor progress, String tableName, File fileName, Str
throw new SQLException("The select query must be enclosed in parenthesis: '(SELECT * FROM ORDERS)'.");
}
} else {
if (FileUtilities.isExtensionWellFormated(fileName, "geojson")) {
if (FileUtilities.isExtensionWellFormated(fileName, "geojson")|| FileUtilities.isExtensionWellFormated(fileName, "json")) {
if (deleteFile) {
Files.deleteIfExists(fileName.toPath());
} else if (fileName.exists()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1325,4 +1325,22 @@ public void testWriteNullData() throws Exception {
stat.execute("CALL GeoJsonWrite('target/lines.geojson', 'DATA', true);");
}
}

@Test
public void testWriteReadJsonExtension() throws Exception {
try (Statement stat = connection.createStatement()) {
File folderOut = new File("target/");
String geojson = folderOut.getAbsolutePath() + File.separator + "mutilines.json";
stat.execute("DROP TABLE IF EXISTS TABLE_MULTILINESTRINGS");
stat.execute("create table TABLE_MULTILINESTRINGS(the_geom GEOMETRY(MULTILINESTRING))");
stat.execute("insert into TABLE_MULTILINESTRINGS values( 'MULTILINESTRING ((90 220, 260 320, 280 200))')");
stat.execute("CALL GeoJsonWrite('" +geojson+ "', 'TABLE_MULTILINESTRINGS', true);");
stat.execute("CALL GeoJsonRead('" +geojson+ "', 'TABLE_MULTILINESTRINGS_READ');");
ResultSet res = stat.executeQuery("SELECT * FROM TABLE_MULTILINESTRINGS_READ;");
res.next();
assertTrue(((Geometry) res.getObject(1)).equals(WKTREADER.read("MULTILINESTRING ((90 220, 260 320, 280 200))")));
res.close();
stat.execute("DROP TABLE IF EXISTS TABLE_MULTILINESTRINGS_READ");
}
}
}

0 comments on commit 87e254f

Please sign in to comment.