Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Biodiversity
#1
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:

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.
Reply


Messages In This Thread
Biodiversity - by annelewis - Apr-23-2020, 06:22 PM
RE: Biodiversity - by ndc85430 - Apr-23-2020, 06:29 PM
RE: Biodiversity - by stullis - Apr-23-2020, 07:20 PM
RE: Biodiversity - by Jeff900 - Apr-23-2020, 07:36 PM
RE: Biodiversity - by annelewis - Apr-24-2020, 01:09 PM
RE: Biodiversity - by Jeff900 - Apr-24-2020, 01:33 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020