DPM
DPM

Reputation: 935

Can't select Continental Portugal when using OSMNX

The issue I am having is the following: Portugal is a country next to Spain and it also has some islands. I want to select only Continental Portugal, what I mean by this is just the part of Portugal next to Spain and not include the islands.

Can you help me please?

Thank you for the attention.

Upvotes: 1

Views: 92

Answers (1)

swiss_knight
swiss_knight

Reputation: 7781

What you have probably done to download your data from OpenStreetMap using OSMNX is this:

import osmnx as ox
import geopandas as gpd
region = {'country':'Portugal'}
gdf = ox.gdf_from_place(region)
fig, ax = ox.plot_shape(gdf, figsize=(7,7))

which results in:

Portugal

namely, the continental part of Portugal + its islands, the Azores.

As the continental part is the largest, you can filter the other out using shapely or GeoPandas for example.


But first, let's explore the data, here's gdf:

Initial gdf content

It's a multi-polygon. So, we need to explode it, e.g. according to this:

exploded_gdf = gdf.explode()

I am using GeoPandas version '0.7.0', this may not work with older versions.


Let's explore this new geometry:

Exploded geometry

As the continental part is probably the largest, you can compute and sort them by their area:

exploded_gdf['area'] = exploded_gdf.area
exploded_gdf.sort_values(by='area', inplace=True)
exploded_gdf

Portugal areas

And finally take the largest, here the last one, hence the index -1:

# Extract the shapely underlying geometry:
continental_part = exploded_gdf.iloc[-1]['geometry']

Continental part

Upvotes: 1

Related Questions