# -*- coding: utf-8 -*- import json import psycopg2 from adjacent import calc_adjacent_relation def get_data(sql): record = [] try: connection = psycopg2.connect( database='postgres', user='postgres', password='123456', host='192.168.20.250', port='5432' ) cursor = connection.cursor() cursor.execute(sql) record = cursor.fetchall() except (Exception, psycopg2.Error) as error: print("Error while connecting to PostgreSQL", error) finally: if (connection): cursor.close() connection.close() print('PostgreSQL connection is closed') return record def loads(x): x['location'] = json.loads(x['location']) return x def loads_curve(x): x['curve'] = json.loads(x['curve']) return x if __name__ == '__main__': segment_sql = "SELECT * FROM revit.boundary_segment where model_id = '3af6d175c34e11e993ac85337be80696'" wall_sql = "SELECT * FROM revit.wall where model_id = '3af6d175c34e11e993ac85337be80696'" v_wall_sql = "SELECT * FROM revit.virtual_wall where model_id = '3af6d175c34e11e993ac85337be80696'" columns_sql = "SELECT * FROM revit.column where model_id = '3af6d175c34e11e993ac85337be80696'" segment_data = get_data(segment_sql) wall_data = get_data(wall_sql) v_wall_data = get_data(v_wall_sql) columns_data = get_data(columns_sql) SEGMENT_KEYS = [ 'id', 'model_id', 'belong', 'belong_type', 'curve', 'space_id', 'group_index', 'sequence', 'reference', 'revit_id', 'type', ] WALL_KEYS = [ 'id', 'model_id', 'level_id', 'width', 'location', 'outline', 'last_update', 'create_time', 'name', 'source_id', 'revit_id', 'type', ] V_WALL_KEYS = [ 'id', 'model_id', 'location', 'outline', 'last_update', 'create_time', 'name', 'source_id', 'revit_id', 'type', ] COLUMNS_KEYS = [ 'id', 'model_id', 'location', 'outline', 'bounding', 'last_update', 'create_time', 'name', 'source_id', 'revit_id', 'type', ] segment_data = [dict(zip(SEGMENT_KEYS, item)) for item in segment_data] segment_data = list(map(loads_curve, segment_data)) wall_data = [dict(zip(WALL_KEYS, item)) for item in wall_data] wall_data = list(map(loads, wall_data)) v_wall_data = [dict(zip(V_WALL_KEYS, item)) for item in v_wall_data] v_wall_data = list(map(loads, v_wall_data)) columns_data = [dict(zip(COLUMNS_KEYS, item)) for item in columns_data] columns_data = list(map(loads, columns_data)) test_result = calc_adjacent_relation( segments=segment_data, walls=wall_data, v_walls=v_wall_data, columns=columns_data ) for item in test_result: print('SideSpace:', item, type(item))