Leafmap, a powerful Python package designed for interactive mapping and geospatial analysis, has recently added an impressive feature: the ability to download Google Open Buildings data with just one line of code. This dataset is the largest of its kind and provides detailed information on buildings across various countries.
Getting Started with Leafmap
Leafmap makes geospatial data analysis accessible, whether you’re an experienced programmer or just starting. It leverages popular libraries like Folium and ipyleaflet for mapping, and WhiteboxTools for advanced geospatial analysis. The package’s user-friendly interface allows users to load and visualize data with minimal coding, making it ideal for Jupyter environments.
Installation
Before diving into the new feature, ensure you have Leafmap installed. You can install it using pip:
pythonCopy code# Uncomment the following line to install leafmap if needed.
# %pip install -U leafmap geopandas
Downloading Google Open Buildings Data
With Leafmap, downloading the Google Open Buildings dataset for any country is incredibly simple. Here’s how you can do it in just one line of code:
pythonCopy codeimport leafmap
# Specify the country name
country = "Libya"
# Download the Google building footprints
leafmap.download_google_buildings(country, out_dir="buildings", merge_output=f"{country}_google.shp", head=None, overwrite=True)
This script will download the building footprints for the specified country and save them as a shapefile.
Visualizing the Data
After downloading the data, you can visualize it using Leafmap’s interactive mapping capabilities. Here’s an example of how to display the Google building footprints:
pythonCopy code# Display the Google building footprints
m = leafmap.Map()
m.add_basemap("SATELLITE")
m.add_vector(f"{country}_google.shp", layer_name="Google Buildings")
m
This code snippet will create an interactive map with the satellite basemap and overlay the downloaded building footprints.
Merging Microsoft and Google Building Footprints
If you want to compare or merge data from Microsoft Global Building Footprints with Google Open Buildings, Leafmap makes it easy. Here’s a step-by-step guide to downloading and merging these datasets:
- Download Microsoft Building Footprints:pythonCopy code
# Download the Microsoft building footprints leafmap.download_ms_buildings(country, out_dir="buildings", merge_output=f"{country}_ms.shp", head=None)
- Display Microsoft Building Footprints:pythonCopy code
# Display the Microsoft building footprints m = leafmap.Map() m.add_basemap("SATELLITE") m.add_vector(f"{country}_ms.shp", layer_name="MS Buildings") m
- Download Google Building Footprints:pythonCopy code
# Download the Google building footprints leafmap.download_google_buildings(country, out_dir="buildings", merge_output=f"{country}_google.shp", head=None, overwrite=True)
- Display Google Building Footprints:pythonCopy code
# Display the Google building footprints url = "https://sites.research.google/open-buildings/tiles.geojson" m = leafmap.Map() m.add_basemap("SATELLITE") m.add_geojson(url, layer_name="Google Building Coverage") m.add_vector(f"{country}_google.shp", layer_name="Google Buildings") m
Conclusion
Leafmap’s new feature for downloading Google Open Buildings data simplifies access to comprehensive building datasets, enabling researchers and developers to enhance their geospatial analysis. With its intuitive interface and powerful capabilities, Leafmap continues to be a valuable tool in the geospatial community.
For more information, check out the Leafmap GitHub repository and explore the Leafmap documentation.
Happy mapping!