47 numpy to cog
In [1]:
Copied!
# !pip install leafmap
# !pip install leafmap
In [2]:
Copied!
# !pip install rio-cogeo
# !pip install rio-cogeo
In [3]:
Copied!
import leafmap
import leafmap
In [4]:
Copied!
url = "https://github.com/opengeos/leafmap/raw/master/examples/data/cog.tif"
in_cog = "cog.tif"
out_cog = "ndvi.tif"
url = "https://github.com/opengeos/leafmap/raw/master/examples/data/cog.tif"
in_cog = "cog.tif"
out_cog = "ndvi.tif"
Download a sample dataset.
In [5]:
Copied!
leafmap.download_from_url(url, in_cog)
leafmap.download_from_url(url, in_cog)
Downloading https://github.com/opengeos/leafmap/raw/master/examples/data/cog.tif ...
Data downloaded to: /home/runner/work/leafmap/leafmap/docs/notebooks/cog.tif
Convert image to numpy array.
In [6]:
Copied!
arr = leafmap.image_to_numpy(in_cog)
arr = leafmap.image_to_numpy(in_cog)
In [7]:
Copied!
arr.shape
arr.shape
Out[7]:
(4, 206, 343)
Computer NDVI.
In [8]:
Copied!
ndvi = (arr[3] - arr[0]) / (arr[3] + arr[0])
ndvi = (arr[3] - arr[0]) / (arr[3] + arr[0])
In [9]:
Copied!
ndvi.shape
ndvi.shape
Out[9]:
(206, 343)
Convert numpy array to COG.
In [10]:
Copied!
leafmap.numpy_to_cog(ndvi, out_cog, profile=in_cog)
leafmap.numpy_to_cog(ndvi, out_cog, profile=in_cog)
In [11]:
Copied!
m = leafmap.Map()
m.add_raster(in_cog, band=[4, 1, 2], layer_name="Color infrared")
m.add_raster(out_cog, palette="Greens", layer_name="NDVI")
m
m = leafmap.Map()
m.add_raster(in_cog, band=[4, 1, 2], layer_name="Color infrared")
m.add_raster(out_cog, palette="Greens", layer_name="NDVI")
m
Out[11]: