Skip to content

Instantly share code, notes, and snippets.

@smcl
Created April 1, 2013 18:05
Show Gist options
  • Save smcl/5286563 to your computer and use it in GitHub Desktop.
Save smcl/5286563 to your computer and use it in GitHub Desktop.
Plot the earth in Processing.org using data from (http://download.geonames.org/export/dump/). Need to download cities1000.txt from the geonames.org site and add to the sketch.
class City {
String name;
int population;
color col;
float x;
float y;
float z;
City(String cityname, int pop, float lat, float lon) {
name = cityname;
population = pop;
float rad = (float)(spheresz+1);
lat = radians(lat);
lon = -radians(lon);
x = rad * cos(lat) * cos(lon);
y = rad * cos(lat) * sin(lon);
z = rad * sin(lat);
}
}
boolean hidesurface=false;
int spheresz;
// start pointing at the UK-ish
float rotX = 0.7;
float rotY = 0.0;
float rotZ = 1.9;
ArrayList cities;
void setup() {
size(800, 800, OPENGL);
stroke(255, 50);
fill(100, 0, 100);
smooth();
spheresz = int(0.4*width);
cities = new ArrayList();
BufferedReader reader = createReader("cities1000.txt");
String[] cols;
try {
cols = split(reader.readLine(), '\t');
while (cols != null) {
float lat = float(cols[4]);
float lon = float(cols[5]);
int pop = int(cols[14]);
if (pop > 0) {
cities.add(new City(cols[1], pop, lat, lon));
}
cols = split(reader.readLine(), '\t');
}
}
catch (IOException e) {
e.printStackTrace();
exit();
}
}
void draw() {
background(0);
translate(width/2, width/2, 0);
fill(0, 0, 100);
noStroke();
if(!hidesurface)
sphere(spheresz);
rotateY(rotY);
rotateX(rotX);
rotateZ(rotZ);
fill(0, 150, 0);
stroke(0, 150, 0);
beginShape(POINTS);
for (int i=0; i<cities.size(); i++) {
City c = (City)cities.get(i);
vertex(c.x,c.y,c.z);
// create new shape every 200 pts to prevent out of memory error
if (i%200==0) {
endShape(POINTS);
beginShape(POINTS);
}
}
endShape(POINTS);
}
void keyPressed() {
switch (key) {
case 'x':
rotX+=0.1;
break;
case 'X':
rotX-=0.1;
break;
case 'y':
rotY+=0.1;
break;
case 'Y':
rotY-=0.1;
break;
case 'z':
rotZ+=0.1;
break;
case 'Z':
rotZ-=0.1;
break;
case ' ':
hidesurface = !hidesurface;
break;
default:
println("(rotX,rotY,rotZ)=("+rotX+","+rotY+","+rotZ+")");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment