SHARE
TWEET

biodiversity

a guest Mar 26th, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import math
  2.  
  3.     # CalculateDistance calculates the distance in kilometres (km) between two locations.
  4. # CalculateDistance takes four variables, the latitude and longitude of the first
  5. #   location and the latitude and longitude of the second location. It returns the
  6. #   distance in km between the two locations.
  7.  
  8. #Example
  9. #Distance between Exeter (50.7, -3.533333) and Newcastle (54.988056, -1.619444)
  10. #CalculateDistance (50.7, -3.533333, 54.988056, -1.619444) = 493.92 km
  11.  
  12. #Distance between London (51.508129, -0.128005) and New York (40.4521, 73.5911)
  13. #CalculateDistance(51.508129, -0.128005, 40.4521, 73.5911) = 5579.49 km
  14.  
  15. def CalculateDistance(Lat1, Lon1, Lat2, Lon2):
  16.  
  17.     Lat1 = float(Lat1)
  18.     Lon1 = float(Lon1)
  19.     Lat2 = float(Lat2)
  20.     Lon2 = float(Lon2)
  21.    
  22.     nDLat = (Lat1 - Lat2) * 0.017453293
  23.     nDLon = (Lon1 - Lon2) * 0.017453293
  24.  
  25.     Lat1 = Lat1 * 0.017453293
  26.     Lat2 = Lat2 * 0.017453293
  27.  
  28.     nA = (math.sin(nDLat/2) ** 2) + math.cos(Lat1) * math.cos(Lat2) * (math.sin(nDLon/2) ** 2 )
  29.     nC = 2 * math.atan2(math.sqrt(nA),math.sqrt( 1 - nA ))
  30.     nD = 6372.797 * nC
  31.  
  32.     return nD
  33.  
  34.  
  35. # LineToList takes a string and converts it to a list breaking it on the tabs.
  36. # new lines are remove
  37. def LineToList(Str):
  38.     Str = Str.rstrip()
  39.     return Str.split("\t")
  40.  
  41. def LocationCount(MammalFile):
  42.     FIn = open(MammalFile, "r")
  43.     def SeqList():
  44.         for Line in FIn:
  45.             Line = Line.rstrip()
  46.             SeqList.append(list)
  47.         FIn.close()
  48.         return SeqList
  49.     Counter = 0
  50.     for Line in range(len(MammalFile)):
  51.         line = LineToList(Line)
  52.         print(MammalFile[0], MammalFile[1], MammalFile[2])
  53.         if CalculateDistance(line[1], line[2], 54.988056, -1.619444) < 10:
  54.             Counter = Counter + 1
  55.     print(Counter)
  56.  
  57. MammalFile = LocationCount("Mammal.txt")
  58. print(MammalFile)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top