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

Add get_image_collection_video() function #225

Closed
ErikSeras opened this issue Dec 16, 2020 · 4 comments
Closed

Add get_image_collection_video() function #225

ErikSeras opened this issue Dec 16, 2020 · 4 comments
Labels
Feature Request New feature or request

Comments

@ErikSeras
Copy link
Contributor

ErikSeras commented Dec 16, 2020

  • geemap version: 0.8.5
  • Python version: 3.6.9
  • Operating System: Google Colab

Description

I would like geemap to have a function to download images made with cartoee.

This is my attempt, in which a video can also be generated using OpenCV.

What I Did

import ee
import geemap
import cv2
import matplotlib.pyplot as plt
import os
from datetime import datetime
from geemap import cartoee

try:
    ee.Initialize()
except Exception as e:
    ee.Authenticate()
    ee.Initialize()

def get_image_collection_video(
    ee_ic,
    out_dir,
    vis_params,
    show_region,
    grid_interval,
    plot_title,
    date_format, 
    fig_size,
    dpi_plot,
    format,
    color_north_arrow,
    scale_bar_length,
    scale_bar_color,
    scale_bar_unit,
    video_filename,
    fps,
):
    if not os.path.exists(out_dir):
        os.makedirs(out_dir)

    out_dir = os.path.join(os.getcwd(), out_dir)

    count = int(ee_ic.size().getInfo())
    names = ee_ic.aggregate_array('system:index').getInfo()
    images = ee_ic.toList(count)

    # list of file name
    img_list = []

    for i in range(0, count):
        image = ee.Image(images.get(i))
        name = str(names[i])
        ext = os.path.splitext(name)[0][1:]
        name = name + "." + "png"
        out_img = os.path.join(out_dir, name)
        img_list.append(out_img)

        # Size plot
        plt.figure(figsize = fig_size)

        # Plot image
        ax = cartoee.get_map(image, region=show_region, vis_params = vis_params)

        # Add grid
        cartoee.add_gridlines(ax, interval = grid_interval, linestyle=":")

        # Add title
        ax.set_title(
            label = plot_title + " " + image.date().format(date_format).getInfo() +  " UTC"+ "\n",
            fontsize = 15
        )

        # Add scale bar
        cartoee.add_scale_bar(ax, scale_bar_length, xy=(0.9, 0.02), linewidth=3, color = scale_bar_color, unit = scale_bar_unit, fontsize = 16)
        # Add north arrow
        cartoee.add_north_arrow(ax, 'N', xy=(0.9, 0.18), arrow_length=0.1, text_color= color_north_arrow, arrow_color = color_north_arrow)

        # Save plot
        plt.savefig(fname = out_img, dpi = dpi_plot)

       
        plt.clf()
        plt.close()
    
    
    # Video file name
    output_video_file_name = os.path.join(os.getcwd(), out_dir, video_filename)

    frame = cv2.imread(img_list[0])
    height, width, channels = frame.shape
    frame_size = (width,height)
    fps_video = fps

    # Make mp4
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')

    # Function
    def convert_frames_to_video(
        input_list,
        output_video_file_name,
        fps_video,
        frame_size
    ):   
        out = cv2.VideoWriter(output_video_file_name, fourcc, fps_video, frame_size)
        num_frames = len(input_list)

        for i in range(num_frames):
            img_path = input_list[i]
            img = cv2.imread(img_path)
            out.write(img)
        
        out.release()
        cv2.destroyAllWindows()

    # Use function  
    convert_frames_to_video(
        input_list = img_list,
        output_video_file_name = output_video_file_name,
        fps_video= fps_video,
        frame_size = frame_size
    )

# Example
l8 = ee.ImageCollection("LANDSAT/LC08/C01/T1_SR").filterBounds(point) \
    .filterDate("2015-01-01", "2018-01-01")

visParams = {
  "bands": ['B4', 'B3', 'B2'],
  "min": 0,
  "max": 3000
}

region = [-71,-13.5,-70,-12.5]

get_image_collection_video(
    ee_ic = l8,
    out_dir = "example",
    vis_params = visParams,
    show_region = region,
    grid_interval = (0.2, 0.2),
    plot_title = "Example plot",
    date_format = 'YYYY-MM-dd',
    fig_size = (10.8, 10.8),
    dpi_plot = 100,
    format = "png",
    color_north_arrow = "white",
    scale_bar_length = 10,
    scale_bar_color = "white",
    scale_bar_unit = "km",
    video_filename = "example.mp4",
    fps = 10
)

@giswqs giswqs added the Feature Request New feature or request label Dec 16, 2020
@giswqs
Copy link
Member

giswqs commented Dec 16, 2020

@ErikSeras Good work! This function can potentially be added to the cartoee module. The function can be improved with docstrings. Look at this example for documenting functions if you have not done this before. Would you like to submit a pull request so that you can get credits for this and be listed as one of the contributors?

@ErikSeras
Copy link
Contributor Author

Thanks @giswqs. I have never tried to make a pull request for a Python package. But I'll try.

@giswqs
Copy link
Member

giswqs commented Dec 16, 2020

Take a look at the pull request instructions here.

For adding docstrings, you can use the VSCode Python Docstring Generator. Docstrings will be automatically extracted when building the API documentation. See https://geemap.org/cartoee/

giswqs added a commit that referenced this issue Dec 18, 2020
@giswqs
Copy link
Member

giswqs commented Dec 18, 2020

@ErikSeras I have improved the function and renamed it as get_image_collection_gif(). In this way, the opencv-python package is optional. Other parameters are optional as well, such as plot title, girdline, north arrow, scale bar, etc. See this notebook example. Thank you for your contribution!

@giswqs giswqs closed this as completed Dec 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants