is_point_in_polygon_v1.md 1.8 KB

判断一个点是否在楼层的轮廓里

前置条件

楼层必须有轮廓, 且合法

处理逻辑

1. 楼层外轮廓数据只有一个由点组成的数组围成, 因此直接判断指定的点是否在点数组围成的多边形内即可

楼层轮廓格式示例


[
    {点坐标的XYZ信息},
    {点坐标的XYZ信息}...
]

函数

版本1

输入为浮点型
CREATE OR REPLACE FUNCTION "public"."is_point_in_polygon_v1"("x" float8, "y" float8, "json_poly" jsonb)
  RETURNS "pg_catalog"."bool" AS $BODY$
from matplotlib.path import Path
import json
try:
    poly = []
    polygon = json.loads(json_poly)
    l = len(polygon)
    points = (x, y)
    for i in range(l):
        pair = polygon[i]
        poly.append((pair["X"], pair["Y"]))

    p = Path(poly)
except Exception as e:
    return False
else:
    return p.contains_points([points], None, -0.0001)

$BODY$
  LANGUAGE plpython3u VOLATILE
  COST 100

版本2

输入为字符串类型
CREATE OR REPLACE FUNCTION "public"."is_point_in_polygon_v1_str"("x" varchar, "y" varchar, "json_poly" jsonb)
  RETURNS "pg_catalog"."bool" AS $BODY$
from matplotlib.path import Path
import json
try:
    poly = []
    polygon = json.loads(json_poly)
    l = len(polygon)
    points = (float(x), float(y))
    for i in range(l):
        pair = polygon[i]
        poly.append((pair["X"], pair["Y"]))

    p = Path(poly)
except Exception as e:
    return False
else:
    return p.contains_points([points], None, -0.0001)

$BODY$
  LANGUAGE plpython3u VOLATILE
  COST 100

输入

1. 点的x坐标
2. 点的y坐标
3. 楼层的轮廓

返回结果

true    成功
false   失败