-
Notifications
You must be signed in to change notification settings - Fork 2
/
combine_birds_site_year.py
46 lines (38 loc) · 1.5 KB
/
combine_birds_site_year.py
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
import os
import sys
import geopandas
import pandas as pd
import tools
def combine_files(bird_detection_files, year, site, score_thresh, savedir):
"""Load shapefiles and concat into large frame"""
# load all shapefiles to create a dataframe
df = []
for x in bird_detection_files:
try:
# Catch and skip badly structured file names
# TODO: fix file naming issues so we don't need this
eventdf = geopandas.read_file(x)
eventdf["Site"] = tools.get_site(x)
eventdf["Date"] = tools.get_date(x)
eventdf["Year"] = tools.get_year(x)
eventdf["event"], eventdf["file_postscript"] = tools.get_event(x)
eventdf = eventdf[eventdf.score > score_thresh]
df.append(eventdf)
except IndexError as e:
print("Filename issue:")
print(e)
df = geopandas.GeoDataFrame(pd.concat(df, ignore_index=True))
df.crs = eventdf.crs
df = df.assign(bird_id=range(1, len(df) + 1)) # Index bird IDs starting at 1
filename = os.path.join(savedir, f"{site}_{year}_combined.shp")
df.to_file(filename)
return df
if __name__ == "__main__":
score_thresh = 0.3
paths = sys.argv[1:]
split_path = os.path.normpath(paths[0]).split(os.path.sep)
year = split_path[5]
site = split_path[6]
working_dir = tools.get_working_dir()
savedir = os.path.join(working_dir, "predictions", year, site)
combine_files(paths, year, site, score_thresh, savedir=savedir)