Skip to content

Instantly share code, notes, and snippets.

@soxofaan
Created March 10, 2023 15:28
Show Gist options
  • Save soxofaan/c52066597cfcbb647d57fc7a9e343823 to your computer and use it in GitHub Desktop.
Save soxofaan/c52066597cfcbb647d57fc7a9e343823 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "63f07a15-6a73-4de4-8f1e-5e131743d1d6",
"metadata": {},
"source": [
"https://github.com/Open-EO/openeo-geopyspark-driver/issues/367"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "e78e5357-55af-4433-b5ff-98bb1ae47d06",
"metadata": {},
"outputs": [],
"source": [
"import openeo\n",
"import openeo.processes"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "c5459c7e-c067-496f-844c-e357ce945f93",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'biopar': '1.2.2',\n",
" 'cropsar': '1.4.11.dev20220902+8',\n",
" 'cropsar_px': '0.0.10',\n",
" 'geopyspark': '0.4.7+openeo',\n",
" 'geotrellis-backend-assembly-static': '0.4.6-openeo',\n",
" 'geotrellis-extensions-static': '2.3.0_2.12-SNAPSHOT 66bf1a0',\n",
" 'nextland_services': '0.1.0.dev20230215+150',\n",
" 'openeo': '0.15.0',\n",
" 'openeo-geopyspark': '0.9.3a1.dev20230310+1086',\n",
" 'openeo_driver': '0.37.2a1.dev20230308+444',\n",
" 'openeo_r_udf': '0.5.0',\n",
" 'openeo_udf': '1.0.0rc3'}"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"connection = openeo.connect(\n",
" # \"openeo.vito.be\"\n",
" \"openeo-dev.vito.be\"\n",
")\n",
"connection.capabilities().capabilities[\"processing:software\"]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "4f9d3949-4521-4796-9cad-1fcf1be8d571",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Authenticated using refresh token.\n"
]
},
{
"data": {
"text/plain": [
"<Connection to 'https://openeo-dev.vito.be/openeo/1.1/' with OidcBearerAuth>"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"connection.authenticate_oidc()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "9c6c2a06-cc34-4268-a441-73e323e78556",
"metadata": {},
"outputs": [],
"source": [
"collection_id = \"TERRASCOPE_S2_NDVI_V2\"\n",
"# connection.describe_collection(collection_id)\n",
"bands = [\"NDVI_10M\"]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "dc7b64aa-1f9d-4555-aefe-8abcd5781138",
"metadata": {},
"outputs": [],
"source": [
"cube = connection.load_collection(\n",
" collection_id,\n",
" temporal_extent=[\"2022-09-01\", \"2022-09-30\"],\n",
" bands=bands\n",
") "
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "c81f6e3b-8306-4ec6-97df-bb3b6c5a8c00",
"metadata": {},
"outputs": [],
"source": [
"geometries = {\n",
" \"type\": \"FeatureCollection\",\n",
" \"properties\": {},\n",
" \"features\": [\n",
" {\n",
" \"type\": \"Feature\",\n",
" \"properties\": {},\n",
" \"geometry\": {\n",
" \"type\": \"Polygon\",\n",
" \"coordinates\": [((3.001, 51.000), (3.001, 51.001), (3.000, 51.001), (3.000, 51.000), (3.001, 51.000))],\n",
" }\n",
" },\n",
" {\n",
" \"type\": \"Feature\",\n",
" \"properties\": {},\n",
" \"geometry\": {\n",
" \"type\": \"Polygon\",\n",
" \"coordinates\": [((3.011, 51.000), (3.011, 51.001), (3.010, 51.001), (3.010, 51.000), (3.011, 51.000))],\n",
" }\n",
" }\n",
" ]\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "8ed7b230-d6b6-415b-bf09-6715c878eb99",
"metadata": {},
"outputs": [],
"source": [
"aggregated = cube.aggregate_spatial(geometries=geometries, reducer=\"mean\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "ce6ac100-971b-47de-bcb5-4325832348ce",
"metadata": {},
"outputs": [],
"source": [
"udf = \"\"\"\n",
"def udf_apply_feature_dataframe(df: pd.DataFrame) -> pd.Series:\n",
" # Sum along index (time dimension)\n",
" return df.sum(axis=0)\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "aa81a5d0-bf1b-4227-aa60-c108d58bf88d",
"metadata": {},
"outputs": [],
"source": [
"post_processed = aggregated.process(\n",
" \"run_udf\",\n",
" data=aggregated,\n",
" udf=udf,\n",
" runtime=\"Python\",\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "6a84702c-dc47-4049-8cee-5a990ea4da55",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'columns': ['feature_index', 'avg(band_0)'],\n",
" 'data': [[1, 519.5059523809524], [0, 611.348901098901]],\n",
" 'index': [0, 1]}"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"post_processed.execute()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "52fdd14b-d964-44d6-8c17-e7357cd41149",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "7d8489d1-ffcc-40d6-a917-04299cb639b7",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "3bd78079-b8be-43b4-972d-762d3964d5d3",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "60946f5c-6325-43ec-b76e-47269a779959",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "openeo-client-venv3.9",
"language": "python",
"name": "openeo-3.9"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.15"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment