-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLifeExpectancy.java
More file actions
78 lines (62 loc) · 2.39 KB
/
LifeExpectancy.java
File metadata and controls
78 lines (62 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package EarthQuakeMap;
import processing.core.PApplet;
import de.fhpotsdam.unfolding.UnfoldingMap;
import de.fhpotsdam.unfolding.utils.MapUtils;
import parsing.ParseFeed;
import de.fhpotsdam.unfolding.providers.*;
import de.fhpotsdam.unfolding.providers.Google.*;
import java.util.List;
import de.fhpotsdam.unfolding.data.Feature;
import de.fhpotsdam.unfolding.data.GeoJSONReader;
import java.util.HashMap;
import de.fhpotsdam.unfolding.marker.Marker;
/**
* Visualizes life expectancy in different countries.
*
* It loads the country shapes from a GeoJSON file via a data reader, and loads the population density values from
* another CSV file (provided by the World Bank). The data value is encoded to transparency via a simplistic linear
* mapping.
*/
public class LifeExpectancy extends PApplet {
UnfoldingMap map;
HashMap<String, Float> lifeExpMap;
List<Feature> countries;
List<Marker> countryMarkers;
public void setup() {
size(800, 600, OPENGL);
map = new UnfoldingMap(this, 50, 50, 700, 500, new Google.GoogleMapProvider());
MapUtils.createDefaultEventDispatcher(this, map);
// Load lifeExpectancy data
lifeExpMap = ParseFeed.loadLifeExpectancyFromCSV(this,"LifeExpectancyWorldBank.csv");
// Load country polygons and adds them as markers
countries = GeoJSONReader.loadData(this, "countries.geo.json");
countryMarkers = MapUtils.createSimpleMarkers(countries);
map.addMarkers(countryMarkers);
System.out.println(countryMarkers.get(0).getId());
// Country markers are shaded according to life expectancy (only once)
shadeCountries();
}
public void draw() {
// Draw map tiles and country markers
map.draw();
}
//Helper method to color each country based on life expectancy
//Red-orange indicates low (near 40)
//Blue indicates high (near 100)
private void shadeCountries() {
for (Marker marker : countryMarkers) {
// Find data for country of the current marker
String countryId = marker.getId();
System.out.println(lifeExpMap.containsKey(countryId));
if (lifeExpMap.containsKey(countryId)) {
float lifeExp = lifeExpMap.get(countryId);
// Encode value as brightness (values range: 40-90)
int colorLevel = (int) map(lifeExp, 40, 90, 10, 255);
marker.setColor(color(255-colorLevel, 100, colorLevel));
}
else {
marker.setColor(color(150,150,150));
}
}
}
}