Browse Source

修改设备所在业务空间的bug, 完成设备所在楼层

jxing 5 years ago
parent
commit
b9d0012e73
2 changed files with 154 additions and 60 deletions
  1. 96 11
      datacenter/docs/relation_calc/Eq2Fl.md
  2. 58 49
      datacenter/docs/relation_calc/Eq2Sp.md

+ 96 - 11
datacenter/docs/relation_calc/Eq2Fl.md

@@ -1,17 +1,102 @@
 设备所在楼层
 ## 前置条件
-设备已经入库
-
-## 触发点
-数据中心楼层绑定模型文件管理楼层接口。
+    1. 设备有ModelId信息点
+    2. 楼层有ModelId信息点
+    3. 楼层的ModelId信息点等于设备的ModelId信息点
+    4. 设备没有所在楼层关系
+    5. 设备有Location(非必要)
+    6. 楼层有Outline(非必要)
+    
 
 ## 处理方式
-数据中心提供接口。入参(BIMID的前缀--冒号之前的内容,楼层ID)
-数据中心根据入参,以及楼层的outline和设备的location将设备所属关系填上。
+    1. 如果楼层有Outline, 设备有Location, 做点(Location)在多边形(Outline)内的判断, 在多边形内则认为有关系
+    2. 如果不满足条件1, 则判断ModelId信息点的值, 相等则认为有关系
+
+# 函数
+
+## 代码
+```
+-- 设备所在楼层
+create or replace function public.rel_eq2fl(project_id character varying) returns boolean
+as
+$$
+from matplotlib.path import Path
+import json
 
-## 实现方式
+def is_in_meta_polygon(point, single_poly, radius):
+    poly_len = len(single_poly)
+    poly = []
+    for i in range(poly_len):
+        pair = single_poly[i]
+        poly.append((pair["X"], pair["Y"]))
+    p = Path(poly)
+    return p.contains_points([point], None, radius)
+
+def is_point_in_polygon(x, y, json_poly):
+    try:
+        polygons = json.loads(json_poly)
+        total_len = len(polygons)
+        point_pair = (float(x), float(y))
+        if total_len == 0:
+            return False
+        for i in range(total_len):
+            polygon = polygons[i]
+            if(i == 0):
+                if not is_in_meta_polygon(point_pair, polygon, -0.001):
+                    return False
+            else:
+                if is_in_meta_polygon(point_pair, polygon, 0.001):
+                    return False
+        return True
+    except Exception as e:
+        return False
+    else:
+        return True
+try:
+    # 将下面对数据库的操作作为一个事务, 出异常则自动rollback
+    with plpy.subtransaction():
+        floor_plan = plpy.prepare("select id, outline, model_id from floor where project_id = $1 and model_id is not null", ["text"])
+        floors = floor_plan.execute([project_id])
+        equip_plan = plpy.prepare("select id, bim_location, model_id from equipment where project_id = $1 and model_id is not null", ["text"])
+        equips = equip_plan.execute([project_id])
+        for floor in floors:
+            for equip in equips:
+                outline = floor['outline']
+                location = equip['bim_location']
+                if outline == None or len(outline) == 0 or location == None or len(location) == 0:
+                    if floor['model_id'] == floor['model_id']:
+                        equip['floor_id'] = floor['id']
+                    else:
+                        continue
+                location = location.split(',')
+                try:
+                    if is_point_in_polygon(location[0], location[1], outline):
+                        equip['floor_id'] = floor['id']
+                except Exception as ex:
+                    continue
+        for equip in equips:
+            if equip.__contains__('floor_id'):
+                try:
+                    plpy.info("equip id {0}, floor id {1}".format(equip['id'], equip['floor_id']))
+                    plan = plpy.prepare("update equipment set floor_id = $1 where id = $2", ["text", "text"])
+                    plan.execute([equip['floor_id'], equip['id']])
+                except Exception as ex:
+                    plpy.info(ex)
+                    continue
+except Exception as e:
+    plpy.warning(e)
+    return False
+else:
+    return True
+$$
+LANGUAGE 'plpython3u' VOLATILE;
+
+
+select public.rel_eq2fl('Pj1101010015');
 ```
-UPDATE equipment SET "floor_id" = 
-	SELECT "id" FROM "floor" WHERE postgres.is_point_in_polygon_v1_str(X, Y, "outline")
-WHERE project_id='Pj1101010015' AND bim_id like 'Pj111111111%' AND floor_id is null
-```
+
+## 输入
+    1. 项目id
+## 返回结果
+    true    成功
+    false   失败

+ 58 - 49
datacenter/docs/relation_calc/Eq2Sp.md

@@ -20,25 +20,33 @@ import json
 
 def is_in_meta_polygon(point, single_poly, radius):
     poly_len = len(single_poly)
+    plpy.info(poly_len)
     poly = []
     for i in range(poly_len):
         pair = single_poly[i]
         poly.append((pair["X"], pair["Y"]))
+        plpy.info(pair["X"])
+
     p = Path(poly)
     return p.contains_points([point], None, radius)
 
 
 def is_in_polygon(point, polygons):
-    length = len(polygons)
-    for j in range(length):
+    polygons_length = len(polygons)
+    plpy.info(polygons_length)
+    if polygons_length == 0:
+        return False
+    for j in range(polygons_length):
         polygon = polygons[j]
         if j == 0:
-            if not is_in_meta_polygon(point_pair, polygon, -0.001):
+            if not is_in_meta_polygon(point, polygon, -0.001):
                 return False
         else:
-            if is_in_meta_polygon(point_pair, polygon, 0.001):
+            if is_in_meta_polygon(point, polygon, 0.001):
                 return False
     return True
+
+
 def is_point_in_polygon(x, y, json_poly):
     try:
         polygon_list = json.loads(json_poly)
@@ -49,55 +57,56 @@ def is_point_in_polygon(x, y, json_poly):
                 return True
         return False
     except Exception as e:
+        plpy.info(e)
         return False
-try:
-    # 将下面对数据库的操作作为一个事务, 出异常则自动rollback
-    input_table_list = tables.split(',')
-    output_table_list = out_tables.split(',')
-    #if len(input_table_list) > 0
-        #plpy.info(input_table_list[0])
-    with plpy.subtransaction():
-        for i in range(0, len(input_table_list)):
-            in_table_name = input_table_list[i]
-            out_table_name = output_table_list[i]
-            # 删除原来关系表中的数据
-            plan1 = plpy.prepare("delete from {0} where project_id = $1 and (sign = $2 or sign = $3)".format(out_table_name.strip()), ["text", "integer", "integer"])
-            plan1.execute([project_id, sign1, sign2])
-            # 计算关系
-            plan_floor = plpy.prepare("select id from floor where project_id = $1", ["text"])
-            floors = plan_floor.execute([project_id])
-            # 按楼层计算
-            for floor in floors:
-                floor_id = floor['id']
-                # 获取楼层下的设备
-                plan_equip = plpy.prepare("select id, bim_location from equipment where project_id = $1 and bim_location is not null and floor_id = $2", ["text", "text"])
-                equips = plan_equip.execute([project_id, floor_id])
-                if len(equips) == 0:
-                    continue
-                # 获取楼层下的业务空间
-                space_plan = plpy.prepare("select id, outline from {0} as sp where project_id = $1 and outline is not null and floor_id = $2".format(in_table_name), ["text", "text"])
-                spaces = space_plan.execute([project_id, floor_id])
-                if len(spaces) == 0:
-                    continue
-                # 判断设备的bim_location是否在业务空间的outline内
-                for equip in equips:
-                    for space in spaces:
-                        try:
-                            location = equip['bim_location'].split(',')
-                            if is_point_in_polygon(location[0], location[1], space['outline']):
-                                # 设备在业务空间内, 添加关系
-                                insert_plan = plpy.prepare("insert into {0}(equip_id, space_id, project_id, sign) values($1, $2, $3, 2) ".format(out_table_name.strip()), ["text", "text", 'text'])
-                                insert_plan.execute([equip['id'], space['id'], project_id])
-                        except Exception as ex:
-                            continue
-except Exception as e:
-    plpy.warning(e)
-    return False
-else:
-    return True
+
+
+# 将下面对数据库的操作作为一个事务, 出异常则自动rollback
+input_table_list = tables.split(',')
+output_table_list = out_tables.split(',')
+#if len(input_table_list) > 0
+    #plpy.info(input_table_list[0])
+with plpy.subtransaction():
+    for i in range(0, len(input_table_list)):
+        in_table_name = input_table_list[i]
+        out_table_name = output_table_list[i]
+        # 删除原来关系表中的数据
+        plan1 = plpy.prepare("delete from {0} where project_id = $1 and (sign = $2 or sign = $3)".format(out_table_name.strip()), ["text", "integer", "integer"])
+        plan1.execute([project_id, sign1, sign2])
+        # 计算关系
+        plan_floor = plpy.prepare("select id from floor where project_id = $1", ["text"])
+        floors = plan_floor.execute([project_id])
+        # 按楼层计算
+        for floor in floors:
+            floor_id = floor['id']
+            # 获取楼层下的设备
+            plan_equip = plpy.prepare("select id, bim_location from equipment where project_id = $1 and bim_location is not null and floor_id = $2", ["text", "text"])
+            equips = plan_equip.execute([project_id, floor_id])
+            if len(equips) == 0:
+                continue
+            # 获取楼层下的业务空间
+            space_plan = plpy.prepare("select id, outline from {0} as sp where project_id = $1 and outline is not null and floor_id = $2".format(in_table_name), ["text", "text"])
+            spaces = space_plan.execute([project_id, floor_id])
+            if len(spaces) == 0:
+                continue
+            # 判断设备的bim_location是否在业务空间的outline内
+            # plpy.info("设备有{0}个, 业务空间有{1}个".format(len(equips), len(spaces)))
+            for equip in equips:
+                for space in spaces:
+                    try:
+                        location = equip['bim_location'].split(',')
+                        if is_point_in_polygon(location[0], location[1], space['outline']):
+                            # 设备在业务空间内, 添加关系
+                            insert_plan = plpy.prepare("insert into {0}(equip_id, space_id, project_id, sign) values($1, $2, $3, 2) ".format(out_table_name.strip()), ["text", "text", 'text'])
+                            insert_plan.execute([equip['id'], space['id'], project_id])
+                    except Exception as ex:
+                        continue
+return True
 $$
 LANGUAGE 'plpython3u' VOLATILE;
 
+
+例子:
 select public.rel_eq2sp('zone_general,zone_lighting', 'r_eq_in_sp_zone_general,r_eq_in_sp_zone_lighting', 'Pj1101010015', 2, 2);
 ```
 ## 输入