Skip to content

Commit

Permalink
Update test and writer
Browse files Browse the repository at this point in the history
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
  • Loading branch information
emanuelaepure10 committed Jul 3, 2023
1 parent a970f6e commit 358e160
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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.
*/
Expand Down Expand Up @@ -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<File> handler,
Consumer<ShapefileInstanceWriter> configurator = null) {
Expand All @@ -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()
}
Expand Down Expand Up @@ -240,6 +258,7 @@ class ShapefileInstanceWriterTest {
num++
}
}

// 593 instances were loaded
assertEquals(593, num)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -724,4 +730,35 @@ private List<String> 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());
}
}
}

}

0 comments on commit 358e160

Please sign in to comment.