%congestion_detection.m

clc
clear

% load file
load 2014-03-26/wednesday_26_03.mat;

%{
% gps coordinates of Vienna north-east corner
ne_x = 48.217672776660926;
ne_y = 16.386011559486406;
% gps coordinates of Vienna south-west corner
sw_x = 48.19875949301708;
sw_y = 16.353625584602355;
%}

%{
ne_x = 48.292079905275806;
ne_y = 16.56538598632801;
% gps coordinates of Vienna south-west corner
sw_x = 48.09216946074534;
sw_y = 16.195977050781266;
%}

%{
% gps coordinates of Vienna north-east corner
ne_x = 48.25551896134078;
ne_y = 16.491577941894548;
% gps coordinates of Vienna south-west corner
sw_x = 48.16504057625637;
sw_y = 16.29793756103527;
%}

%{
% gps coordinates of Vienna north-east corner
ne_x = 48.217730896932636;
ne_y = 16.37665065002443;
% gps coordinates of Vienna south-west corner
sw_x = 48.211333276457026;
sw_y = 16.361538101196174;
%}

%{
% gps coordinates of Vienna north-east corner
ne_x = 48.22350667395572;
ne_y = 16.38943942260744;
% gps coordinates of Vienna south-west corner
sw_x = 48.19720384566253;
sw_y = 16.331583190917854;
%}

% gps coordinates of Vienna north-east corner
ne_x = 48.21017064971968;
ne_y = 16.373560745239274;
% gps coordinates of Vienna south-west corner
sw_x = 48.20794428453343;
sw_y = 16.368683506011962;

% variable declaration
j = 1;
k = 1;
l = 1;
m = 1;
n = 1;
o = 1;
speed_10_x = [];
speed_10_y = [];
speed_20_x = [];
speed_20_y = [];
speed_30_x = [];
speed_30_y = [];
speed_40_x = [];
speed_40_y = [];
speed_50_x = [];
speed_50_y = [];
speed_60_x = [];
speed_60_y = [];

% loop through the whole data set for the given day
for i = 1:length(m_id)
    
    % check whether the vehicle's gps coordinates are within Vienna region
    if (m_x(i) >= sw_y && m_x(i) <= ne_y) && (m_y(i) >= sw_x && m_y(i) <= ne_x)
        
        % get hours
        hours = str2num(substring(m_time{i},11,13));
        mins = str2num(substring(m_time{i},14,16));
        
        % filter given time period
        if hours >= 14 && hours <= 17  
        
            speed = m_speed(i);
            % differentiate between individual speed clusters
            if speed < 10
                speed_10_x(j) = m_x(i);
                speed_10_y(j) = m_y(i);
                j = j + 1;
            elseif speed >= 11 && speed < 20
                speed_20_x(k) = m_x(i);
                speed_20_y(k) = m_y(i);
                k = k + 1;
            elseif speed >= 21 && speed < 30
                speed_30_x(l) = m_x(i);
                speed_30_y(l) = m_y(i);
                l = l + 1;
            elseif speed >= 31 && speed < 40
                speed_40_x(m) = m_x(i);
                speed_40_y(m) = m_y(i);
                m = m + 1;
            elseif speed >= 41 && speed < 50
                speed_50_x(n) = m_x(i);
                speed_50_y(n) = m_y(i);
                n = n + 1;
            else
                speed_60_x(o) = m_x(i);
                speed_60_y(o) = m_y(i);
                o = o + 1;
            end
        
        end
        
    end
    
end

% load satellite image
%img = imread('vienna_closer_look.png');
%img = imread('vienna_city.png');
%img = imread('vienna_c.png');
%img = imread('vienna_city_centre_closer.png');
img = imread('congested_spot.png');

% set GPS coordinates of the given area
min_long = sw_y;
min_lati = sw_x;

max_long = ne_y;
max_lati = ne_x;

% define step of resolution
step = 0.000000001;

hold on

% set axis range according to GPS coordinates of the image
long_range = min_long:step:max_long;
lati_range = min_lati:step:max_lati;

% flip cooridantes as matlab starts image coordinates from top left corner
for i=1:3
    img(:,:,i)=flipud(img(:,:,i));
end

% plot satellite image of a certain area in Vienna
image(long_range,lati_range,img);

% set min and max range of both X and Y axis
set(gca,'XLim',[min_long max_long]);
set(gca,'YLim',[min_lati max_lati]);

% plot the different speeds
plot(speed_10_x,speed_10_y,'Color',[1 0 0],'Marker','.','LineStyle','none','MarkerSize',15);
plot(speed_20_x,speed_20_y,'Color',[1 0.5 0],'Marker','.','LineStyle','none','MarkerSize',15);
plot(speed_30_x,speed_30_y,'Color',[1 1 0],'Marker','.','LineStyle','none','MarkerSize',15);
plot(speed_40_x,speed_40_y,'Color',[0.5 1 0],'Marker','.','LineStyle','none','MarkerSize',15);
plot(speed_50_x,speed_50_y,'Color',[0 1 0],'Marker','.','LineStyle','none','MarkerSize',15);
plot(speed_60_x,speed_60_y,'Color',[0 0 1],'Marker','.','LineStyle','none','MarkerSize',15);

% set axis labels
xlabel('Longitude (decimals)');
ylabel('Latitude (decimals)');

% set color bar scheme
map = [1, 0, 0
    1, 0.5, 0
    1, 1, 0
    0.5, 1, 0
    0, 1, 0
    0, 0, 1];

% set color bar label, font size, and tick labels
c = colorbar;
c.Label.String = 'Velocity (km/h)';
c.Label.FontSize = 12;
set(c,'TickLabels',{'0','10','20','30','40','50','>50'});

% show color map
colormap(map)
