Hi everyone,
I’m not very good at Python development, but I have to admit that with the help of today’s conversational engines, it’s pretty easy to get things done.
For a specific need, I need to export a list of my observations according to certain criteria, in XLSX format.
The code below works perfectly, but I’d like to add a ‘Family’ column (to sort the butterflies by family), and I haven’t managed to do so (I should rather say, ‘the conversational engine hasn’t managed to do so).
Do you think this is possible?
import requests
import pandas as pd
# Function to get taxon information by its ID
def get_taxon_info(taxon_id):
url = f"https://api.inaturalist.org/v1/taxa/{taxon_id}"
response = requests.get(url)
if response.status_code != 200:
raise Exception(f"Error fetching taxon data: {response.status_code}")
return response.json()
# Define parameters
observer = "sylvainm_53"
taxon_id = 47157 # Taxon ID for Lepidoptera
start_date = "2024-07-19"
end_date = "2024-07-20"
# URL of iNaturalist API
url = "https://api.inaturalist.org/v1/observations"
# Query parameters
params = {
"user_id": observer,
"taxon_id": taxon_id,
"d1": start_date,
"d2": end_date,
"per_page": 200, # Maximum number of observations per page
"page": 1
}
# Make the request and get the data
response = requests.get(url, params=params)
data = response.json()
# Check if the request was successful
if response.status_code != 200:
raise Exception(f"Error in request: {response.status_code}")
# Initialize a list to store observations
observations = []
# Iterate through the pages of results
while True:
for result in data["results"]:
# Extract taxon ID
taxon_id = result.get("taxon", {}).get("id")
family_name = None
# Get taxon information
if taxon_id:
taxon_info = get_taxon_info(taxon_id)
# Search for family among ancestors
ancestors = taxon_info.get("taxon", {}).get("ancestors", [])
for ancestor in ancestors:
if ancestor.get("rank") == "family":
family_name = ancestor.get("name")
break
# Prepare the observation record
observation = {
"id": result["id"],
"species_guess": result["species_guess"],
"observed_on": result["observed_on"],
"place_guess": result["place_guess"],
"latitude": result["geojson"]["coordinates"][1] if result.get("geojson") else None,
"longitude": result["geojson"]["coordinates"][0] if result.get("geojson") else None,
"user_login": result["user"]["login"],
"taxon_name": result["taxon"]["name"] if result.get("taxon") else None,
"family_name": family_name
}
observations.append(observation)
# Check if there is another page
if data["total_results"] > params["page"] * params["per_page"]:
params["page"] += 1
response = requests.get(url, params=params)
data = response.json()
else:
break
# Convert the observations to a pandas DataFrame
df = pd.DataFrame(observations)
# Export the observations to an Excel file
excel_file = "observations_inaturalist.xlsx"
df.to_excel(excel_file, index=False, encoding="utf-8")
print(f"Observations exported to {excel_file}")
Thanks for your help!