WorldCereal / worldcereal-classification

This repository contains the classification module of the WorldCereal system.
https://esa-worldcereal.org/
MIT License
37 stars 4 forks source link

First version of a rdm api interaction module #125 #161

Closed VincentVerelst closed 1 month ago

VincentVerelst commented 2 months ago

This is a first version of how we can construct a GeoParquet file from the RDM API, based on a user-defined AOI.

How it works:

import shapely
from worldcereal.rdm_api import query_rdm

poly = shapely.Polygon([(1.824467, 50.34406), (1.824467, 50.950124), (3.735661, 50.950124), (3.735661, 50.34406), (1.824467, 50.34406)])

gdf = query_rdm(poly)

A visualization of the output:

image

All feedback is welcome!

VincentVerelst commented 1 month ago

@kvantricht , quite some changes since your last review:

With the new changes made around authentication, a way to interact with the RDM API would now be as follows:

from worldcereal.rdm_api import RdmInteraction

interaction = RdmInteraction().authenticate()

gdf = interaction.query_rdm(geometry=multi_polygon, temporal_extent=temporal_extent)

Authentication now works similary to the OpenEO Python Client. You can also leave out authenticate(), in which case the rdm query will only interact with public datasets.

For a STAC collection of patch extractions, you can extract the geometries of all patchtes into one MultiPolygon as follows (the following code will probably become part of the patch-to-points extraction script):

import pystac
from shapely.geometry import shape, MultiPolygon

collection = pystac.read_file('/path/to/stac/collection.json')

polygons = []

for item in collection.get_items():  
    polygons.append(shape(item.geometry).buffer(1e-9))  # Add buffer to avoid TopologyException

multi_polygon = MultiPolygon(polygons)
temporal_extent = [collection.extent.temporal.intervals[0][0], collection.extent.temporal.intervals[0][1]]

The results for a query on this MultiPolygon and temporal_extent look like this (cached 64x64 patches in pink, RDM query results in purple): image

VincentVerelst commented 1 month ago

@kvantricht, added some resilience and a timeout to the RDM interaction. That should cover your final comments!

kvantricht commented 1 month ago

Perfect, thanks!