test.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # -*- coding: utf-8 -*-
  2. import json
  3. import psycopg2
  4. from adjacent import calc_adjacent_relation
  5. def save_data(sql):
  6. record = []
  7. try:
  8. connection = psycopg2.connect(
  9. database='datacenter',
  10. user='postgres',
  11. password='123456',
  12. host='192.168.20.234',
  13. port='5432'
  14. )
  15. cursor = connection.cursor()
  16. print(sql)
  17. cursor.execute(sql)
  18. except (Exception, psycopg2.Error) as error:
  19. print("Error while connecting to PostgreSQL", error)
  20. finally:
  21. if (connection):
  22. cursor.close()
  23. connection.commit()
  24. connection.close()
  25. print('PostgreSQL connection is closed')
  26. return record
  27. def get_data(sql):
  28. record = []
  29. try:
  30. connection = psycopg2.connect(
  31. database='datacenter',
  32. user='postgres',
  33. password='123456',
  34. host='192.168.20.234',
  35. port='5432'
  36. )
  37. cursor = connection.cursor()
  38. print(sql)
  39. cursor.execute(sql)
  40. record = cursor.fetchall()
  41. except (Exception, psycopg2.Error) as error:
  42. print("Error while connecting to PostgreSQL", error)
  43. finally:
  44. if (connection):
  45. cursor.close()
  46. connection.close()
  47. print('PostgreSQL connection is closed')
  48. return record
  49. def loads(x):
  50. x['location'] = json.loads(x['location'])
  51. return x
  52. def loads_curve(x):
  53. x['curve'] = json.loads(x['curve'])
  54. return x
  55. if __name__ == '__main__':
  56. segment_sql = "SELECT * FROM revit.boundary_segment where model_id = '8544a3c3cd2611e99abc839db1015353'"
  57. wall_sql = "SELECT * FROM revit.wall where model_id = '8544a3c3cd2611e99abc839db1015353'"
  58. v_wall_sql = "SELECT * FROM revit.virtual_wall where model_id = '8544a3c3cd2611e99abc839db1015353'"
  59. columns_sql = "SELECT * FROM revit.column where model_id = '8544a3c3cd2611e99abc839db1015353'"
  60. segment_data = get_data(segment_sql)
  61. wall_data = get_data(wall_sql)
  62. v_wall_data = get_data(v_wall_sql)
  63. columns_data = get_data(columns_sql)
  64. SEGMENT_KEYS = [
  65. 'id',
  66. 'model_id',
  67. 'belong',
  68. 'belong_type',
  69. 'curve',
  70. 'space_id',
  71. 'group_index',
  72. 'sequence',
  73. 'reference',
  74. 'revit_id',
  75. 'type',
  76. ]
  77. WALL_KEYS = [
  78. 'id',
  79. 'model_id',
  80. 'level_id',
  81. 'width',
  82. 'location',
  83. 'outline',
  84. 'last_update',
  85. 'create_time',
  86. 'name',
  87. 'source_id',
  88. 'revit_id',
  89. 'type',
  90. ]
  91. V_WALL_KEYS = [
  92. 'id',
  93. 'model_id',
  94. 'location',
  95. 'outline',
  96. 'last_update',
  97. 'create_time',
  98. 'name',
  99. 'source_id',
  100. 'revit_id',
  101. 'type',
  102. ]
  103. COLUMNS_KEYS = [
  104. 'id',
  105. 'model_id',
  106. 'location',
  107. 'outline',
  108. 'bounding',
  109. 'last_update',
  110. 'create_time',
  111. 'name',
  112. 'source_id',
  113. 'revit_id',
  114. 'type',
  115. ]
  116. segment_data = [dict(zip(SEGMENT_KEYS, item)) for item in segment_data]
  117. segment_data = list(map(loads_curve, segment_data))
  118. wall_data = [dict(zip(WALL_KEYS, item)) for item in wall_data]
  119. wall_data = list(map(loads, wall_data))
  120. v_wall_data = [dict(zip(V_WALL_KEYS, item)) for item in v_wall_data]
  121. v_wall_data = list(map(loads, v_wall_data))
  122. columns_data = [dict(zip(COLUMNS_KEYS, item)) for item in columns_data]
  123. columns_data = list(map(loads, columns_data))
  124. test_result = calc_adjacent_relation(
  125. segments=segment_data,
  126. walls=wall_data,
  127. v_walls=v_wall_data,
  128. columns=columns_data
  129. )
  130. for item in test_result:
  131. print(item)