gee-community / geemap

A Python package for interactive geospatial analysis and visualization with Google Earth Engine.
https://geemap.org
MIT License
3.47k stars 1.09k forks source link

Exporting Landsat 5 as GeoTIFF #169

Closed PERCY-ESCOBAR closed 4 years ago

PERCY-ESCOBAR commented 4 years ago

Description

Dear professor Qiusheng Wu, I have a problem trying to export landsat 5 masked image as GeoTiff. The output image is a rectangular area not the clipped image by ROI. Thank you for your help!

What I Did

import ee
import os
import geemap

Map = geemap.Map()
Map

tambopata_roi = ee.Geometry.Polygon([[-69.152775, -12.596743],
    [-69.145598, -12.599121],
    [-69.138388, -12.600361],
    [-69.134336, -12.600964],
    [-69.119736, -12.601643],
    [-69.11153, -12.602179],
    [-69.100023, -12.603385],
    [-69.092846, -12.604022],
    [-69.084708, -12.603016],
    [-69.078493, -12.600369],
    [-69.071955, -12.59593],
    [-69.068762, -12.590502],
    [-69.06732, -12.584236],
    [-69.062925, -12.58494],
    [-69.064882, -12.590938],
    [-69.067801, -12.594757],
    [-69.071337, -12.598912],
    [-69.076204, -12.603042],
    [-69.082453, -12.605487],
    [-69.090581, -12.607586],
    [-69.09853, -12.607485],
    [-69.108651, -12.606074],
    [-69.122635, -12.60439],
    [-69.132487, -12.603423],
    [-69.139853, -12.603289],
    [-69.154267, -12.600152],
    [-69.152775, -12.596743]])

l5_vis = {'bands':['B3','B2','B1'], 'min': 0, 'max': 5000}

# This example demonstrates the use of the Landsat 4, 5 or 7
# surface reflectance QA band to mask clouds.

# cloudMaskL457 = function(image) {
def cloudMaskL457(image):
  qa = image.select('pixel_qa')
  # If the cloud bit (5) is set and the cloud confidence (7) is high
  # or the cloud shadow bit is set (3), then it's a bad pixel.
  cloud = qa.bitwiseAnd(1 << 5) \
          .And(qa.bitwiseAnd(1 << 7)) \
          .Or(qa.bitwiseAnd(1 << 3))
  # Remove edge pixels that don't occur in all bands
  mask2 = image.mask().reduce(ee.Reducer.min())
  return image.updateMask(cloud.Not()).updateMask(mask2)
# }

# Landsat 5 image collection
l5collection = ee.ImageCollection("LANDSAT/LT05/C01/T1_SR")\
.filterBounds(tambopata_roi)\
.map(cloudMaskL457)\
.map(lambda image: image.clip(tambopata_roi))\

Map.addLayer(l5collection.first(), l5_vis, 'L5 clipped')
Map.addLayer(tambopata_roi, {}, 'ROI')
Map.centerObject(tambopata_roi, zoom=13)

out_dir = os.path.join(os.path.expanduser('~'), 'Downloads')
filename = os.path.join(out_dir, 'L5_Image.tif')

geemap.ee_export_image(l5collection.first().select(['B3','B4']),filename=filename, scale=30, region=tambopata_roi,
                       file_per_band=False)
PERCY-ESCOBAR commented 4 years ago

The exported image displayed on QGIS is the following:

exported_L5_image

giswqs commented 4 years ago
geemap.ee_export_image(l5collection.first().select(['B3','B4']).selfMask(),filename=filename, scale=30, region=tambopata_roi,
                       file_per_band=False)