How to display data on map using csv and json files and unfoldingmaps library?

edited May 2015 in Library Questions

For some reason the csv file and the json files aren't working together because the data isn't showing up on the map. I'm thinking that there may be a problem with the markers but I'm not sure. I've simply adapted the example code ChoroplethMapApp with my own data. Here's my code if anyone can tell me what I'm doing wrong.

import de.fhpotsdam.unfolding.*;
import de.fhpotsdam.unfolding.data.*;
import de.fhpotsdam.unfolding.marker.*;
import de.fhpotsdam.unfolding.utils.*;
import java.util.List;

UnfoldingMap map;

HashMap<String, DataEntry> dataEntriesMap;
List<Marker> stateMarkers;

void setup() {
  size(800, 600, P2D);
  smooth();

  map = new UnfoldingMap(this);
  map.zoomAndPanTo(190, 210, 4);
  map.setBackgroundColor(240);
  MapUtils.createDefaultEventDispatcher(this, map);

  // Load country polygons and adds them as markers
  List<Feature> state = GeoJSONReader.loadData(this, "map.geojson");
  stateMarkers = MapUtils.createSimpleMarkers(state);
  map.addMarkers(stateMarkers);

  // Load population data
  dataEntriesMap = loadPopulationDesityFromCSV("PovertyUnder18-2.csv");
  println("Loaded " + dataEntriesMap.size() + " data entries");

  // Country markers are shaded according to its population density (only once)
  shadeStates();
}

void draw() {
  background(240);

  // Draw map tiles and country markers
  map.draw();
}

void shadeStates() {
  for (Marker marker : stateMarkers) {
    // Find data for country of the current marker
    String stateId = marker.getId();
    DataEntry dataEntry = dataEntriesMap.get(stateId);

    if (dataEntry != null && dataEntry.value != null) {
      // Encode value as brightness (values range: 0-1000)
      float transparency = map(dataEntry.value, 0, 700, 10, 255);
      marker.setColor(color(255, 0, 0, transparency));
    } 
    else {
      // No value available
      marker.setColor(color(100, 120));
    }
  }
}

HashMap<String, DataEntry> loadPopulationDesityFromCSV(String fileName) {
  HashMap<String, DataEntry> dataEntriesMap = new HashMap<String, DataEntry>();

  String[] rows = loadStrings(fileName);
  for (String row : rows) {
    // Reads country name and population density value from CSV row
    String[] columns = row.split(";");
    if (columns.length >= 3) {
      DataEntry dataEntry = new DataEntry();
      dataEntry.stateName = columns[0];
      dataEntry.id = columns[1];
      dataEntry.value = Float.parseFloat(columns[2]);
      dataEntriesMap.put(dataEntry.id, dataEntry);
    }
  }

  return dataEntriesMap;
}

class DataEntry {
  String stateName;
  String id;
  Integer year;
  Float value;
}

Answers

Sign In or Register to comment.