Buildings are one of the most important types of geospatial data, useful for a wide range of applications such as urban analysis, city planning, population studies, disaster management, etc. In the previous post, I discussed how to download building footprints from Google and Microsoft. Some of you might be curious about how these companies are able to provide such extensive building data for the entire globe. Of course, they don’t do it manually. They rely on advanced computational approaches using the latest machine learning and AI technologies.
In this post, I’ll discuss how you can implement a similar approach to automatically detect buildings from satellite imagery using GeoAI.
We’ll cover the following topics:
- What is GeoAI?
- Installing the GeoAI library on your machine
- Downloading a building detection model
- Running building detection
- Generalizing building geometries
- Exporting results to vector format
What is GeoAI?
GeoAI is a powerful, open-source Python package designed to integrate Artificial Intelligence (AI) with geospatial data analysis and visualization. It serves as a bridge between AI and geospatial analysis, providing tools to process, analyze, and visualize various geospatial data types, such as satellite imagery, LiDAR point clouds, and vector data, using advanced machine learning techniques.
Key capabilities of GeoAI include:
- Advanced multi-layer geospatial data visualization with customizable styling and time-series options.
- Streamlined data preparation for remote sensing imagery from sources like Sentinel, Landsat, and NAIP, including automated training data generation and augmentation.
- Image segmentation via integration with Meta's Segment Anything Model (SAM) to extract features like buildings, roads, vegetation, and water bodies, with export to standard geospatial formats.
- Image classification using pre-trained models for land cover/use and support for multi-temporal classification for change detection.
- Object detection in aerial and satellite imagery for features such as cars, ships, and solar panels.
GeoAI is actively developed, offers comprehensive documentation and tutorials, and is supported by organizations like NASA and the U.S. Geological Survey.
Installing GeoAI
GeoAI is built on top of several widely used Python libraries such as PyTorch, GeoPandas, Rasterio, Shapely, NumPy, and many others. Before installing it on your local machine, I strongly recommend creating a new environment to avoid potential conflicts with existing packages in your system. If you're using Anaconda, you can create a new environment by running the following command in your terminal.
conda create -n geoai
Next, activate the new environment with:
conda activate geoai
You can install the GeoAI module using common Python package manager such as Pip, Conda or Mamba with the following command:
- pip install geoai-py
- conda install -c conda-forge geoai
- mamba install -c conda-forge geoai
These installation methods automatically manage the package’s dependencies, which is one of the key advantages of using a fresh environment. This way, you don’t have to worry about package conflicts or compatibility issues with your existing setup.
Importing GeoAI and Model
To import the GeoAI module and the AI model can be done with the following code.
import geoai extractor = geoai.BuildingFootprintExtractor (model_path="building_footprints_usa.pth")
From the code above, we can see that the model used is "building_footprints_usa.pth". Based on its name, this model was likely trained on a dataset of building footprints from the USA. The model is hosted on the Hugging Face Hub, and running the code will automatically download it into your machine’s memory. For repeated or long-term use, it’s recommended to download the model and save it locally. After doing so, update the model_path variable to point to the local file path on your machine.
Figure 1 shows the model in the hugging face.
![]() |
Figure 1. Building Detection Model in Hugging Face |
Performing Building Detection
The code below is used to perform building detection and consists of two main parts: defining the input image (satellite imagery) and running the building detection process. Several parameters can be adjusted to improve the results, such as minimum object area, confidence threshold, and mask threshold. These threshold values typically range from 0 to 1. Increasing the threshold makes the model more selective, classifying an object as a building only when it's more certain, while lowering the threshold makes the model more lenient, potentially identifying more buildings but with less confidence.
1 2 3 4 5 6 7 8 9 10 11 12 | #Input Image path='/media/GeoAI/' raster_path = path+'input_img.tif' #Masking (Building Detection) masks_path = extractor.generate_masks( raster_path, min_object_area=50, confidence_threshold=0.1, mask_threshold=0.1, threshold=0.1, ) |
Figure 2 shows the result of detected building. To view the result can be used the following code.
geoai.view_raster(masks_path, opacity=0.7, colormap="tab20", basemap=raster_path)
![]() |
Figure 2. Building Detection Result |
Geometry Normalization and Exporting to Vector
The result as view in figure 2 is a masking image in raster format. Usually we want the result in vector format. Before exporting to vector format, the geometry needs to be normalized to get a rectangular building geometry. The result can be seen as in figure 3.
#Normalization gdf = geoai.orthogonalize( input_path=masks_path, output_path=path+"building_footprints.geojson", epsilon=1.0) #View the result geoai.view_vector_interactive(gdf, style_kwds={"color": "red", "fillOpacity": 0.2}, tiles=raster_path)
![]() |
Figure 3. Vector Result |
That’s all for this tutorial on how to automatically detect building footprints using GeoAI. The results may not accurately detect all buildings in the input image, likely because the model was primarily trained on buildings in the U.S., and building styles can vary significantly in other parts of the world. This highlights the potential for improving the model to achieve better performance across diverse regions. Lastly, thank you for reading this post. I hope you found it useful!