View a vector file interactively in the browser using maplibregl. Args: file_path (str): Path or URL to the vector file to view. Supports both local paths and HTTP/HTTPS URLs. style (str, optional): Map style. Defaults to "dark-matter". open_browser (boo
(
file_path: str,
style: str = "dark-matter",
open_browser: bool = True,
)
| 377 | |
| 378 | |
| 379 | def view_vector( |
| 380 | file_path: str, |
| 381 | style: str = "dark-matter", |
| 382 | open_browser: bool = True, |
| 383 | ) -> None: |
| 384 | """ |
| 385 | View a vector file interactively in the browser using maplibregl. |
| 386 | |
| 387 | Args: |
| 388 | file_path (str): Path or URL to the vector file to view. Supports both local |
| 389 | paths and HTTP/HTTPS URLs. |
| 390 | style (str, optional): Map style. Defaults to "dark-matter". |
| 391 | open_browser (bool, optional): Whether to open browser automatically. Defaults to True. |
| 392 | |
| 393 | Raises: |
| 394 | FileNotFoundError: If the local vector file does not exist. |
| 395 | ImportError: If required dependencies are not installed. |
| 396 | """ |
| 397 | try: |
| 398 | from leafmap import maplibregl |
| 399 | except ImportError: |
| 400 | print( |
| 401 | "Error: maplibregl dependencies are not installed. " |
| 402 | "Install them with: pip install 'leafmap[maplibre]'" |
| 403 | ) |
| 404 | sys.exit(1) |
| 405 | |
| 406 | # Check if it's a URL or local file |
| 407 | is_url = file_path.startswith("http://") or file_path.startswith("https://") |
| 408 | |
| 409 | if not is_url: |
| 410 | # Expand and validate local file path |
| 411 | if file_path.startswith("~"): |
| 412 | file_path = os.path.expanduser(file_path) |
| 413 | |
| 414 | file_path = os.path.abspath(file_path) |
| 415 | |
| 416 | if not os.path.exists(file_path): |
| 417 | print(f"Error: File not found: {file_path}") |
| 418 | sys.exit(1) |
| 419 | |
| 420 | print(f"Loading vector: {file_path}") |
| 421 | |
| 422 | try: |
| 423 | # Read vector file |
| 424 | gdf = read_vector(file_path) |
| 425 | |
| 426 | # Get info |
| 427 | n_features = len(gdf) |
| 428 | geom_type = gdf.geom_type.iloc[0] if len(gdf) > 0 else "Unknown" |
| 429 | crs = gdf.crs.to_string() if gdf.crs else "Unknown" |
| 430 | gdf = gdf.to_crs(epsg=4326) |
| 431 | |
| 432 | # Create map |
| 433 | m = maplibregl.Map( |
| 434 | style=style, |
| 435 | height="100%", |
| 436 | use_message_queue=True, |
no test coverage detected