Skip to content

Commit

Permalink
Update ShapefileInstanceWriter removing one method
Browse files Browse the repository at this point in the history
  • Loading branch information
emanuelaepure10 committed Jun 29, 2023
1 parent 0afec65 commit 37d0458
Showing 1 changed file with 29 additions and 44 deletions.
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 @@ -104,36 +108,31 @@ protected IOReport execute(ProgressIndicator progress, IOReporter reporter)
throws IOProviderConfigurationException, IOException {
progress.begin("Generating Shapefile/CPG", ProgressIndicator.UNKNOWN);
InstanceCollection instances = getInstances();
/**
* @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;
}
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 = new ArrayList<>();

for (String f : filesWritten) {
File file = new File(filePath + "/" + f + ShapefileConstants.SHP_EXTENSION);
uris.add(file.toURI());

String cpgFileName = filePath + "/" + f + ShapefileConstants.CPG_EXTENSION;
writeCodePageFile(cpgFileName);
file = new File(cpgFileName);
uris.add(file.toURI());
}

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

reporter.setSuccess(true);
} catch (Exception e) {
Expand Down Expand Up @@ -485,7 +484,7 @@ 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
* Create the CPG file starting from the Shapefile names
*
* @param location file location.
* @param numberOfSchemas number of schemas.
Expand Down Expand Up @@ -740,23 +739,8 @@ 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
Expand All @@ -766,6 +750,7 @@ private void writeCodePageFile(String filePath) throws IOException {
try (BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(filePath), StandardCharsets.UTF_8))) {
writer.write("UTF-8");
writer.close();
} catch (IOException e) {
throw new IOException("An error occurred while creating/writing the file: filePath "
+ e.getMessage());
Expand Down

0 comments on commit 37d0458

Please sign in to comment.