# !pip install -U geemap
import ee
import geemap
Use a Landsat image covering San Francisco Bay Area.
m = geemap.Map()
image = ee.Image("LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318").select(
['B5', 'B4', 'B3']
)
vis_params = {
'min': 0.0,
'max': 0.4,
}
m.add_layer(image, vis_params, 'False Color (543)')
m.centerObject(image)
m
Retrieve the projection information from the image.
projection = image.select(0).projection().getInfo()
crs = projection['crs']
crs
Read the Earth Engine image into an Xarray DataArray.
ds = geemap.ee_to_xarray(
image,
crs="EPSG:32610",
scale=300,
geometry=image.geometry(),
ee_mask_value=0
)
ds
Save the DataArray as a Cloud Optimized GeoTIFF.
geemap.xee_to_image(ds, filenames=['landsat.tif'])
Calculate the NDVI and save it to the DataArray.
ndvi = (ds['B5'] - ds['B4']) / (ds['B5'] + ds['B4'])
Create an in-memory raster dataset from the DataArray.
ndvi_image = geemap.array_to_image(ndvi, source="landsat.tif")
Visualize the in-memory raster dataset.
m.add_raster('landsat.tif', indexes=[1, 2, 3], nodata=-1, layer_name="Landsat 7")
m.add_raster(ndvi_image, colormap="Greens", layer_name="NDVI")
m
Save the in-memory raster dataset to a GeoTIFF file.
geemap.array_to_image(ndvi, output="ndvi.tif", source="landsat.tif")