Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
giswqs
GitHub Repository: giswqs/geemap
Path: blob/master/docs/notebooks/100_numpy_to_cog.ipynb
2313 views
Kernel: Python 3

image

Create a fresh conda env to run this example if needed.

conda create -n cog python=3.9 conda install mamba -c conda-forge mamba install geemap rio-cogeo -c conda-forge
# !pip install geemap rio-cogeo
import geemap
url = "https://github.com/giswqs/leafmap/raw/master/examples/data/cog.tif" in_cog = "cog.tif" out_cog = "ndvi.tif"

Download a sample dataset.

geemap.download_from_url(url, in_cog)

Convert image to numpy array.

arr = geemap.image_to_numpy(in_cog)
arr.shape

Computer NDVI.

ndvi = (arr[3] - arr[0]) / (arr[3] + arr[0])
ndvi.shape

Convert numpy array to COG.

geemap.numpy_to_cog(ndvi, out_cog, profile=in_cog)
m = geemap.Map() m.add_raster(in_cog, indexes=[4, 1, 2], layer_name="Color infrared") m.add_raster(out_cog, colormap="Greens", layer_name="NDVI") m