Skip to content

Commit 029bcba

Browse files
Basics_of_H3_in_Fused_: after_edit (#1972)
Basics_of_H3_in_Fused_: after_edit Made in [Fused Workbench](https://www.fused.io/workbench) Co-authored-by: aman@fused.io <aman@fused.io>
1 parent f1b0098 commit 029bcba

File tree

20 files changed

+19
-25
lines changed

20 files changed

+19
-25
lines changed

public/Basics_of_H3_in_Fused/collection.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
1-
<!--fused:preview-->
2-
<p align="center"><img src="fused_uploaded_preview" width="600" alt="UDF preview image"></p>
3-
41
<!--fused:readme-->
52
Reading H3 Ingested Copernicus 30m DEM dataset

public/Basics_of_H3_in_Fused/era5_full_year/era5_full_year.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def udf(bounds: fused.types.Bounds = [-125, 32, -114, 42]):
1010
# Run all months in parallel
1111
pool = monthly_udf.map([{
1212
'month': m,
13-
'bounds': list(bounds)
13+
'bounds': bounds
1414
} for m in months])
1515
df = pool.df()
1616

public/Basics_of_H3_in_Fused/era5_full_year/meta.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
},
5858
"fused:udfType": "auto"
5959
},
60-
"code": "@fused.udf\ndef udf(bounds: fused.types.Bounds = [-125, 32, -114, 42]):\n import pandas as pd\n\n monthly_udf = fused.load('era5_temp_monthly_average')\n\n # All months from 2019 to 2024\n months = [f'{y}-{m:02d}' for y in range(2019, 2025) for m in range(1, 13)]\n\n # Run all months in parallel\n pool = monthly_udf.map([{\n 'month': m,\n 'bounds': list(bounds)\n } for m in months])\n df = pool.df()\n\n df = df.reset_index(drop=True).sort_values('date').reset_index(drop=True)\n\n print(f\"{df.shape=}\")\n print(df.head())\n\n return df",
60+
"code": "@fused.udf\ndef udf(bounds: fused.types.Bounds = [-125, 32, -114, 42]):\n import pandas as pd\n\n monthly_udf = fused.load('era5_temp_monthly_average')\n\n # All months from 2019 to 2024\n months = [f'{y}-{m:02d}' for y in range(2019, 2025) for m in range(1, 13)]\n\n # Run all months in parallel\n pool = monthly_udf.map([{\n 'month': m,\n 'bounds': bounds\n } for m in months])\n df = pool.df()\n\n df = df.reset_index(drop=True).sort_values('date').reset_index(drop=True)\n\n print(f\"{df.shape=}\")\n print(df.head())\n\n return df",
6161
"headers": []
6262
}
6363
}

public/Basics_of_H3_in_Fused/era5_hex_temp/era5_hex_temp.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
def udf():
33
map_utils = fused.load("https://github.com/fusedio/udfs/tree/3eb82cb/community/milind/map_utils/")
44

5-
import h3
65
import geopandas as gpd
7-
from shapely.geometry import Point
86

97
hex_data = fused.load("era5_monthly_mean")
108
data = hex_data()
119

12-
# We can't visualize hex 15 hexagons, so turning to small points to visualixe
10+
# We can't visualize hex 15 hexagons, so turning to small points to visualise
1311
gdf = data.copy()
1412
common = fused.load("https://github.com/fusedio/udfs/tree/9bad664/public/common/")
1513
con = common.duckdb_connect()

public/Basics_of_H3_in_Fused/era5_hex_temp/meta.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
},
5858
"fused:udfType": "auto"
5959
},
60-
"code": "@fused.udf\ndef udf():\n map_utils = fused.load(\"https://github.com/fusedio/udfs/tree/3eb82cb/community/milind/map_utils/\")\n\n import h3\n import geopandas as gpd\n from shapely.geometry import Point\n\n hex_data = fused.load(\"era5_monthly_mean\")\n data = hex_data()\n\n # We can't visualize hex 15 hexagons, so turning to small points to visualixe\n gdf = data.copy()\n common = fused.load(\"https://github.com/fusedio/udfs/tree/9bad664/public/common/\")\n con = common.duckdb_connect()\n gdf = con.sql(\"\"\"\n SELECT *, \n h3_cell_to_latlng(CAST(hex AS UBIGINT)) AS latlng\n FROM gdf\n \"\"\").df()\n gdf['geometry'] = gpd.points_from_xy(\n gdf['latlng'].apply(lambda x: x[1]),\n gdf['latlng'].apply(lambda x: x[0])\n )\n gdf = gpd.GeoDataFrame(gdf.drop(columns=['latlng']), geometry='geometry', crs='EPSG:4326')\n \n print(f\"{data.T=}\")\n\n val_min = data['monthly_mean_temp'].min()\n val_max = data['monthly_mean_temp'].max()\n\n # Layer config for Census population data\n config_census = {\n \"style\": {\n \"fillColor\": {\n \"type\": \"continuous\",\n \"attr\": \"monthly_mean_temp\",\n \"domain\": [val_min, val_max],\n \"steps\": 20,\n \"palette\": \"DarkMint\",\n },\n \"filled\": True,\n \"stroked\": False,\n \"opacity\": 0.9,\n \"pointRadius\": 1,\n },\n \"tooltip\": [\"hex\", \"monthly_mean_temp\"],\n }\n\n widgets = {\n \"controls\": \"bottom-right\",\n \"scale\": \"bottom-left\",\n \"basemap\": \"bottom-right\",\n \"layers\": {\"position\": \"top-right\", \"expanded\": False},\n \"legend\": {\"position\": \"top-right\", \"expanded\": True},\n }\n\n html = map_utils.deckgl_layers(\n layers=[\n {\n \"type\": \"vector\",\n \"data\": gdf,\n \"config\": config_census,\n \"visible\": True,\n \"name\": \"Hex 15 Temperature\",\n }\n ],\n basemap=\"dark\",\n theme=\"dark\",\n initialViewState=None,\n widgets=widgets,\n \n )\n return html",
60+
"code": "@fused.udf\ndef udf():\n map_utils = fused.load(\"https://github.com/fusedio/udfs/tree/3eb82cb/community/milind/map_utils/\")\n\n import geopandas as gpd\n\n hex_data = fused.load(\"era5_monthly_mean\")\n data = hex_data()\n\n # We can't visualize hex 15 hexagons, so turning to small points to visualise\n gdf = data.copy()\n common = fused.load(\"https://github.com/fusedio/udfs/tree/9bad664/public/common/\")\n con = common.duckdb_connect()\n gdf = con.sql(\"\"\"\n SELECT *, \n h3_cell_to_latlng(CAST(hex AS UBIGINT)) AS latlng\n FROM gdf\n \"\"\").df()\n gdf['geometry'] = gpd.points_from_xy(\n gdf['latlng'].apply(lambda x: x[1]),\n gdf['latlng'].apply(lambda x: x[0])\n )\n gdf = gpd.GeoDataFrame(gdf.drop(columns=['latlng']), geometry='geometry', crs='EPSG:4326')\n \n print(f\"{data.T=}\")\n\n val_min = data['monthly_mean_temp'].min()\n val_max = data['monthly_mean_temp'].max()\n\n # Layer config for Census population data\n config_census = {\n \"style\": {\n \"fillColor\": {\n \"type\": \"continuous\",\n \"attr\": \"monthly_mean_temp\",\n \"domain\": [val_min, val_max],\n \"steps\": 20,\n \"palette\": \"DarkMint\",\n },\n \"filled\": True,\n \"stroked\": False,\n \"opacity\": 0.9,\n \"pointRadius\": 1,\n },\n \"tooltip\": [\"hex\", \"monthly_mean_temp\"],\n }\n\n widgets = {\n \"controls\": \"bottom-right\",\n \"scale\": \"bottom-left\",\n \"basemap\": \"bottom-right\",\n \"layers\": {\"position\": \"top-right\", \"expanded\": False},\n \"legend\": {\"position\": \"top-right\", \"expanded\": True},\n }\n\n html = map_utils.deckgl_layers(\n layers=[\n {\n \"type\": \"vector\",\n \"data\": gdf,\n \"config\": config_census,\n \"visible\": True,\n \"name\": \"Hex 15 Temperature\",\n }\n ],\n basemap=\"dark\",\n theme=\"dark\",\n initialViewState=None,\n widgets=widgets,\n \n )\n return html",
6161
"headers": []
6262
}
6363
}

public/Basics_of_H3_in_Fused/era5_monthly_mean/era5_monthly_mean.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@fused.udf
22
def udf(month='2024-05',
3-
bounds:list=[-130, 25, -60, 50]
3+
bounds:fused.types.Bounds=[-130, 25, -60, 50]
44
):
55
path = f's3://fused-asset/data/era5/t2m_daily_mean_v4_1000/month={month}/0.parquet'
66

public/Basics_of_H3_in_Fused/era5_monthly_mean/meta.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
},
5151
"fused:udfType": "auto"
5252
},
53-
"code": "@fused.udf\ndef udf(month='2024-05',\n bounds:list=[-130, 25, -60, 50]\n):\n path = f's3://fused-asset/data/era5/t2m_daily_mean_v4_1000/month={month}/0.parquet'\n \n common = fused.load(\"https://github.com/fusedio/udfs/tree/56ec615/public/common/\")\n con = common.duckdb_connect() \n\n query = f\"\"\"\n SELECT \n hex, \n avg(daily_mean) - 273.15 as monthly_mean_temp\n FROM read_parquet('{path}')\n WHERE 1=1\n AND h3_cell_to_lng(hex) between {bounds[0]} and {bounds[2]} \n AND h3_cell_to_lat(hex) between {bounds[1]} and {bounds[3]} \n GROUP BY 1\n \"\"\" \n\n data = con.execute(query).df()\n\n print(f\"{data.T=}\")\n\n return data",
53+
"code": "@fused.udf\ndef udf(month='2024-05',\n bounds:fused.types.Bounds=[-130, 25, -60, 50]\n):\n path = f's3://fused-asset/data/era5/t2m_daily_mean_v4_1000/month={month}/0.parquet'\n \n common = fused.load(\"https://github.com/fusedio/udfs/tree/56ec615/public/common/\")\n con = common.duckdb_connect() \n\n query = f\"\"\"\n SELECT \n hex, \n avg(daily_mean) - 273.15 as monthly_mean_temp\n FROM read_parquet('{path}')\n WHERE 1=1\n AND h3_cell_to_lng(hex) between {bounds[0]} and {bounds[2]} \n AND h3_cell_to_lat(hex) between {bounds[1]} and {bounds[3]} \n GROUP BY 1\n \"\"\" \n\n data = con.execute(query).df()\n\n print(f\"{data.T=}\")\n\n return data",
5454
"headers": []
5555
}
5656
}

public/Basics_of_H3_in_Fused/era5_temp_map_hex4/era5_temp_map_hex4.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@fused.udf(cache_max_age=0)
2-
def udf(bounds:list=[-130,-50,-35,50]):
2+
def udf(bounds:fused.types.Bounds=[-130,-50,-35,50]):
33
map_utils = fused.load("https://github.com/fusedio/udfs/tree/5c526cc/community/milind/map_utils/")
44

55
era5_udf = fused.load("era5_temp_month_hex4")

public/Basics_of_H3_in_Fused/era5_temp_map_hex4/meta.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
},
5858
"fused:udfType": "auto"
5959
},
60-
"code": "@fused.udf(cache_max_age=0)\ndef udf(bounds:list=[-130,-50,-35,50]):\n map_utils = fused.load(\"https://github.com/fusedio/udfs/tree/5c526cc/community/milind/map_utils/\")\n\n era5_udf = fused.load(\"era5_temp_month_hex4\")\n data = era5_udf(month='2025-03', hex_res=4)\n \n val_min = float(data[\"monthly_mean_temp\"].min())\n val_max = float(data[\"monthly_mean_temp\"].max())\n\n # Layer config for Census population data\n config_census = {\n \"style\": {\n \"fillColor\": {\n \"type\": \"continuous\",\n \"attr\": \"monthly_mean_temp\",\n \"domain\": [val_min, val_max],\n \"steps\": 20,\n \"palette\": \"DarkMint\",\n },\n \"filled\": True,\n \"stroked\": False,\n \"opacity\": 0.9,\n },\n \"tooltip\": [\"hex\", \"monthly_mean_temp\"],\n }\n\n widgets = {\n \"controls\": \"bottom-right\",\n \"scale\": \"bottom-left\",\n \"basemap\": \"bottom-right\",\n \"layers\": {\"position\": \"top-right\", \"expanded\": False},\n \"legend\": {\"position\": \"top-right\", \"expanded\": True},\n }\n\n html = map_utils.deckgl_layers(\n layers=[\n {\n \"type\": \"hex\",\n \"data\": data,\n \"config\": config_census,\n \"visible\": True,\n \"name\": \"Hex 4 Temperature\",\n }\n ],\n basemap=\"dark\",\n theme=\"dark\",\n initialViewState=None,\n widgets=widgets,\n debug=False,\n )\n return html",
60+
"code": "@fused.udf(cache_max_age=0)\ndef udf(bounds:fused.types.Bounds=[-130,-50,-35,50]):\n map_utils = fused.load(\"https://github.com/fusedio/udfs/tree/5c526cc/community/milind/map_utils/\")\n\n era5_udf = fused.load(\"era5_temp_month_hex4\")\n data = era5_udf(month='2025-03', hex_res=4)\n \n val_min = float(data[\"monthly_mean_temp\"].min())\n val_max = float(data[\"monthly_mean_temp\"].max())\n\n # Layer config for Census population data\n config_census = {\n \"style\": {\n \"fillColor\": {\n \"type\": \"continuous\",\n \"attr\": \"monthly_mean_temp\",\n \"domain\": [val_min, val_max],\n \"steps\": 20,\n \"palette\": \"DarkMint\",\n },\n \"filled\": True,\n \"stroked\": False,\n \"opacity\": 0.9,\n },\n \"tooltip\": [\"hex\", \"monthly_mean_temp\"],\n }\n\n widgets = {\n \"controls\": \"bottom-right\",\n \"scale\": \"bottom-left\",\n \"basemap\": \"bottom-right\",\n \"layers\": {\"position\": \"top-right\", \"expanded\": False},\n \"legend\": {\"position\": \"top-right\", \"expanded\": True},\n }\n\n html = map_utils.deckgl_layers(\n layers=[\n {\n \"type\": \"hex\",\n \"data\": data,\n \"config\": config_census,\n \"visible\": True,\n \"name\": \"Hex 4 Temperature\",\n }\n ],\n basemap=\"dark\",\n theme=\"dark\",\n initialViewState=None,\n widgets=widgets,\n debug=False,\n )\n return html",
6161
"headers": []
6262
}
6363
}

0 commit comments

Comments
 (0)