maplibre module¶
Map
¶
Bases: AnyWidget
Create a MapLibre map widget.
Source code in mapwidget/maplibre.py
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 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 247 248 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 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 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 | |
layer_names
property
¶
Get the names of the layers in the map.
layers
property
¶
Get the current style of the map.
__init__(center=[0, 20], zoom=2, bearing=0, pitch=0, style='https://tiles.openfreemap.org/styles/liberty', controls=None, **kwargs)
¶
Initialize the Map widget.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
center
|
Initial center [lng, lat]. Defaults to [0, 20] |
[0, 20]
|
|
zoom
|
Initial zoom level. Defaults to 2 |
2
|
|
controls
|
List of controls to add by default. Defaults to ["navigation", "fullscreen", "globe"] |
None
|
|
**kwargs
|
Additional widget parameters |
{}
|
Source code in mapwidget/maplibre.py
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 | |
add_call(method, args=None, kwargs=None)
¶
Invoke a JS map method with arguments.
Source code in mapwidget/maplibre.py
132 133 134 135 136 137 138 | |
add_cog_layer(url, source_id=None, layer_id=None, source_options=None, layer_options=None)
¶
Adds a Cloud Optimized GeoTIFF (COG) layer to the map.
This method uses the maplibre-cog-protocol to add COG raster data as a layer on the map. The COG protocol allows for efficient streaming of large raster datasets directly from cloud storage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The URL of the COG file to load. |
required |
source_id
|
Optional[str]
|
The ID for the COG source. If not provided, a unique ID will be generated based on the URL. |
None
|
layer_id
|
Optional[str]
|
The ID for the COG layer. If not provided, a unique ID will be generated based on the source ID. |
None
|
source_options
|
Optional[Dict[str, Any]]
|
Additional options for the COG source. Common options include: - tileSize (int): Tile size in pixels. Defaults to 256. - maxzoom (int): Maximum zoom level for the source. - minzoom (int): Minimum zoom level for the source. |
None
|
layer_options
|
Optional[Dict[str, Any]]
|
Additional options for the COG layer. Common options include: - paint (Dict): Paint properties for the layer (e.g., opacity). - layout (Dict): Layout properties for the layer. - beforeId (str): ID of the layer before which to insert this layer. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Example
1 2 3 4 5 6 7 8 9 10 11 | |
Note
The COG file must be accessible via HTTP/HTTPS and should be properly formatted as a Cloud Optimized GeoTIFF with internal tiling and overviews.
Source code in mapwidget/maplibre.py
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 | |
add_control(control_type, position='top-right', options=None)
¶
Add a control to the map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
control_type
|
str
|
Type of control to add. Options include: - 'navigation' or 'NavigationControl' - 'geolocate' or 'GeolocateControl' - 'scale' or 'ScaleControl' - 'fullscreen' or 'FullscreenControl' - 'attribution' or 'AttributionControl' - 'globe' or 'GlobeControl' - 'logo' or 'LogoControl' - 'terrain' or 'TerrainControl' |
required |
position
|
str
|
Position on the map. Options: 'top-left', 'top-right', 'bottom-left', 'bottom-right' |
'top-right'
|
options
|
dict
|
Optional configuration for the control |
None
|
Source code in mapwidget/maplibre.py
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
add_draw_control(options=None, controls=None, position='top-right', geojson=None, **kwargs)
¶
Adds a drawing control to the map.
This method enables users to add interactive drawing controls to the map, allowing for the creation, editing, and deletion of geometric shapes on the map. The options, position, and initial GeoJSON can be customized.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
options
|
Optional[Dict[str, Any]]
|
Configuration options for the drawing control. Defaults to None. |
None
|
controls
|
Optional[Dict[str, Any]]
|
The drawing controls to enable. Can be one or more of the following: 'polygon', 'line_string', 'point', 'trash', 'combine_features', 'uncombine_features'. Defaults to None. |
None
|
position
|
str
|
The position of the control on the map. Defaults to "top-right". |
'top-right'
|
geojson
|
Optional[Dict[str, Any]]
|
Initial GeoJSON data to load into the drawing control. Defaults to None. |
None
|
**kwargs
|
Any
|
Additional keyword arguments to be passed to the drawing control. |
{}
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in mapwidget/maplibre.py
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 | |
add_geojson(data, source_id=None, layer_id=None, layer_type=None, paint=None, layout=None, source_options=None, fit_bounds=True, before_id=None)
¶
Add GeoJSON data to the map as a source and layer.
This method supports multiple input formats: - A GeoJSON dictionary (FeatureCollection, Feature, or Geometry) - A file path to a .geojson or .json file - A URL pointing to a GeoJSON resource - A GeoDataFrame (requires geopandas)
The method automatically detects geometry types and creates appropriate map layers (fill for Polygon, line for LineString, circle for Point). For mixed-geometry FeatureCollections, multiple layers are created.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Union[str, Dict[str, Any]]
|
GeoJSON data as a dict, file path, URL string, or GeoDataFrame. |
required |
source_id
|
Optional[str]
|
ID for the GeoJSON source. Auto-generated if not provided. |
None
|
layer_id
|
Optional[str]
|
Base ID for the layer(s). Auto-generated if not provided. For mixed-geometry data, suffixes like '-fill', '-line', '-circle' are appended. |
None
|
layer_type
|
Optional[str]
|
Explicit layer type override ('fill', 'line', 'circle', 'fill-extrusion', 'heatmap', 'symbol'). If not provided, the type is inferred from the geometry. |
None
|
paint
|
Optional[Dict[str, Any]]
|
Paint properties for the layer. If not provided, sensible defaults are used based on the layer type. |
None
|
layout
|
Optional[Dict[str, Any]]
|
Layout properties for the layer. |
None
|
source_options
|
Optional[Dict[str, Any]]
|
Additional options for the GeoJSON source (e.g., 'cluster', 'clusterMaxZoom', 'clusterRadius', 'tolerance'). |
None
|
fit_bounds
|
bool
|
Whether to automatically fit the map to the data bounds. Defaults to True. |
True
|
before_id
|
Optional[str]
|
ID of an existing layer to insert the new layer(s) before. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The source ID used for the added data. |
Example
1 2 3 4 5 6 7 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 | |
Source code in mapwidget/maplibre.py
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 | |
add_layer(layer, before_id=None)
¶
Add a new layer to the map.
Source code in mapwidget/maplibre.py
192 193 194 195 196 197 | |
add_legend(targets, options=None, position='top-right')
¶
Adds a legend control to the map using mapbox-gl-legend plugin.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
targets
|
Dict[str, str]
|
A dictionary mapping layer IDs to display names |
required |
options
|
Optional[Dict[str, Any]]
|
Configuration options for the legend control. Available options: - showDefault (bool): Whether to show default legend. Defaults to False. - showCheckbox (bool): Whether to show checkboxes. Defaults to False. - onlyRendered (bool): Whether to show only rendered layers. Defaults to True. - reverseOrder (bool): Whether to reverse the order. Defaults to True. |
None
|
position
|
str
|
The position of the control on the map. Defaults to "top-right". |
'top-right'
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in mapwidget/maplibre.py
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 | |
add_opacity_control(base_layers=None, over_layers=None, options=None, position='top-right', default_visibility=None, collapsible=False)
¶
Adds an opacity control to the map using maplibre-gl-opacity plugin.
This control allows users to adjust the opacity of multiple tile layers and switch between base layers and overlays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_layers
|
Optional[Dict[str, str]]
|
A dictionary mapping base layer IDs to display names. Only one base layer can be visible at a time. |
None
|
over_layers
|
Optional[Dict[str, str]]
|
A dictionary mapping overlay layer IDs to display names. Multiple overlays can be visible simultaneously. |
None
|
options
|
Optional[Dict[str, Any]]
|
Configuration options for the opacity control. Available options: - opacityControl (bool): Whether to show opacity sliders. Defaults to True. |
None
|
position
|
str
|
The position of the control on the map. Defaults to "top-right". |
'top-right'
|
default_visibility
|
Optional[Dict[str, bool]]
|
A dictionary mapping layer IDs to their default visibility state. If not specified, overlay layers will be visible by default and base layers will follow the first-wins rule. |
None
|
collapsible
|
bool
|
Whether to add a toggle button to show/hide the control. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
Source code in mapwidget/maplibre.py
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 | |
add_source(source_id, source)
¶
Add a new source to the map.
Source code in mapwidget/maplibre.py
184 185 186 | |
add_stac_layer(url, asset_key='visual', source_id=None, layer_id=None, source_options=None, layer_options=None, fit_bounds=True)
¶
Add a STAC (SpatioTemporal Asset Catalog) item's raster asset to the map.
This method loads a STAC item from a URL, extracts the specified asset's COG href, and adds it as a raster layer using the COG protocol. Optionally fits the map to the item's bounding box.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
URL to a STAC Item JSON (e.g., 'https://planetarycomputer.microsoft.com/api/stac/v1/collections/...') |
required |
asset_key
|
str
|
The asset key to load from the STAC item. Common values include 'visual', 'B04', 'data', 'rendered_preview'. Defaults to 'visual'. |
'visual'
|
source_id
|
Optional[str]
|
ID for the raster source. Auto-generated if not provided. |
None
|
layer_id
|
Optional[str]
|
ID for the raster layer. Auto-generated if not provided. |
None
|
source_options
|
Optional[Dict[str, Any]]
|
Additional options for the raster source (e.g., tileSize, maxzoom, minzoom). |
None
|
layer_options
|
Optional[Dict[str, Any]]
|
Additional options for the raster layer (e.g., paint properties like raster-opacity). |
None
|
fit_bounds
|
bool
|
Whether to fit the map to the STAC item's bounding box. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Example
1 2 3 4 5 6 7 8 9 10 11 12 | |
Note
The STAC item must contain the specified asset key, and the asset must have an 'href' pointing to a Cloud Optimized GeoTIFF (COG).
Source code in mapwidget/maplibre.py
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 | |
draw_features_delete_all()
¶
Deletes all features from the drawing control.
This method removes all drawn features from the map and updates the model accordingly.
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in mapwidget/maplibre.py
316 317 318 319 320 321 322 323 324 325 326 | |
fit_bounds(bounds, options=None)
¶
Fit the map to given bounds [[west, south], [east, north]].
Source code in mapwidget/maplibre.py
165 166 167 168 169 170 | |
fly_to(center=None, zoom=None, bearing=None, pitch=None)
¶
Fly to a given location with optional zoom, bearing, and pitch.
Source code in mapwidget/maplibre.py
152 153 154 155 156 157 158 159 160 161 162 163 | |
pan_to(lng, lat)
¶
Pan the map to a given location.
Source code in mapwidget/maplibre.py
148 149 150 | |
remove_control(control_type)
¶
Remove a control from the map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
control_type
|
str
|
The type of control to remove (e.g., 'navigation', 'fullscreen') |
required |
Source code in mapwidget/maplibre.py
248 249 250 251 252 253 254 255 256 257 | |
remove_draw_control()
¶
Removes the drawing control from the map.
This method removes the drawing control and clears all associated draw features from the map and model.
Returns:
| Type | Description |
|---|---|
None
|
None |
Source code in mapwidget/maplibre.py
304 305 306 307 308 309 310 311 312 313 314 | |
remove_layer(layer_id)
¶
Remove a layer from the map.
Source code in mapwidget/maplibre.py
199 200 201 | |
remove_source(source_id)
¶
Remove a source from the map.
Source code in mapwidget/maplibre.py
188 189 190 | |
resize()
¶
Trigger map resize.
Source code in mapwidget/maplibre.py
180 181 182 | |
set_bearing(bearing)
¶
Set the bearing of the map.
Source code in mapwidget/maplibre.py
176 177 178 | |
set_center(lng, lat)
¶
Set the center of the map.
Source code in mapwidget/maplibre.py
140 141 142 | |
set_draw_mode(mode)
¶
Set the drawing mode, even if the map is not yet loaded.
Source code in mapwidget/maplibre.py
365 366 367 368 369 370 371 372 | |
set_filter(layer_id, filter_expr)
¶
Set a filter expression on a layer.
Source code in mapwidget/maplibre.py
211 212 213 | |
set_layer_visibility(layer_id, visibility)
¶
Set visibility of a layer ('visible' or 'none').
Source code in mapwidget/maplibre.py
219 220 221 | |
set_layout_property(layer_id, prop, value)
¶
Set a layout property on a layer.
Source code in mapwidget/maplibre.py
207 208 209 | |
set_paint_property(layer_id, prop, value)
¶
Set a paint property on a layer.
Source code in mapwidget/maplibre.py
203 204 205 | |
set_pitch(pitch)
¶
Set the pitch of the map.
Source code in mapwidget/maplibre.py
172 173 174 | |
set_style(style_url)
¶
Set the map style.
Source code in mapwidget/maplibre.py
215 216 217 | |
set_zoom(zoom)
¶
Set the zoom level.
Source code in mapwidget/maplibre.py
144 145 146 | |