Нормализовать координату многоугольника в PythonPython

Программы на Python
Anonymous
Нормализовать координату многоугольника в Python

Сообщение Anonymous »

Я пытаюсь нормализовать многоугольник с двумя координатами. Однако после нормализации они просто накладываются друг на друга, чего не должно происходить.
Это то, что я пробовал.

Код: Выделить всё

import geopandas as gpd
from shapely.affinity import scale, translate
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.

: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)
length_polygon = polygon_gdf[polygon_gdf.columns[0]].count()
normalized_geometries = []
for i in range(length_polygon):
geometry = polygon_gdf.loc[i].geometry

# Assume 'geometry' is your initial Polygon object
bounds = geometry.bounds  # Returns a tuple (minx, miny, maxx, maxy)

# Calculate the range for x and y
x_range = bounds[2] - bounds[0]  # maxx - minx
y_range = bounds[3] - bounds[1]  # maxy - miny

# Normalize coordinates to the range [0, 1]
normalized_coords = [((x - bounds[0]) / x_range, (y - bounds[1]) / y_range) for x, y in geometry.exterior.coords]

# Create a new polygon with these normalized coordinates
normalized_polygon = Polygon(normalized_coords)
normalized_geometries.append(normalized_polygon)

polygon_gdf.geometry = normalized_geometries
print(polygon_gdf)
polygon_gdf.to_file(output_shapefile)

input_log = "input.shp"
output_log = "output.shp"

normalize_polygon(input_log, output_log)
Изображение
Изображение


Подробнее здесь: https://stackoverflow.com/questions/785 ... -in-python

Вернуться в «Python»