Skip to content

Commit

Permalink
feat: extend Shapefile writer with log messages on written files
Browse files Browse the repository at this point in the history
ING-4479
  • Loading branch information
stempler committed Oct 22, 2024
1 parent 1f3d98a commit 9b06d1e
Showing 1 changed file with 41 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,11 @@ protected IOReport execute(ProgressIndicator progress, IOReporter reporter)
}).collect(Collectors.toList());

// Reset the target property so that a caller can find out
// which
// files were created.
// which files were created.
setTarget(new MultiLocationOutputSupplier(uris));

reporter.info("Multiple Shapefiles have been exported. The file names are: "
+ filesWritten.stream().collect(Collectors.joining(", ")));
}

for (String f : filesWritten) {
Expand Down Expand Up @@ -194,7 +196,7 @@ protected List<String> writeInstances(InstanceCollection instanceCollection,
Map<String, Map<String, List<SimpleFeature>>> schemaFeaturesMap = createFeatures(
instanceCollection, progress, reporter, schemaFtMap);

return writeToFile(schemaDataStoreMap, schemaFtMap, schemaFeaturesMap);
return writeToFile(schemaDataStoreMap, schemaFtMap, schemaFeaturesMap, reporter);
}

/**
Expand Down Expand Up @@ -750,7 +752,8 @@ private void addGeometryData(IOReporter reporter,
private List<String> writeToFile(
Map<String, Map<String, ShapefileDataStore>> schemaDataStoreMap,
Map<String, Map<String, SimpleFeatureType>> schemaFtMap,
Map<String, Map<String, List<SimpleFeature>>> schemaFeaturesMap) throws IOException {
Map<String, Map<String, List<SimpleFeature>>> schemaFeaturesMap, SimpleLog log)
throws IOException {

List<String> filesWritten = new ArrayList<String>();

Expand All @@ -764,37 +767,43 @@ private List<String> writeToFile(
ShapefileConstants.CREATE_CONSTANT);

ShapefileDataStore dataStore = geomEntry.getValue();
String typeName = dataStore.getTypeNames()[0];
for (Name name : dataStore.getNames()) {
// The local part of the Name contains the file name of the
// ShapefileDataStore (without suffix)
filesWritten.add(name.getLocalPart());
}
try {
String typeName = dataStore.getTypeNames()[0];
for (Name name : dataStore.getNames()) {
// The local part of the Name contains the file name of the
// ShapefileDataStore (without suffix)
filesWritten.add(name.getLocalPart());
}

SimpleFeatureSource geomSpecificFeatureSource = geomEntry.getValue()
.getFeatureSource(typeName);
if (geomSpecificFeatureSource instanceof SimpleFeatureStore) {
SimpleFeatureStore geomSpecificFeatureStore = (SimpleFeatureStore) geomSpecificFeatureSource;

// create collection to write to the shape file.
SimpleFeatureCollection collection = new ListFeatureCollection(
schemaFtMap.get(localPart).get(geomEntry.getKey()),
schemaFeaturesMap.get(localPart).get(geomEntry.getKey()));
geomSpecificFeatureStore.setTransaction(transaction);
try {
geomSpecificFeatureStore.addFeatures(collection);
transaction.commit();
} catch (IOException e) {
transaction.rollback();
throw e;
} finally {
SimpleFeatureSource geomSpecificFeatureSource = geomEntry.getValue()
.getFeatureSource(typeName);
if (geomSpecificFeatureSource instanceof SimpleFeatureStore) {
SimpleFeatureStore geomSpecificFeatureStore = (SimpleFeatureStore) geomSpecificFeatureSource;

// create collection to write to the shape file.
SimpleFeatureCollection collection = new ListFeatureCollection(
schemaFtMap.get(localPart).get(geomEntry.getKey()),
schemaFeaturesMap.get(localPart).get(geomEntry.getKey()));
geomSpecificFeatureStore.setTransaction(transaction);
try {
log.info("Writing {0} features to Shapefile {1} with geometry type {2}",
collection.size(), localPart, geomEntry.getKey());
geomSpecificFeatureStore.addFeatures(collection);
transaction.commit();
} catch (IOException e) {
transaction.rollback();
throw e;
} finally {
transaction.close();
}
}
else {
// throw exception
transaction.close();
throw new IOException(typeName + " does not support read/write access");
}
}
else {
// throw exception
transaction.close();
throw new IOException(typeName + " does not support read/write access");
} finally {
dataStore.dispose();
}
}
}
Expand Down

0 comments on commit 9b06d1e

Please sign in to comment.