I am trying to normalize the coordinate two polygon. However, after normalizing they just overlap with each other which is not supposed to happen. This is what i have tried.
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)
A bit similar to your previous question where you do the transformation relative to the bounds
of each single geometry (i.e Polygon) instead of the total_bounds
of the entire shapefile. A quick fix would be to remove bounds = geometry.bounds
and put bounds = polygon_gdf.total_bounds
right before the loop :