-
Notifications
You must be signed in to change notification settings - Fork 1
Home
Hier finden sich alle Handouts der ersten Termine sowie angefertigte Mitschriften (Notizen, Dokus für Code,...). Der Sinn ist alle Erkenntnisse zentral zu sammeln und übersichtlich allen Mitwirkenden bereitzustellen.
stac client for python:
import json # Zur Anzeige der Abfrageergebnisse import pystac import requests # Für Interaktion mit APIs
from pystac import Catalog, get_stac_version # Erweiterung von pystac zum Einbinden von bestehenden Catalogs from pystac_client import Client # Erweiterung von pystac u.a. zum suchen in STACs
root_catalog = Catalog.from_file('https://raw.githubusercontent.com/stac-utils/pystac/main/docs/example-catalog/catalog.json') root_catalog.describe() # Aufbau des Catalogs
print(f"ID: {root_catalog.id}") print(f"Title: {root_catalog.title or 'N/A'}") print(f"Description: {root_catalog.description or 'N/A'}")
collections = list(root_catalog.get_collections()) # get_collections() und weitere Func. im Handout erläutert print(f"Number of collections: {len(collections)}") # Anzahl der vorhandenen Collections print("Collections IDs:") for collection in collections: print(f"- {collection.id}")
items = list(root_catalog.get_all_items()) print(f"Number of items: {len(items)}") for item in items: print(f"- {item.id}")
item = root_catalog.get_item("LC80140332018166LGN00", recursive=True) # Einzelenes Item, weitere Benutzung im Folgenden
print(item.geometry) print(item.bbox) print(item.datetime) print(item.collection_id) item.get_collection() # Abfrage, zu welcher Collection das item gehört
print(item.common_metadata.instruments) print(item.common_metadata.platform) print(item.common_metadata.gsd)
for asset_key in item.assets: #.assets als Func zur Abfrage aller Assets eines Items asset = item.assets[asset_key] print('{}: {} ({})'.format(asset_key, asset.href, asset.media_type)) # asset-key,(..) werden in den String {},(..) eingesetzt
asset = item.assets['B3'] print(asset.to_dict()) # Ähnlich zur Abfrage mit .format
for asset_key in item.assets: asset = item.assets[asset_key] asset_url = asset.href file_name = asset_key + '.' + asset.media_type.split('/')[-1]
# Fragt die Daten von der API ab
response = requests.get(asset_url) # Nutzung der requests-Library
# Speichere die Datei
with open(file_name, 'wb') as f:
f.write(response.content)
print(f'{file_name} heruntergeladen.')
catalog_url = 'https://planetarycomputer.microsoft.com/api/stac/v1' client = Client.open(catalog_url) # Client interagiert mit API-Endpunkt (URL)
search = client.search( collections=['sentinel-2-l2a'], bbox=[-47.02148, -17.35063, -42.53906, -12.98314], datetime='2023-01-01/2023-01-31', limit = 10 )
items = list(search.items()) print(len(items)) print(items) item = items[5] print(f"Item ID: {item.id}") print(f"Item datetime: {item.datetime}")
for asset_key, asset in item.assets.items(): print(f"Asset Key: {asset_key}") print(f"Asset URL: {asset.href}") print(f"Asset Media Type: {asset.media_type}")
stac client for R:
install.packages("rstac") install.packages("sf") install.packages("terra") install.packages("tibble") library(terra) library(sf) library(tibble) library(rstac)
stac_url <- "https://planetarycomputer.microsoft.com/api/stac/v1"
s_obj <- stac(stac_url) str(s_obj)
get_request(s_obj)
s_obj %>% get_request()
conformance_classes <- s_obj %>% conformance() %>% get_request() conformance_classes
collections_query <- s_obj %>% collections()
collections_query %>% get_request()
stac_search( q = s_obj, collections = "usgs-lcmap-conus-v13", datetime = "2021-01-01/2021-12-31", limit = 10 ) %>% get_request()
ashe <- read_sf(system.file("shape/nc.shp", package = "sf"))[1, ] plot(st_geometry(ashe))
ashe_bbox <- ashe %>% st_transform(4326) %>% st_bbox() ashe_bbox
stac_query <- stac_search( q = s_obj, collections = "usgs-lcmap-conus-v13", bbox = ashe_bbox, datetime = "2021-01-01/2021-12-31", limit = 10 ) %>% get_request() stac_query
signed_stac_query <- items_sign( stac_query, sign_planetary_computer() # Authentifizierung beim Planetary Computer ) signed_stac_query
output_directory <- "C:/Users/lraeu/OneDrive/Desktop/Geosoftware II/geosoft2-2024/data" assets_download(signed_stac_query, "lcpri", output_dir = output_directory, overwrite = TRUE) output_file <- file.path("C:/Users/lraeu/OneDrive/Desktop/Geosoftware II/geosoft2-2024/data/lcmap/CU/V13/025011/2021/LCMAP_CU_025011_2021_20220721_V13_CCDC/LCMAP_CU_025011_2021_20220629_V13_LCPRI.tif") %>% rast() plot(output_file) rast("C:/Users/lraeu/OneDrive/Desktop/Geosoftware II/geosoft2-2024/data/B1.tiff")
ashe %>% st_transform(st_crs(output_file)) %>% st_geometry() %>% plot(add = TRUE, lwd = 3)