Extracting building Geojson from Open street map code

Assaad MOAWAD
1 min readJan 6, 2024

As part of a side project i am working on, i decided to extract building data from open street map. Here is the code:

First install the necessary libraries:

pip install osmnx geopandas matplotlib

Then try the following code:

import osmnx as ox
import geopandas as gpd
import matplotlib.pyplot as plt

place_name = "Lebanon"
# Get place boundary related to the place name as a geodataframe
area = ox.geocode_to_gdf(place_name)

# List key-value pairs for tags
tags = {'building': True}

buildings = ox.features_from_place(place_name, tags)
buildings = buildings.drop('nodes',axis=1)
buildings = buildings.drop('building',axis=1)

# Get the centroid of the buildings
buildings["centroid_lat"] = buildings["geometry"].centroid.y
buildings["centroid_lng"] = buildings["geometry"].centroid.x

#calculate the area of the buildings
buildings["area"] = buildings["geometry"].to_crs({'proj':'cea'}).area

# buildings = buildings.drop('geometry',axis=1)
print(buildings)
buildings.to_csv('Lebanon.csv')

#Calculate the sum of areas
sum = buildings["area"].sum()
count = buildings.shape[0]
print("Total buildings: " + str(count) + " Sum of building areas: "+ str(sum) +" sqm or "+ str(sum/10**6)+" km2, avg: "+ str(sum/count)+ " sqm")

#Display histogram
quantile = 0.99
trimmed_data = buildings[(buildings["area"] > buildings["area"].quantile(1-quantile)) & (buildings["area"] < buildings["area"].quantile(quantile))]

plt.hist(trimmed_data["area"], bins=100)
plt.show()

For Lebanon here is the result:

Total buildings: 565568 Sum of building areas: 130440919.4740112 sqm or 130.4409194740112 km2, avg: 230.63702238105975 sqm

Building area histogram distribution

--

--

Assaad MOAWAD

Interested in artificial intelligence, machine learning, neural networks, data science, blockchain, technology, astronomy. Co-founder of Datathings, Luxembourg