Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

geemap.ee_to_numpy() Does not work #168

Closed
PERCY-ESCOBAR opened this issue Nov 6, 2020 · 12 comments
Closed

geemap.ee_to_numpy() Does not work #168

PERCY-ESCOBAR opened this issue Nov 6, 2020 · 12 comments

Comments

@PERCY-ESCOBAR
Copy link

PERCY-ESCOBAR commented Nov 6, 2020

  • geemap version: 0.8.1
  • Python version: 3.8
  • Operating System: windows 10

Description

Dear profesor Qiusheng Wu. I am trying to extract a cloud masked image as numpy array but it does not work. The output message is the following:

<<Image.sampleRectangle: Fully masked pixels / pixels outside of the image footprint when sampling band 'B4' with no default value set. Note that calling sampleRectangle() on an image after ee.Image.clip() may result in a sampling bounding box outside the geometry passed to clip().>>

GEE cloud masked image to numpy array error

Thank you very much for your hep.

What I Did

Paste the command(s) you ran and the output.
If there was a crash, please include the traceback here.
@giswqs
Copy link
Member

giswqs commented Nov 6, 2020

You probably need to set the default_value parameter for the ee_to_numpy() function, e.g., default_value=0

https://geemap.org/common/#geemap.common.ee_to_numpy

@PERCY-ESCOBAR
Copy link
Author

Thank you for your answer. It works by setting the default_value = 0. However, the output region is a rectangular area not the limited area. Is there the way how to get only the limited region??

Thanks again for your time.

region_output

@giswqs
Copy link
Member

giswqs commented Nov 6, 2020

Did you try masking out the area of the image outside your ROI before using the ee_to_numpy() function?

@PERCY-ESCOBAR
Copy link
Author

Yes. I did. The same output happens when I try to export as Geo TIFF, the region exported is a rectangular area not the ROI limited which is an irregular polygon.

Thank you!
Exported_irregular_roi

@giswqs
Copy link
Member

giswqs commented Nov 6, 2020

Display your l8sr_coll_CloudMasked.first() on the Map. If it is not masked to your ROI, the output image might not be masked.

@PERCY-ESCOBAR
Copy link
Author

This is the l8sr_coll_CloudMasked.first() displayed on the Map.
l8_masked_first

@giswqs
Copy link
Member

giswqs commented Nov 6, 2020

I am very confused. All three images (plt, qigs, geemap) are all different. Not sure what you are trying to get.

@PERCY-ESCOBAR
Copy link
Author

Yeah. I have been trying to get a masked landsat image clipping by irregular polygon ROI. The exported images is always rectangular area as it shows on QGIS and plt.. Here I display my ROI on the Map.

Thank you!
ROI

@giswqs
Copy link
Member

giswqs commented Nov 6, 2020

Please share your source code for debugging.

@PERCY-ESCOBAR
Copy link
Author

PERCY-ESCOBAR commented Nov 7, 2020

import ee
import os
import geemap

Map = geemap.Map()
Map

# roi
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]])

# Cloud, snow, and shallow mask function
def maskL8sr(image):
  # Bits 3 and 5 are cloud shadow and cloud, respectively.
  cloudShadowBitMask = 1 << 3
  cloudsBitMask = 1 << 5

  # Get the pixel QA band.
  qa = image.select('pixel_qa')

  # Both flags should be set to zero, indicating clear conditions.
  mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0) \
      .And(qa.bitwiseAnd(cloudsBitMask).eq(0))

  # Return the masked image, scaled to reflectance, without the QA bands.
  return image.updateMask(mask).divide(10000) \
      .select("B[0-9]*") \
      .copyProperties(image, ["system:time_start"])

# Landsat 8 image collection
l8collection = ee.ImageCollection("LANDSAT/LC08/C01/T1_SR")\
.filterBounds(tambopata_roi)\

print('# Landsat 8 Images: ',l8collection.size().getInfo())

l8_vis = {
    'bands': ['B4','B3','B2'],
    'min': 0,
    'max': 4000
}

# Masked image collection
l8sr_coll_CloudMasked = l8collection.map(maskL8sr)
l8sr_coll_clipped = l8sr_coll_CloudMasked.map(lambda image: image.clip(tambopata_roi))

Map.addLayer(l8sr_coll_CloudMasked.first(), l8_vis, 'L8 collection')
Map.addLayer(l8sr_coll_clipped.first(), l8_vis, 'L8 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, 'L8_Image.tif')

geemap.ee_export_image(l8sr_coll_clipped.first(), filename=filename, scale=30,
                      region=tambopata_roi, file_per_band=True)

import numpy as np
l8_array = geemap.ee_to_numpy(l8sr_coll_CloudMasked.first(), bands=['B4','B5'], region=tambopata_roi, default_value=0)

# Plotting the array
import matplotlib.pyplot as plt

plt.imshow(l8_array[:,:,0])

@giswqs
Copy link
Member

giswqs commented Nov 7, 2020

import numpy as np
l8_array = geemap.ee_to_numpy(l8sr_coll_clipped.first(), bands=['B4','B5'], region=tambopata_roi, default_value=0)

# Plotting the array
import matplotlib.pyplot as plt

plt.imshow(l8_array[:,:,0])

@giswqs giswqs closed this as completed Nov 7, 2020
@PERCY-ESCOBAR
Copy link
Author

Dear Professor Qiusheng Wu, I was not aware of that. I appreciate your help so much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants