100 numpy to cog
In [1]:
Copied!
# !pip install geemap rio-cogeo
# !pip install geemap rio-cogeo
In [2]:
Copied!
import geemap
import geemap
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!
geemap.download_from_url(url, in_cog)
geemap.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/geemap/cog.tif
Convert image to numpy array.
In [5]:
Copied!
arr = geemap.image_to_numpy(in_cog)
arr = geemap.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!
geemap.numpy_to_cog(ndvi, out_cog, profile=in_cog)
geemap.numpy_to_cog(ndvi, out_cog, profile=in_cog)
In [10]:
Copied!
m = geemap.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 = geemap.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/geemap/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/geemap/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/geemap/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/geemap/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