Python API¶
The public API is exposed at the top level of the osm_rasterizer package:
from osm_rasterizer import rasterize, RasterizeResult, fetch_features, get_utm_crs
rasterize¶
RasterizeResult
dataclass
¶
Result of a rasterization operation.
When single_layer=False (default): array shape is (n_bands, H, W),
values are 0/1 per-feature presence masks.
When single_layer=True: array shape is (1, H, W), values are
1-based category indices into categories (0 = no data). The priority
order is determined by the feature list order passed to rasterize():
the last feature has the highest priority and wins conflicts.
Source code in osm_rasterizer/rasterize.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
rasterize(bbox, features, resolution=10.0, single_layer=False, fill_nodata=False, fill_nodata_distance=None, output_path=None, transform=None, crs=None, date=None, provider='osm')
¶
Rasterize OSM features into a GeoTIFF or return a RasterizeResult.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
|
tuple[float, float, float, float]
|
|
required |
features
|
list
|
List of OSM tag dicts, The options dict controls per-feature behaviour:
Without options, lines burn as 1-pixel-wide traces. Polygons and points are never buffered. |
required |
resolution
|
float
|
Pixel size in metres (ignored when transform is supplied). |
10.0
|
single_layer
|
bool
|
If True, merge all features into a single categorical band. Pixel values are 1-based indices into the feature list (0 = no data). Conflicts are resolved by priority: features listed later win. Reorder your feature list from least to most important category. |
False
|
fill_nodata
|
bool
|
If True, replace empty (zero) pixels with the value of their nearest non-zero neighbour. Applied per-band in multi-band mode and on the merged array in single-layer mode. |
False
|
fill_nodata_distance
|
Union[float, None]
|
Maximum distance in pixels within which an empty pixel will be
filled. Pixels farther than this from any labelled pixel are left
as 0, preventing border areas outside OSM coverage from being
flooded with a nearby label. Ignored when fill_nodata is False.
|
None
|
output_path
|
Union[str, Path, None]
|
Write a GeoTIFF here; return None instead of RasterizeResult. |
None
|
transform
|
Union[Affine, None]
|
Explicit affine transform (overrides resolution). |
None
|
crs
|
Union[CRS, str, None]
|
Output CRS; auto-detected from bbox if None. |
None
|
date
|
Union[str, None]
|
Optional ISO 8601 date string (e.g. |
None
|
provider
|
str
|
Data provider: |
'osm'
|
Returns:
| Type | Description |
|---|---|
RasterizeResult or None
|
None when output_path is given; RasterizeResult otherwise. |
Source code in osm_rasterizer/rasterize.py
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 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 | |
RasterizeResult¶
Result of a rasterization operation.
When single_layer=False (default): array shape is (n_bands, H, W),
values are 0/1 per-feature presence masks.
When single_layer=True: array shape is (1, H, W), values are
1-based category indices into categories (0 = no data). The priority
order is determined by the feature list order passed to rasterize():
the last feature has the highest priority and wins conflicts.
Source code in osm_rasterizer/rasterize.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
fetch_features¶
Fetch OSM or OpenHistoricalMap features for a WGS84 bounding box.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
|
tuple[float, float, float, float]
|
|
required |
tags
|
dict
|
Tag dict in osmnx convention, e.g. |
required |
date
|
str | None
|
Optional ISO 8601 date string (e.g. |
None
|
provider
|
str
|
|
'osm'
|
Returns:
| Type | Description |
|---|---|
GeoDataFrame
|
Features clipped to the exact bbox polygon. Returns an empty GeoDataFrame (with a geometry column) if no features are found. |
Source code in osm_rasterizer/fetch.py
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 | |
get_utm_crs¶
Auto-detect best UTM CRS for a WGS84 bbox.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
|
tuple[float, float, float, float]
|
|
required |
Returns:
| Type | Description |
|---|---|
CRS
|
The best-fit UTM CRS for the given bounding box. |
Source code in osm_rasterizer/crs.py
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 | |