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 a new file .cpg which should contain just the text
“UTF-8” together together with the .shp file.

ING-3911
  • Loading branch information
emanuelaepure10 committed Jun 28, 2023
1 parent a970f6e commit e1c8033
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
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 @@ -15,10 +15,14 @@

package eu.esdihumboldt.hale.io.shp.writer;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Serializable;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Paths;
import java.util.ArrayList;
Expand Down Expand Up @@ -102,20 +106,27 @@ public boolean isCancelable() {
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter)
throws IOProviderConfigurationException, IOException {
progress.begin("Generating Shapefile", ProgressIndicator.UNKNOWN);
progress.begin("Generating Shapefile/CPG", ProgressIndicator.UNKNOWN);
InstanceCollection instances = getInstances();
List<String> filesWritten;
List<String> codepagefilesWritten;
try {
URI location = getTarget().getLocation();
String filePath = Paths.get(location).getParent().toString();

filesWritten = writeInstances(instances, progress, reporter, location);
codepagefilesWritten = writeCodePageFiles(filesWritten);

if (filesWritten.size() > 1) {
List<URI> uris = filesWritten.stream().map(f -> {
if (filesWritten.size() > 1 && codepagefilesWritten.size() > 1) {

List<URI> uris = new ArrayList<>();

for (String f : filesWritten) {
File file = new File(filePath + "/" + f + ShapefileConstants.SHP_EXTENSION);
return file.toURI();
}).collect(Collectors.toList());
uris.add(file.toURI());
file = new File(filePath + "/" + f + ShapefileConstants.CPG_EXTENSION);
uris.add(file.toURI());
}

// Reset the target property so that a caller can find out which
// files were created.
Expand All @@ -126,7 +137,7 @@ protected IOReport execute(ProgressIndicator progress, IOReporter reporter)
} catch (Exception e) {
reporter.error(new IOMessageImpl(e.getMessage(), e));
reporter.setSuccess(false);
reporter.setSummary("Saving instances to Shapefile failed.");
reporter.setSummary("Saving instances to Shapefile/CPG failed.");
} finally {
progress.end();
}
Expand Down Expand Up @@ -472,6 +483,8 @@ private Map<String, Map<String, ShapefileDataStore>> createSchema(URI location,
* - filename_geometryType.shp if multiple geometries.<br>
* - filename.shp single schema and geom.
*
* Create the CPG file starting from the Shapefile names
*
* @param location file location.
* @param numberOfSchemas number of schemas.
* @param schemaEntry current schema in process.
Expand Down Expand Up @@ -507,6 +520,8 @@ else if (numberOfGeometries > 1) {
File file;
try {
file = new File(filenameWithType);
writeCodePageFile(filenameWithType.replace(ShapefileConstants.SHP_EXTENSION,
ShapefileConstants.CPG_EXTENSION));
} catch (Exception e) {
throw new IllegalArgumentException("Only files are supported as data source", e);
}
Expand Down Expand Up @@ -724,4 +739,35 @@ private List<String> writeToFile(
return filesWritten;
}

/**
* @param filesWritten List of file names that were written (without
* suffixes)
*
* @return same list of Files written
* @throws IOException exception in any.
*/
private List<String> writeCodePageFiles(List<String> filesWritten) throws IOException {
for (String filePath : filesWritten) {
writeCodePageFile(filePath);
}
return filesWritten;
}

/**
*
*
* @param filePath Path of the file to be written with just one line of the
* encoding
* @throws IOException exception in any.
*/
private void writeCodePageFile(String filePath) throws IOException {
try (BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(filePath), StandardCharsets.UTF_8))) {
writer.write("UTF-8");
} catch (IOException e) {
throw new IOException("An error occurred while creating/writing the file: filePath "
+ e.getMessage());
}
}

}

0 comments on commit e1c8033

Please sign in to comment.