-
Notifications
You must be signed in to change notification settings - Fork 62
feat: add bigframes.bigquery.st_regionstats to join raster data from Earth Engine #2228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8706023
feat: add bigframes.bigquery.st_regionstats to join raster data from …
tswast 924dd2c
Merge branch 'main' into tswast-st-regionstats
tswast a0d8e28
upgrade sqlglot
tswast 8a897cd
Merge remote-tracking branch 'origin/tswast-st-regionstats' into tswa…
tswast e000ce0
address samples lint error
tswast 9aaf580
Merge branch 'main' into tswast-st-regionstats
tswast 771dd50
Merge branch 'main' into tswast-st-regionstats
tswast 2ddb0eb
avoid sqlglot rtrim/ltrim bug
tswast File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| # Copyright 2025 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Code sample for https://docs.cloud.google.com/bigquery/docs/raster-data#analytics-hub-source""" | ||
|
|
||
|
|
||
| def test_st_regionstats() -> None: | ||
| project_id = "bigframes-dev" | ||
|
|
||
| # [START bigquery_dataframes_st_regionstats] | ||
| import datetime | ||
| from typing import cast | ||
|
|
||
| import bigframes.bigquery as bbq | ||
| import bigframes.pandas as bpd | ||
|
|
||
| # TODO: Set the project_id to your Google Cloud project ID. | ||
| # project_id = "your-project-id" | ||
| bpd.options.bigquery.project = project_id | ||
|
|
||
| # TODO: Set the dataset_id to the ID of the dataset that contains the | ||
| # `climate` table. This is likely a linked dataset to Earth Engine. | ||
| # See: https://cloud.google.com/bigquery/docs/link-earth-engine | ||
| linked_dataset = "era5_land_daily_aggregated" | ||
|
|
||
| # For the best efficiency, use partial ordering mode. | ||
| bpd.options.bigquery.ordering_mode = "partial" | ||
|
|
||
| # Load the table of country boundaries. | ||
| countries = bpd.read_gbq("bigquery-public-data.overture_maps.division_area") | ||
|
|
||
| # Filter to just the countries. | ||
| countries = countries[countries["subtype"] == "country"].copy() | ||
| countries["name"] = countries["names"].struct.field("primary") | ||
| countries["simplified_geometry"] = bbq.st_simplify( | ||
| countries["geometry"], | ||
| tolerance_meters=10_000, | ||
| ) | ||
|
|
||
| # Get the reference to the temperature data from a linked dataset. | ||
| # Note: This sample assumes you have a linked dataset to Earth Engine. | ||
| image_href = ( | ||
| bpd.read_gbq(f"{project_id}.{linked_dataset}.climate") | ||
| .set_index("start_datetime") | ||
| .loc[[datetime.datetime(2025, 1, 1, tzinfo=datetime.timezone.utc)], :] | ||
| ) | ||
| raster_id = image_href["assets"].struct.field("image").struct.field("href") | ||
| raster_id = raster_id.item() | ||
| stats = bbq.st_regionstats( | ||
| countries["simplified_geometry"], | ||
| raster_id=cast(str, raster_id), | ||
| band="temperature_2m", | ||
| ) | ||
|
|
||
| # Extract the mean and convert from Kelvin to Celsius. | ||
| countries["mean_temperature"] = stats.struct.field("mean") - 273.15 | ||
|
|
||
| # Sort by the mean temperature to find the warmest countries. | ||
| result = countries[["name", "mean_temperature"]].sort_values( | ||
| "mean_temperature", ascending=False | ||
| ) | ||
| print(result.head(10)) | ||
| # [END bigquery_dataframes_st_regionstats] | ||
|
|
||
| assert len(result) > 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| test_st_regionstats() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
tests/unit/core/compile/sqlglot/snapshots/test_compile_geo/test_st_regionstats/out.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| WITH `bfcte_0` AS ( | ||
| SELECT | ||
| * | ||
| FROM UNNEST(ARRAY<STRUCT<`bfcol_0` STRING, `bfcol_1` INT64>>[STRUCT('POINT(1 1)', 0)]) | ||
| ), `bfcte_1` AS ( | ||
| SELECT | ||
| *, | ||
| ST_REGIONSTATS( | ||
| `bfcol_0`, | ||
| 'ee://some/raster/uri', | ||
| band => 'band1', | ||
| include => 'some equation', | ||
| options => JSON '{"scale": 100}' | ||
| ) AS `bfcol_2` | ||
| FROM `bfcte_0` | ||
| ), `bfcte_2` AS ( | ||
| SELECT | ||
| *, | ||
| `bfcol_2`.`min` AS `bfcol_5`, | ||
| `bfcol_2`.`max` AS `bfcol_6`, | ||
| `bfcol_2`.`sum` AS `bfcol_7`, | ||
| `bfcol_2`.`count` AS `bfcol_8`, | ||
| `bfcol_2`.`mean` AS `bfcol_9`, | ||
| `bfcol_2`.`area` AS `bfcol_10` | ||
| FROM `bfcte_1` | ||
| ) | ||
| SELECT | ||
| `bfcol_5` AS `min`, | ||
| `bfcol_6` AS `max`, | ||
| `bfcol_7` AS `sum`, | ||
| `bfcol_8` AS `count`, | ||
| `bfcol_9` AS `mean`, | ||
| `bfcol_10` AS `area` | ||
| FROM `bfcte_2` | ||
| ORDER BY | ||
| `bfcol_1` ASC NULLS LAST |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be consistent, we may want to make the optional args as kwargs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. I'm okay either way with this one. In my testing, BigQuery SQL actually accepts the optional parameters to ST_REGIONSTATS as either keyword arguments or positional, so the Python default seems sensible here.