| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- from shapely.geometry import Polygon, Point
- # 你提供的原始坐标点
- points_raw = [
- [
- 484,
- 2686
- ],
- [
- 484,
- 2693
- ],
- [
- 485,
- 2694
- ],
- [
- 485,
- 2697
- ],
- [
- 486,
- 2698
- ],
- [
- 486,
- 2702
- ],
- [
- 487,
- 2703
- ],
- [
- 487,
- 2705
- ],
- [
- 488,
- 2706
- ],
- [
- 488,
- 2709
- ],
- [
- 489,
- 2710
- ],
- [
- 489,
- 2714
- ],
- [
- 490,
- 2715
- ],
- [
- 490,
- 2717
- ],
- [
- 491,
- 2718
- ],
- [
- 491,
- 2722
- ],
- [
- 492,
- 2723
- ],
- [
- 492,
- 2725
- ],
- [
- 499,
- 2732
- ],
- [
- 500,
- 2732
- ],
- [
- 500,
- 2731
- ],
- [
- 501,
- 2730
- ],
- [
- 501,
- 2729
- ],
- [
- 502,
- 2728
- ],
- [
- 502,
- 2711
- ],
- [
- 501,
- 2710
- ],
- [
- 501,
- 2709
- ],
- [
- 500,
- 2708
- ],
- [
- 500,
- 2707
- ],
- [
- 499,
- 2706
- ],
- [
- 499,
- 2705
- ],
- [
- 498,
- 2704
- ],
- [
- 498,
- 2703
- ],
- [
- 497,
- 2702
- ],
- [
- 497,
- 2699
- ],
- [
- 496,
- 2698
- ],
- [
- 496,
- 2696
- ],
- [
- 495,
- 2695
- ],
- [
- 495,
- 2693
- ],
- [
- 494,
- 2692
- ],
- [
- 494,
- 2691
- ],
- [
- 491,
- 2688
- ],
- [
- 488,
- 2688
- ],
- [
- 487,
- 2687
- ],
- [
- 485,
- 2687
- ]
- ]
- # 1. 创建一个 Shapely Polygon 对象
- original_polygon = Polygon(points_raw)
- print(f"原始点数量: {len(points_raw)}")
- print(f"原始多边形面积: {original_polygon.area:.2f}")
- print("-" * 30)
- # 2. 使用 .simplify() 方法进行简化
- # tolerance 参数就是 RDP 算法的阈值(epsilon)
- # 它决定了简化的程度,单位与你的坐标单位相同。
- # 你需要根据实际情况调整这个值。
- # 尝试一个较小的 tolerance
- tolerance_small = 0.8
- simplified_polygon_small = original_polygon.simplify(tolerance_small, preserve_topology=True)
- points_small = list(simplified_polygon_small.exterior.coords)
- print(f"使用 tolerance = {tolerance_small}")
- print(f"简化后点数量: {len(points_small)}")
- print(f"简化后多边形面积: {simplified_polygon_small.area:.2f}")
- # 将点坐标转换为整数列表
- points_small_int = [[int(x), int(y)] for x, y in points_small]
- print("简化后的点:", points_small_int)
- print("-" * 30)
- # 尝试一个较大的 tolerance
- tolerance_large = 2.0
- simplified_polygon_large = original_polygon.simplify(tolerance_large, preserve_topology=True)
- points_large = list(simplified_polygon_large.exterior.coords)
- print(f"使用 tolerance = {tolerance_large}")
- print(f"简化后点数量: {len(points_large)}")
- print(f"简化后多边形面积: {simplified_polygon_large.area:.2f}")
- # 将点坐标转换为整数列表
- points_large_int = [[int(x), int(y)] for x, y in points_large]
- print("简化后的点:", points_large_int)
- print("-" * 30)
|