Interpretazione coordinate (Geo)JSON
// height=300 lines=auto
let terremoti;
function preload() {
terremoti = loadJSON( "assets/terremoti_200504_0240.json" );
}
function setup() {
createCanvas(600, 300);
stroke(0,48);
noFill();
background(255);
for (let i = 0; i < terremoti.features.length; i++) {
let info = terremoti.features[i].properties;
let coordinate = terremoti.features[i].geometry.coordinates;
let x = map(coordinate[0], -180, 180, 0, width); // longitudine
let y = map(coordinate[1], 90, -90, 0, height); // latitudine
circle(x, y, info.mag * 20);
}
}
let coordinate = terremoti.features[i].geometry.coordinates;
Ogni elemento dell'array features contiene anche un oggetto geometry che a sua volta contiene un array (coordinates) con i valori di longitudine, latitudine e profondità del posizione della scossa.
let x = map(coordinate[0], -180, 180, 0, width); // longitudine
La longitudine dev'essere mappata sulla larghezza del canvas tenendo conto che i valori minimo e massimo sono -180 e +180.
let y = map(coordinate[1], 90, -90, 0, height); // latitudine
La latitudine dev'essere mappata sull'altezza del canvas tenendo conto che i valori minimo e massimo sono -90 e +90 e che le coordinate sono invertite.
Nella colonna "Feeds" della pagina "GeoJSON Summary Format" ci sono i link ai JSON aggiornati in tempo reale. Con il tasto destro del mouse è possibile copiare uno degli indirizzi e usarlo come parametro dell'istruzione loadJSON(), facendo attenzione a non cancellare le virgolette che devono delimitare l'URL.
Strutturazione dei file:
- CARTELLA-SKETCH
- assets
- libs
- p5.min.js
- index.html
- sketch.js