Liquidgenius
Liquidgenius

Reputation: 708

Combining OSMnx Multipolygons

What are best practices in generating a union of Multipolygons acquired as a group using OSMnx's gdf_from_places()?

In gboeing's 02-example-osm-to-shapefile.ipynb example, multiple shapefiles are downloaded from OSM to a geodataframe using the gdf_from_places() method. The geometry is stored as Multipolygons in a Geopanda's dataframe with each row representing a place.

# you can pass multiple queries with mixed types (dicts and strings)
mx_gt_tx = ox.gdf_from_places(queries=[{'country':'Mexico'}, 'Guatemala', {'state':'Texas'}])
mx_gt_tx = ox.project_gdf(mx_gt_tx)
fig, ax = ox.plot_shape(mx_gt_tx)

osmnx gdf_from_places example

In regards to the question, I've experimented with using Geopanda's GeoSeries.unary_union but wanted to know how others were accomplishing this programmatically in Python.

Current Process 2018

This method uses the Shapely function of unary_union (it would otherwise be mx_gt_tx["geometry"].unary_union through Geopandas as pointed out by @joris comment.

queries = [{'country':'Mexico'}, 'Guatemala', {'state':'Texas'}]

# buffer_dist is in meters
mx_gt_tx = ox.gdf_from_places(queries, gdf_name='region_mx_gt_tx')
mx_gt_tx

Imgur

# project the geometry to the appropriate UTM zone then plot it
mx_gt_tx = ox.project_gdf(mx_gt_tx)
fig, ax = ox.plot_shape(mx_gt_tx)

Imgur

# unary union through Geopandas
region_mx_gt_tx = gpd.GeoSeries(unary_union(mx_gt_tx["geometry"]))
region_mx_gt_tx.plot(color = 'blue')
plt.show()
print(region_mx_gt_tx )

Imgur

Upvotes: 1

Views: 1080

Answers (1)

gboeing
gboeing

Reputation: 6422

import osmnx as ox
gdf = ox.gdf_from_places(queries=[{'country':'Mexico'}, 'Guatemala', {'state':'Texas'}])
unified = gdf.unary_union

Upvotes: 2

Related Questions