def test_rfc3339_datetime_dont_propagate_none(): formatter = Rfc3339(propagate_none=False) assert formatter.datetime("2020-03-17") == "2020-03-17T00:00:00Z" assert formatter.date("2020-03-17") == "2020-03-17" with pytest.raises(ValueError): formatter.datetime(None) with pytest.raises(ValueError): formatter.date(None)
def test_rfc3339_datetime_propagate_none(): formatter = Rfc3339(propagate_none=True) assert formatter.datetime("2020-03-17") == "2020-03-17T00:00:00Z" assert formatter.datetime(None) is None assert formatter.date("2020-03-17") == "2020-03-17" assert formatter.date(None) is None
def test_rfc3339_parse_date_or_datetime_none(): with pytest.raises(ValueError): rfc3339.parse_date_or_datetime(None) assert Rfc3339(propagate_none=True).parse_date_or_datetime(None) is None
def extract_result_metadata(tracer: DryRunDataTracer) -> dict: logger.info("Extracting result metadata from {t!r}".format(t=tracer)) rfc3339 = Rfc3339(propagate_none=True) source_constraints = tracer.get_source_constraints() # Take union of extents temporal_extent = temporal_extent_union(*[ sc["temporal_extent"] for _, sc in source_constraints if "temporal_extent" in sc ]) extents = [ sc["spatial_extent"] for _, sc in source_constraints if "spatial_extent" in sc ] if (len(extents) > 0): spatial_extent = spatial_extent_union(*extents) bbox = [spatial_extent[b] for b in ["west", "south", "east", "north"]] if all(b is not None for b in bbox): polygon = Polygon.from_bounds(*bbox) geometry = mapping(polygon) area = area_in_square_meters(polygon, spatial_extent["crs"]) else: bbox = None geometry = None area = None else: bbox = None geometry = None area = None start_date, end_date = [rfc3339.datetime(d) for d in temporal_extent] aggregate_spatial_geometries = tracer.get_geometries( ) # TODO: consider "filter_spatial" geometries too? if aggregate_spatial_geometries: if len(aggregate_spatial_geometries) > 1: logger.warning("Multiple aggregate_spatial geometries: {c}".format( c=len(aggregate_spatial_geometries))) agg_geometry = aggregate_spatial_geometries[0] if isinstance(agg_geometry, BaseGeometry): bbox = agg_geometry.bounds geometry = mapping(agg_geometry) area = area_in_square_meters(agg_geometry, "EPSG:4326") elif isinstance(agg_geometry, DelayedVector): bbox = agg_geometry.bounds # Intentionally don't return the complete vector file. https://github.com/Open-EO/openeo-api/issues/339 geometry = mapping(Polygon.from_bounds(*bbox)) area = agg_geometry.area else: logger.warning("Unsupported geometry to calculate area: " + str(agg_geometry)) links = tracer.get_metadata_links() links = [link for k, v in links.items() for link in v] # TODO: dedicated type? # TODO: match STAC format? return { 'geometry': geometry, 'bbox': bbox, 'area': { 'value': area, 'unit': 'square meter' } if area else None, 'start_datetime': start_date, 'end_datetime': end_date, 'links': links }