Interface API¶
osm_powerplants.interface
¶
OpenStreetMap power plant data interface.
This module provides the main interface for extracting and processing power plant data from OpenStreetMap. It handles country validation, multi-level caching, and data processing.
Main functions: process_units: Simplified entry point for most use cases process_countries: Lower-level function with more options validate_countries: Validate country names with fuzzy matching
VALID_FUELTYPES = ['Nuclear', 'Solid Biomass', 'Biogas', 'Wind', 'Hydro', 'Solar', 'Oil', 'Natural Gas', 'Hard Coal', 'Lignite', 'Geothermal', 'Waste', 'Other']
module-attribute
¶
VALID_TECHNOLOGIES = ['Steam Turbine', 'OCGT', 'CCGT', 'Run-Of-River', 'Reservoir', 'Pumped Storage', 'Offshore', 'Onshore', 'PV', 'CSP', 'Combustion Engine', 'Marine']
module-attribute
¶
VALID_SETS = ['PP', 'CHP', 'Store']
module-attribute
¶
process_countries(countries, csv_cache_path, cache_dir, update, osm_config, raw=False)
¶
Process power plant data for specified countries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
countries
|
list of str
|
Country names or ISO codes |
required |
csv_cache_path
|
str
|
Path to CSV cache file |
required |
cache_dir
|
str
|
Cache directory path |
required |
update
|
bool
|
Force cache update if True |
required |
osm_config
|
dict
|
Configuration dictionary |
required |
raw
|
bool
|
If True, return all columns including metadata |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Power plant data |
Source code in src/osm_powerplants/interface.py
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
process_units(countries, config, cache_dir, output_path=None, raw=True)
¶
Process power plant data for specified countries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
countries
|
list[str]
|
Country names or ISO codes (e.g., ['Germany', 'FR', 'ESP']) |
required |
config
|
dict
|
Configuration from get_config() |
required |
cache_dir
|
str
|
Cache directory from get_cache_dir() |
required |
output_path
|
str
|
Save CSV to this path if provided |
None
|
raw
|
bool
|
If True, return all columns. If False, remove metadata columns (config_hash, created_at, processing_parameters, id). |
True
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Power plant data with columns: projectID, Name, Country, lat, lon, Fueltype, Technology, Set, Capacity, DateIn, type, capacity_source. When raw=True, also includes metadata columns. |
Examples:
>>> from osm_powerplants import process_units, get_config, get_cache_dir
>>> config = get_config()
>>> df = process_units(
... countries=['Malta', 'Luxembourg'],
... config=config,
... cache_dir=str(get_cache_dir(config)),
... )
Source code in src/osm_powerplants/interface.py
validate_countries(countries, omitted_countries=[])
¶
Validate country names and provide helpful suggestions for invalid entries.
Uses pycountry to validate country names, supporting full names, ISO codes, and common variations. Provides fuzzy matching suggestions for typos or incorrect names. Checks against omitted countries list from configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
countries
|
list of str
|
Country names to validate. Accepts: - Full names: 'Germany', 'United States' - ISO 3166-1 alpha-2: 'DE', 'US' - ISO 3166-1 alpha-3: 'DEU', 'USA' - Common variations: 'USA', 'UK', 'South Korea' |
required |
omitted_countries
|
list of str
|
List of countries to omit from processing |
[]
|
Returns:
| Name | Type | Description |
|---|---|---|
valid_countries |
list of str
|
List of validated country names as provided (minus omitted ones) |
country_code_map |
dict
|
Mapping of country names to ISO alpha-2 codes |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any country names are invalid. Error message includes suggestions for similar valid names and shows which entries were valid vs invalid. |
Examples:
>>> valid, codes = validate_countries(['Germany', 'France'])
>>> print(codes)
{'Germany': 'DE', 'France': 'FR'}
>>> validate_countries(['Germny']) # Typo
ValueError: ❌ Invalid country names detected...
ℹ️ Did you mean: 'Germany', 'Armenia'
Source code in src/osm_powerplants/interface.py
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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
validate_and_standardize_df(df, valid_fueltypes=None, valid_technologies=None, valid_sets=None)
¶
Remove metadata columns and validate data.
Removes cache-related columns (config_hash, created_at, etc.) and logs warnings for invalid fuel types, technologies, or sets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Power plant data |
required |
valid_fueltypes
|
list of str
|
Allowed fuel types |
None
|
valid_technologies
|
list of str
|
Allowed technologies |
None
|
valid_sets
|
list of str
|
Allowed set types |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with metadata columns removed |