/usr/lib/python3/dist-packages/rasterio/rio/warp.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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | import logging
from math import ceil
import warnings
import click
from cligj import files_inout_arg, format_opt
from .helpers import resolve_inout
from . import options
import rasterio
from rasterio.crs import CRS
from rasterio.errors import CRSError
from rasterio.transform import Affine
from rasterio.warp import (
reproject, Resampling, calculate_default_transform, transform_bounds)
# Improper usage of rio-warp can lead to accidental creation of
# extremely large datasets. We'll put a hard limit on the size of
# datasets and raise a usage error if the limits are exceeded.
MAX_OUTPUT_WIDTH = 100000
MAX_OUTPUT_HEIGHT = 100000
def bounds_handler(ctx, param, value):
"""Warn about future usage changes."""
if value:
click.echo(
"Future Warning: "
"the semantics of the `--bounds` option will change in Rasterio "
"version 1.0 from bounds of the source dataset to bounds of the "
"destination dataset.", err=True)
return value
def x_dst_bounds_handler(ctx, param, value):
"""Warn about future usage changes."""
if value:
click.echo(
"Future Warning: "
"the `--x-dst-bounds` option will be removed in Rasterio version "
"1.0 in favor of `--bounds`.", err=True)
return value
@click.command(short_help='Warp a raster dataset.')
@files_inout_arg
@options.output_opt
@format_opt
@click.option(
'--like',
type=click.Path(exists=True),
help='Raster dataset to use as a template for obtaining affine '
'transform (bounds and resolution), and crs.')
@click.option('--dst-crs', default=None,
help='Target coordinate reference system.')
@options.dimensions_opt
@click.option(
'--src-bounds',
nargs=4, type=float, default=None,
help="Determine output extent from source bounds: left bottom right top "
"(note: for future backwards compatibility in 1.0).")
@click.option(
'--x-dst-bounds',
nargs=4, type=float, default=None, callback=x_dst_bounds_handler,
help="Set output extent from bounding values: left bottom right top "
"(note: this option will be removed in 1.0).")
@click.option(
'--bounds',
nargs=4, type=float, default=None, callback=bounds_handler,
help="Determine output extent from source bounds: left bottom right top "
"(note: the semantics of this option will change to those of "
"`--x-dst-bounds` in version 1.0).")
@options.resolution_opt
@click.option('--resampling', type=click.Choice([r.name for r in Resampling]),
default='nearest', help="Resampling method.",
show_default=True)
@click.option('--src-nodata', default=None, show_default=True,
type=float, help="Manually override source nodata")
@click.option('--dst-nodata', default=None, show_default=True,
type=float, help="Manually override destination nodata")
@click.option('--threads', type=int, default=1,
help='Number of processing threads.')
@click.option('--check-invert-proj', type=bool, default=True,
help='Constrain output to valid coordinate region in dst-crs')
@options.force_overwrite_opt
@options.creation_options
@click.pass_context
def warp(ctx, files, output, driver, like, dst_crs, dimensions, src_bounds,
x_dst_bounds, bounds, res, resampling, src_nodata, dst_nodata, threads, check_invert_proj,
force_overwrite, creation_options):
"""
Warp a raster dataset.
If a template raster is provided using the --like option, the
coordinate reference system, affine transform, and dimensions of
that raster will be used for the output. In this case --dst-crs,
--bounds, --res, and --dimensions options are ignored.
\b
$ rio warp input.tif output.tif --like template.tif
The output coordinate reference system may be either a PROJ.4 or
EPSG:nnnn string,
\b
--dst-crs EPSG:4326
--dst-crs '+proj=longlat +ellps=WGS84 +datum=WGS84'
or a JSON text-encoded PROJ.4 object.
\b
--dst-crs '{"proj": "utm", "zone": 18, ...}'
If --dimensions are provided, --res and --bounds are ignored.
Resolution is calculated based on the relationship between the
raster bounds in the target coordinate system and the dimensions,
and may produce rectangular rather than square pixels.
\b
$ rio warp input.tif output.tif --dimensions 100 200 \\
> --dst-crs EPSG:4326
If --bounds are provided, --res is required if --dst-crs is provided
(defaults to source raster resolution otherwise).
\b
$ rio warp input.tif output.tif \\
> --bounds -78 22 -76 24 --res 0.1 --dst-crs EPSG:4326
"""
verbosity = (ctx.obj and ctx.obj.get('verbosity')) or 1
output, files = resolve_inout(
files=files, output=output, force_overwrite=force_overwrite)
resampling = Resampling[resampling] # get integer code for method
if not len(res):
# Click sets this as an empty tuple if not provided
res = None
else:
# Expand one value to two if needed
res = (res[0], res[0]) if len(res) == 1 else res
with rasterio.Env(CPL_DEBUG=verbosity > 2,
CHECK_WITH_INVERT_PROJ=check_invert_proj):
with rasterio.open(files[0]) as src:
l, b, r, t = src.bounds
out_kwargs = src.meta.copy()
out_kwargs['driver'] = driver
# Sort out the bounds options.
src_bounds = bounds or src_bounds
dst_bounds = x_dst_bounds
if src_bounds and dst_bounds:
raise click.BadParameter(
"Source and destination bounds may not be specified "
"simultaneously.")
if like:
with rasterio.open(like) as template_ds:
dst_crs = template_ds.crs
dst_transform = template_ds.affine
dst_height = template_ds.height
dst_width = template_ds.width
elif dst_crs is not None:
try:
dst_crs = CRS.from_string(dst_crs)
except ValueError as err:
raise click.BadParameter(
str(err), param='dst_crs', param_hint='dst_crs')
if dimensions:
# Calculate resolution appropriate for dimensions
# in target.
dst_width, dst_height = dimensions
try:
xmin, ymin, xmax, ymax = transform_bounds(
src.crs, dst_crs, *src.bounds)
except CRSError as err:
raise click.BadParameter(
str(err), param='dst_crs', param_hint='dst_crs')
dst_transform = Affine(
(xmax - xmin) / float(dst_width),
0, xmin, 0,
(ymin - ymax) / float(dst_height),
ymax
)
elif src_bounds or dst_bounds:
if not res:
raise click.BadParameter(
"Required when using --bounds.",
param='res', param_hint='res')
if src_bounds:
try:
xmin, ymin, xmax, ymax = transform_bounds(
src.crs, dst_crs, *src_bounds)
except CRSError as err:
raise click.BadParameter(
str(err), param='dst_crs',
param_hint='dst_crs')
else:
xmin, ymin, xmax, ymax = dst_bounds
dst_transform = Affine(res[0], 0, xmin, 0, -res[1], ymax)
dst_width = max(int(ceil((xmax - xmin) / res[0])), 1)
dst_height = max(int(ceil((ymax - ymin) / res[1])), 1)
else:
try:
dst_transform, dst_width, dst_height = calculate_default_transform(
src.crs, dst_crs, src.width, src.height,
*src.bounds, resolution=res)
except CRSError as err:
raise click.BadParameter(
str(err), param='dst_crs', param_hint='dst_crs')
elif dimensions:
# Same projection, different dimensions, calculate resolution.
dst_crs = src.crs
dst_width, dst_height = dimensions
dst_transform = Affine(
(r - l) / float(dst_width),
0, l, 0,
(b - t) / float(dst_height),
t
)
elif src_bounds or dst_bounds:
# Same projection, different dimensions and possibly
# different resolution.
if not res:
res = (src.affine.a, -src.affine.e)
dst_crs = src.crs
xmin, ymin, xmax, ymax = (src_bounds or dst_bounds)
dst_transform = Affine(res[0], 0, xmin, 0, -res[1], ymax)
dst_width = max(int(ceil((xmax - xmin) / res[0])), 1)
dst_height = max(int(ceil((ymax - ymin) / res[1])), 1)
elif res:
# Same projection, different resolution.
dst_crs = src.crs
dst_transform = Affine(res[0], 0, l, 0, -res[1], t)
dst_width = max(int(ceil((r - l) / res[0])), 1)
dst_height = max(int(ceil((t - b) / res[1])), 1)
else:
dst_crs = src.crs
dst_transform = src.affine
dst_width = src.width
dst_height = src.height
# If src_nodata is not None, update the dst metadata NODATA
# value to src_nodata (will be overridden by dst_nodata if it is not None
if src_nodata is not None:
# Update the dst nodata value
out_kwargs.update({
'nodata': src_nodata
})
# Validate a manually set destination NODATA value
# against the input datatype.
if dst_nodata is not None:
if src_nodata is None and src.meta['nodata'] is None:
raise click.BadParameter(
"--src-nodata must be provided because dst-nodata is not None")
else:
# Update the dst nodata value
out_kwargs.update({
'nodata': dst_nodata
})
# When the bounds option is misused, extreme values of
# destination width and height may result.
if (dst_width < 0 or dst_height < 0 or
dst_width > MAX_OUTPUT_WIDTH or
dst_height > MAX_OUTPUT_HEIGHT):
raise click.BadParameter(
"Invalid output dimensions: {0}.".format(
(dst_width, dst_height)))
out_kwargs.update({
'crs': dst_crs,
'transform': dst_transform,
'affine': dst_transform,
'width': dst_width,
'height': dst_height
})
out_kwargs.update(**creation_options)
with rasterio.open(output, 'w', **out_kwargs) as dst:
for i in range(1, src.count + 1):
reproject(
source=rasterio.band(src, i),
destination=rasterio.band(dst, i),
src_transform=src.affine,
src_crs=src.crs,
src_nodata=src_nodata,
dst_transform=out_kwargs['transform'],
dst_crs=out_kwargs['crs'],
dst_nodata=dst_nodata,
resampling=resampling,
num_threads=threads)
|