An R package for interfacing with Google Earth Engine (GEE). rpygee provides a collection of functions to export imagery, feature collections, and time-series data from GEE, and includes tools for creating maps and plots.
Installation
You can install the development version of rpygee from GitHub with:
# install.packages("devtools")
devtools::install_github("pulakeshpradhan/rpygee")Prerequisites
This package requires a working Google Earth Engine setup with Python. Before using the package, please ensure you have:
- A Google Earth Engine account.
- A Python environment with the
earthengine-apiandgeemaplibraries installed. - Configured the
reticulatepackage in R to point to your Python environment if it’s not found automatically.
Example Usage
First, load the package and initialize your GEE connection with your Google Cloud Project ID.
# Load your library and the 'terra' library for mosaicking
library(rpygee)
library(terra)
library(reticulate)
# Initialize GEE with specific Python environment and project ID
py_env_path <- "C:/Users/pulak/anaconda3/envs/maps/python.exe"
activate_rpygee(project_id = "spatialgeography", python_path = py_env_path)
# --- PART 1: DOWNLOAD TILES ---
# Define the image you want to download (e.g., SRTM Elevation)
srtm_image <- rgee::ee$Image('USGS/SRTMGL1_003')
# Define your Area of Interest using the Delhi asset
# We use .geometry() to get the shape from the FeatureCollection
delhi_fc <- rgee::ee$FeatureCollection('projects/spatialgeography/assets/delhi')
delhi_aoi <- delhi_fc$geometry()
# Define the output folder for the tiles
output_folder <- "delhi_data"
# Call your function to download the image as 2x2 tiles
cat("Starting tile download...\n")
export_local_tile(
image = srtm_image,
region = delhi_aoi,
scale = 30,
name = "srtm_delhi_tile_", # Prefix for each tile file
rows = 2,
cols = 2,
folder = output_folder
)
cat("\nTile download initiated. Check console for progress from geemap.\n")
# --- PART 2: MOSAIC THE DOWNLOADED TILES ---
cat("\nStarting mosaicking process...\n")
# Find all the .tif files that were just downloaded
tile_files <- list.files(output_folder, pattern = "\\.tif$", full.names = TRUE)
# Check if tiles were found before proceeding
if (length(tile_files) > 0) {
cat(paste("Found", length(tile_files), "tiles. Mosaicking now...\n"))
# Load all the individual tile rasters into a list
list_of_rasters <- lapply(tile_files, terra::rast)
# Combine the list of rasters into a single raster mosaic
mosaic_raster <- do.call(terra::mosaic, list_of_rasters)
# Define the path for the final, combined image
mosaic_output_path <- file.path(output_folder, "delhi_mosaic.tif")
# Save the final mosaicked raster
terra::writeRaster(mosaic_raster, filename = mosaic_output_path, overwrite = TRUE)
cat(paste("\n---------------------------------------------------\n"))
cat(paste("Success! Final mosaicked image saved to:\n", mosaic_output_path, "\n"))
cat(paste("---------------------------------------------------\n"))
# Optional: Plot the final result
plot(mosaic_raster)
} else {
cat("\nError: No tile files were found in the output folder. The download may have failed.\n")
}