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
#2
Why are you calling LocationCount without any arguments? Why, on line 51, are you calling that function and just throwing the return value away? Don't you want to instead print it?
Reply
#3
In CalculateDistance(), there should be no reason to convert the arguments to floats; they should be floats when passed into the function.

In LocationCount(), the FileName parameter isn't being used. No matter what file you declare, it will always pull from Mammal.txt. Plus, the function will always return 0 because animalList is instantiated but nothing is ever appended to it. You want to return LocationCount.
Reply
#4
I'm not sure if this is what you want, but the last lines probably should be something like this.

var = LocationCount("Mammal.txt", 10, 50.988056, -1.619444)
print("There are {} present within the given distance.", var)
Reply
#5
(Apr-23-2020, 07:36 PM)Jeff900 Wrote: I'm not sure if this is what you want, but the last lines probably should be something like this.

var = LocationCount("Mammal.txt", 10, 50.988056, -1.619444)
print("There are {} present within the given distance.", var)

The code works without error now. Thank you!

Do you know how to make the output, to a file called “output.kml”, in kml format the no. of animals found within the distance of the specified location? I know this function will be similar to LocationCount in its logic but I'm not quite sure how.

Many thanks.
Reply
#6
Good to hear that your code works without errors now! I'm not familiar with KML format, but I think that would be a whole new question to post anyway. There are a lot of similar question about output to files by the way. And if you can/want, please like my answer that resolved the problem :). I am getting a bit sad about my 0 reputation :p.
Reply


Forum Jump:

User Panel Messages

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