Search code examples
openstreetmapshapefilegeopandasoverpass-api

How to visualize and convert retrieved OSM buildings data to shapefile?


I have retrieved data from OSM using this script:

import overpy
api = overpy.Overpass()
print('Obtaining Data...')
result = api.query("""
[out:json]
[timeout:25]
;
(
  relation
    ["building"]
    (51.909331730124,4.3151378631592,51.954898210091,4.405689239502);
  
    way
    ["building"]
    (51.909331730124,4.3151378631592,51.954898210091,4.405689239502);
);
out;
>;
out skel qt;
""")

I would like to visualize the data and save it as a shapefile before performing some spatial analysis. I cannot figure out how to convert/save it as a shapefile, can anyone offer any advice?

Thank you!


Solution

  • import osmnx as ox 
    import ast
    
    point = 'point coordinates'
    dist = 'distance in m'
    buildings = ox.geometries.geometries_from_point(point, {'building': True}, dist=dist)
    

    And convert to a geodataframe:

    buildings_save = buildings.applymap(lambda x: str(x) if isinstance(x, list) else x)