本文整理汇总了Python中rasterio.open方法的典型用法代码示例。如果您正苦于以下问题:Python rasterio.open方法的具体用法?Python rasterio.open怎么用?Python rasterio.open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rasterio
的用法示例。
在下文中一共展示了rasterio.open方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: spatial_info
# 需要导入模块: import rasterio [as 别名]
# 或者: from rasterio import open [as 别名]
def spatial_info(address: str) -> Dict:
"""
Return COGEO spatial info.
Attributes
----------
address : str or PathLike object
A dataset path or URL. Will be opened in "r" mode.
Returns
-------
out : dict.
"""
with rasterio.open(address) as src_dst:
minzoom, maxzoom = get_zooms(src_dst)
bounds = transform_bounds(
src_dst.crs, constants.WGS84_CRS, *src_dst.bounds, densify_pts=21
)
center = [(bounds[0] + bounds[2]) / 2, (bounds[1] + bounds[3]) / 2, minzoom]
return dict(
address=address, bounds=bounds, center=center, minzoom=minzoom, maxzoom=maxzoom
)
示例2: preview
# 需要导入模块: import rasterio [as 别名]
# 或者: from rasterio import open [as 别名]
def preview(address: str, **kwargs: Any,) -> Tuple[numpy.ndarray, numpy.ndarray]:
"""
Return small version of a raster.
Attributes
----------
address: str
file url.
kwargs: dict, optional
These will be passed to the 'rio_tiler.reader.preview' function.
Returns
-------
data : numpy ndarray
mask: numpy array
"""
with rasterio.open(address) as src_dst:
return reader.preview(src_dst, **kwargs)
示例3: point
# 需要导入模块: import rasterio [as 别名]
# 或者: from rasterio import open [as 别名]
def point(address: str, lon: float, lat: float, **kwargs: Any) -> List:
"""
Read point value from a file.
Attributes
----------
address: str
file url.
lon: float
Longitude
lat: float
Latittude.
kwargs: dict, optional
These will be passed to the 'rio_tiler.reader.point' function.
Returns
-------
point: list
List of pixel values per bands indexes.
"""
with rasterio.open(address) as src_dst:
return reader.point(src_dst, (lon, lat), **kwargs)
示例4: test_bounds_valid
# 需要导入模块: import rasterio [as 别名]
# 或者: from rasterio import open [as 别名]
def test_bounds_valid(rio):
"""Should work as expected (get bounds)"""
rio.open = mock_rasterio_open
meta = cbers.bounds(CBERS_MUX_SCENE)
assert meta.get("sceneid") == CBERS_MUX_SCENE
assert len(meta.get("bounds")) == 4
meta = cbers.bounds(CBERS_AWFI_SCENE)
assert meta.get("sceneid") == CBERS_AWFI_SCENE
assert len(meta.get("bounds")) == 4
meta = cbers.bounds(CBERS_PAN10M_SCENE)
assert meta.get("sceneid") == CBERS_PAN10M_SCENE
assert len(meta.get("bounds")) == 4
meta = cbers.bounds(CBERS_PAN5M_SCENE)
assert meta.get("sceneid") == CBERS_PAN5M_SCENE
assert len(meta.get("bounds")) == 4
示例5: test_resampling_returns_different_results
# 需要导入模块: import rasterio [as 别名]
# 或者: from rasterio import open [as 别名]
def test_resampling_returns_different_results():
bounds = (
-8844681.416934313,
3757032.814272982,
-8766409.899970293,
3835304.331237001,
)
with rasterio.open(f"{LANDSAT_PATH}_B2.TIF") as src_dst:
arr, _ = reader.part(
src_dst, bounds, 16, 16, dst_crs=constants.WEB_MERCATOR_CRS
)
arr2, _ = reader.part(
src_dst,
bounds,
16,
16,
dst_crs=constants.WEB_MERCATOR_CRS,
resampling_method="bilinear",
)
assert not numpy.array_equal(arr, arr2)
示例6: test_that_tiling_ignores_padding_if_web_friendly_internal_tiles_exist
# 需要导入模块: import rasterio [as 别名]
# 或者: from rasterio import open [as 别名]
def test_that_tiling_ignores_padding_if_web_friendly_internal_tiles_exist():
"""Ignore Padding when COG is aligned."""
with rasterio.open(COG_WEB_TILED) as src_dst:
arr, _ = reader.tile(
src_dst, 147, 182, 9, tilesize=256, padding=0, resampling_method="bilinear"
)
arr2, _ = reader.tile(
src_dst,
147,
182,
9,
tilesize=256,
padding=100,
resampling_method="bilinear",
)
assert numpy.array_equal(arr, arr2)
示例7: test_tile_read_alpha
# 需要导入模块: import rasterio [as 别名]
# 或者: from rasterio import open [as 别名]
def test_tile_read_alpha():
"""Read masked area."""
# non-boundless tile covering the alpha masked part
with rasterio.open(S3_ALPHA_PATH) as src_dst:
arr, mask = reader.tile(
src_dst, 876432, 1603670, 22, tilesize=256, indexes=(1, 2, 3)
)
assert arr.shape == (3, 256, 256)
assert not mask.all()
with pytest.warns(AlphaBandWarning):
with rasterio.open(S3_ALPHA_PATH) as src_dst:
nb = src_dst.count
arr, mask = reader.tile(src_dst, 876432, 1603670, 22, tilesize=256)
assert not nb == arr.shape[0]
assert arr.shape == (3, 256, 256)
assert not mask.all()
示例8: test_tile_read_wrong_nodata
# 需要导入模块: import rasterio [as 别名]
# 或者: from rasterio import open [as 别名]
def test_tile_read_wrong_nodata():
"""Return empty mask on wrong nodata."""
# non-boundless tile covering the nodata part
with rasterio.open(S3_NODATA_PATH) as src_dst:
arr, mask = reader.tile(
src_dst, 438217, 801835, 21, tilesize=256, indexes=(1, 2, 3), nodata=1000
)
assert arr.shape == (3, 256, 256)
assert mask.all()
# Mask boundless values
arr, mask = reader.tile(
src_dst, 109554, 200458, 19, tilesize=256, indexes=(1, 2, 3), nodata=1000
)
assert arr.shape == (3, 256, 256)
assert not mask.all()
示例9: test_tile_read_vrt_option
# 需要导入模块: import rasterio [as 别名]
# 或者: from rasterio import open [as 别名]
def test_tile_read_vrt_option():
"""Should work as expected (read landsat band)."""
bounds = (
-8844681.416934313,
3757032.814272982,
-8766409.899970293,
3835304.331237001,
)
tilesize = 16
with rasterio.open(f"{LANDSAT_PATH}_B2.TIF") as src_dst:
arr, mask = reader.part(
src_dst,
bounds,
tilesize,
tilesize,
warp_vrt_option=dict(source_extra=10, num_threads=10),
)
assert arr.shape == (1, 16, 16)
assert mask.shape == (16, 16)
示例10: test_read_unscale
# 需要导入模块: import rasterio [as 别名]
# 或者: from rasterio import open [as 别名]
def test_read_unscale():
"""Should or Shouldn't apply scale and offset to a data."""
with rasterio.open(COG_SCALE) as src_dst:
arr, mask = reader.tile(src_dst, 218, 99, 8, tilesize=128)
arrS, maskS = reader.tile(src_dst, 218, 99, 8, tilesize=128, unscale=True)
assert arr.dtype == "int16"
assert arrS.dtype == "float32"
assert not numpy.array_equal(arr, arrS)
numpy.testing.assert_array_equal(mask, maskS)
meta = reader.metadata(src_dst)
assert isinstance(meta["statistics"][1]["min"], int)
meta = reader.metadata(src_dst, unscale=True)
assert isinstance(meta["statistics"][1]["min"], float)
p = reader.point(src_dst, [310000, 4100000], coord_crs=src_dst.crs)
assert p == [8917]
p = reader.point(
src_dst, [310000, 4100000], coord_crs=src_dst.crs, unscale=True
)
assert round(p[0], 3) == 1000.892
示例11: test_metadata_valid
# 需要导入模块: import rasterio [as 别名]
# 或者: from rasterio import open [as 别名]
def test_metadata_valid(rio, app):
"""Get bounds and get stats for all bands."""
rio.open = mock_rasterio_open
with pytest.raises(InvalidBandName):
app.metadata(stac_item, "vert")
meta = app.metadata(stac_item, "green")
assert meta["id"] == stac_item["id"]
assert len(meta["bounds"]) == 4
assert meta["band_descriptions"][0] == (1, "green")
assert len(meta["statistics"].items()) == 1
assert meta["nodata_types"] == {"green": "Nodata"}
assert meta["dtypes"] == {"green": "uint16"}
meta = app.metadata(stac_item, ["green", "red", "blue"])
assert meta["id"] == stac_item["id"]
assert len(meta["bounds"]) == 4
assert meta["band_descriptions"] == [(1, "green"), (2, "red"), (3, "blue")]
assert len(meta["statistics"].items()) == 3
assert meta["nodata_types"] == {
"green": "Nodata",
"red": "Nodata",
"blue": "Nodata",
}
示例12: test_metadata_valid_default
# 需要导入模块: import rasterio [as 别名]
# 或者: from rasterio import open [as 别名]
def test_metadata_valid_default(landsat_get_mtl, rio):
"""Get bounds and get stats for all bands."""
landsat_get_mtl.return_value = LANDSAT_METADATA
rio.open = mock_rasterio_open
meta = landsat8.metadata(LANDSAT_SCENE_C1)
assert meta["sceneid"] == LANDSAT_SCENE_C1
assert len(meta["bounds"]) == 4
assert len(meta["statistics"].items()) == 12
assert len(meta["statistics"]["1"]["histogram"][0]) == 10
assert list(map(int, meta["statistics"]["1"]["pc"])) == [1206, 6957]
meta = landsat8.metadata(LANDSAT_SCENE_C1, hist_options=dict(bins=20))
assert meta["sceneid"] == LANDSAT_SCENE_C1
assert len(meta["statistics"]["1"]["histogram"][0]) == 20
meta = landsat8.metadata(LANDSAT_SCENE_C1, hist_options=dict(range=[1000, 4000]))
assert meta["sceneid"] == LANDSAT_SCENE_C1
assert len(meta["statistics"]["1"]["histogram"][0]) == 10
示例13: test_metadata_valid_custom
# 需要导入模块: import rasterio [as 别名]
# 或者: from rasterio import open [as 别名]
def test_metadata_valid_custom(rio):
"""Get bounds and get stats for all bands with custom percentiles."""
rio.open = mock_rasterio_open
meta = sentinel2.metadata(SENTINEL_SCENE, pmin=5, pmax=95)
assert meta["sceneid"] == SENTINEL_SCENE
assert len(meta["bounds"]) == 4
assert len(meta["statistics"].items()) == 13
assert meta["statistics"]["01"]["pc"] == [1116, 7166]
meta = sentinel2.metadata(
SENTINEL_SCENE, pmin=5, pmax=95, hist_options=dict(bins=20)
)
assert meta["sceneid"] == SENTINEL_SCENE
assert len(meta["bounds"]) == 4
assert len(meta["statistics"].items()) == 13
assert meta["statistics"]["01"]["pc"] == [1116, 7166]
assert len(meta["statistics"]["01"]["histogram"][0]) == 20
meta = sentinel2.metadata(SENTINEL_SCENE, hist_options=dict(range=[1000, 4000]))
assert meta["sceneid"] == SENTINEL_SCENE
assert len(meta["statistics"]["01"]["histogram"][0]) == 10
示例14: test_metadata
# 需要导入模块: import rasterio [as 别名]
# 或者: from rasterio import open [as 别名]
def test_metadata(rio):
"""Test sentinel1.metadata."""
rio.open = mock_rasterio_open
meta = sentinel1.metadata(SENTINEL_SCENE, bands=("vv", "vh"))
assert meta["sceneid"] == SENTINEL_SCENE
assert len(meta["bounds"]) == 4
assert len(meta["statistics"].items()) == 2
assert meta["statistics"]["vv"]["min"] == 4
assert meta["statistics"]["vh"]["max"] == 623
meta = sentinel1.metadata(SENTINEL_SCENE, bands="vv")
assert len(meta["statistics"].items()) == 1
with pytest.raises(InvalidBandName):
sentinel1.metadata(SENTINEL_SCENE, bands=("nope", "vh"))
with pytest.raises(InvalidBandName):
sentinel1.metadata(SENTINEL_SCENE)
示例15: test_get_vrt_transform_valid
# 需要导入模块: import rasterio [as 别名]
# 或者: from rasterio import open [as 别名]
def test_get_vrt_transform_valid():
"""Should return correct transform and size."""
bounds = (
-11663507.036777973,
4715018.0897710975,
-11663487.927520901,
4715037.199028169,
)
with rasterio.open(S3_PATH) as src:
vrt_transform, vrt_width, vrt_height = utils.get_vrt_transform(
src, bounds, 64, 64
)
assert vrt_transform[2] == -11663507.036777973
assert vrt_transform[5] == 4715037.199028169
assert vrt_width == 100
assert vrt_height == 100
vrt_transform, vrt_width, vrt_height = utils.get_vrt_transform(
src, bounds, 256, 256
)
assert vrt_transform[2] == -11663507.036777973
assert vrt_transform[5] == 4715037.199028169
assert vrt_width == 256
assert vrt_height == 256