Skip to content

Instantly share code, notes, and snippets.

@roryf
Created September 9, 2011 12:52
Show Gist options
  • Save roryf/1206123 to your computer and use it in GitHub Desktop.
Save roryf/1206123 to your computer and use it in GitHub Desktop.
import org.json.*;
import java.net.*;
import java.util.Iterator;
PImage bg;
JSONObject data;
double west = -3.414001;
double east = -3.017120;
double north = 56.002605;
double south = 55.882629;
double longDiff = east - west;
double latDiff = north - south;
int width = 10240;
double ratio = 0.54;
int height = (int)Math.floor((double)width * ratio);
int spotSize = 11;
/*
||******************************************************************************************
|| Read the JSON data from the web server
||******************************************************************************************
*/
JSONObject pullJSON(String targetURL) {
String[] lines = loadStrings(targetURL);
try {
return new JSONObject(join(lines, ""));
} catch(JSONException ex) {
println(ex.getMessage());
return null;
}
}
/*
||******************************************************************************************
|| Do all the processing setup stuff
||******************************************************************************************
*/
void setup() {
data = pullJSON("https://roryf.cloudant.com/festival/_design/output/_view/geo");
size(width, height);
//bg = loadImage("map-really-large.png");
background(0);
noStroke();
try {
JSONArray rows = data.getJSONArray("rows");
for (int i = 0, l = rows.length(); i < l; i++) {
JSONObject row = rows.getJSONObject(i);
JSONObject val = row.getJSONArray("value").getJSONObject(0);
//println(val.toString());
double lat = val.getDouble("lat");
double lon = val.getDouble("lon");
int x = (int)(((west - lon) / longDiff) * width) * -1;
int y = (int)(((north - lat) / latDiff) * height);
String source = val.getString("source");
if (source.equals("twitter")) {
radialGradient(x, y, 0xFF2AA9D4, spotSize);
} else if (source.equals("gowalla")) {
//radialGradient(x, y, 0xFFF79120, spotSize);
} else if (source.equals("flickr")) {
//radialGradient(x, y, 0xFFFF0084, spotSize);
}
}
} catch(JSONException ex) {
println(ex.getMessage());
}
save("output.png");
}
void radialGradient(float x, float y, int c, int size) {
PGraphics pg = createGraphics(size, size, JAVA2D);
pg.beginDraw();
pg.background(30, 0);
int halfsize = size / 2;
for (int i = 0; i <= size; i += 1) {
for (int j = 0; j <= size; j += 1) {
// calculate distance to center
//float distance = (float) Math.hypot(i - size / 2, j - size / 2) / (size / 2);
//float distance = (float) sqrt(sq(i-size/2) + sq(j-size/2)) / (size/2);
float xDist = i - halfsize;
float yDist = j - halfsize;
float distance = (float) Math.sqrt(xDist*xDist + yDist*yDist) / halfsize;
float scale = 1 - distance;
if (scale < 0 ) {
scale = 0;
}
float transparency = 255 * (scale * scale);
int thisColour = color(c, int(transparency));
pg.set(i, j, thisColour);
}
}
pg.endDraw();
imageMode(CENTER);
image(pg, x, y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment