Basic Examples

Simple examples to get you started with common tasks.

Finding Available Data

import open_geodata_api as ogapi

# Setup client
pc = ogapi.planetary_computer(auto_sign=True)

# List all collections
collections = pc.list_collections()
print(f"Available collections: {len(collections)}")

# Find Sentinel collections
sentinel_collections = [c for c in collections if 'sentinel' in c.lower()]
print(f"Sentinel collections: {sentinel_collections}")

Working with Search Results

# Convert to DataFrame for analysis
df = items.to_dataframe()

# Basic statistics
print(f"Date range: {df['datetime'].min()} to {df['datetime'].max()}")
print(f"Cloud cover: {df['eo:cloud_cover'].min():.1f}% to {df['eo:cloud_cover'].max():.1f}%")
print(f"Collections: {df['collection'].unique()}")

Getting Data URLs

# Get first item
item = items[0]

# Show available assets
item.print_assets_info()

# Get specific band URLs
rgb_urls = item.get_band_urls(['B04', 'B03', 'B02'])  # Red, Green, Blue
print(f"RGB URLs: {list(rgb_urls.keys())}")

# Get all asset URLs
all_urls = item.get_all_asset_urls()
print(f"Total assets: {len(all_urls)}")

Using URLs with Different Packages

# Get red band URL
red_url = item.get_asset_url('B04')

# Option 1: rioxarray
import rioxarray
data_xr = rioxarray.open_rasterio(red_url)
print(f"rioxarray shape: {data_xr.shape}")

# Option 2: rasterio
import rasterio
with rasterio.open(red_url) as src:
    data_rio = src.read(1)
    print(f"rasterio shape: {data_rio.shape}")

# Option 3: GDAL
from osgeo import gdal
dataset = gdal.Open(red_url)
band = dataset.GetRasterBand(1)
data_gdal = band.ReadAsArray()
print(f"GDAL shape: {data_gdal.shape}")