Apr-23-2020, 06:22 PM
Hello. I need help with this piece of work.
Find the number of animals at a given distance from a specified location. Write a function LocationCount which returns the number of animals found within a specified distance of a given location, the function should take four parameters, a file name containing the data, a distance in kilometres, and the latitude and longitude of the location. It should return the number of animals found within the specified distance of the location. Your script should call LocationCount with the data file “Mammal.txt”, a distance of 10.0, a latitude of 54.988056 and longitude of -1.619444 (Newcastle). The result should be printed to the screen.
This is what I have so far:
Find the number of animals at a given distance from a specified location. Write a function LocationCount which returns the number of animals found within a specified distance of a given location, the function should take four parameters, a file name containing the data, a distance in kilometres, and the latitude and longitude of the location. It should return the number of animals found within the specified distance of the location. Your script should call LocationCount with the data file “Mammal.txt”, a distance of 10.0, a latitude of 54.988056 and longitude of -1.619444 (Newcastle). The result should be printed to the screen.
This is what I have so far:
import math def LineToList(FileName): FileIn = open(FileName, "r") DataList = [] for Line in FileIn: Line = Line.rstrip().split("\t") DataList.append(Line) FileIn.close() return DataList 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 def LocationCount(FileName, dist, Lat, Lon): """ Input: FileName = a file name containing animal location data (str) dist = a distance in kilometres (float) Lat = Latitude (float) Lon = Longitude (float) Output: returns number of animals found in the area (int) """ DataList = LineToList("Mammal.txt") LocationCount = 0 animalList = list() for Line in DataList: if CalculateDistance(Line[1], Line[2], 54.988056, -1.619444)<=10: LocationCount += 1 return len(animalList) LocationCount("Mammal.txt", 10, 50.988056, -1.619444) print("There are {} present within the given distance.", LocationCount())But there's an error:
Error:Traceback (most recent call last):
File "<LocationCount.py>", line 52, in <module>
print("There are {} present within the given distance.", LocationCount())
TypeError: LocationCount() missing 4 required positional arguments: 'FileName', 'dist', 'Lat', and 'Lon'
Can I get help for this? Thank you.