Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scale polygon using shapely?

Tags:

python

shapely

Im trying to scale one shape to a larger one, like this: enter image description here

I have an example here

poly_context = {'type': 'MULTIPOLYGON',
'coordinates': [[[[1, 2], [2, 1], [4, 3], [3, 4]]]]}
poly_shape = shapely.geometry.asShape(poly_context)
like image 440
P. Scotty Avatar asked Sep 12 '25 06:09

P. Scotty


1 Answers

If your polygon is not convex, the scale method may not give you the desired output. For example:

import geopandas as gpd
from shapely import Polygon
from shapely import affinity

vertices = [(0, 0), (1, 1), (2, 0.5), (2.5, 2), (0.5, 2.5)]

# Create the polygon
polygon = Polygon(vertices)

scaled_polygon = affinity.scale(polygon, xfact=1.2, yfact=1.2)
gdf = gpd.GeoDataFrame({'geometry': [scaled_polygon, polygon]})
gdf.plot(column='geometry')

enter image description here

So, maybe the desired method should be bufferinstead of scale. Example:

buffered_polygon = polygon.buffer(0.2, join_style=2)
gdf = gpd.GeoDataFrame({'geometry': [buffered_polygon, polygon]})
gdf.plot(column='geometry')

enter image description here

like image 90
Maurício Cordeiro Avatar answered Sep 13 '25 20:09

Maurício Cordeiro