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
| def buildArray(number:int): result = [] for i in range(180): cache = [] for j in range(360): cache.append(number) result.append(cache) return result
import numpy as np import rasterio from rasterio.transform import Affine from rasterio.crs import CRS
driver = 'GTiff' dtype = 'int16' nodata = None width = 360 height = 180 count = 2 crs = CRS.from_wkt('GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]]') transform = Affine(1.0, 0.0, -0.5, 0.0, -1.0, 90.0)
metadata = { 'driver': driver, 'dtype': dtype, 'nodata': nodata, 'width': width, 'height': height, 'count': count, 'crs': crs, 'transform': transform }
band1 = np.array(buildArray(180), dtype=np.int16) band2 = np.array(buildArray(180), dtype=np.int16)
output_path = "sample.tiff" with rasterio.open(output_path, 'w', **metadata) as dst: dst.write(band1, 1) dst.write(band2, 2)
print(f"GeoTIFF file created: {output_path}")
|