【问题标题】:How to scale polygon using shapely?如何使用 shapely 缩放多边形?
【发布时间】:2023-02-17 20:07:19
【问题描述】:

我试图将一种形状缩放为更大的形状,如下所示:

我这里有一个例子

poly_context = {'type': 'MULTIPOLYGON',
'coordinates': [[[[1, 2], [2, 1], [4, 3], [3, 4]]]]}
poly_shape = shapely.geometry.asShape(poly_context)

【问题讨论】:

  • 缩放是一种仿射变换,因此您可以使用shapely.affinity.scale()
  • affinity.scale 只能按比例缩放多边形,我想按固定标量放大它。
  • 要使其成为固定标量,只需将 xfactyfact 参数设置为相同的值即可。
  • @martineau,你应该把这个作为答案

标签: python shapely


【解决方案1】:

如果您的多边形不是凸面,scale 方法可能无法为您提供所需的输出。例如:

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

因此,也许所需的方法应该是buffer而不是scale。 例子:

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

【讨论】:

    猜你喜欢
    • 2020-09-07
    • 1970-01-01
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    • 1970-01-01
    • 2019-08-26
    • 2014-03-16
    相关资源
    最近更新 更多