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 files whenever a .shp file is created.
The .cpg files contain just the text “UTF-8”
  • Loading branch information
emanuelaepure10 committed Jun 29, 2023
1 parent a970f6e commit 0afec65
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 17 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 @@ -102,31 +102,44 @@ 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;
try {
URI location = getTarget().getLocation();
String filePath = Paths.get(location).getParent().toString();

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

if (filesWritten.size() > 1) {
List<URI> uris = filesWritten.stream().map(f -> {
File file = new File(filePath + "/" + f + ShapefileConstants.SHP_EXTENSION);
return file.toURI();
}).collect(Collectors.toList());
/**
* @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;
}

// Reset the target property so that a caller can find out which
// files were created.
setTarget(new MultiLocationOutputSupplier(uris));
/**
*
*
* @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());
}
}

reporter.setSuccess(true);
} 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 +485,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 +522,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 @@ -723,5 +740,36 @@ 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 0afec65

Please sign in to comment.