Erster Test

Distance calculator.py 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # Programmierung 2
  2. # Pruefungvorleistung Aufgabe
  3. # Luftlinienberechner
  4. # Chen Yuan MAB14 4060920
  5. # 07.06.2017
  6. #------------------------------------------------------------------------
  7. # Import the modules
  8. import re # For the searching funktion
  9. import os
  10. import sys
  11. import logging # For outputing a log file
  12. from math import cos, sin, asin, sqrt, radians
  13. #------------------------------------------------------------------------
  14. # Using the logging module
  15. logging.basicConfig(level=logging.INFO, # Show all the loggings which have higher level than INFO
  16. filename='./log.txt', # The log will be saved in a new txt file called log
  17. filemode='w', # File right: write
  18. format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
  19. #------------------------------------------------------------------------
  20. # Print a cut-off rule
  21. def rule(): # Make it as a funktion so the lenth of all the
  22. print("-"*80) # rules and can be easily changed in the sametime
  23. #-------------------------------------------------------------------------
  24. # Search for the cities
  25. # For both start city and end city
  26. # Input city name return the line of the chosen city in the citylist
  27. def find_city ():
  28. citylist = [] # The cities you might choose
  29. citylist_PLZ = [] # The cities which have PLZ that you might choose
  30. cities = open("DE.tab",encoding="utf-8").readlines()
  31. logging.info('The positon file has been opened.') # Log information
  32. while len(citylist) == 0 : # Always ask for input if there is no city added to the citylist
  33. logging.info('There is no city has been added to the citylist yet.')# Log information
  34. city = input("Please give the name of the city.\n").upper()
  35. city = city.replace('Ä', 'AE') # Umlauts conversion
  36. city = city.replace('Ö', 'OE') # Umlauts conversion
  37. city = city.replace('Ü', 'UE') # Umlauts conversion
  38. city = city.replace('ß', 'SS') # Umlauts conversion
  39. print("You mean one of these cities?")
  40. print("Name PLZ")
  41. for line in cities :
  42. if re.search(city,line): # Searching the cityname in each line of the cities Daten
  43. citylist.append(line.split("\t")) # Add the line to citylist if there is cityname in the line
  44. i=0
  45. for j in range(len(citylist)):
  46. if len(citylist[j][7]) != 0: # Checking is there PLZ in the line
  47. print("%i. %s %s"%(i+1,citylist[j][3],citylist[j][7])) # Print all the cities which has the name and PLZ
  48. rule()
  49. citylist_PLZ.append(citylist[j]) # Add these cities into the citylist with PLZ
  50. i=i+1
  51. choice = int(input("Give the number before the city.\n")) - 1 # Asking for the number of the chosen city and give the number to the 'choice'
  52. city = citylist_PLZ[choice] # Give the line of the chosen city to the 'city'
  53. print("You have chosen[",city[3],"]\n") # Chosen city varification
  54. rule()
  55. print("\n")
  56. return city # Return the line of the chosen city
  57. #-------------------------------------------------------------------------
  58. # Calculation of the airline Distance
  59. # Input longitude and latitude of both cities return airline Distance
  60. def calculation(lon1, lat1, lon2, lat2):
  61. R = 6371 # Average EARTH R in km
  62. lat1 = radians(float(lat1)) # Trasformation of the latitude of the first ciry from degree system to radian system
  63. lon1 = radians(float(lon1)) # Trasformation of the longitude of the first ciry from degree system to radian system
  64. lat2 = radians(float(lat2)) # Trasformation of the latitude of the seconed ciry from degree system to radian system
  65. lon2 = radians(float(lon2)) # Trasformation of the longitude of the seconed ciry from degree system to radian system
  66. dlon = lon2-lon1 # The calculation
  67. dlat = lat2-lat1
  68. a = sin(dlat/2)**2 + cos(lat1)*cos(lat2)*sin(dlon/2)**2
  69. c = 2*asin(sqrt(a))
  70. return c*R # Return the result of the calculation
  71. #-------------------------------------------------------------------------
  72. # Main programm
  73. def main():
  74. print("\nStart City:\n")
  75. city1 = find_city () # Give the cityline to city1
  76. lon1 = city1[5] # Read the longitude
  77. lat1 = city1[4] # Read the latitude
  78. logging.info('The first city has been choosen.') # Log information
  79. print("End City:\n")
  80. city2 = find_city () # Give the ciryline to city2
  81. logging.info('The seconed city has been choosen.') # Log information
  82. lon2 = city2[5] # Read the longitude
  83. lat2 = city2[4] # Read the latitude
  84. L = round(calculation(lon1, lat1, lon2, lat2),2) # Rounding off the result and keep 2 number after the radix point
  85. print("The Distance between [{}] and [{}] is:{}".format(city1[3],city2[3],L)+'km\n\n')
  86. logging.info('The Distance has been calculated and printed.') # Log information
  87. rule()
  88. #------------------------------------------------------------------------
  89. # Asking for runnung the programm again
  90. print("Air Line Distance Calculator \nby Chen Yuan MAB14 4060920 \n\n")
  91. a=1
  92. while a == 1:
  93. main()
  94. a= int(input("Do you want to run the programm again?\n1. yes 2.no"))
  95. if (a == 1):
  96. logging.info('User has choosed to run the programm again.') # Log information
  97. print("[Finish!]")
  98. logging.info('User has choosed stop the programm.') # Log information
  99. #------------------------------------------------------------------------