From 358e160c93b673137cbaf1a63ed8dfd5d70164e3 Mon Sep 17 00:00:00 2001 From: Emanuela Epure <67077116+emanuelaepure10@users.noreply.github.com> Date: Thu, 29 Jun 2023 09:55:09 +0200 Subject: [PATCH] Update test and writer From the test class the whenReadWithBufferedReader_thenCorrect() and testCPGFileCreation() have been removed and a new method testCpgFileAgainstRelatedSpapefile() called from all the Test methods. The writer has been updated taking the charset from the ShapefileDataStore of the related shapefile or adding the default charset in case the above one is null feat: extend shapefile writer to create .cpg file Create .cpg file/files when one/more .shp files are created. The .cpg file contain only the encoding used to write the shapefile. Create test for creating .cpg file --- .../io/shp/ShapefileInstanceWriterTest.groovy | 23 +++++++++++- .../hale/io/shp/ShapefileConstants.java | 6 +++ .../shp/writer/ShapefileInstanceWriter.java | 37 +++++++++++++++++++ 3 files changed, 64 insertions(+), 2 deletions(-) diff --git a/io/plugins/eu.esdihumboldt.hale.io.shp.test/src/eu/esdihumboldt/hale/io/shp/ShapefileInstanceWriterTest.groovy b/io/plugins/eu.esdihumboldt.hale.io.shp.test/src/eu/esdihumboldt/hale/io/shp/ShapefileInstanceWriterTest.groovy index 9cfc8b9a8d..422f7af747 100644 --- a/io/plugins/eu.esdihumboldt.hale.io.shp.test/src/eu/esdihumboldt/hale/io/shp/ShapefileInstanceWriterTest.groovy +++ b/io/plugins/eu.esdihumboldt.hale.io.shp.test/src/eu/esdihumboldt/hale/io/shp/ShapefileInstanceWriterTest.groovy @@ -25,6 +25,7 @@ import java.time.Instant import java.time.LocalDate import java.util.function.Consumer +import org.geotools.data.shapefile.ShapefileDataStore import org.junit.Test import org.locationtech.jts.geom.Coordinate import org.locationtech.jts.geom.Geometry @@ -100,7 +101,7 @@ class ShapefileInstanceWriterTest { String filenameOnly = Paths.get(location).getFileName().toString(); filenameOnly = filenameOnly.substring(0, filenameOnly.lastIndexOf(".")); - String filename = filePath + "/" + filenameOnly + "_" + additionalName + ".shp"; + String filename = filePath + "/" + filenameOnly + "_" + additionalName + ShapefileConstants.SHP_EXTENSION; file = new File(filename) Schema schema = loadSchema(file) @@ -114,10 +115,11 @@ class ShapefileInstanceWriterTest { assertTrue(report.isSuccess()) assertTrue(report.getErrors().isEmpty()) + testCpgFileAgainstRelatedSpapefile(file) + return reader.getInstances(); } - /** * Write an instance collection to a Shapefile. */ @@ -184,11 +186,25 @@ class ShapefileInstanceWriterTest { println "Temporary file is $tmpFile" writeInstances(tmpFile.toFile(), schema, instances, configurator) handler.accept(tmpFile.toFile()) + + testCpgFileAgainstRelatedSpapefile(tmpFile.toFile()) } finally { tmpDir.deleteDir() } } + private static testCpgFileAgainstRelatedSpapefile(File shpFile) { + ShapefileDataStore store = new ShapefileDataStore(shpFile.toURL()); + String cpgFilePath = shpFile.getAbsolutePath().replace(ShapefileConstants.SHP_EXTENSION, ShapefileConstants.CPG_EXTENSION) + + def cpgFile = new File(cpgFilePath) + if (cpgFile.exists()) { + assertTrue(store.getCharset().toString().equals(cpgFile.text)) + } else { + println("File not found.") + } + } + //@CompileStatic static void withNewShapefileWithReporterErrors(Schema schema, InstanceCollection instances, Consumer handler, Consumer configurator = null) { @@ -199,6 +215,8 @@ class ShapefileInstanceWriterTest { println "Temporary file is $tmpFile" writeInstancesWithReporterErrors(tmpFile.toFile(), schema, instances, configurator) handler.accept(tmpFile.toFile()) + + testCpgFileAgainstRelatedSpapefile(tmpFile.toFile()) } finally { tmpDir.deleteDir() } @@ -240,6 +258,7 @@ class ShapefileInstanceWriterTest { num++ } } + // 593 instances were loaded assertEquals(593, num) } diff --git a/io/plugins/eu.esdihumboldt.hale.io.shp/src/eu/esdihumboldt/hale/io/shp/ShapefileConstants.java b/io/plugins/eu.esdihumboldt.hale.io.shp/src/eu/esdihumboldt/hale/io/shp/ShapefileConstants.java index 18f7701281..bffeb96ab1 100644 --- a/io/plugins/eu.esdihumboldt.hale.io.shp/src/eu/esdihumboldt/hale/io/shp/ShapefileConstants.java +++ b/io/plugins/eu.esdihumboldt.hale.io.shp/src/eu/esdihumboldt/hale/io/shp/ShapefileConstants.java @@ -81,6 +81,12 @@ public interface ShapefileConstants { * Constant for the shape file extension. */ public static final String SHP_EXTENSION = ".shp"; + + /** + * Constant for the CPG file extension. + */ + public static final String CPG_EXTENSION = ".cpg"; + /** * Constant for underscore. */ diff --git a/io/plugins/eu.esdihumboldt.hale.io.shp/src/eu/esdihumboldt/hale/io/shp/writer/ShapefileInstanceWriter.java b/io/plugins/eu.esdihumboldt.hale.io.shp/src/eu/esdihumboldt/hale/io/shp/writer/ShapefileInstanceWriter.java index ad6e86c954..dd0147951b 100644 --- a/io/plugins/eu.esdihumboldt.hale.io.shp/src/eu/esdihumboldt/hale/io/shp/writer/ShapefileInstanceWriter.java +++ b/io/plugins/eu.esdihumboldt.hale.io.shp/src/eu/esdihumboldt/hale/io/shp/writer/ShapefileInstanceWriter.java @@ -16,6 +16,7 @@ package eu.esdihumboldt.hale.io.shp.writer; import java.io.File; +import java.io.FileWriter; import java.io.IOException; import java.io.Serializable; import java.net.URI; @@ -122,6 +123,11 @@ protected IOReport execute(ProgressIndicator progress, IOReporter reporter) setTarget(new MultiLocationOutputSupplier(uris)); } + for (String f : filesWritten) { + String cpgFileName = filePath + "/" + f + ShapefileConstants.CPG_EXTENSION; + writeCodePageFile(cpgFileName); + } + reporter.setSuccess(true); } catch (Exception e) { reporter.error(new IOMessageImpl(e.getMessage(), e)); @@ -724,4 +730,35 @@ private List writeToFile( return filesWritten; } + /** + * Create the CPG file starting from the Shapefile + * + * @param cpgFilePath Path of the file to be written with just one line of + * the encoding + * @throws IOException exception in any. + */ + public void writeCodePageFile(String cpgFilePath) throws IOException { + File cpgFile = new File(cpgFilePath); + FileWriter fileWriter = new FileWriter(cpgFile); + + ShapefileDataStore store = new ShapefileDataStore( + new File(cpgFilePath.replace(ShapefileConstants.CPG_EXTENSION, + ShapefileConstants.SHP_EXTENSION)).toURL()); + + try { + fileWriter.write(store.getCharset() != null ? store.getCharset().toString() + : getDefaultCharset().toString()); + } catch (IOException e) { + throw new IOException("An error occurred while writing the CPG file: " + cpgFilePath + + " " + e.getMessage()); + } finally { + try { + fileWriter.close(); + } catch (IOException e) { + throw new IOException("An error occurred while trying to close the CPG file: " + + cpgFilePath + " " + e.getMessage()); + } + } + } + }