def rename_in_memory(self): # Create a fake 3-band image for testing arr = np.random.rand(3, 64, 64) file = NamedTemporaryFile(prefix="test", suffix=".tif").name with rasterio.open(file, "w", width=64, height=64, count=3, dtype=np.float32) as dst: dst.write(arr) r = Raster(file) in_memory = r.aggregate((32, 32), in_memory=True) renamed = r.rename(dict(zip(in_memory.names, ["Red", "Green", "Blue"]))) self.assertListEqual(list(renamed.names), ["Red", "Green", "Blue"])
import tempfile import rasterio # First, import the extract and predict functions: predictors = [nc.band1, nc.band2, nc.band3, nc.band4, nc.band5, nc.band7] # Create a RasterStack instance stack = Raster(predictors) stack.count # Perform band math ndvi = (stack.iloc[3] - stack.iloc[2]) / (stack.iloc[3] + stack.iloc[2]) ndvi = Raster(ndvi) ndvi.plot() tmp = ndvi.aggregate(out_shape=(50, 50)) tmp.plot() # Perform OR operation (union two layers) (stack.iloc[5] | stack.iloc[0]).plot() plt.show() # Perform AND operation (intersect two layers) (stack.iloc[5] & stack.iloc[0]).plot() plt.show() # Perform XOR operation (symmetrical difference) (stack.iloc[5] ^ stack.iloc[0]).plot() plt.show() import math
band4 = os.path.join(basedir, 'pyspatialml', 'tests', 'lsat7_2000_40.tif') band5 = os.path.join(basedir, 'pyspatialml', 'tests', 'lsat7_2000_50.tif') band7 = os.path.join(basedir, 'pyspatialml', 'tests', 'lsat7_2000_70.tif') multiband = os.path.join(basedir, 'pyspatialml', 'tests', 'landsat_multiband.tif') predictors = [band1, band2, band3, band4, band5, band7] # Create a RasterStack instance stack = Raster([band1, band2, band3, band4, band5, band7]) stack.count stack_rs = stack.to_crs(crs={'init': 'EPSG:4326'}, progress=False) stack_rs.plot() # Aggregate a raster to a coarser cell size stack_new = stack.aggregate(out_shape=(100, 100)) stack_new.iloc[0].plot() # Plot a RasterLayer stack.lsat7_2000_10.plot() # Plot a Raster stack.lsat7_2000_10.cmap = 'plasma' stack.plot(label_fontsize=8, title_fontsize=8) # Iterate through RasterLayers for name, layer in stack: print(layer) # RasterLayer indexing which returns a RasterLayer stack.iloc[0] # index