-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcore.py
161 lines (153 loc) · 7.83 KB
/
core.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import ogr
from sys import path
from os import getcwd
path.append(getcwd())
from math import pi
from datetime import datetime
#from preprocessing import *
# def calculateDistance(pnt1, pnt2):
# earth_mean_radius = 6371
# return pnt1.Distance(pnt2) * earth_mean_radius * pi/180
def calculateDistanceFromXY(x1, y1, x2, y2, crsESPG):
srs = ogr.osr.SpatialReference()
srs.ImportFromEPSG(crsESPG)
point1 = ogr.Geometry(ogr.wkbPoint)
point1.AddPoint(x1, y1)
point1.AssignSpatialReference(srs)
point2 = ogr.Geometry(ogr.wkbPoint)
point2.AddPoint(x2, y2)
point2.AssignSpatialReference(srs)
return point1.Distance(point2) / 1000
#This function splits the layer into a dictionary for each owl and its corresponding SORTED features by timestamp
def getOwlLists(layer, owl_id_field, timestamp_field):
owlUniqueIds = set(feat[owl_id_field] for feat in layer)
layer.ResetReading()
print(owlUniqueIds)
owlDict = {}
for id in owlUniqueIds:
owlDict[id] = []
for feat in layer:
owlDict[feat[owl_id_field]].append({'feature': feat, 'timestamp': datetime.strptime(feat[timestamp_field], '%Y-%m-%d %H:%M:%S')})
for key in owlDict.keys():
owlDict[key] = sorted(owlDict[key], key = lambda feat: feat['timestamp'])
return owlDict
#This function calculates statistics based on the group_by parameter
#group_by parameter can be "month", "day", "year"
def calculateOwlStats(owlInfo, speed_field, group_by = "month"):
index = 0
stats = {}
for obs in owlInfo:
if(index == 0):
index += 1
previousFeat = obs
continue
if(previousFeat is not None):
if(group_by == "month"):
currentCycle = str(obs['timestamp'].month) +"-"+ str(obs['timestamp'].year)#f"{obs['timestamp']:%m}" + "-" + f"{obs['timestamp']:%Y}"
elif (group_by == "day"):
currentCycle = str(obs['timestamp'].day) +"-" + str(obs['timestamp'].month) +"-"+ str(obs['timestamp'].year)
else :#should be by year
currentCycle = str(obs['timestamp'].year)
#print(obs)
#32632 is ESPG for WGS UTM Zone 32N http://spatialreference.org/ref/epsg/32632/
distance = calculateDistanceFromXY(obs['feature']['utm_north'],
obs['feature']['utm_east'],
previousFeat['feature']['utm_north'],
previousFeat['feature']['utm_east'],
32632)
#distance = calculateDistance(obs['feature'].geometry(), previousFeat['feature'].geometry())
if(currentCycle not in stats):
stats[currentCycle] = {}
stats[currentCycle]['observationCount'] = 0
stats[currentCycle]['totalDistance'] = 0
stats[currentCycle]['totalSpeed'] = 0
stats[currentCycle]['averageSpeed'] = 0
stats[currentCycle]['observationCount'] += 1
stats[currentCycle]['totalDistance'] += distance
stats[currentCycle]['totalSpeed'] += obs['feature'][speed_field]
stats[currentCycle]['averageSpeed'] = stats[currentCycle]['totalSpeed']/stats[currentCycle]['observationCount']
index += 1
previousFeat = obs
return stats
def parseOwlDataForBoxplots(owlData, property):
#create an array of 12 element for 12 months
resultData = [[],[],[],[],[],[],[],[],[],[],[],[]]
for owlKey in owlData:
owlMonths = owlData[owlKey]
for month in owlMonths:
#for each month add one entry for the owl distance in this month regardress of the year
monthNumberString = month.split('-')[0].strip()
monthIndex = int(monthNumberString) - 1
monthData = owlMonths[month]
resultData[monthIndex].append(monthData[property])
return resultData
def parseOwlDataToAvgPerOwlPerMonth(owlData):
resultData = {'months':[1,2,3,4,5,6,7,8,9,10,11,12], 'owls': []}
for owlKey in owlData:
owlMonths = owlData[owlKey]
currentOwlData = {'label':owlKey, 'averageDistances': [None, None, None, None, None, None, None, None, None, None, None, None],\
'averageSpeeds':[None, None, None, None, None, None, None, None, None, None, None, None]}
monthsOccurances = [0,0,0,0,0,0,0,0,0,0,0,0]
for month in owlMonths:
monthNumberString = month.split('-')[0].strip()
monthIndex = int(monthNumberString) - 1
monthsOccurances[monthIndex] += 1
monthData = owlMonths[month]
if(currentOwlData['averageDistances'][monthIndex] is None):
currentOwlData['averageDistances'][monthIndex] = monthData['totalDistance']
else: currentOwlData['averageDistances'][monthIndex] += monthData['totalDistance']
if(currentOwlData['averageSpeeds'][monthIndex] is None):
currentOwlData['averageSpeeds'][monthIndex] = monthData['averageSpeed']
else: currentOwlData['averageSpeeds'][monthIndex] += monthData['averageSpeed']
for x in range(12):
if(currentOwlData['averageDistances'][x] is not None):
currentOwlData['averageDistances'][x] = currentOwlData['averageDistances'][x]/monthsOccurances[x]
currentOwlData['averageSpeeds'][x] = currentOwlData['averageSpeeds'][x]/monthsOccurances[x]
resultData['owls'].append(currentOwlData)
return resultData
def parseOwlDataToAverageByMonth(owlData):
resultData = {'months':None, 'averageDistances':None, 'averageSpeeds':None}
monthsOccurances = [0,0,0,0,0,0,0,0,0,0,0,0]
resultData['months'] = [1,2,3,4,5,6,7,8,9,10,11,12]
resultData['averageDistances'] = [0,0,0,0,0,0,0,0,0,0,0,0]
resultData['averageSpeeds'] = [0,0,0,0,0,0,0,0,0,0,0,0]
for owlKey in owlData:
owlMonths = owlData[owlKey]
for month in owlMonths:
monthNumberString = month.split('-')[0].strip()
monthIndex = int(monthNumberString) - 1
monthsOccurances[monthIndex] += 1
monthData = owlMonths[month]
currentAvgDist = resultData['averageDistances'][monthIndex]
currentAvgSpeed = resultData['averageSpeeds'][monthIndex]
resultData['averageDistances'][monthIndex] = currentAvgDist + monthData['totalDistance']
resultData['averageSpeeds'][monthIndex] = currentAvgSpeed + monthData['averageSpeed']
for x in range(12):
if(monthsOccurances[x] != 0):
resultData['averageDistances'][x] = resultData['averageDistances'][x]/monthsOccurances[x]
resultData['averageSpeeds'][x] = resultData['averageSpeeds'][x]/monthsOccurances[x]
return resultData
def getOwlsAggregateData(shapefile_path, timestamp_field, speed_field, owl_id_field, filters, group_by = "month"):
driver = ogr.GetDriverByName("ESRI Shapefile")
data_source = driver.Open(shapefile_path, 0)
layer = data_source.GetLayer(0)
layer.SetAttributeFilter(filters)
#layer = preprocess(layer)
attributes = layer.GetLayerDefn()
timestamp_field_found = False
speed_field_found = False
#Check if fields exist in the shapefile
for i in range(attributes.GetFieldCount()):
current_field = attributes.GetFieldDefn(i).GetName()
if(current_field == timestamp_field): timestamp_field_found = True
if(current_field == speed_field): speed_field_found = True
if(not timestamp_field_found or not speed_field_found):
raise ValueError("One or more of the field(%s,%s) don't exist in the shapefile"%(timestamp_field, speed_field))
owlLists = getOwlLists(layer,owl_id_field, timestamp_field)
statDict = {}
for key in owlLists:
stats = calculateOwlStats(owlLists[key], speed_field, group_by)
statDict[key] = stats
#print(stats)
return statDict
#in_path = "D:\\Mastergeotech\\Munster\\Python\\Project\\movebank\\eagle_owl\\Eagle owl Reinhard Vohwinkel MPIO\\points.shp"