xxxxxxxxxx
82
let strings = [];
let minLat;
let maxLat;
let minLong;
let maxLong;
// using preload function to optimize the program
function preload() {
strings = loadStrings("airports.csv");
}
function setup() {
createCanvas(600, 430);
background(0);
// condition for the program to stop if continuing doesn't make sense
if (strings == null) {
print("Failed to load the file, stopping here");
while (true) {}
}
print(
"Strings loaded from file successfully, read " + strings.length + " lines"
);
findMinMaxLatLong();
noLoop(); // We only want to draw once.
}
function findMinMaxLatLong() {
minLat = 0;
maxLat = 0;
minLong = 0;
maxLong = 0;
// for loop for finding max and min longitude & latitude
for (let csvRowNumber = 1; csvRowNumber < strings.length; csvRowNumber++) {
let singleRow = split(strings[csvRowNumber], ",");
let longitude = float(singleRow[6]);
let latitude = float(singleRow[7]);
if (!isNaN(longitude) && !isNaN(latitude)) {
if (latitude < minLat) minLat = latitude;
if (latitude > maxLat) maxLat = latitude;
if (longitude < minLong) minLong = longitude;
if (longitude > maxLong) maxLong = longitude;
}
}
print("Latitude (min, max) = (" + minLat + "," + maxLat + ") ");
print("Longitude (min, max) = (" + minLong + "," + maxLong + ")");
}
function draw() {
for (let csvRowNumber = 1; csvRowNumber < strings.length; csvRowNumber++) {
let singleRow = split(strings[csvRowNumber], ",");
let longitude = float(singleRow[6]);
let latitude = float(singleRow[7]);
// Making sure that longitude and latitude coordinates exist
if (!isNaN(longitude) && !isNaN(latitude)) {
//Map function is used for calculating the coodinates x and y
// Invert x-axis to match typical map orientation
let x = map(longitude, minLong, maxLong, height, 0);
let y = map(latitude, minLat, maxLat, 0, width);
stroke(255);
// changing x and y to match typical map orientation
point(y, x);
}
}
print("Finished drawing points.");
noLoop(); // Stop drawing after one frame.
}