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

fix: static map plot using matplotlib for Image #311

Merged
merged 2 commits into from
Oct 8, 2024
Merged

Conversation

12rambau
Copy link
Member

@12rambau 12rambau commented Oct 7, 2024

related to #303

Earthengine has been designed with a big focus on the interactive mappng of the code editor. None the less, people still need to perform presentation and report where a reproducible static image is much more meaningful than a screenshot of the Google interface. This issue is still very real even when using geemap. To simplify the interface I propose in this PR a pot() method for the Image object.

Few technical choices:

  • I only support the object based implementation of matplotlib, i.e. you need to provide an ax. (can be modified in a follow-up PR)
  • I only support images and not imageCollection as I want to leave the user to filter and composite from GEE side
  • I only support named colormap for now (can be cahnged in a follow-up PR

demo:

Using the following code you can create a simple S2 image over vatican city:

import ee
import geetools

level0 = ee.FeatureCollection("FAO/GAUL/2015/level0")
vatican = level0.filter(ee.Filter.eq("ADM0_NAME", "Holy See"))

def mask_s2_clouds(image):
  qa = image.select('QA60')
  cloud_bit_mask, cirrus_bit_mask = 1 << 10, 1 << 11
  mask = qa.bitwiseAnd(cloud_bit_mask).eq(0).And(qa.bitwiseAnd(cirrus_bit_mask).eq(0))
  return image.updateMask(mask)

image = (
    ee.ImageCollection('COPERNICUS/S2_HARMONIZED')
    .filterDate('2022-06-01', '2022-06-30')
    .filterBounds(vatican)
    .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
    .map(mask_s2_clouds)
    .geetools.spectralIndices("NDVI")
    .mosaic()
)

Now to display this image with the overlay of the vatican shape you can do:

from matplotlib import pyplot as plt

fig, ax = plt.subplots(figsize=(10,10))
image.geetools.plot(
    bands = ["NDVI"], 
    ax=ax,
    region=vatican.geometry(), 
    crs="EPSG:3857", 
    scale=10, 
    fc=vatican, 
    cmap="viridis",
    color="k"
)

# as it's a figure you can then edit the information as you see fit
ax.set_title("NDVI in Vatican City")
ax.set_xlabel("x coordinates (m)")
ax.set_ylabel("y coordinates (m)")
fig.colorbar(ax.images[0], orientation='horizontal', label="NDVI")

and get this:

image

works as well with RGB band combination:

from matplotlib import pyplot as plt

fig, ax = plt.subplots(figsize=(10,10))
image.geetools.plot(
    bands = ["B4", "B3", "B2"], 
    ax=ax, 
    region=vatican.geometry(), 
    fc=vatican, 
    color="k"
)

# as it's a figure you can then edit the information as you see fit
ax.set_title("Sentinel 2 composite in Vatican City")
ax.set_xlabel("longitude")
ax.set_ylabel("latitude")

image

Copy link

codecov bot commented Oct 7, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 76.52%. Comparing base (820827c) to head (be2f523).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #311      +/-   ##
==========================================
+ Coverage   76.18%   76.52%   +0.34%     
==========================================
  Files          30       30              
  Lines        1579     1602      +23     
  Branches      193      196       +3     
==========================================
+ Hits         1203     1226      +23     
  Misses        356      356              
  Partials       20       20              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@12rambau 12rambau merged commit 44dd64d into main Oct 8, 2024
12 checks passed
@12rambau 12rambau deleted the plot-image branch October 8, 2024 18:41
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

Successfully merging this pull request may close these issues.

1 participant