Permalink
Browse files

json load sighting counts

  • Loading branch information...
camillol committed Nov 1, 2011
1 parent d7839e9 commit 463dc5a34d23b3fcf00ca6596695c41a208c6647
Showing with 54 additions and 5 deletions.
  1. +15 −3 Sighting.pde
  2. +38 −1 WebDataSource.pde
  3. +1 −1 cs424p3.pde
View
@@ -67,6 +67,11 @@ class SightingType {
active = act;
activeAnimator.target(active ? 1.0 : 0.0);
}
+
+ int hashCode()
+ {
+ return id;
+ }
}
final static int CITY = 0;
@@ -209,10 +214,17 @@ class SightingsFilter {
String toString()
{
- return viewMinYear + "-" + viewMaxYear + " "
+ StringBuffer s = new StringBuffer();
+ s.append(viewMinYear + "-" + viewMaxYear + " "
+ viewMinMonth + "-" + viewMaxMonth + " "
- + viewMinHour + "-" + viewMaxHour + " "
- + activeTypes;
+ + viewMinHour + "-" + viewMaxHour + " ");
+ boolean first = true;
+ for (SightingType st : activeTypes) {
+ if (first) first = false;
+ else s.append(",");
+ s.append(st.id);
+ }
+ return s.toString();
}
}
View
@@ -1,4 +1,12 @@
+import org.json.*;
+
class WebDataSource implements DataSource {
+ String baseURL;
+
+ WebDataSource(String baseURL)
+ {
+ this.baseURL = baseURL;
+ }
void tsvLoad(String filename, String tag)
{
@@ -104,7 +112,36 @@ class WebDataSource implements DataSource {
void reloadCitySightingCounts()
{
- /* TODO */
+ minCountSightings = 10000;
+ maxCountSightings = 0;
+ totalCountSightings = 0;
+
+ String request = baseURL + "/sightingCounts/" + activeFilter.toString();
+ println(request);
+ try {
+ JSONObject result = new JSONObject(join(loadStrings(request), ""));
+ JSONArray cities = result.getJSONArray("cities");
+ for (int i = 0; i < cities.length(); i++) {
+ JSONObject city = cities.getJSONObject(i);
+ Place p = cityMap.get(city.getInt("id"));
+ p.sightingCount = 0;
+
+ JSONArray counts = city.getJSONArray("counts");
+ int idx = 0;
+ for (SightingType st : sightingTypeMap.values()) {
+ int typeCount = counts.getInt(idx);
+ p.counts[idx] = typeCount;
+ if (st.active) p.sightingCount += typeCount;
+ idx++;
+ }
+ minCountSightings = min(p.sightingCount, minCountSightings);
+ maxCountSightings = max(p.sightingCount, maxCountSightings);
+ totalCountSightings += p.sightingCount;
+ }
+ }
+ catch (JSONException e) {
+ println ("There was an error parsing the JSONObject.");
+ }
}
void loadAirports()
View
@@ -98,7 +98,7 @@ void setup()
/* load data */
data = new SQLiteDataSource();
-// data = new WebDataSource();
+// data = new WebDataSource(dataPath("jsontest"));
data.loadSightingTypes();
activeFilter = new SightingsFilter();

0 comments on commit 463dc5a

Please sign in to comment.