/usr/lib/python3/dist-packages/rasterio/rio/convert.py is in python3-rasterio 0.36.0-2build5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | """File translation command"""
import logging
import click
from cligj import format_opt
import numpy as np
from .helpers import resolve_inout
from . import options
import rasterio
@click.command(short_help="Copy and convert raster dataset.")
@click.argument(
'files',
nargs=-1,
type=click.Path(resolve_path=True),
required=True,
metavar="INPUT OUTPUT")
@options.output_opt
@format_opt
@options.dtype_opt
@click.option('--scale-ratio', type=float, default=None,
help="Source to destination scaling ratio.")
@click.option('--scale-offset', type=float, default=None,
help="Source to destination scaling offset.")
@options.rgb_opt
@options.creation_options
@click.pass_context
def convert(
ctx, files, output, driver, dtype, scale_ratio, scale_offset,
photometric, creation_options):
"""Copy and convert raster datasets to other data types and formats.
Data values may be linearly scaled when copying by using the
--scale-ratio and --scale-offset options. Destination raster values
are calculated as
dst = scale_ratio * src + scale_offset
For example, to scale uint16 data with an actual range of 0-4095 to
0-255 as uint8:
$ rio convert in16.tif out8.tif --dtype uint8 --scale-ratio 0.0625
Format specific creation options may also be passed using --co. To
tile a new GeoTIFF output file, do the following.
--co tiled=true --co blockxsize=256 --co blockysize=256
To compress it using the LZW method, add
--co compress=LZW
"""
verbosity = (ctx.obj and ctx.obj.get('verbosity')) or 1
with rasterio.Env(CPL_DEBUG=verbosity > 2):
outputfile, files = resolve_inout(files=files, output=output)
inputfile = files[0]
with rasterio.open(inputfile) as src:
# Use the input file's profile, updated by CLI
# options, as the profile for the output file.
profile = src.profile
if 'affine' in profile:
profile['transform'] = profile.pop('affine')
if driver:
profile['driver'] = driver
if dtype:
profile['dtype'] = dtype
dst_dtype = profile['dtype']
if photometric:
creation_options['photometric'] = photometric
profile.update(**creation_options)
with rasterio.open(outputfile, 'w', **profile) as dst:
data = src.read()
if scale_ratio:
# Cast to float64 before multiplying.
data = data.astype('float64', casting='unsafe', copy=False)
np.multiply(
data, scale_ratio, out=data, casting='unsafe')
if scale_offset:
# My understanding of copy=False is that this is a
# no-op if the array was cast for multiplication.
data = data.astype('float64', casting='unsafe', copy=False)
np.add(
data, scale_offset, out=data, casting='unsafe')
# Cast to the output dtype and write.
result = data.astype(dst_dtype, casting='unsafe', copy=False)
dst.write(result)
|