xxxxxxxxxx
131
/*
* example to process a CSV file containing data
* about wind turbines in the USA
* Source: https://eerscmap.usgs.gov/uswtdb/
*/
// An array of strings to hold the entire file
let strings = [];
// For scaling, we want to know the minimum and maximum latitude and longitude
let minLat;
let maxLat;
let minLong;
let maxLong;
function preload() {
// The text from the file is loaded into an array.
strings = loadStrings("uswtdb_v5_3_20230113.csv");
}
function setup() {
createCanvas(500, 400);
background(235);
// Did we succeed to load anything?
if (strings == null) {
print("failed to load the file, stopping here");
// this is an endless loop; it's a common way
// to prevent a program from continuing when
// something is so wrong that there is no sense
// in continuing
while (true) {}
}
print(
"strings loaded from file successfully, read " + strings.length + " lines"
);
// Find the minimum and maximum latitude
// and longitude
findMinMaxLatLong();
}
function findMinMaxLatLong() {
let singleRow = [];
// loop over each row in the file
for (let csvRowNumber = 1; csvRowNumber < strings.length; csvRowNumber++) {
// get a single row and split that row
// into individual words
singleRow = split(strings[csvRowNumber], ",");
// We know that the last two fields are the
// latitude and longitude and so they are
// numerical:
let longitude = float(singleRow[25]);
let latitude = float(singleRow[26]);
// The file may be missing a field, in which case
// the converstion to a float might have failed
if (isNaN(longitude) || isNaN(latitude)) {
print("conversion to float failed; skipping row " + csvRowNumber);
} else {
if (csvRowNumber == 1) {
minLat = latitude - 10;
maxLat = latitude + 10;
minLong = longitude - 10;
maxLong = longitude + 10;
}
if (latitude < minLat) minLat = latitude;
if (latitude > maxLat) maxLat = latitude;
if (longitude < minLong) minLong = longitude;
if (longitude > maxLong) maxLong = longitude;
}
} // end of for() loop
print("Latitude (min, max) = (" + minLat + "," + maxLat + ") ");
print("Longitude (min, max) = (" + minLong + "," + maxLong + ")");
} // end of findMinMaxLatLong
let csvRowNumber = 1;
// Skip the first line, since we know it's a header
function draw() {
let singleRow = [];
// get a single row and split that row into
// individual words
singleRow = split(strings[csvRowNumber], ",");
// This really slows things
// down so use only when debugging
//print("Row " +
// csvRowNumber +
// " contains " +
// singleRow.length +
// " fields" );
// We know that the last two fields are the
// latitude and longitude and so they are
// numerical:
let longitude = float(singleRow[25]);
let latitude = float(singleRow[26]);
// use only when debugging
// print("Latitude " +
// latitude +
// " longitude " +
// longitude );
// Check for non-numerical strings.
if (isNaN(longitude) || isNaN(latitude)) {
print("conversion to float failed; skipping row " + csvRowNumber);
} else {
// scale that to fit on our canvas
//print(csvRowNumber);
let ypos = map(latitude, minLat, maxLat, 0, height);
let xpos = map(longitude, minLong, maxLong, 0, width);
// Put a mark there
point(xpos, ypos);
} // end of valid data
csvRowNumber++;
if (csvRowNumber >= strings.length) {
print("finished");
noLoop();
}
}