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.
  • Loading branch information
emanuelaepure10 committed Jun 29, 2023
1 parent a970f6e commit de91b2d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 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 @@ -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 @@ -472,6 +478,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 @@ -724,4 +732,30 @@ private List<String> writeToFile(
return filesWritten;
}

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

}

0 comments on commit de91b2d

Please sign in to comment.