2.48KiB; Perl | 2020-04-11 18:41:43+02 | Statements 55 | SLOC 80
1
use 5.028;
2
3
use strict;
4
use warnings;
5
use utf8;
6
7
binmode STDOUT, ":encoding(UTF-8)";
8
9
# This script will take the IJF world tour data and calculate the distances involved
10
# and the carbon footprint. Later versions may allow you to set a home location and
11
# calculate based on return journeys
12
13
# steps
14
# * get the events for the year
15
# * loop around getting the lat/lon
16
# * calculate distance beteen and add to an array of hashes
17
# * display results
18
19
use Carp 'croak';
20
use JSON qw( decode_json );
21
use LWP::Simple;
22
use Geo::Coder::OSM;
23
use GIS::Distance;
24
25
our $VERSION = 1.0000;
26
my @dates;
27
my $geocoder = Geo::Coder::OSM->new();
28
my $gis = GIS::Distance->new();
29
my $json;
30
31
$|++;
32
33
for my $year (qw/2019/) {
34
    for my $age (qw/SEN/) {
35
        $json
36
            = get('http://data.judobase.org/'
37
                . 'api/get_json'
38
                . '?params[action]=competition.get_list'
39
                . '&params[year]='
40
                . $year
41
                . '&params[id_age]='
42
                . $age );
43
44
        my $decoded_json = decode_json($json);
45
46
        for my $event ( @{$decoded_json} ) {
47
            $event->{age} = $age || 'SEN';
48
            push @dates, $event;
49
        }
50
    }
51
}
52
53
@dates = sort { $a->{date_from} cmp $b->{date_from} } @dates;
54
55
use Data::Dumper;
56
57
my @locations;
58
59
for my $event (@dates) {
60
    next unless $event->{prime_event};
61
    #    warn Dumper $event;
62
63
    my $location = $geocoder->geocode(
64
        location => $event->{city} . ', ' . $event->{country},
65
        city     => $event->{city},
66
        country  => $event->{country},
67
        limit    => 1,
68
    );
69
70
71
    push @locations, {
72
    	name => $event->{city},
73
	lat  => $location->{lat},
74
	lon  => $location->{lon}
75
    };
76
}
77
78
79
my $index = 0;
80
for my $event (@locations){
81
    if ($index == 0) {
82
	    $event->{distance} = 0;
83
        say '* ' . sprintf("%8d", $event->{distance}) . ' km' . "\t\t" . $event->{name};
84
	    $index++;
85
	    next;
86
    }
87
88
    my $prev = $locations[$index - 1];
89
    my $distance = $gis->distance( $prev->{lat}, $prev->{lon}, $event->{lat}, $event->{lon} );
90
    $event->{distance} = int($distance->kilometre);
91
    say '* ' . sprintf("%8d", $event->{distance}) . ' km' . "\t\t" . $event->{name};
92
93
    $index++;
94
}
95
96
97
my $total_distance;
98
99 1
map { $total_distance += $_->{distance}} @locations;
100
101
say "Total distance: " . int($total_distance);
102
103
# 259 g/km pesimistic co2 per km fot flight
104
# from https://en.wikipedia.org/wiki/Carbon_footprint#Flight
105
106
my $carbon = 259 * $total_distance;
107
say 'Carbon for the tour: ' . int($carbon/1000) . 'kg Co2';