read_from_clustered_merged.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. if len(row3) == 1:
  23. element = elem[4]
  24. xmin = float(elem[0])
  25. ymin = float(elem[1])
  26. xmax = float(elem[2])
  27. ymax = float(elem[3])
  28. else:
  29. if isinstance(elem[0],list):
  30. merged_elements += elem
  31. if len(merged_elements) < length:
  32. continue
  33. if int(ausrichtung) == 1:
  34. merged_elements = sorted(merged_elements, key=lambda k: [float(k[3])], reverse=True)
  35. for elemt in merged_elements:
  36. element += elemt[4] + " "
  37. if float(elemt[0]) < xmin:
  38. xmin = float(elemt[0])
  39. if float(elemt[1]) < ymin:
  40. ymin = float(elemt[1])
  41. if float(elemt[2]) > xmax:
  42. xmax = float(elemt[2])
  43. if float(elemt[3]) > ymax:
  44. ymax = float(elemt[3])
  45. else:
  46. element += elem[4] + " "
  47. if float(elem[0]) < xmin:
  48. xmin = float(elem[0])
  49. if float(elem[1]) < ymin:
  50. ymin = float(elem[1])
  51. if float(elem[2]) > xmax:
  52. xmax = float(elem[2])
  53. if float(elem[3]) > ymax:
  54. ymax = float(elem[3])
  55. result.append(element)
  56. coords.append(xmin)
  57. coords.append(ymin)
  58. coords.append(xmax)
  59. coords.append(ymax)
  60. dict[element] = coords
  61. return dict