|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +def test_st_regionstats(): |
| 17 | + project_id = "bigframes-dev" |
| 18 | + |
| 19 | + # [START bigquery_dataframes_st_regionstats] |
| 20 | + from typing import cast |
| 21 | + |
| 22 | + import bigframes.bigquery as bq |
| 23 | + import bigframes.pandas as bpd |
| 24 | + |
| 25 | + # TODO: Set the project_id to your Google Cloud project ID. |
| 26 | + # project_id = "your-project-id" |
| 27 | + # |
| 28 | + # TODO: Set the dataset_id to the ID of the dataset that contains the |
| 29 | + # `climate` table. This is likely a linked dataset to Earth Engine. |
| 30 | + # See: https://cloud.google.com/bigquery/docs/link-earth-engine |
| 31 | + linked_dataset = "era5_land_daily_aggregated" |
| 32 | + |
| 33 | + # Load the table of country boundaries. |
| 34 | + bpd.options.bigquery.project = project_id |
| 35 | + countries = bpd.read_gbq("bigquery-public-data.overture_maps.division_area") |
| 36 | + |
| 37 | + # Filter to just the countries. |
| 38 | + countries = countries[countries["subtype"] == "country"].copy() |
| 39 | + countries["name"] = countries["names"].struct.field("primary") |
| 40 | + |
| 41 | + # TODO: Add st_simplify when it is available in BigFrames. |
| 42 | + # https://github.com/googleapis/python-bigquery-dataframes/issues/1497 |
| 43 | + # countries["simplified_geometry"] = bq.st_simplify(countries["geometry"], 10000) |
| 44 | + countries["simplified_geometry"] = countries["geometry"] |
| 45 | + |
| 46 | + # Get the reference to the temperature data from a linked dataset. |
| 47 | + # Note: This sample assumes you have a linked dataset to Earth Engine. |
| 48 | + # See: https://cloud.google.com/bigquery/docs/link-earth-engine |
| 49 | + image_href = bpd.read_gbq(f"{project_id}.{linked_dataset}.climate").where( |
| 50 | + lambda df: df["start_datetime"] == "2025-01-01 00:00:00" |
| 51 | + ) |
| 52 | + raster_id = image_href["assets"].struct.field("image").struct.field("href").item |
| 53 | + stats = bq.st_regionstats( |
| 54 | + countries["simplified_geometry"], |
| 55 | + raster_id=cast(str, raster_id), |
| 56 | + band="temperature_2m", |
| 57 | + ) |
| 58 | + |
| 59 | + # Extract the mean and convert from Kelvin to Celsius. |
| 60 | + countries["mean_temperature"] = stats.struct.field("mean") - 273.15 |
| 61 | + |
| 62 | + # Sort by the mean temperature to find the warmest countries. |
| 63 | + result = countries[["name", "mean_temperature"]].sort_values( |
| 64 | + "mean_temperature", ascending=False |
| 65 | + ) |
| 66 | + print(result.head()) |
| 67 | + # [END bigquery_dataframes_st_regionstats] |
0 commit comments