Unlock 3D City Models: A Python Tutorial with OSM Data

Updated on Aug 04,2025

Table of Contents

Ever wanted to build your own interactive 3D city models? This tutorial breaks down the process using Python and open-source tools, allowing anyone to create stunning visualizations from readily available OpenStreetMap (OSM) data. Whether you're a developer, researcher, or just a curious enthusiast, this guide will walk you through every step, providing the knowledge and resources needed to turn geographical data into captivating 3D experiences. Dive in and discover the exciting world of 3D city modeling!

Key Points

Learn to create a 3D environment using Python and Anaconda.

Extract data from OpenStreetMap (OSM) for various cities.

Generate building footprints and 3D building models.

Visualize city models with PyVista.

Integrate LiDAR data for enhanced realism.

Automate the entire process for multiple locations.

Building Your 3D City Modeling Toolkit

Setting up Your Python Environment with Anaconda

The foundation of our 3D city modeling adventure is Python, along with the Anaconda distribution. Anaconda streamlines package management and virtual environment creation, essential for keeping your project organized and preventing dependency conflicts.

This setup ensures a smooth workflow, allowing you to focus on the creative aspects of model building. Python, the versatile programming language, brings all the feature to create the whole project.

Installing Necessary Libraries: Before diving into the code, we need to install two key libraries: OSMnx and PyVista. OSMnx simplifies the process of downloading and manipulating data from OpenStreetMap, while PyVista provides the tools for 3D visualization. To install these packages, you'll typically use pip, the Python package installer, within your Anaconda environment.

conda activate your_environment_name
pip install osmnx
pip install pyvista

Replace your_environment_name with the name of the Anaconda environment you've created for this project.

CloudCompare: The presenter touches briefly on using CloudCompare, a powerful open-source 3D point cloud processing software. While not strictly required for the basic Tutorial, CloudCompare can be invaluable for advanced tasks like aligning and merging your OSM-derived models with LiDAR data. This software offers a plethora of tools for manipulating point clouds, performing geometric analysis, and much more.

The Workflow: From OSM Data to 3D Visualization

The process of creating a 3D city model from OSM data involves a series of well-defined steps.

Let's break down the key stages:

  1. Environment Setup: As discussed above, this is the crucial first step, ensuring you have Python, Anaconda, and the necessary libraries installed.
  2. Extracting OSM Data: OSMnx enables us to query and download data from OpenStreetMap for a specified location and radius. This data includes information about buildings, streets, and other geographical features.
  3. Generating Building Footprints: The downloaded data is then processed to generate 2D polygons representing the footprints of buildings. These footprints form the basis for our 3D models.
  4. Generating 3D Buildings: Building on the 2D footprints, we extrude them vertically to create 3D building models. This extrusion process often involves assigning random heights to the buildings, although more sophisticated techniques using LiDAR data can be employed.
  5. 3D Visualization: PyVista comes into play at this stage, allowing us to visualize the 3D city model. PyVista provides interactive tools for exploring the model from different angles and perspectives.
  6. Export Building Function: Creating function about export 3D building file, e.g. obj, gltf.
  7. Reusing the cloudgify Principle: It can save the city models and reuse the saved file.
  8. Single Location Experiment: Do some experiments with single location, get models and change radius.
  9. Script Automation: It can realize function of automatically working.
  10. Going beyond: To go the height from other dataset, and integrate in a 3D World.

Advanced Techniques: LiDAR Integration and Spatial Analysis

Leveraging LiDAR Data for Enhanced Realism

The tutorial also hints at the possibility of integrating LiDAR (Light Detection and Ranging) data into the modeling process.

LiDAR data provides highly accurate elevation information, enabling the creation of more realistic building models with varying roof heights and detailed terrain. Integrating LiDAR can significantly improve the visual fidelity of your 3D city models. Here are the benefits to use LiDAR :

  • It will make model close to the later data.
  • Can leverage data to get height.
  • Model the roof differently.

CloudCompare for LiDAR Integration: The presenter also mentions CloudCompare, a powerful tool for manipulating LiDAR data, to compare any output outside of python, like OBJ/GLTF. CloudCompare is valuable for tasks such as aligning and merging LiDAR data with OSM-derived models.

Spatial Analysis with 3D City Models

Once you have a 3D city model, the possibilities for spatial analysis are vast.

You can analyze building heights, street widths, and population density, gaining valuable insights into urban planning and development. 3D city models can be used for a wide range of applications, including:

  • Urban planning and design
  • Disaster management and risk assessment
  • Real estate development
  • Environmental modeling

Step-by-Step Guide: Creating a 3D City Model with Python

Step 1: Setting up the Environment

First, make sure Python and Anaconda are installed. Create a virtual environment using Anaconda:

conda create -n citymodel python=3.9
conda activate citymodel

Then, install the necessary libraries:

pip install osmnx
pip install pyvista

Step 2: Extract OSM Data

Use the OSMnx library to download data for your chosen city:

import osmnx as ox

place = "Toulouse, France"
graph = ox.graph_from_place(place, network_type="drive")
fig, ax = ox.plot_graph(graph)

Replace "Toulouse, France" with the desired city and adjust the network_type as needed. This example downloads data for road networks, but you can adjust the type, too.

Step 3: Generating Building Footprints

Extract building footprints from the OSM data:

import geopandas as gpd

buildings = ox.features_from_place(place, tags={"building": True})
buildings.head()

This code snippet extracts the building footprints and displays the first few entries.

Step 4: Visualizing the City Model in 3D

Use PyVista to visualize the buildings in 3D:

import pyvista as pv

# Assuming 'buildings' is a GeoDataFrame with building footprints
mesh = pv.GeoDataFrame(buildings).extract_surface()
plotter = pv.Plotter()
plotter.add_mesh(mesh, show_edges=True)
plotter.show()

OpenStreetMap 3D Modeling: Weighing the Pros and Cons

👍 Pros

Free and open-source data source.

Large community of contributors.

Provides a wealth of geographical information.

Versatile and adaptable to various applications.

👎 Cons

Data completeness and accuracy can vary.

Limited attribute information for buildings.

May require additional data sources for advanced modeling.

Requires familiarity with Python and geospatial libraries.

Frequently Asked Questions (FAQ)

What is OpenStreetMap (OSM)?
OpenStreetMap (OSM) is a collaborative, open-source project that aims to create a free and editable map of the world. It relies on contributions from volunteers who collect and submit geographical data.
What is LiDAR data, and why is it useful?
LiDAR (Light Detection and Ranging) is a remote sensing technology that uses laser light to create highly accurate 3D models of the Earth's surface. In the context of city modeling, LiDAR data provides precise elevation information, enabling the creation of realistic terrain and building heights.
What are the potential applications of 3D city models?
The potential applications of 3D city models are vast, spanning across various sectors. Some notable examples include: Urban planning and design, disaster management and risk assessment, real estate development, and environmental modeling.
What is CloudCompare?
CloudCompare is a free, open-source, point cloud processing software for translating, registering, editing, and comparing clouds.

Dive Deeper: Related Questions

How can I improve the realism of my 3D city models?
You can enhance the realism of your models through several techniques: LiDAR Integration: Incorporating LiDAR data for accurate elevation information. Texturing: Adding textures to building surfaces to simulate real-world materials. Detailed Modeling: Creating more complex building geometries with varying roof shapes and architectural details. Vegetation: Adding trees and other vegetation to enhance the natural environment. Realistic Lighting: Configuring realistic lighting and shadows to create a more visually appealing scene.
What are the limitations of using OpenStreetMap data?
While OpenStreetMap is a valuable resource, it's important to be aware of its limitations: Data Completeness: OSM data is not always complete or up-to-date, especially in less developed areas. Data Accuracy: The accuracy of OSM data can vary depending on the quality of contributions from volunteers. Attribute Information: OSM data may lack detailed attribute information about buildings, such as building height or construction materials. These limitations can be mitigated by supplementing OSM data with other sources, such as LiDAR or aerial imagery.
What is pyvista?
Pyvista is a Python package toolkit for 3D plotting and mesh analysis. It is based on the Visualization Toolkit (VTK). With pyvista, you can analyze or create meshes of your data.

Most people like