-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetDetailedMeteor.php
134 lines (116 loc) · 6.2 KB
/
getDetailedMeteor.php
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
<?php
// Config
include './includes/config.php';
// Select meteors
$query = $pdo->prepare('SELECT m.id, m.name, m.recclass, m.mass, m.found, m.year, m.reclat, m.reclong, l.city, l.country, l.flag, l.id as localisationId
FROM meteorite_landings as m
LEFT OUTER JOIN localisations as l
ON m.id = l.id_meteor
WHERE m.id = :id
');
$query->bindValue(':id', $_GET['id'], PDO::PARAM_INT);
$query->execute();
$meteor = $query->fetch();
// Fetch OpenCage API
if(empty($meteor->localisationId) && !empty($meteor->reclat) && !empty($meteor->reclong)){
$url = 'https://api.opencagedata.com/geocode/v1/json?q='.$meteor->reclat.'+'.$meteor->reclong.'&key='.OPEN_CAGE_API_KEY;
$data = @file_get_contents($url);
if ($data !== false) {
$data = json_decode($data);
// Save in database
$prepare = $pdo->prepare('
INSERT INTO
localisations (id_meteor, city, country, flag)
VALUES
(:id_meteor, :city, :country, :flag)
');
$prepare->bindValue(':id_meteor', $meteor->id, PDO::PARAM_INT);
$prepare->bindValue(':city', $data->results[0]->components->city ?: null);
$prepare->bindValue(':country', $data->results[0]->components->country ?: null);
$prepare->bindValue(':flag', $data->results[0]->annotations->flag ?: null);
$prepare->execute();
$meteor->city = $data->results[0]->components->city ?: '';
$meteor->country = $data->results[0]->components->country ?: '';
$meteor->flag = $data->results[0]->annotations->flag ?: '';
}
}
// Mass and energy comparisons
if(!empty($meteor->mass)){
// Calculate speed
// 100 km of distance, with a speed of 15 km/s
// Convert km/s to m/s
$v = 15 * 1000; // m/s
// Convert g to kg
$m = $meteor->mass / 1000;
// Calculate energy
// 0.5 * m (kg) * v^2 (m/s)
$Ec = 0.5 * $m * $v * $v; // J
$meteor->energy_in_joules = round($Ec, 4); // kWh
// Convert J to kWh
$Ec = $Ec / 3600000;
$meteor->energy_in_kWh = round($Ec, 4); // kWh
// Mass comparisons
if(1 <= $meteor->mass && $meteor->mass < 50){
$meteor->mass_comparison = '2 AA batteries 🔋';
} else if(50 <=$meteor->mass && $meteor->mass < 100){
$meteor->mass_comparison = 'Pack of 54 cards 🃏';
} else if(100 <= $meteor->mass && $meteor->mass < 150){
$meteor->mass_comparison = 'A Baseball ⚾️';
} else if(150 <= $meteor->mass && $meteor->mass < 200){
$meteor->mass_comparison = 'A Hamster 🐹';
} else if(200 <= $meteor->mass && $meteor->mass < 250){
$meteor->mass_comparison = 'Can of Soup Cambell’s 🥣';
} else if(250 <= $meteor->mass && $meteor->mass < 500){
$meteor->mass_comparison = 'Human Heart ♥️';
} else if(500 <= $meteor->mass && $meteor->mass < 750){
$meteor->mass_comparison = 'A football ⚽️';
} else if(750 <= $meteor->mass && $meteor->mass < 1000){
$meteor->mass_comparison = 'A basketball 🏀';
} else if(1000 <= $meteor->mass && $meteor->mass < 1500){
$meteor->mass_comparison = 'A liter of water 🚰';
} else if(1500 <= $meteor->mass && $meteor->mass < 2000){
$meteor->mass_comparison = 'A chihuahua 🐶';
} else if(2000 <= $meteor->mass && $meteor->mass < 2500){
$meteor->mass_comparison = '2 carpfishes 🐟';
} else if(2500 <= $meteor->mass && $meteor->mass < 3000){
$meteor->mass_comparison = 'A brick 🧱';
} else if(3000 <= $meteor->mass && $meteor->mass < 3500){
$meteor->mass_comparison = 'A guitar 🎸';
} else if(3500 <= $meteor->mass && $meteor->mass < 4000){
$meteor->mass_comparison = 'A cat 🐱';
} else if(4000 <= $meteor->mass){
$meteor->mass_comparison = 'Woah that’s big ☄️';
}
// Energy comparisons
if($meteor->energy_in_kWh < 0.2){
$meteor->energy_comparison = 'Insignificant 😴';
} else if(0.2 <= $meteor->energy_in_kWh && $meteor->energy_in_kWh < 0.8){
$mult = $meteor->energy_in_kWh / 0.27;
$meteor->energy_comparison = $mult > 1 ? 'The nutritional value of '.round($mult, 1).' meals 🍝' : 'The nutritional value of '.round($mult, 1).' meal 🍝';
} else if(0.8 <= $meteor->energy_in_kWh && $meteor->energy_in_kWh < 270){
$mult = $meteor->energy_in_kWh / 1;
$meteor->energy_comparison = $mult > 1 ? 'The explosion of '.round($mult, 1).' shells 💣' : 'The explosion of '.round($mult, 1).' shell 💣';
} else if(270 <= $meteor->energy_in_kWh){
$mult = $meteor->energy_in_kWh / 277;
$meteor->energy_comparison = $mult > 1 ? round($mult, 1).' lightnings during a thunderstorm ⚡️' : round($mult, 1).' lightning during a thunderstorm ⚡️';
}
}
// Classification description
if(!empty($meteor->recclass)){
if(substr($meteor->recclass, 0, 1) == 'H'){
$meteor->classification_description = 'High on iron';
} else if(substr($meteor->recclass, 0, 1) == 'L'){
$meteor->classification_description = 'Low on Iron';
} else if(substr($meteor->recclass, 0, 2) == 'LL'){
$meteor->classification_description = 'Low total iron and Low metal contents';
} else if(substr($meteor->recclass, 0, 2) == 'EH'){
$meteor->classification_description = 'Pyroxene high on Iron';
} else if(substr($meteor->recclass, 0, 2) == 'EL'){
$meteor->classification_description = 'Pyroxene low on Iron';
} else if(substr($meteor->recclass, 0, 1) == 'C'){
$meteor->classification_description = 'Contains Carbon';
} else if(substr($meteor->recclass, 0, 1) == 'K'){
$meteor->classification_description = 'Very rich in oxidised Iron';
}
}
echo json_encode($meteor);