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 a --like option to rio-edit-info #399
Conversation
In combination with, eg, `--crs ?`, this will extract the metadata value from another file. See #387.
@sgillies It would be nice if
#387 suggests that the command below should only copy the CRS from the $ rio edit-info --like EPSG4326-nodata100.tif --crs ? tests/data/RGB.byte.tif This demonstrates the problem: import rasterio as rio
from click.testing import CliRunner
from rasterio.rio.main import main_group
# This is the file to alter
basefile = 'BASE.tif'
meta = {
'driver': 'GTiff',
'height': 120,
'width': 220,
'nodata': 0.0,
'dtype': rio.ubyte,
'crs': 'EPSG:32610',
'count': 1
}
with rio.open(basefile, 'w', **meta) as src:
assert src.nodata == 0
assert src.crs == {'init': 'epsg:32610'}
# This is the file to inherit from
template = 'LIKE.tif'
meta = {
'driver': 'GTiff',
'width': 100,
'height': 200,
'crs': 'EPSG:4326',
'nodata': 100,
'count': 1,
'dtype': rio.ubyte
}
with rio.open(template, 'w', **meta) as src:
assert src.nodata == 100
assert src.crs == {'init': 'epsg:4326'}
result = CliRunner().invoke(main_group, [
'edit-info',
'--like', template,
'--crs', '?',
basefile
])
assert result.exit_code == 0
with rio.open(basefile) as src:
assert src.crs == {'init': 'epsg:4326'}, 'CRS failed'
assert src.nodata == 0, "nodata failed" |
@geowurster thanks, I'll look into that.
|
Oh right, duh. |
combination of the --like, --crs, --nodata, --transform, and --all | ||
options. | ||
|
||
rio-edit-info example.tif --all --like template.tif |
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.
@sgillies typo: rio-edit-info
-> rio edit-info
Also, the description above would be more clear like this:
Metadata items may also be read from an existing dataset using a
combination of the --like option with at least one of --all, --crs,
--nodata, and --transform options.
It is unclear what values are going to be copied from template when --like
is provided but others are not. When I tested this option only, I got several unexpected results given that --all
is supposed to be False
by default:
$ cp tests/data/shade.tif /tmp/test.tif
$ rio info /tmp/test.tif
{"count": 1, "crs": "EPSG:3857", "res": [9.5546285343, 9.5546285343], "bounds": [-11858134.818413004, 4803914.3530251775, -11848350.87879388, 4813698.2926443005], "dtype": "uint8", "driver": "GTiff", "transform": [9.5546285343, 0.0, -11858134.818413004, 0.0, -9.5546285343, 4813698.2926443005], "lnglat": [-106.47949217280885, 39.605688173875606], "height": 1024, "width": 1024, "shape": [1024, 1024], "blockxsize": 1024, "tiled": false, "blockysize": 8, "nodata": 255.0}
$ rio edit-info /tmp/test.tif --like tests/data/RGB.byte.tif
$ rio info /tmp/test.tif
{"count": 1, "crs": "EPSG:32618", "res": [300.0379266750948, 300.041782729805], "bounds": [101985.0, 2519672.2144846795, 409223.8369152971, 2826915.0], "dtype": "uint8", "driver": "GTiff", "transform": [300.0379266750948, 0.0, 101985.0, 0.0, -300.041782729805, 2826915.0], "lnglat": [-77.40522561315726, 24.153251565772365], "height": 1024, "width": 1024, "shape": [1024, 1024], "blockxsize": 1024, "tiled": false, "blockysize": 8, "nodata": 255.0}
Looks like crs and transform were copied across but not nodata.
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.
Please consider updating the docstring comments for --transform
because the value must be string quoted but isn't demonstrated that way:
--transform '[300.038, 0.0, 101985.0, 0.0, -300.042, 2826915.0]'
I don't know that calling them JSON-encoded makes that super clear in this context; they are just plain arrays, in quotes...
zsh doesn't appear to like the question marks used for placeholders:
Quoting like this fixes it I'm finding the same issues as above related to more values than specified via the option getting copied across: |
Rather than use a single semi-ambiguous character, why not just use the word The value is already being handled in a callback so we can make it whatever we want so we may as well take advantage of that and be cleaner and explicit. $ rio edit-info \
Input.tif \
--nodata 0 \
--like Template.tif \
--crs like \
--transform like |
Also add more tests of bad parameters.
Great comments, @brendan-ward @geowurster. I believe I've addressed them in the recent commit. |
retval = json.loads(value) | ||
except ValueError: | ||
retval = value | ||
if not (rasterio.crs.is_geographic_crs(retval) or |
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.
@sgillies consider adding a rasterio.crs.is_valid_crs
function to handle this objective (internally it can make these two calls). It seems a little out of place to use these tests because in this context we don't care if it is geographic or projected, we only care that it is valid...
Also add another test of the --all callback and fix the bug it exposed.
Add a --like option to rio-edit-info
People say the best parts of rasterio are that "its more Pythonic" or, "it has a modern and well composed CLI" or "it gives you a numpy array like, wicked fast", but they're all wrong. The best part of rasterio is topical Ralph Wiggum GIFs. |
In combination with, eg,
--crs ?
, this will extract the metadata value from another file.So far, it seems like a value will be required for the options. I think
?
makes a decent placeholder.This PR also moves validation of the --crs, --transform, and --nodata options to callbacks, simplifying the command's function quite a bit.
Thoughts, @brendan-ward @jacquestardie @geowurster?
See #387.