5 Python Libraries for Earth Observation
Earth observation has become a cornerstone of the GIS industry, driven by the exponential growth in satellite missions and their diverse applications. From monitoring environmental changes to supporting precision agriculture, the ability to access, process, and visualize this data is more critical than ever. Python, with its versatile ecosystem of libraries, empowers users to unlock the full potential of Earth observation data, making it accessible and actionable for a wide range of industries.
Checkout Video at - https://www.youtube.com/watch?v=DsapQDZtkY4
rioxarray
rioxarray is a powerful Python library that extends the capabilities of xarray by adding geospatial support for working with raster datasets. It is especially useful for Earth Observation (EO) workflows where spatial metadata and coordinate reference systems (CRS) are crucial.
Purpose:
Handling raster data with geospatial metadata (e.g., CRS, bounds, resolution). Simplifying raster operations by combining geospatial features of Rasterio with the multi-dimensional array handling of xarray.
Key Features:
import rioxarray
# Open a GeoTIFF file
raster = rioxarray.open_rasterio("example.tif")
print(raster)
# Reproject raster to a new CRS
reprojected_raster = raster.rio.reproject("EPSG:4326")
# Crop raster to bounding box
cropped_raster = raster.rio.clip_box(minx, miny, maxx, maxy)
# Save cropped raster to a new file
cropped_raster.rio.to_raster("cropped_output.tif")
pytstac-client
pystac-client is a Python library designed for interacting with STAC (SpatioTemporal Asset Catalog) APIs. It is particularly useful for Earth Observation (EO) workflows where managing and querying large catalogs of geospatial assets (e.g., satellite imagery, climate data) is essential.
Purpose:
Key Features:
Example Workflow:
from pystac_client import Client
# Connect to a public STAC API
stac_api_url = "https://planetarycomputer.microsoft.com/api/stac/v1"
catalog = Client.open(stac_api_url)
print(catalog.description)
# Define search parameters
search = catalog.search(
collections=["sentinel-2-l2a"], # Collection name
bbox=[-120.0, 35.0, -119.0, 36.0], # Bounding box
datetime="2023-01-01/2023-01-31", # Temporal range
limit=10 # Number of results
)
# Retrieve items
items = list(search.get_items())
print(f"Found {len(items)} items.")
# Access metadata for the first item
first_item = items[0]
print(first_item.properties)
# Access an asset (e.g., a Cloud-Optimized GeoTIFF)
asset_href = first_item.assets["B04"].href
print("Asset URL:", asset_href)
import rasterio
# Open and process the asset
with rasterio.open(asset_href) as dataset:
data = dataset.read(1) # Read the first band
print(data.shape)
Earth Engine Python API
The Earth Engine Python API is a Python library for accessing Google Earth Engine (GEE), a cloud-based platform for planetary-scale geospatial analysis. It provides powerful tools for working with satellite imagery, vector data, and geospatial datasets directly in Python.
Purpose:
Key Features:
Recommended by LinkedIn
Example Workflow:
import ee
# Initialize the Earth Engine API
ee.Initialize()
# Print some information to confirm
print("Earth Engine initialized successfully!")
# Load a Landsat 8 collection
landsat = ee.ImageCollection("LANDSAT/LC08/C01/T1_SR").filterDate("2023-01-01", "2023-12-31")
# Define a region of interest (ROI) and filter the dataset
roi = ee.Geometry.Rectangle([-120.0, 35.0, -119.0, 36.0])
filtered = landsat.filterBounds(roi)
# Compute NDVI for the filtered images
def compute_ndvi(image):
return image.normalizedDifference(["B5", "B4"]).rename("NDVI")
ndvi_collection = filtered.map(compute_ndvi)
# Export the first image to Google Drive
ndvi_image = ndvi_collection.first()
task = ee.batch.Export.image.toDrive(
image=ndvi_image,
description="NDVI_Image",
region=roi,
scale=30
)
task.start()
odc-stac
odc-stac is a Python library for integrating Open Data Cube (ODC) workflows with SpatioTemporal Asset Catalogs (STAC). It enables users to load and manipulate geospatial data from STAC-compliant APIs in a way that is compatible with ODC, providing a seamless bridge between modern geospatial data sources and powerful analysis pipelines.
Purpose:
Key Features:
Example Workflow:
from odc.stac import stac_load
import pystac_client
# Connect to a STAC API
stac_api_url = "https://planetarycomputer.microsoft.com/api/stac/v1"
catalog = pystac_client.Client.open(stac_api_url)
# Search for Sentinel-2 data
search = catalog.search(
collections=["sentinel-2-l2a"],
bbox=[-120.0, 35.0, -119.0, 36.0],
datetime="2023-01-01/2023-01-31",
limit=10,
)
# Get the STAC items
items = list(search.get_items())
# Load STAC data into an xarray dataset
ds = stac_load(
items,
bands=["B04", "B08"], # Load red and NIR bands
crs="EPSG:4326",
resolution=(-0.0001, 0.0001), # Approx. 10m resolution
)
print(ds)
# Compute NDVI
ndvi = (ds["B08"] - ds["B04"]) / (ds["B08"] + ds["B04"])
ndvi.plot()
# Export the NDVI dataset to a GeoTIFF
ndvi.rio.to_raster("ndvi_output.tif")
titiler
titiler is a modern dynamic tile server built on FastAPI and Rasterio. It specializes in serving Cloud-Optimized GeoTIFFs (COGs) and other raster data as map tiles and web services. titiler allows users to interact with raster data dynamically, making it ideal for web mapping and visualization workflows.
Purpose:
Key Features:
Example Workflow:
1. Installing titiler:
pip install titiler
2. Serving a COG as Map Tiles
from titiler.application import app
from fastapi import FastAPI
# Create a FastAPI app
my_app = FastAPI()
# Mount titiler's application
my_app.mount("/", app)
Run the application:
uvicorn my_app:app --host 0.0.0.0 --port 8000
Access tiles at: `http://localhost:8000/cog/tilejson.json?url=<URL_to_COG>`
good
Perfect content...please does this code work in arcpy...as seen hear