-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add GDALDemProcessing, other cleanup #54
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/* global Module, FS, MEMFS */ | ||
import randomKey from '../randomKey.js'; | ||
import guessFileExtension from '../guessFileExtension.js'; | ||
import ParamParser from '../stringParamAllocator.js'; | ||
|
||
// TODO: This is another good reason to switch to Typescript #55 | ||
const DEMProcessingModes = Object.freeze({ | ||
hillshade: 'hillshade', | ||
slope: 'slope', | ||
aspect: 'aspect', | ||
'color-relief': 'color-relief', | ||
TRI: 'TRI', | ||
TPI: 'TPI', | ||
roughness: 'roughness', | ||
}); | ||
|
||
export default function (GDALDEMProcessing, errorHandling, rootPath) { | ||
/* mode: one of the options in DEMProcessingModes | ||
* colors: Array of strings matching the format of the color file defined at | ||
* https://gdal.org/programs/gdaldem.html#color-relief | ||
* args: Array of strings matching the remaining arguments of gdaldem, excluding output filename | ||
*/ | ||
return function (dataset, packedArgs) { | ||
// TODO: Make this unnecessary by switching to comlink or similar (#49) | ||
const mode = packedArgs[0]; | ||
const colors = packedArgs[1]; | ||
const args = packedArgs.slice(2); | ||
|
||
if (!mode || !DEMProcessingModes.hasOwnProperty(mode)) { | ||
throw new Error(`mode must be one of {Object.keys(DEMProcessingModes)}`); | ||
} else if (mode === DEMProcessingModes['color-relief'] && !colors) { | ||
throw new Error( | ||
'A color definition array must be provided if `mode` is "color-relief"' | ||
); | ||
} else if (mode !== DEMProcessingModes['color-relief'] && colors && colors.length > 0) { | ||
throw new Error( | ||
'A color definition array should not be provided if `mode` is not "color-relief"' | ||
); | ||
} | ||
|
||
// If mode is hillshade, we need to create a color file path | ||
let colorFilePath = null; | ||
|
||
if (mode === DEMProcessingModes['color-relief']) { | ||
colorFilePath = rootPath + randomKey() + '.txt'; | ||
|
||
FS.writeFile(colorFilePath, colors.join('\n')); | ||
} | ||
let params = new ParamParser(args); | ||
|
||
params.allocate(); | ||
|
||
// argPtrsArrayPtr is now the address of the start of the list of | ||
// pointers in Emscripten heap space. Each pointer identifies the address of the start of a | ||
// parameter string, also stored in heap space. This is the direct equivalent of a char **, | ||
// which is what GDALDEMProcessingOptionsNew requires. | ||
const demOptionsPtr = Module.ccall( | ||
'GDALDEMProcessingOptionsNew', | ||
'number', | ||
['number', 'number'], | ||
[params.argPtrsArrayPtr, null] | ||
); | ||
|
||
// Validate that the options were correct | ||
const optionsErrType = errorHandling.CPLGetLastErrorType(); | ||
|
||
if ( | ||
optionsErrType === errorHandling.CPLErr.CEFailure || | ||
optionsErrType === errorHandling.CPLErr.CEFatal | ||
) { | ||
if (colorFilePath) { | ||
FS.unlink(colorFilePath); | ||
} | ||
params.deallocate(); | ||
const message = errorHandling.CPLGetLastErrorMsg(); | ||
|
||
throw new Error('Error in GDALDEMProcessing: ' + message); | ||
} | ||
|
||
// Now that we have our options, we need to make a file location to hold the output. | ||
let directory = rootPath + randomKey(); | ||
|
||
FS.mkdir(directory); | ||
// This makes it easier to remove later because we can just unmount rather than recursing | ||
// through the whole directory structure. | ||
FS.mount(MEMFS, {}, directory); | ||
let filename = randomKey(8) + '.' + guessFileExtension(args); | ||
|
||
let filePath = directory + '/' + filename; | ||
|
||
// And then we can kick off the actual processing. | ||
// The last parameter is an int* that can be used to detect certain kinds of errors, | ||
// but I'm not sure how it works yet and whether it gives the same or different information | ||
// than CPLGetLastErrorType. | ||
// Malloc ourselves an int and set it to 0 (False) | ||
let usageErrPtr = Module._malloc(Int32Array.BYTES_PER_ELEMENT); | ||
|
||
Module.setValue(usageErrPtr, 0, 'i32'); | ||
|
||
let newDatasetPtr = GDALDEMProcessing( | ||
filePath, // Output | ||
dataset, | ||
mode, | ||
colorFilePath, | ||
demOptionsPtr, | ||
usageErrPtr | ||
); | ||
|
||
let errorType = errorHandling.CPLGetLastErrorType(); | ||
// If we ever want to use the usage error pointer: | ||
// let usageErr = Module.getValue(usageErrPtr, 'i32'); | ||
|
||
// The final set of cleanup we need to do, in a function to avoid writing it twice. | ||
function cleanUp() { | ||
if (colorFilePath) { | ||
FS.unlink(colorFilePath); | ||
} | ||
Module.ccall('GDALDEMProcessingOptionsFree', null, ['number'], [demOptionsPtr]); | ||
Module._free(usageErrPtr); | ||
params.deallocate(); | ||
} | ||
|
||
// Check for errors; clean up and throw if error is detected | ||
if ( | ||
errorType === errorHandling.CPLErr.CEFailure || | ||
errorType === errorHandling.CPLErr.CEFatal | ||
) { | ||
cleanUp(); | ||
const message = errorHandling.CPLGetLastErrorMsg(); | ||
|
||
throw new Error('Error in GDALDEMProcessing: ' + message); | ||
} else { | ||
const result = { | ||
datasetPtr: newDatasetPtr, | ||
filePath: filePath, | ||
directory: directory, | ||
filename: filename, | ||
}; | ||
|
||
cleanUp(); | ||
return result; | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensuring that
inputdem
andoutput
aren't in this array when a user calls the function doesn't seem to really be a thing that could be validated in code, right? You'd just be looking for a string filename and presumably if you passed them inargs
the existing error handling would throw with a somewhat helpful error?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good question -- I don't know what will happen if the user erroneously puts in an input or output filename. I'll write a test for it and see what happens. But yeah, my hope is that the GDAL argument parsing will be able to provide helpful feedback in this case, because it would basically end up with two of them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like it says something along the lines of
Too many command options 'input.tif'
, which is not totally clear about what's going on but at least identifies the problematic argument correctly. I think seeing that would drive most users to try removing theinput.tif
option as a first step, and that would be the right thing to do even though the explanation wouldn't have reinforced that the filenames are handled internally.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a note, I'm not including the test I wrote because all the relevant functionality is on the GDAL side so the test wouldn't exercise any Loam code that doesn't already have a test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That error seems to be good enough for now. If this trips people up in the future, we could look for the "Too many command options" part of the error message and append something like "Be sure you're not including your input and output tif paths to args." to it. Otherwise, like you said, doesn't seem to be much can be done.