Commit 5896a79d authored by Emma Fritzberg's avatar Emma Fritzberg
Browse files

reorganize logic

parent 9a42233b
This diff is collapsed.
import csv
# Parse CSV data into global var spotters with a dict representing spotters and their paths.
#
# Each key is a spotter ID and its associated value is a dict representing the
# path of the spotter with that ID, in which keys are timestamp strings and
# values are tuples containing the location in the format (latitude, longitude).
#
# key: value:
# spotter ID --> [
# sub-key: sub-value:
# timestamp --> (latitude, longitude),
# timestamp --> (latitude, longitude),
# timestamp --> (latitude, longitude),
# ]
spotters = {}
with open('data/raw_spotter_data.csv') as raw_data:
reader = csv.DictReader(raw_data)
for row in reader:
spotter_id = row['spotterId']
timestamp = row['timestamp']
latitude = float(row['latitude'])
longitude = float(row['longitude'])
if spotter_id in spotters:
# Assumption: each spotter has only one recorded location for a given timestamp
spotters[spotter_id][timestamp] = (latitude, longitude)
else:
path = dict([(timestamp, (latitude, longitude))])
spotters[spotter_id] = path
with open('data/encriched_spotter_data.csv', 'w', newline='') as enriched_data:
fieldnames = ['spotter_id', 'timestamp', 'latitude', 'longitude', 'velocity', 'coastline_distance']
writer = csv.DictWriter(enriched_data, fieldnames=fieldnames)
writer.writeheader()
for spotter_id in spotters.keys():
timestamps = spotters[spotter_id]
for timestamp in timestamps:
(latitude, longitude) = timestamps[timestamp]
writer.writerow({
'spotter_id': spotter_id,
'timestamp': timestamp,
'latitude': latitude,
'longitude': longitude,
# TODO: use velocity helper function to store velocity at each time
'velocity': 0,
# TODO: calculate distance from nearest coastline
'coastline_distance': 0,
})
import csv
# import numpy as npl
from datetime import datetime, timedelta
from geopy import distance
from matplotlib import pyplot as plt
......@@ -11,34 +9,6 @@ TIMESTAMP_FORMAT = '%Y-%m-%dT%H:%M:%S.000Z'
def get_datetime(timestamp):
return datetime.strptime(timestamp, TIMESTAMP_FORMAT)
# Parse CSV data into global var spotters with a dict representing spotters and their paths.
#
# Each key is a spotter ID and its associated value is a dict representing the
# path of the spotter with that ID, in which keys are timestamp strings and
# values are tuples containing the location in the format (latitude, longitude).
#
# key: value:
# spotter ID --> [
# sub-key: sub-value:
# timestamp --> (latitude, longitude),
# timestamp --> (latitude, longitude),
# timestamp --> (latitude, longitude),
# ]
spotters = {}
with open('../data/all_time_spotter_tracks.csv') as raw_data:
reader = csv.DictReader(raw_data)
for row in reader:
spotter_id = row['spotterId']
timestamp = row['timestamp']
latitude = float(row['latitude'])
longitude = float(row['longitude'])
if spotter_id in spotters:
# Assumption: each spotter has only one recorded location for a given timestamp
spotters[spotter_id][timestamp] = (latitude, longitude)
else:
path = dict([(timestamp, (latitude, longitude))])
spotters[spotter_id] = path
# Returns time delta between two timestamps in hours
def time_elapsed(start_timestamp, end_timestamp):
return (get_datetime(end_timestamp) - get_datetime(start_timestamp)) / timedelta(hours=1)
......@@ -54,7 +24,7 @@ def segment_speed(start, end):
return distance_travelled(start_location, end_location) / time_elapsed(start_timestamp, end_timestamp)
# Returns the average speed at which the spotter traveled its path, in kilometers per hour
def average_speed(spotter_id):
def average_speed(spotters, spotter_id):
path = spotters[spotter_id]
total_distance = 0
total_time = 0
......@@ -73,7 +43,7 @@ def average_segment_velocity(start_index, end_index, sorted_timestamps, path):
return distance_travelled(path[sorted_timestamps[start_index]], path[sorted_timestamps[end_index]]) / time_elapsed(sorted_timestamps[start_index], sorted_timestamps[end_index])
# Returns the velocity of the spotter at the given time
def time_resolved_velocity(spotter_id, time):
def time_resolved_velocity(spotters, spotter_id, time):
path = spotters[spotter_id]
timestamps = list(path.keys())
if len(timestamps) <= 1:
......@@ -100,7 +70,7 @@ def time_resolved_velocity(spotter_id, time):
return average_segment_velocity(i, i + 1, timestamps, path)
return math.nan
def plot_single_trajectory(spotter_id):
def plot_single_trajectory(spotters, spotter_id):
path = spotters[spotter_id]
timestamps = list(path.keys())
timestamps.sort(key=(lambda e: get_datetime(e)))
......@@ -125,45 +95,3 @@ def plot_single_trajectory(spotter_id):
velocity_color = 'red'
plt.plot([start_longitude, end_longitude], [start_latitude, end_latitude], color=velocity_color, transform=ccrs.Geodetic())
plt.show()
def plot_all_trajectories():
ax = plt.axes(projection=ccrs.Mollweide())
ax.set_global()
ax.coastlines()
print(len(spotters.keys()))
#for spotter_id in spotters.keys():
for s in range(50):
spotter_id = list(spotters.keys())[s]
print(spotter_id)
path = spotters[spotter_id]
timestamps = list(path.keys())
timestamps.sort(key=(lambda e: get_datetime(e)))
for i in range(len(timestamps) - 1):
(start_latitude, start_longitude) = path[timestamps[i]]
#print('start: lat: ', start_latitude, ' , long: ', start_longitude)
(end_latitude, end_longitude) = path[timestamps[i + 1]]
#print('end: lat: ', end_latitude, ' , long: ', end_longitude)
velocity = average_segment_velocity(i, i + 1, timestamps, path)
#print('velocity: ', velocity, 'km/h')
velocity_color = 'gray'
if not math.isnan(velocity):
# TODO: make continuous map of speed
if velocity < 0.5:
velocity_color = 'blue'
if velocity >= 0.5:
velocity_color = 'purple'
if velocity >= 1.0:
velocity_color = 'red'
plt.plot([start_longitude, end_longitude], [start_latitude, end_latitude], color=velocity_color, transform=ccrs.Geodetic())
plt.show()
with open('test.csv', 'w', newline='') as enriched_data:
fieldnames = ['spotter_id', 'timestamp', 'latitude', 'longitude', 'velocity', 'coastline_distance']
writer = csv.DictWriter(enriched_data, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'spotter_id': 'SPOT-0284', 'coastline_distance': 1, 'velocity': 0})
# Example usage:
#spotter_id = 'SPOT-0284'
#plot_single_trajectory(spotter_id)
#plot_all_trajectories()
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment