read_from_clustered_merged.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import csv
  2. def read(file):
  3. with open(file, "r") as f:
  4. reader = csv.reader(f, delimiter=";")
  5. result = []
  6. dict = {}
  7. for row in reader:
  8. ausrichtung = row[1]
  9. row3 = row[2]
  10. row3 = eval(row3)
  11. element = ""
  12. ymin = 100000000.0
  13. ymax = 0.0
  14. xmin = 100000000.0
  15. xmax = 0.0
  16. coords = []
  17. merged_elements = []
  18. length = 0
  19. for e in row3:
  20. length += len(e)
  21. for elem in row3:
  22. #print("start")
  23. #print(len(elem))
  24. if len(row3) == 1:
  25. element = elem[4]
  26. xmin = float(elem[0])
  27. ymin = float(elem[1])
  28. xmax = float(elem[2])
  29. ymax = float(elem[3])
  30. else:
  31. if isinstance(elem[0],list):
  32. merged_elements += elem
  33. #print(length, len(merged_elements))
  34. if len(merged_elements) < length: ####woher weiß ich die länge????
  35. #print("bb", len(merged_elements), len(elem))
  36. continue
  37. if int(ausrichtung) == 1:
  38. #print(elem)
  39. merged_elements = sorted(merged_elements, key=lambda k: [float(k[3])], reverse=True)
  40. #print(merged_elements)
  41. for elemt in merged_elements:
  42. #print(merged_elements)
  43. #print(elem)
  44. element += elemt[4] + " "
  45. if float(elemt[0]) < xmin:
  46. xmin = float(elemt[0])
  47. if float(elemt[1]) < ymin:
  48. ymin = float(elemt[1])
  49. if float(elemt[2]) > xmax:
  50. xmax = float(elemt[2])
  51. if float(elemt[3]) > ymax:
  52. ymax = float(elemt[3])
  53. else:
  54. element += elem[4] + " "
  55. if float(elem[0]) < xmin:
  56. xmin = float(elem[0])
  57. if float(elem[1]) < ymin:
  58. ymin = float(elem[1])
  59. if float(elem[2]) > xmax:
  60. xmax = float(elem[2])
  61. if float(elem[3]) > ymax:
  62. ymax = float(elem[3])
  63. #print(element)
  64. result.append(element)
  65. coords.append(xmin)
  66. coords.append(ymin)
  67. coords.append(xmax)
  68. coords.append(ymax)
  69. dict[element] = coords
  70. #print("\n")
  71. return dict