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(
    columns = ['geometry'],
    crs = denmark.crs)

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

fig, ax = plt.subplots(figsize=(20,20))

# 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?

  • 1
    Have a look at the buffer method: shapely.readthedocs.io/en/latest/manual.html#object.buffer By playing with the cap_style or join_style arguments, you should be able to make a buffer around that box that still results in a square box (and not with rounded corners, which will be the default) – joris Apr 19 '19 at 11:11

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')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.