You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中多边形坐标归一化后重叠问题求助

多边形归一化后重叠的问题修复

你当前的代码逻辑是对每个多边形单独基于自身的边界做归一化,这会导致所有多边形都被缩放到[0,1]的坐标区间内,必然出现重叠。要保持多边形之间的相对位置,应该基于整个图层的全局边界来做统一归一化。

解决方案

修改代码,先计算所有多边形的整体边界,再用这个全局边界对每个多边形进行坐标转换,这样既能把所有多边形缩放到[0,1]范围,又能保留它们原有的相对位置。

修改后的完整代码

import geopandas as gpd
from shapely.geometry import Polygon

def normalize_polygon(polygon_shapefile, output_shapefile):
    """
    Normalizes the coordinates of the polygons in a shapefile to fit within a 0 to 1 range,
    while preserving their relative positions.

    :param polygon_shapefile: Path to the input polygon shapefile.
    :param output_shapefile: Path for the output shapefile with the normalized polygons.
    """
    
    polygon_gdf = gpd.read_file(polygon_shapefile)
    
    # 计算整个图层的全局边界,而非单个多边形的边界
    global_bounds = polygon_gdf.total_bounds  # 返回 (minx, miny, maxx, maxy)
    global_minx, global_miny, global_maxx, global_maxy = global_bounds
    
    # 计算全局的x、y坐标范围
    x_range = global_maxx - global_minx
    y_range = global_maxy - global_miny

    normalized_geometries = []
    for geometry in polygon_gdf.geometry:
        # 使用全局边界参数对每个多边形的坐标做归一化
        normalized_coords = [
            ((x - global_minx) / x_range, (y - global_miny) / y_range) 
            for x, y in geometry.exterior.coords
        ]
        normalized_polygon = Polygon(normalized_coords)
        normalized_geometries.append(normalized_polygon)
    
    polygon_gdf.geometry = normalized_geometries
    polygon_gdf.to_file(output_shapefile)
    
input_log = "input.shp"
output_log = "output.shp"

normalize_polygon(input_log, output_log)

关键修改说明

  • 用total_bounds获取整个数据集的全局边界,确保归一化后所有多边形的相对空间位置不变
  • 移除了原代码中冗余的length_polygon计算,直接遍历几何列更简洁高效
  • 如果你的需求是单个多边形独立归一化(比如用于单图形特征提取),那重叠是预期结果;但需要保留空间关系时,必须使用全局边界归一化

内容的提问来源于stack exchange,提问作者Purple_Ad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 05:42:20