Geo API
Geo Package
This package contains generic geospatial logic and data for the NGED substation forecast project.
Contents
geo.h3: H3-related utilities, including grid weight computation.geo.assets: Dagster assets for the UK boundary and H3 grid weights.assets/: Generic geospatial assets like GeoJSON files.

Purpose
The geo package is designed to decouple generic geospatial operations from dataset-specific ingestion logic (e.g., ECMWF data processing in dynamical_data). This ensures that any package in the workspace can perform spatial transformations, such as mapping latitude/longitude grids to H3 hexagons, without depending on heavy or unrelated packages.
Key Features
- H3 Grid Mapping: Utilities to map regular latitude/longitude grids to H3 hexagons.
- Dagster Assets: Provides
uk_boundaryandgb_h3_grid_weightsassets for use in the main forecasting pipeline. - Parameterization: Functions are parameterized to support different grid sizes (e.g., 0.25-degree vs. 1km) and H3 resolutions.
- Data Contracts: Uses Patito contracts (defined in
packages/contracts) likeH3GridWeightsto ensure strict validation of spatial mapping data.
geo.assets
Classes
H3GridConfig
Bases: Config
Configuration for the H3 grid weights computation.
Attributes:
| Name | Type | Description |
|---|---|---|
h3_res |
int
|
The H3 resolution to use for the grid (default 5). |
grid_size |
float
|
The size of the regular lat/lng grid in degrees (default 0.25). |
child_res |
int | None
|
The H3 resolution to use for the underlying points. If None, it defaults to h3_res + 2. |
Source code in packages/geo/src/geo/assets.py
15 16 17 18 19 20 21 22 23 24 25 26 27 | |
Functions
gb_h3_grid_weights(context, config, uk_boundary)
Computes the H3 grid weights for Great Britain based on the UK boundary.
This asset dynamically generates the spatial mapping between the hexagonal H3 grid and the regular lat/lng NWP grid. It acts as the foundational reference data for downstream weather data ingestion (e.g., ECMWF ENS forecasts), ensuring weather variables are correctly area-weighted to the H3 cells.
The mapping is calculated by sampling each H3 cell with finer-resolution child
cells and determining which regular grid cell each child falls into. The
grid_size parameter is used to snap high-resolution H3 cells to the nearest
regular NWP grid points.
Source code in packages/geo/src/geo/assets.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
uk_boundary(context)
Loads the UK boundary geometry from a local GeoJSON file.
The boundary is projected to EPSG:27700 (British National Grid) and buffered by 25,000 meters to ensure that coastal substations and nearby islands are included in the resulting H3 grid without spatial distortion.
Source code in packages/geo/src/geo/assets.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
geo.h3
H3-related utilities for geospatial operations.
Classes
Functions
compute_h3_grid_weights(df, grid_size, child_res=7)
Computes the proportion mapping for H3 grid cells to a regular lat/lng grid.
This function takes a DataFrame containing H3 indices and calculates how many
child H3 cells at a finer resolution (child_res) fall into each cell of a
regular lat/lng grid of size grid_size.
The regular grid is assumed to be perfectly aligned to 0.0 (e.g., 0.0, grid_size,
grid_size * 2).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
A Polars DataFrame containing an 'h3_index' column (UInt64). |
required |
grid_size
|
float
|
The size of the regular lat/lng grid in degrees (e.g., 0.25). |
required |
child_res
|
int
|
The H3 resolution to use for the underlying points. Must be
strictly greater than the resolution of the input 'h3_index' column.
Defaults to 7. The |
7
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
A Polars DataFrame with columns: - h3_index: The original H3 index. - nwp_lat: The latitude of the regular grid cell. - nwp_lng: The longitude of the regular grid cell. - len: The number of child H3 cells in this grid cell. - total: The total number of child H3 cells for this h3_index. - proportion: The proportion of the H3 cell that falls into this grid cell. |
Source code in packages/geo/src/geo/h3.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | |