Skip to content

Instantly share code, notes, and snippets.

@pradoz
Created July 6, 2022 17:02
Show Gist options
  • Save pradoz/eaf42947c2c6f118e4b22cd0e39a1330 to your computer and use it in GitHub Desktop.
Save pradoz/eaf42947c2c6f118e4b22cd0e39a1330 to your computer and use it in GitHub Desktop.
import rasterio
import numpy as np
INP_FILE = "91_1040010039040600.tif"
dataset = rasterio.open(INP_FILE, "r")
print(f'dataset: {dataset}')
# r = np.zeros(data.shape)
# g = np.zeros(data.shape)
# b = np.zeros(data.shape)
print(f'dataset.name: {dataset.name}')
print(f'dataset.mode: {dataset.mode}')
print(f'dataset.closed: {dataset.closed}')
print(f'dataset.width: {dataset.width}')
print(f'dataset.height: {dataset.height}')
print(f'dataset.meta: {dataset.meta}')
# dtype_dict = {i: dtype for i, dtype in zip(dataset.indexes, dataset.dtypes)}
# print(f'dtype_dict: {dtype_dict}')
# meta = src.meta
# print(f'meta: {meta}')
# meta.update(
# dtype=rasterio.uint8,
# nodata=0,
# count=3
# )
print(f'dataset.bounds: {dataset.bounds}')
print(f'dataset.transform: \n{dataset.transform}')
upper_left_corner = dataset.transform*(0,0)
lower_right_corner = dataset.transform * (dataset.width, dataset.height)
print(f'upper_left_corner: {upper_left_corner}')
print(f'lower_right_corner: {lower_right_corner}')
print(f'dataset.crs: {dataset.crs}')
# band indexing
print(f'dataset.indexes: {dataset.indexes}')
read_band_1 = dataset.read(1)
read_band_2 = dataset.read(2)
read_band_3 = dataset.read(3)
print(f'read_band_1: {read_band_1}')
print(f'read_band_2: {read_band_2}')
print(f'read_band_3: {read_band_3}')
check1 = read_band_1[dataset.height // 2, dataset.width // 2]
print(f'check1: {check1}')
check2 = dataset.xy(dataset.height // 2, dataset.width // 2)
print(f'check2: {check2}')
# wrong... check2 = read_band_2[dataset.height // 2][dataset.width // 2]
# print(f'check2: {check2}')
# with rasterio.open('outfile.tif', 'w', **meta) as dst:
# dst.write_band(1, r.astype(rasterio.uint8))
# dst.write_band(2, g.astype(rasterio.uint8))
# dst.write_band(3, b.astype(rasterio.uint8))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment