Data from OSM (overpy) to geodataframe with polygons

I try to put OSM data (some polygons) to geodataframe. Export from OSM contains LineString. But in the end i need to converte all data into geodataframe in this format:

0 → name_from_tag_first_area → polygon (or multipolygon) type with coordinates

1 → name_from_tag_second_area → polygon (or multipolygon) type with coordinates

And then i will use this GeoDataFrame to visualize this polygons.

import overpy
import requests
import json
import geopandas as gpd

from shapely.geometry import shape

url = "https://maps.mail.ru/osm/tools/overpass/api/interpreter"
query = """[out:json];
area['boundary' = 'administrative']['name' = 'Москва'] -> .MSK;
(
relation(area.MSK)['admin_level' = 8]['boundary' = 'administrative']['name'='Бескудниковский район'];
relation(area.MSK)['admin_level' = 8]['boundary' = 'administrative']['name'='район Восточное Дегунино'];
);
convert item ::=::,::geom=geom(),_osm_type=type();
out geom;"""
response = requests.get(url, params={'data': query})
data = response.json()

geo_df = gpd.GeoDataFrame(data['elements'])

wrong result: https://i.stack.imgur.com/J2J85offset-smoker-in-1000-dollar

In my dataframe not a polygons - only geometrycollection with LineString. Please could you explain how I can do this taskю Thanks.

I haven’t looked into this in more detail, just wanted to make you aware that the convert statement creates some Overpass API specific output format where the geometry fields contains some GeoJSON-like format. I’d be very surprised if geopandas understands this format out of the box.

My recommendation is to download the query results to a file, then look at some examples which can be successfully processed by GeoDataFrame, and finally do some mapping between those two formats.