Skip to content
This repository has been archived by the owner on Jul 15, 2020. It is now read-only.

Commit

Permalink
Cleanup ingested files
Browse files Browse the repository at this point in the history
  • Loading branch information
go0ty committed Apr 12, 2016
1 parent d1b9859 commit c57b1c7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
28 changes: 21 additions & 7 deletions src/main/java/ingest/inspect/GeoTiffInspector.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@
import org.geotools.coverage.grid.io.GridFormatFinder;
import org.geotools.referencing.CRS;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import util.PiazzaLogger;

/**
* Inspects GeoTIFF file, parsing essential metadata from it.
*
Expand All @@ -41,6 +44,8 @@
*/
@Component
public class GeoTiffInspector implements InspectorType {
@Autowired
private PiazzaLogger logger;
@Value("${data.temp.path}")
private String DATA_TEMP_PATH;
@Value("${vcap.services.pz-blobstore.credentials.access:}")
Expand All @@ -52,7 +57,9 @@ public class GeoTiffInspector implements InspectorType {
public DataResource inspect(DataResource dataResource, boolean host) throws Exception {

// Gather GeoTIFF relevant metadata
GridCoverage2D coverage = getGridCoverage(dataResource);
String fileName = String.format("%s%s%s.%s", DATA_TEMP_PATH, File.separator, dataResource.getDataId(), "tif");
File geoTiffFile = new File(fileName);
GridCoverage2D coverage = getGridCoverage(dataResource, geoTiffFile);
CoordinateReferenceSystem coordinateReferenceSystem = coverage.getCoordinateReferenceSystem();
double[] upperRightCorner = coverage.getEnvelope().getUpperCorner().getDirectPosition().getCoordinate();
double[] lowerLeftCorner = coverage.getEnvelope().getLowerCorner().getDirectPosition().getCoordinate();
Expand All @@ -71,6 +78,15 @@ public DataResource inspect(DataResource dataResource, boolean host) throws Exce
// Set the Spatial Metadata
dataResource.spatialMetadata = spatialMetadata;

// Delete the file; cleanup.
try {
coverage.dispose(true);
geoTiffFile.delete();
} catch (Exception exception) {
logger.log(String.format("Error cleaning up GeoTiff file for %s ingest: %s", dataResource.getDataId(),
exception.getMessage()), PiazzaLogger.WARNING);
}

// Return the metadata
return dataResource;
}
Expand All @@ -82,17 +98,15 @@ public DataResource inspect(DataResource dataResource, boolean host) throws Exce
* The DataResource to gather GeoTIFF source info
* @return GridCoverage2D grid coverage
*/
private GridCoverage2D getGridCoverage(DataResource dataResource) throws Exception {
private GridCoverage2D getGridCoverage(DataResource dataResource, File file) throws Exception {
// Get the file from S3
FileAccessFactory fileFactory = new FileAccessFactory(AMAZONS3_ACCESS_KEY, AMAZONS3_PRIVATE_KEY);
InputStream tiffFileStream = fileFactory.getFile(((RasterDataType) dataResource.getDataType()).getLocation());
File geoTiffFile = new File(String.format("%s%s%s.%s", DATA_TEMP_PATH, File.separator,
dataResource.getDataId(), "tif"));
FileUtils.copyInputStreamToFile(tiffFileStream, geoTiffFile);
FileUtils.copyInputStreamToFile(tiffFileStream, file);

// Read the coverage file
AbstractGridFormat format = GridFormatFinder.findFormat(geoTiffFile);
GridCoverage2DReader reader = format.getReader(geoTiffFile);
AbstractGridFormat format = GridFormatFinder.findFormat(file);
GridCoverage2DReader reader = format.getReader(file);
GridCoverage2D coverage = (GridCoverage2D) reader.read(null);

return coverage;
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ pz.logger.console=true
pz.uuid.url=pz-uuidgen.stage.geointservices.io/v1/uuids
pz.workflow.event.id=W7

vcap.services.pz-blobstore.credentials.bucket=piazza-persist
vcap.services.pz-blobstore.credentials.bucket=external-public-access-test

0 comments on commit c57b1c7

Please sign in to comment.