计算业务空间的面积 ## 前置条件 无 ## 处理方式 根据业务空间轮廓的格式, 以此计算内部每个小轮廓的面积, 加和在一起为最终结果 小轮廓面积计算: 计算首个子轮廓面积, 并减去随后所有子轮廓的面积. 结果为小轮廓面积 ## 实现方式 # 函数 ``` CREATE OR REPLACE FUNCTION "public"."calc_area_v3"("json_poly" jsonb) RETURNS "pg_catalog"."float8" AS $BODY$ from shapely.geometry import Polygon import json def meta_polygon_area(single_poly): try: poly_len = len(single_poly) poly = [] for i in range(poly_len): pair = single_poly[i] poly.append((pair["X"], pair["Y"])) p = Polygon(poly) return p.area except Exception as e: plpy.info(e) return 0.0 def get_area(polygons): length = len(polygons) if length == 0: return 0.0 total_area = 0.0 for j in range(length): polygon = polygons[j] single_area = meta_polygon_area(polygon) if single_area == 0.0: return 0.0 if j == 0: total_area = single_area else: total_area -= single_area return total_area try: polygon_list = json.loads(json_poly) total_len = len(polygon_list) area = 0.0 for index in range(total_len): single_area = get_area(polygon_list[index]) if single_area == 0: return None area += single_area return area except Exception as e: plpy.info(e) return None $BODY$ LANGUAGE plpython3u VOLATILE COST 100 ``` ## 输入 1. 业务空间的轮廓 ## 返回结果 面积 float8