Skip to content

Commit

Permalink
feat: extend shapefile writer to create .cpg file
Browse files Browse the repository at this point in the history
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 Jun 30, 2023
1 parent a970f6e commit 58b31f1
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,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 @@ -117,6 +117,19 @@ class ShapefileInstanceWriterTest {
return reader.getInstances();
}

@Test
public void whenReadWithBufferedReader_thenCorrect()
throws IOException {
String expected_value = "UTF-8";
String file ="src/test/resources/fileTest.txt";

BufferedReader reader = new BufferedReader(new FileReader(file));
String currentLine = reader.readLine();
reader.close();

assertEquals(expected_value, currentLine);
}


/**
* Write an instance collection to a Shapefile.
Expand Down Expand Up @@ -1443,4 +1456,24 @@ class ShapefileInstanceWriterTest {
assertEquals(2, num)
}
}

@Test
void testCPGFileCreation() {
def tmpDir = Files.createTempDirectory("CPGFileTest")
def tmpFile = Files.createTempFile(tmpDir, 'new', '.cpg')

try {
ShapefileInstanceWriter writer = new ShapefileInstanceWriter();
writer.writeCodePageFile(tmpFile.toFile().getAbsolutePath());
assertTrue(tmpFile.toFile().exists());

String expected_value = writer.getCharset().toString();
BufferedReader bufferedReader = new BufferedReader(new FileReader(tmpFile.toFile().getAbsolutePath()));
String currentLine = bufferedReader.readLine();
bufferedReader.close();
assertEquals(expected_value, currentLine);
} finally {
tmpDir.deleteDir()
}
}
}
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,29 @@ private List<String> writeToFile(
return filesWritten;
}

/**
*
* @param filePath Path of the file to be written with just one line of the
* encoding
* @throws IOException exception in any.
*/
public void writeCodePageFile(String filePath) throws IOException {
File file = new File(filePath);
FileWriter fileWriter = new FileWriter(file);
try {
fileWriter.write(getCharset() != null ? getCharset().toString()
: getDefaultCharset().toString());
} catch (IOException e) {
throw new IOException("An error occurred while creating/writing the CPG file: "
+ filePath + " " + e.getMessage());
} finally {
try {
fileWriter.close();
} catch (IOException e) {
throw new IOException("An error occurred while trying to close the CPG file: "
+ filePath + " " + e.getMessage());
}
}
}

}

0 comments on commit 58b31f1

Please sign in to comment.