/usr/lib/python3/dist-packages/rasterio/rio/clip.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 | """File translation command"""
import click
from cligj import format_opt
from .helpers import resolve_inout
from . import options
import rasterio
from rasterio.coords import disjoint_bounds
# Clip command
@click.command(short_help='Clip a raster to given bounds.')
@click.argument(
'files',
nargs=-1,
type=click.Path(),
required=True,
metavar="INPUT OUTPUT")
@options.output_opt
@options.bounds_opt
@click.option(
'--like',
type=click.Path(exists=True),
help='Raster dataset to use as a template for bounds')
@format_opt
@options.creation_options
@click.pass_context
def clip(
ctx,
files,
output,
bounds,
like,
driver,
creation_options):
"""Clips a raster using bounds input directly or from a template raster.
\b
$ rio clip input.tif output.tif --bounds xmin ymin xmax ymax
$ rio clip input.tif output.tif --like template.tif
If using --bounds, values must be in coordinate reference system of input.
If using --like, bounds will automatically be transformed to match the
coordinate reference system of the input.
It can also be combined to read bounds of a feature dataset using Fiona:
\b
$ rio clip input.tif output.tif --bounds $(fio info features.shp --bounds)
"""
from rasterio.warp import transform_bounds
verbosity = (ctx.obj and ctx.obj.get('verbosity')) or 1
with rasterio.Env(CPL_DEBUG=verbosity > 2):
output, files = resolve_inout(files=files, output=output)
input = files[0]
with rasterio.open(input) as src:
if bounds:
if disjoint_bounds(bounds, src.bounds):
raise click.BadParameter('must overlap the extent of '
'the input raster',
param='--bounds',
param_hint='--bounds')
elif like:
with rasterio.open(like) as template_ds:
bounds = template_ds.bounds
if template_ds.crs != src.crs:
bounds = transform_bounds(template_ds.crs, src.crs,
*bounds)
if disjoint_bounds(bounds, src.bounds):
raise click.BadParameter('must overlap the extent of '
'the input raster',
param='--like',
param_hint='--like')
else:
raise click.UsageError('--bounds or --like required')
window = src.window(*bounds)
out_kwargs = src.meta.copy()
out_kwargs.update({
'driver': driver,
'height': window[0][1] - window[0][0],
'width': window[1][1] - window[1][0],
'transform': src.window_transform(window)
})
out_kwargs.update(**creation_options)
with rasterio.open(output, 'w', **out_kwargs) as out:
out.write(src.read(window=window))
|