1

I am exporting a single image and a collection of Landsat raw images from Google Earth Engine using Python, but all my images are in black and white. Should they be like that? Isn't there a way of getting coloured Landsat images. I am very new. Below is my code:

# Import libraries.
import ee
import webbrowser

# Trigger the authentication flow.
# ee.Authenticate()

# Initialize the library.
ee.Initialize()

# Access a collection
collection = 'LANDSAT/LC08/C01/T1' #Landsat 8 Collection

#Landsat_composite in Nithi area
area_nithi = list([(37.288, 0.172), (38.333, 0.172), (38.333, -0.568), (37.288, -0.568), (37.288, 0.172)])
area_nithi = ee.Geometry.Polygon(area_nithi)
time_range_nithi = ['2017-01-01', '2017-12-31']

collection_nithi = ('LANDSAT/LC08/C01/T1')
print(type(area_nithi))

#Methods from climada.util.earth_engine module
def obtain_image_landsat_composite(collection, time_range, area):
    """ Selection of Landsat cloud-free composites in the Earth Engine library
    See also: https://developers.google.com/earth-engine/landsat

    Parameters:
        collection (): name of the collection
        time_range (['YYYY-MT-DY','YYYY-MT-DY']): must be inside the available data
        area (ee.geometry.Geometry): area of interest

    Returns:
        image_composite (ee.image.Image)
     """
    collection = ee.ImageCollection(collection)

    ## Filter by time range and location
    collection_time = collection.filterDate(time_range[0], time_range[1])
    image_area = collection_time.filterBounds(area)
    image_composite = ee.Algorithms.Landsat.simpleComposite(image_area, 75, 3)
    return image_composite

#Application to examples
composite_nithi = obtain_image_landsat_composite(collection_nithi, time_range_nithi, area_nithi)


#Selection of specific bands from an image
nithi_band = composite_nithi.select(['B4', 'B3', 'B2'])

nithi_bandVis = {
  min: 0.0,
  max: 30000.0,
}
print(composite_nithi.getInfo())

print(type(nithi_band))

print(type(nithi_bandVis))

def get_region(geom):
    """Get the region of a given geometry, needed for exporting tasks.

    Parameters:
        geom (ee.Geometry, ee.Feature, ee.Image): region of interest

    Returns:
        region (list)
    """
    if isinstance(geom, ee.Geometry):
        region = geom.getInfo()["coordinates"]
    elif isinstance(geom, ee.Feature, ee.Image):
        region = geom.geometry().getInfo()["coordinates"]
    elif isinstance(geom, list):
        condition = all([isinstance(item) == list for item in geom])
        if condition:
            region = geom
    return region


region_nithi = get_region(area_nithi)

def get_url(name, image, imageVis, scale, region):
    """It will open and download automatically a zip folder containing Geotiff data of 'image'.
    If additional parameters are needed, see also:
    https://github.com/google/earthengine-api/blob/master/python/ee/image.py

    Parameters:
        name (str): name of the created folder
        image (ee.image.Image): image to export
        scale (int): resolution of export in meters (e.g: 30 for Landsat)
        region (list): region of interest

    Returns:
        path (str)
     """
    path = image.getDownloadURL({
        'name':(name),
        'scale': scale,
        'region':(region)
        })

    webbrowser.open_new_tab(path)
    return path



#url_nithi = get_url('nithi', median_swiss, 900, region_swiss)
url_nithi = get_url('nithi0.1', composite_nithi, 900, region_nithi)
#url_landcover = get_url('landcover_swiss', landCover_layer, 100, region_swiss)

#For the example of nithi, due to size, it doesn't work on Jupyter Notebook but it works on Python
url_nithi2 = get_url('nithi0.2', nithi_band, nithi_bandVis, 1000, region_nithi)


print(url_nithi)
print(url_nithi2)
0

There are a couple of problems with your code.

  • getDownloadURL() expects region to be an ee.Geometry, you provided a list
  • The first invocation of get_url() doesn't specify the image

The resulting image is a three band image. Not black and white in other words. If you're seeing a black and white image, you're probably only visualizing one of the three bands. That's not an Earth Engine related issue.

def get_url(name, image, imageVis, scale, region):
    path = image.getDownloadURL({
        'name': name,
        'scale': scale,
        'region': region,        
    })

    webbrowser.open_new_tab(path)
    return path

url_nithi = get_url('nithi0.1', nithi_band, composite_nithi, 900, area_nithi)
url_nithi2 = get_url('nithi0.2', nithi_band, nithi_bandVis, 1000, area_nithi)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.