Skip to content

Commit

Permalink
Queries, Cleaning Code
Browse files Browse the repository at this point in the history
  • Loading branch information
gpivaro committed Dec 6, 2020
1 parent c55225f commit dbf37b4
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 39 deletions.
10 changes: 9 additions & 1 deletion Database/aircraft_query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ SELECT
FROM
project_2.aircraft_data
WHERE
time = (SELECT
longitude IS NOT NULL
AND time = (SELECT
MAX(time)
FROM
project_2.aircraft_data);

SELECT
*
FROM
project_2.aircraft_data
WHERE
icao24 = 'a8aac8';
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

* http://www.flyingnut.com/adsbmap/

* // https://flightaware.com/live/flight/AFR853/history/20201201/2115Z/SOCA/LFPO/tracklog

#### Weather API:


Expand Down
65 changes: 59 additions & 6 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def api_routes():
f"<h3>API routes available:</h3>"
f"/aircrafts-data<br/>"
f"/airports-data<br/>"
f"/api/v1.0/aircrafts-data/icao24/<icao24><br/>"
f"/api/v1.0/aircrafts-data/callsign/<callsign><br/>"
)

# Return a json with the query results for the aircrafts table
Expand All @@ -58,12 +60,16 @@ def api_aircrafts():
# MySQL query to return all table elements that have not null latitute and have the newest time stamp
aircraft_df = pd.read_sql(
f"""
SELECT *
FROM project_2.aircraft_data
WHERE longitude IS NOT NULL
SELECT
*
FROM
{table_airplanes}
WHERE
longitude IS NOT NULL
AND
time = (SELECT MAX(time)
FROM project_2.aircraft_data);
time = (SELECT MAX(time)
FROM
{table_airplanes});
""",
engine)

Expand All @@ -76,13 +82,60 @@ def api_aircrafts():
@app.route("/api/v1.0/airports-data")
def api_airports():

airports_df = pd.read_sql(f"SELECT * FROM {table_airports} ORDER BY AirportID", engine)
airports_df = pd.read_sql(
f"""
SELECT
*
FROM
{table_airports}
ORDER BY
AirportID;
""",
engine)

result = airports_df.to_json(orient="records")
parsed = json.loads(result)

return jsonify(parsed)

# Return a json with the query results for the aircrafts table for a specific icao24
@app.route("/api/v1.0/aircrafts-data/icao24/<icao24>")
def api_aircrafts_icao24(icao24):

aircraft_df = pd.read_sql(
f"""
SELECT
*
FROM
{table_airplanes}
WHERE
icao24 = '{str(icao24)}';
""",
engine)

result = aircraft_df.to_json(orient="records")
parsed = json.loads(result)


# Return a json with the query results for the aircrafts table for a specific callsign
@app.route("/api/v1.0/aircrafts-data/callsign/<callsign>")
def api_aircrafts_callsign(callsign):

aircraft_df = pd.read_sql(
f"""
SELECT
*
FROM
{table_airplanes}
WHERE
callsign = '{str(callsign)}';
""",
engine)

result = aircraft_df.to_json(orient="records")
parsed = json.loads(result)
return jsonify(parsed)


# The server is set to run on the computer IP address on the port 5100
# Go to your http://ipaddress:5100
Expand Down
53 changes: 21 additions & 32 deletions static/js/logic_Gabriel.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ var config = {
};

// Creating our initial map object
// L.map accepts 2 arguments: id of the HTML element to insert the map, and an object containing the initial options for the new map
/*L.map accepts 2 arguments: id of the HTML element to insert the map,
and an object containing the initial options for the new map */
var myMap = L.map("map", {
// For Houston uncomment:
// center: [31.56, -96.47],
Expand Down Expand Up @@ -71,11 +72,13 @@ d3.json(aircrafts_api_url).then((importedData) => {

var flightData = importedData;

// console.log(flightData);

// Retrieve the newest meas time and convert the format
var newestData = new Date(flightData[0].time * 1000);
var newestDataTime = newestData.toLocaleTimeString("en-US", timeOptions);
console.log(newestDataTime);

// console.log(flightData);


// Display on the screen the number of cleaned data points
document.getElementById('numAircrafts').textContent = `${flightData.length} (${newestDataTime})`;
Expand Down Expand Up @@ -121,7 +124,6 @@ d3.json(aircrafts_api_url).then((importedData) => {
countrytData.push(flightData[i].origin_country)
}
};
// console.log(countrytData);


// Create an object with the aircrafts by origin country
Expand All @@ -136,7 +138,6 @@ d3.json(aircrafts_api_url).then((importedData) => {
}
originCountryAircraft.push({ "country": countrytData[i], "aircrafts": n });
};
// console.log(originCountryAircraft);


// Sort the samples in descending order of sample values
Expand All @@ -148,7 +149,8 @@ d3.json(aircrafts_api_url).then((importedData) => {
// Reverse the list due to the Plotly requeriments
top10originCountryAircraft.reverse()

// Trace1 to display the data

// Trace1 to display the Aircraft by Country of Origin chart
var trace1 = {
x: top10originCountryAircraft.map(element => element.aircrafts),
y: top10originCountryAircraft.map(element => element.country),
Expand All @@ -159,8 +161,6 @@ d3.json(aircrafts_api_url).then((importedData) => {
// create an array to be plotted
var chartData = [trace1];



var layout = {
title: "Aircraft by Country of Origin",
xaxis: {
Expand All @@ -175,6 +175,7 @@ d3.json(aircrafts_api_url).then((importedData) => {
Plotly.newPlot("barChart", chartData, layout, config, { displayModeBar: false });


// Aircraft Altitude Distribution plot
var trace2 = {
x: flightData.map(element => element.baro_altitude * 3.28084),
type: 'histogram',
Expand All @@ -195,35 +196,33 @@ d3.json(aircrafts_api_url).then((importedData) => {

Plotly.newPlot('baroAltitudeHist', histData, layout, config, { displayModeBar: false });


// Data for position source chart
var posSource = [];
var ADSB = 0; var ASTERIX = 0; var MLAT = 0;
for (var i = 0; i < flightData.length; i++) {
// conditional test to get position source type
if (flightData[i].position_source === "ADS-B") {
if (flightData[i].position_source === 0) {
ADSB += 1;
}
else if (flightData[i].position_source === "ASTERIX") {
else if (flightData[i].position_source === 1) {
ASTERIX += 1;
}
else if (flightData[i].position_source === "MLAT") {
else if (flightData[i].position_source === 2) {
MLAT += 1;
}
};
posSource.push({ "Type": "ADS-B", "Qtd": ADSB }, { "Type": "ASTERIX", "Qtd": ASTERIX }, { "Type": "MLAT", "Qtd": MLAT });
// console.log(posSource);


// console.log(Object.entries(posSource));
// console.log(Object.keys(posSource));
posSource.push(
{ "Type": "ADS-B", "Qtd": ADSB },
{ "Type": "ASTERIX", "Qtd": ASTERIX },
{ "Type": "MLAT", "Qtd": MLAT }
);

// Position Source Chart
var data = [{
values: posSource.map(element => element.Qtd),
labels: posSource.map(element => element.Type),
// text: 'CO2',
textposition: 'inside',
domain: { column: 1 },
// name: 'CO2 Emissions',
hoverinfo: 'label+percent+name',
hole: .4,
type: 'pie'
Expand All @@ -240,7 +239,7 @@ d3.json(aircrafts_api_url).then((importedData) => {
Plotly.newPlot('positionSourcePlot', data, layout, config);



// Aircraft Speed vs. Altitude Chart
var trace3 = {
x: flightData.map(element => element.baro_altitude * 3.28084),
y: flightData.map(element => element.velocity * 2.23694),
Expand All @@ -252,7 +251,6 @@ d3.json(aircrafts_api_url).then((importedData) => {
type: 'scatter'
};


var data = [trace3];

var layout = {
Expand All @@ -277,7 +275,6 @@ d3.json(aircrafts_api_url).then((importedData) => {



// https://flightaware.com/live/flight/AFR853/history/20201201/2115Z/SOCA/LFPO/tracklog


/* ************************************************************************************************ */
Expand Down Expand Up @@ -368,9 +365,8 @@ d3.json(airports_api_url).then((importedData) => {
// Reverse the list due to the Plotly requeriments
top10CountryAirports.reverse()

// console.log(top10CountryAirports);

// Trace1 to display the data
// Trace1 to display the Airport by Country Data
var trace1 = {
x: top10CountryAirports.map(element => element.airports),
y: top10CountryAirports.map(element => element.country),
Expand All @@ -384,13 +380,6 @@ d3.json(airports_api_url).then((importedData) => {
// create an array to be plotted
var chartData = [trace1];


// Responsive chart
var config = {
responsive: true,
displayModeBar: false
};

var layout = {
title: "Airports by Country",
xaxis: {
Expand Down

0 comments on commit dbf37b4

Please sign in to comment.