Browse Source

mxg:add is_source param to sys_direction calc

mengxiangge 5 years ago
parent
commit
f6d74e948c

+ 0 - 0
src/space_relation/__init__.py


+ 97 - 0
src/space_relation/calc_spaceadjoinrelation.py

@@ -0,0 +1,97 @@
+from relations import utils
+from shapely.geometry import Polygon
+def calc_plane(space_datas):
+	'''Calc plane business space adjoin relation.
+	:param floor_id:
+	:param space_type:
+	:return:
+	'''
+	relationcollection = utils.BinaryRelationCollection()
+	for i in range(len(space_datas)):
+		for j in range(i+1,len(space_datas)):
+			relations=calc_adjoinspace(space_datas[i],space_datas[j])
+			relationcollection.extend(relations)
+	return relationcollection
+
+def calc_verticalface(space_datas1,space_datas2):
+	'''Calc vertical business space adjoin relaiton.
+	:param floor_id1:
+	:param floor_id2:
+	:param space_type:
+	:return:
+	'''
+	relationcollection = utils.BinaryRelationCollection()
+	for s1 in space_datas1:
+		for s2 in space_datas2:
+			relations = calc_adjoinspace(s1, s2)
+			relationcollection.extend(relations)
+	return relationcollection
+
+def calc_adjoinspace(space,target_spaces):
+	'''Judge business space adjoin to other target_spaces.
+	:param space:
+	:param target_spaces:
+	:return:
+	'''
+	relationcollection=utils.BinaryRelationCollection()
+	for target in target_spaces:
+		if(judge_space(space,target)):
+			relationitem=utils.BinaryRelationItem(space.id,target.id)
+			relationcollection.update(relationitem)
+	return relationcollection
+def judge_space(space,target_space):
+	'''Judge business space adjoin relation.
+	business space may contain more than one basespace
+	:param space:
+	:param target_space:
+	:return:
+	'''
+	for b1 in space.outline2:
+		for b2 in target_space.outline2:
+			# find one,break
+			if(judge_space(b1,b2)):
+				return True
+	return False
+def judge_bspace(b1,b2):
+	'''Judge base space b1,b2 adjoin relation.
+	1. b1 outer polygon intersect b2 out polygon;
+	2. if b1 outer contain b2 outer,judge whether b1 inner contain b2, if true not adjoin,break;
+	3. exchange b1 b2.perform to step2;
+	:param b1:
+	:param b2:
+	:return:
+	'''
+	if(len(b1)<1 or len(b2)<1):
+		return False
+	p1_outer=convert2polygon(b1[0])
+	p2_outer=convert2polygon(b2[0])
+	# judge intersect.
+	result=p1_outer.intersects(p2_outer)
+	if(result):
+		# judge contain,outer contain and inner not contain.
+		result=judge_bspace_contain(p1_outer,p2_outer,b1)
+		if(result):
+			result=judge_bspace_contain(p2_outer, p1_outer, b2)
+	return result
+def judge_bspace_contain(p1_outer,p2_outer,b1):
+	'''Intersect space ,calc contain  relation .
+	:param p1_outer:big outer
+	:param p2_outer:small outer
+	:param b1:big space outline
+	:return:
+	'''
+	result=True
+	if (p1_outer.contains(p2_outer)):
+		for i in range(1, len(b1)):
+			inner = b1[i]
+			p_inner = convert2polygon(inner)
+			if (p_inner.contains(p2_outer)):
+				result = False
+				break
+	return result
+def convert2polygon(p):
+	coords =[]
+	for xyz in p:
+		coords.append((xyz.X,xyz.Y))
+	poly = Polygon(coords)
+	return poly

+ 32 - 0
src/space_relation/space_datautils.py

@@ -0,0 +1,32 @@
+'''Get space from database.
+
+'''
+from relations import test
+def get_space_data(floor_id,space_type):
+	'''Cache space from database by floor_id,space_type.
+	:param floor_id:
+	:param space_type:
+	:return:
+	'''
+	sql = "select " \
+		  "id,name,local_id,local_name,floor_id,outline,object_type" \
+		  "from zone_space_base " \
+		  "where floor_id='%s' " \
+		  "and object_type='%s' " \
+		  % (floor_id, space_type)
+
+	COMPONENT_KEYS = [
+		'id',
+		'name',
+		'local_id',
+		'local_name',
+		'floor_id',
+		'outline',
+		'object_type'
+	]
+	data = test.get_data(sql)
+	element_data = []
+	element_data.extend(utils.get_dicdata(data, COMPONENT_KEYS))
+	element_data = list(map(utils.dic2obj, element_data))
+	objects = utils.sqldata2objlist(element_data)
+	return objects

+ 14 - 0
src/space_relation/test.py

@@ -0,0 +1,14 @@
+import sys
+sys.path.append("../../..")
+from relations.src.space_relation import space_datautils, calc_spaceadjoinrelation
+
+if __name__=='__main__':
+	floor_id=''
+	object_type=''
+	floor_id2 = ''
+	spaces=space_datautils.get_space_data(floor_id,object_type)
+	spaces2 = space_datautils.get_space_data(floor_id2, object_type)
+	calc_spaceadjoinrelation.calc_plane(spaces)
+	calc_spaceadjoinrelation.calc_verticalface(spaces,spaces2)
+
+

+ 41 - 0
utils.py

@@ -37,3 +37,44 @@ class BinaryRelationCollection(object):
 
     def __iter__(self):
         return iter(self.__m_Dic)
+
+
+from relations import test
+
+
+def dic2obj(d):
+    """Convert Dict to Object.
+    """
+    top = type('new', (object,), d)
+    seqs = tuple, list, set, frozenset
+    for i, j in d.items():
+        j0 = j
+        try:
+            j0 = json.loads(j0)
+        except Exception as e:
+            pass
+        if isinstance(j0, dict):
+            setattr(top, i, dic2obj(j0))
+        elif isinstance(j0, seqs):
+            setattr(top, i,
+                    type(j0)(dic2obj(sj) if isinstance(sj, dict) else sj for sj in j0))
+        else:
+            setattr(top, i, j0)
+    return top
+
+def sqldata2objlist(data):
+    '''Convert Sqldata to Object list.
+    '''
+    return list(map(dic2obj,data))
+
+
+def get_dicdata(data,dic):
+    """
+    Request db for data through sql.
+    Convert data to dict.
+    :param sql:
+    :param dic:
+    :return:
+    """
+    dic_data=[dict(zip(dic,item)) for item in data]
+    return list(dic_data)