3

I am trying to create a Cholorpleth map using Processing and geoJSON. I am having difficulty cycling through my HashMap to check the property that will dictate the intensity of the polygons color.

Currently this is my code:

void draw() {
  map.draw();
  for (int i = 0; i < countyMarkers.size(); i++) {
   Marker county = countyMarkers.get(i);
   String pop = countyData.get("pop").toString();
   println(i + "pop: " + pop);   
}

What I would like to do is loop through my HashMap countyData and get the population of each entry. Use an if statement to compare it to a value and then assign it the color based on the statement that is true.

My HashMap can be seen here:

{name=Carlow, pop=54,612}
{name=Cavan, pop=73,183}
{name=Clare, pop=117,196}
{name=Cork, pop=519,032}
{name=Donegal, pop=161,137}
{name=Dublin, pop=1,273,069}
{name=Galway, pop=250,541}
{name=Kerry, pop=145,502}
{name=Kildare, pop=210,312}
{name=Kilkenny, pop=95,419}
{name=Laois, pop=80,559}
{name=Letrim, pop=31,796}
{name=Limerick, pop=191,809}
{name=Longford, pop=39,000}
{name=Louth, pop=122,897}
{name=Mayo, pop=130,638}
{name=Meath, pop=184,135}
{name=Monaghan, pop=60,483}
{name=Offaly, pop=76,687}
{name=Roscommon, pop=64,065}
{name=Sligo, pop=65,393}
{name=Tipperary, pop=158,754}
{name=Waterford, pop=113,795}
{name=Westmeath, pop=86,164}
{name=Wexford, pop=145,320}
{name=Wicklow, pop=136,640}

I am relatively new to working with this type of data so I am at a loss as to where I am going wrong. I think my logic is correct I am just not sure how to implement it. I'd appreciate any help on the issue.

Currently this line -> println(i + "pop: " + pop);

Outputs the last entry of the hashmap over and over.

This is the output I am getting

0pop: 136,640
1pop: 136,640
2pop: 136,640
3pop: 136,640
4pop: 136,640
5pop: 136,640
6pop: 136,640
7pop: 136,640
8pop: 136,640
9pop: 136,640
10pop: 136,640
11pop: 136,640
12pop: 136,640
13pop: 136,640
14pop: 136,640
15pop: 136,640
16pop: 136,640
17pop: 136,640
18pop: 136,640
19pop: 136,640
20pop: 136,640
21pop: 136,640
22pop: 136,640
23pop: 136,640
24pop: 136,640
25pop: 136,640

I populate my HashMap using this method.

void getCountyDetails(List<Marker>m) {  
  countyData = new HashMap();
  for (Marker county: countyMarkers) {
    countyData.putAll(county.getProperties());
    println(countyData);
  }
}

Modified code here

    Set<String> keySet = countyData.keySet();
  for(String pop : keySet){
     String value = countyData.get(pop).toString();
     println("population:" + value);
   }

Now It is outputting:

population:Wicklow
population:136,640
population:Wicklow
population:136,640
population:Wicklow
population:136,640
population:Wicklow
population:136,640
population:Wicklow
population:136,640
population:Wicklow
population:136,640
population:Wicklow
population:136,640
population:Wicklow
population:136,640
population:Wicklow
population:136,640

It still does not seem to be iterating through each entry in the HashMap

New way Of populating HashMap

void getCountyDetails(List<Marker>m) {  
  countyData = new HashMap();
  int i = 0;
  for (Marker county: countyMarkers) {
    countyData.put(i,county.getProperties());
    i++;
  }
}
Javacadabra
  • 5,578
  • 15
  • 84
  • 152

3 Answers3

2

From your code we can see the you are using countyData.get("pop") - i.e you have hardcoded the key, so it will always return one and the same thing - the value associated with this key.

PS: You can iterate over all the elements of the map, by using the keys form the keySet:

Set<KeyClass> keySet = myMap.keySet();
for(KeyClass key : keySet){
     ValueClass value = myMap.get(key);
     //do stuff
 }

Also you should note that the putAll method will overwrite entries with the same key:

Copies all of the mappings from the specified map to this map (optional operation). The effect of this call is equivalent to that of calling put(k, v) on this map once for each mapping from key k to value v in the specified map

Svetlin Zarev
  • 14,713
  • 4
  • 53
  • 82
1
 for (Map.Entry<String, Integer> countyMarkers: map.entrySet())
    {
        System.out.println(countyMarkers.getKey() + " " + countyMarkers.getValue());
    }

here I have use entrySet() method, which returns you a Set view of the mappings contained in this map, and then through these mapping you can get the key and value using getKey() and getValue() method

Girish
  • 1,717
  • 1
  • 18
  • 30
0

The problem is:

String pop = countyData.get("pop").toString();

you hard code the key to get "pop"

you probably want:

 String pop = countyData.get(county).toString();
dkatzel
  • 31,188
  • 3
  • 63
  • 67