本文整理汇总了Python中rasterio.io方法的典型用法代码示例。如果您正苦于以下问题:Python rasterio.io方法的具体用法?Python rasterio.io怎么用?Python rasterio.io使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rasterio
的用法示例。
在下文中一共展示了rasterio.io方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: memory_file
# 需要导入模块: import rasterio [as 别名]
# 或者: from rasterio import io [as 别名]
def memory_file(data=None, profile=None):
"""
Return a rasterio.io.MemoryFile instance from input.
Parameters
----------
data : array
array to be written
profile : dict
rasterio profile for MemoryFile
"""
memfile = MemoryFile()
with memfile.open(
**dict(profile, width=data.shape[-2], height=data.shape[-1])
) as dataset:
dataset.write(data)
return memfile
示例2: preview
# 需要导入模块: import rasterio [as 别名]
# 或者: from rasterio import io [as 别名]
def preview(
src_dst: Union[DatasetReader, DatasetWriter, WarpedVRT],
max_size: int = 1024,
height: int = None,
width: int = None,
**kwargs: Any,
) -> Tuple[numpy.ndarray, numpy.ndarray]:
"""
Read image and resample to low resolution.
Attributes
----------
src_dst : rasterio.io.DatasetReader
rasterio.io.DatasetReader object
max_size : int
`max_size` of the longest dimension, respecting
bounds X/Y aspect ratio.
height: int, optional
output height of the data
width: int, optional
output width of the data
kwargs : Any, optional
Additional options to forward to reader._read()
Returns
-------
data : numpy ndarray
mask: numpy array
"""
if not height and not width:
if max(src_dst.height, src_dst.width) < max_size:
height, width = src_dst.height, src_dst.width
else:
ratio = src_dst.height / src_dst.width
if ratio > 1:
height = max_size
width = math.ceil(height / ratio)
else:
width = max_size
height = math.ceil(width * ratio)
return _read(src_dst, height, width, **kwargs)
示例3: tile
# 需要导入模块: import rasterio [as 别名]
# 或者: from rasterio import io [as 别名]
def tile(
src_dst: Union[DatasetReader, DatasetWriter, WarpedVRT],
x: int,
y: int,
z: int,
tilesize: int = 256,
**kwargs,
) -> Tuple[numpy.ndarray, numpy.ndarray]:
"""
Read mercator tile from an image.
Attributes
----------
src_dst : rasterio.io.DatasetReader
rasterio.io.DatasetReader object
x : int
Mercator tile X index.
y : int
Mercator tile Y index.
z : int
Mercator tile ZOOM level.
tilesize : int, optional
Output tile size. Default is 256.
kwargs : Any, optional
Additional options to forward to part()
Returns
-------
data : numpy ndarray
mask: numpy array
"""
bounds = transform_bounds(
src_dst.crs, constants.WGS84_CRS, *src_dst.bounds, densify_pts=21
)
if not tile_exists(bounds, z, x, y):
raise TileOutsideBounds(f"Tile {z}/{x}/{y} is outside image bounds")
tile_bounds = mercantile.xy_bounds(mercantile.Tile(x=x, y=y, z=z))
return part(
src_dst,
tile_bounds,
tilesize,
tilesize,
dst_crs=constants.WEB_MERCATOR_CRS,
**kwargs,
)
示例4: cloud_optimize
# 需要导入模块: import rasterio [as 别名]
# 或者: from rasterio import io [as 别名]
def cloud_optimize(raster_file, outfile, create_mask=False, remove_nodata=False):
import math
import contextlib
import rasterio
import rasterio.io
import rasterio.shutil
COG_PROFILE = {
'count': 1,
'driver': 'GTiff',
'interleave': 'pixel',
'tiled': True,
'blockxsize': 256,
'blockysize': 256,
'compress': 'DEFLATE',
'photometric': 'MINISBLACK',
'BIGTIFF': 'IF_SAFER'
}
with contextlib.ExitStack() as es:
es.enter_context(rasterio.Env(
GDAL_TIFF_INTERNAL_MASK=True,
GDAL_TIFF_OVR_BLOCKSIZE=256,
))
src = es.enter_context(rasterio.open(str(raster_file)))
profile = src.profile.copy()
profile.update(COG_PROFILE)
if remove_nodata:
profile['nodata'] = None
memfile = es.enter_context(rasterio.io.MemoryFile())
dst = es.enter_context(memfile.open(**profile))
dst.write(src.read())
if create_mask:
dst.write_mask(src.dataset_mask().astype('uint8'))
max_overview_level = math.ceil(math.log2(max(
dst.height // profile['blockysize'],
dst.width // profile['blockxsize']
)))
overviews = [2 ** j for j in range(1, max_overview_level + 1)]
rs_method = rasterio.enums.Resampling.nearest
dst.build_overviews(overviews, rs_method)
rasterio.shutil.copy(dst, str(outfile), copy_src_overviews=True, **COG_PROFILE)