Search code examples
geopandasosmnx

Selecting buildings from multiple locations in osmnx python


I am new to python as well as in osmnx package.

Lets say I have two locations, islands: Nauro and Lakeba Island. What I want to achieve is to have a one dataframe (or geodataframe) which will include buildings from both islands. I have this code but it only prints the results, but do not unite them.

import osmnx as ox

import matplotlib.pyplot as plt

import geopandas as gpd

import pandas as pd

from pyproj import CRS

place_name = ['Nauru', 'Lakeba Island']

tags = {'building': True}

for i in place_name:
    print(ox.geometries_from_place(i, tags))

As you might notice, it only prints the results from both locations, what I want to do is to have a single dataframe (or preferably geodataframe) having all observations united.

Ideally, it would be also very beneficial if I would be able to add the column in this dataframe indicating the island name on every observation, to distinguish which building belongs to which island. In this example, this column would only have two unique values (Nauru or Lakeba Island).

I hope I made a clear example here. Let me know if it is not enough.


Solution

  • You need to load all dataframes you need, store them and then concatenate them together.

    import pandas as pd
    import osmnx as ox
    
    place_name = ['Nauru', 'Lakeba Island']
    
    tags = {'building': True}
    
    gdfs = []
    for i in place_name:
        gdf = ox.geometries_from_place(i, tags)
        gdf["island_name"] = i  # this adds a column with a name
        gdfs.append(gdf)
    
    gdf = pd.concat(gdfs)