Skip to content

Instantly share code, notes, and snippets.

@JeroenVerstraelen
JeroenVerstraelen / randomforest_inference.py
Created May 6, 2024 07:29
OpenEO randomforest inference
import shapely
from shapely.geometry import Polygon, MultiPolygon
import openeo
from openeo.rest.job import JobResults
from openeo.rest.mlmodel import MlModel
from openeo.rest.job import BatchJob, JobResults
from openeo.rest.mlmodel import MlModel
c = openeo.connect("https://openeo.dev.warsaw.openeo.dataspace.copernicus.eu/openeo/").authenticate_oidc()
signed_url = "https://openeo.dataspace.copernicus.eu/openeo/1.2/jobs/j-24050486ed974fdc80e73eff53974d89/results/items/ZTNlOGRjZjMtYWM3Yy00NmMzLThiZGQtMDM0YmYyMmRjMzQy/322cfe4d8974a52f84ff8a30475568d4/ml_model_metadata.json?expires=1715585149"
@JeroenVerstraelen
JeroenVerstraelen / randomforest_training.py
Created May 6, 2024 07:27
OpenEO randomforest training
import shapely
from shapely.geometry import Polygon, MultiPolygon
import openeo
from openeo.rest.job import JobResults
from openeo.rest.mlmodel import MlModel
from openeo.rest.job import BatchJob
feature_collection = {"type": "FeatureCollection", "features": [
{
"type": "Feature",
import openeo
connection = openeo.connect(url="openeo.dataspace.copernicus.eu")
connection.authenticate_oidc()
cube = connection.load_collection(
"COPERNICUS_VEGETATION_INDICES",
temporal_extent=["2020-12-30", "2021-01-01"],
spatial_extent={
'west': 3.39,
import xarray
from openeo.udf import XarrayDataCube
from openeo.udf.debug import inspect
from openeo.metadata import CollectionMetadata, SpatialDimension
import numpy as np
def apply_metadata(metadata: CollectionMetadata,
context: dict) -> CollectionMetadata:
"""
@JeroenVerstraelen
JeroenVerstraelen / Udf.scala
Created January 23, 2024 14:32
allow users to adjust spatial extent in JEP UDFs
/**
* Iterate over every spacetimekey/tile in layer, convert it into a (bands,y,x) datacube, and run the UDF.
*
* @return The resulting MultibandTileLayerRDD.
*/
def runUserCode(code: String, layer: MultibandTileLayerRDD[SpaceTimeKey],
bandNames: util.ArrayList[String], context: util.HashMap[String, Any]): MultibandTileLayerRDD[SpaceTimeKey] = {
// TODO: Implement apply_timeseries, apply_hypercube.
// Map a python function to every tile of the RDD.
// Map will serialize + send partitions to worker nodes
@JeroenVerstraelen
JeroenVerstraelen / example.py
Created November 2, 2023 15:52
Example with no errors
import openeo
import pandas as pd
import json
from pathlib import Path
import datetime
from shapely.geometry import box
from openeo.udf import execute_local_udf
from pathlib import Path
import xarray
@JeroenVerstraelen
JeroenVerstraelen / example_cdse.py
Created October 31, 2023 16:33
MMLab on OpenEO example
import openeo
DEPENDENCIES_URL = 'https://artifactory.vgt.vito.be:443/auxdata-public/openeo/mmlab_dependencies.zip'
MODEL_URL = 'https://artifactory.vgt.vito.be:443/auxdata-public/openeo/windmill_test_model.zip'
connection = openeo.connect("openeo.dataspace.copernicus.eu").authenticate_oidc()
# Load a specific dataset.
datacube = connection.load_collection(collection_id="SENTINEL2_L1C", bands=['B01'])
datacube = datacube.filter_bbox(west=11.279182434082033, south=46.464349400461145,
@JeroenVerstraelen
JeroenVerstraelen / celltype_issue.py
Last active June 23, 2023 15:44
Band 35 cell type does not match, float32ud0.0 != float64ud0.0
# Job id: j-fe4997522f9748ed97ab074322a6d2f6
connection = openeo.connect("https://openeo-staging.dataspace.copernicus.eu/openeo").authenticate_oidc()
temporal_extent = ["2021-01-01", "2021-12-31"]
s1 = connection.load_collection("SENTINEL1_GRD", spatial_extent = {"west": 4220000, "south": 2980000, "east": 4240000, "north": 3000000, "crs": "EPSG:3035"}, temporal_extent = temporal_extent, bands = ["VH", "VV"], properties = {'sat:orbit_state': lambda x: x=='DESCENDING'})
s1 = s1.sar_backscatter(coefficient = "sigma0-ellipsoid", contributing_area = False, elevation_model = "COPERNICUS_30", ellipsoid_incidence_angle = False, local_incidence_angle = False, mask = False, noise_removal = True, options = {"debug": False, "elev_geoid": "/opt/openeo-vito-aux-data/egm96.tif", "implementation_version": "2", "otb_memory": 1024, "tile_size": 256})
s1_resampled = s1.resample_spatial(resolution = 10.0, projection = "EPSG:3035", method = "near")
s1_aggregated = s1_resampled.aggregate_temporal_period(period = "dekad", reducer =
@JeroenVerstraelen
JeroenVerstraelen / gist:34ad57650a7665d9f686789ce2ccf4fe
Last active June 12, 2023 12:46
List all kubernetes spark applications

STAGING

kubectl exec -n spark-jobs-staging openeo-geotrellis-1-driver -i -t -- bash ls . # Shows layercatalog.json cd /opt/openeo Description: Attach to driver.

kubectl get sparkapplication -n spark-jobs-staging | grep 'job-' | grep 'COMPLETED' Description: All batch job that are completed.

@JeroenVerstraelen
JeroenVerstraelen / test_error_batch_creo.py
Created April 7, 2023 11:54
This test fails with openeo.rest.JobFailedException: Batch job 'j-53a1338dbd4a4625961682a3023227b7' didn't finish successfully. Status: error (after 0:02:22).
from openeo.rest.job import BatchJob
import openeo
geometries = {
"type": "FeatureCollection",
"name": "Daft Logic Google Maps Area Calculator Tool",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"