-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwildlife.py
More file actions
118 lines (92 loc) · 4.15 KB
/
wildlife.py
File metadata and controls
118 lines (92 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import requests
def get_species_list(coordinate, radius) -> list:
"""
Retrieves a list of species sightings within a given radius of a coordinate.
Args:
coordinate (dict): A dictionary containing the latitude and longitude of the coordinate.
radius (float): The radius in kilometers.
Returns:
list: A list of species sighting summaries.
"""
url = (f"https://apps.des.qld.gov.au/species/?op=getspecieslist&kingdom=animals"
f"&circle={coordinate['latitude']},{coordinate['longitude']},{radius}")
response = requests.get(url)
if response.status_code == 200:
data = response.json()
species_list = data.get("SpeciesSightingSummariesContainer", {}).get("SpeciesSightingSummary", [])
return species_list
return []
def get_surveys_by_species(coordinate, radius, taxonid) -> list:
"""
Retrieves a list of animal surveys in an area for a given species using the Queensland wildlife data API.
Args:
coordinate (dict): A dictionary containing latitude and longitude keys.
radius (int): The search radius in meters.
taxonid (int): The taxon ID of the species to search for.
Returns:
list: A list of survey dictionaries.
"""
url = "https://apps.des.qld.gov.au/species/"
params = {
"op": "getsurveysbyspecies",
"taxonid": taxonid,
"circle": f"{coordinate['latitude']},{coordinate['longitude']},{radius}"
}
response = requests.get(url, params=params)
response.raise_for_status()
data = response.json()
'''
if the data dictionary has a key "features", this line of code will return the value associated with that key. If there is no "features" key in the dictionary, it will return an empty list.'''
return data.get("features", [])
def test_get_species_list():
"""
Test function for the get_species_list function.
This function tests the get_species_list function by calling it with a set of coordinates and a radius.
It asserts that the returned species list is a list and has a length greater than 0.
It also prints a message indicating that the function works correctly.
Example usage:
- Uncomment the example usage code after reviewing the asserts.
- Replace the coordinate values with the desired latitude and longitude.
Args:
None
Returns:
None
"""
RADIUS = 100000 # 100 km radius
# Testing the get_species_list function
coord = {"latitude": -16.92, "longitude": 145.777}
species_list = get_species_list(coord, RADIUS)
assert isinstance(species_list, list)
assert len(species_list) > 0
print("Get species list function works correctly.")
# Example usage ( uncomment for testing after reviewing asserts)
coordinate = {"latitude": -27.4689682, "longitude": 153.0234991}
species_list = get_species_list(coordinate, RADIUS)
assert isinstance(species_list, list)
assert len(species_list) > 0
print("Get species list function works correctly.")
def test_get_surveys_by_species():
"""
Test function for the get_surveys_by_species function.
This function tests the get_surveys_by_species function by calling it with a set of coordinates, radius, and taxonid.
It asserts that the returned surveys list is a list and prints a message indicating that the function works correctly.
Example usage:
- Uncomment the example usage code after reviewing the asserts.
- Replace the coordinate values with the desired latitude and longitude.
- Replace the taxonid value with the actual taxon ID of the species to test.
Args:
None
Returns:
None
"""
coordinate = {"latitude": -27.4689682, "longitude": 153.0234991}
radius = 100000 # 100 km radius
taxonid = 12345 # Replace with the actual taxon ID of the species to test
surveys = get_surveys_by_species(coordinate, radius, taxonid)
assert isinstance(surveys, list)
assert len(surveys) >= 0 # verify that the list is not empty
print("get_surveys_by_species test passed.")
# Commented in order to avoid running the tests when importing the module
if __name__ == "__main__":
test_get_species_list()
test_get_surveys_by_species()