47 numpy to cog
In [1]:
Copied!
# !pip install leafmap rio-cogeo
# !pip install leafmap rio-cogeo
In [2]:
Copied!
import leafmap
import leafmap
In [3]:
Copied!
url = 'https://github.com/giswqs/leafmap/raw/master/examples/data/cog.tif'
in_cog = 'cog.tif'
out_cog = "ndvi.tif"
url = 'https://github.com/giswqs/leafmap/raw/master/examples/data/cog.tif'
in_cog = 'cog.tif'
out_cog = "ndvi.tif"
Download a sample dataset.
In [4]:
Copied!
leafmap.download_from_url(url, in_cog)
leafmap.download_from_url(url, in_cog)
Downloading https://github.com/giswqs/leafmap/raw/master/examples/data/cog.tif ... Data downloaded to: /home/runner/work/geospatial-notebooks/geospatial-notebooks/docs/leafmap/cog.tif
Convert image to numpy array.
In [5]:
Copied!
arr = leafmap.image_to_numpy(in_cog)
arr = leafmap.image_to_numpy(in_cog)
In [6]:
Copied!
arr.shape
arr.shape
Out[6]:
(4, 206, 343)
Computer NDVI.
In [7]:
Copied!
ndvi = (arr[3] - arr[0]) / (arr[3] + arr[0])
ndvi = (arr[3] - arr[0]) / (arr[3] + arr[0])
In [8]:
Copied!
ndvi.shape
ndvi.shape
Out[8]:
(206, 343)
Convert numpy array to COG.
In [9]:
Copied!
leafmap.numpy_to_cog(ndvi, out_cog, profile=in_cog)
leafmap.numpy_to_cog(ndvi, out_cog, profile=in_cog)
In [10]:
Copied!
m = leafmap.Map()
m.add_geotiff(in_cog, band=[4, 1, 2], layer_name="Color infrared")
m.add_geotiff(out_cog, palette="Greens", layer_name="NDVI")
m
m = leafmap.Map()
m.add_geotiff(in_cog, band=[4, 1, 2], layer_name="Color infrared")
m.add_geotiff(out_cog, palette="Greens", layer_name="NDVI")
m
Warning 1: /home/runner/work/geospatial-notebooks/geospatial-notebooks/docs/leafmap/cog.tif: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples. Warning 1: /home/runner/work/geospatial-notebooks/geospatial-notebooks/docs/leafmap/cog.tif: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples. Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples. Warning 1: /home/runner/work/geospatial-notebooks/geospatial-notebooks/docs/leafmap/cog.tif: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.
Out[10]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Last update:
2022-03-14