I have a homework assignment where I've been provided with a file about 10,000 lines in length. Each line has 3 elements, a species name, latitude and longitude. I need to write a function which returns the number of animals found within a specific distance of a given location, taking into account 4 perameters: the file name, distance, and the latitude and longitude of the location.
In an ideal world, I'd be able to go into the shell, and call the function with the file name, any distance and any longitude and latitude, and have the number of animals within the distance be calculated.
I've successfully managed to import the file, and I've been given samples of code to help calculate distance and also help convert the file to a list. Here's the code I've written so far:
def LocationCount(filename, distance, Lat1, Lon1):
FIn = open(filename, "r")
for Line in FIn:
def LineToList(Line):
Line = Line.rstrip()
FIn.close()
return Line.split("\t")
def CalculateDistance(Lat1, Lon1, Lat2, Lon2):
Lat1 = float(Lat1)
Lon1 = float(Lon1)
Lat2 = float(Lat2)
Lon2 = float(Lon2)
nDLat = (Lat1 - Lat2) * 0.017453293
nDLon = (Lon1 - Lon2) * 0.017453293
Lat1 = Lat1 * 0.017453293
Lat2 = Lat2 * 0.017453293
nA = (math.sin(nDLat/2) ** 2) + math.cos(Lat1) * math.cos(Lat2) * (math.sin(nDLon/2) ** 2 )
nC = 2 * math.atan2(math.sqrt(nA),math.sqrt( 1 - nA ))
nD = 6372.797 * nC
return nD