[GIS] Making Shapely box larger using GeoPandas

geopandaspythonshapely

I'm creating a box that covers the the area of a GeoPandas GeoDataFrame,
so that I can use it as an inverted map to later cover up unwanted spill-over data.

import geopandas as gpd
import geoplot as gplt
from shapely.geometry import box

denmark = world[world.name == 'Denmark']
denmark_box = gpd.GeoDataFrame(
    [box(*denmark.total_bounds)],
    columns = ['geometry'],
    geometry='geometry',
    crs = denmark.crs)

denmark_cover = gpd.tools.overlay(denmark, w3, how="symmetric_difference")

fig, ax = plt.subplots(figsize=(20,20))
ax.set_aspect('equal')

# shp is some other data
# that plots a bit over the country borders
shp.plot(ax=ax, column='sum', edgecolor='none', cmap='hot', alpha=1, legend=True)

denmark.plot(ax=ax, color="none", edgecolor='white', facecolor="None")
denmark_cover.plot(ax=ax, edgecolor='white', alpha=1)

I need to make the box about 5% bigger to cover all unwanted data.

What is a good aproach to make a shapely box bigger?

Best Answer

Use shapely.affinity.scale:

Returns a scaled geometry, scaled by factors along each dimension.

Adjust scaling factors and try:

denmark_box.geometry.iloc[0] = shapely.affinity.scale(denmark_box.geometry.iloc[0], xfact=1.05, yfact=1.05, origin='center')
Related Question