meichnerb / meichnerb.github.io

0 stars 0 forks source link

country numeric to alpha mapping #1

Closed davelandry closed 7 years ago

davelandry commented 7 years ago

Here's a javascript object keyed by the IDs in the topojson file. Use this to get to the alpha codes in your data:

var num2alpha = {"100":"BGR","104":"MMR","108":"BDI","112":"BLR","116":"KHM","120":"CMR","124":"CAN","129":"XXA","132":"CPV","136":"CYM","140":"CAF","144":"LKA","148":"TCD","152":"CHL","156":"CHN","158":"TWN","162":"CXR","166":"CCK","170":"COL","174":"COM","175":"MYT","178":"COG","180":"COD","184":"COK","188":"CRI","191":"HRV","192":"CUB","196":"CYP","200":"CZE","204":"BEN","208":"DNK","212":"DMA","214":"DOM","218":"ECU","221":"XXA","222":"SLV","226":"GNQ","231":"ETH","232":"ERI","233":"EST","234":"FRO","238":"FLK","239":"SGS","242":"FJI","246":"FIN","250":"FRA","254":"GUF","258":"PYF","260":"ATF","262":"DJI","266":"GAB","268":"GEO","270":"GMB","275":"PSE","276":"DEU","278":"DDR","280":"FDR","288":"GHA","290":"XXB","292":"GIB","296":"KIR","300":"GRC","304":"GRL","308":"GRD","312":"GLP","316":"GUM","320":"GTM","324":"GIN","328":"GUY","332":"HTI","334":"HMD","336":"VAT","340":"HND","344":"HKG","348":"HUN","352":"ISL","360":"IDN","364":"IRN","368":"IRQ","372":"IRL","376":"ISR","380":"ITA","384":"CIV","388":"JAM","392":"JPN","398":"KAZ","400":"JOR","404":"KEN","408":"PRK","410":"KOR","412":"KSV","414":"KWT","417":"KGZ","418":"LAO","422":"LBN","426":"LSO","428":"LVA","430":"LBR","434":"LBY","438":"LIE","440":"LTU","442":"LUX","446":"MAC","450":"MDG","454":"MWI","458":"MYS","462":"MDV","466":"MLI","470":"MLT","471":"XXB","473":"XXA","474":"MTQ","478":"MRT","480":"MUS","484":"MEX","488":"MID","490":"XXB","492":"XXB","496":"MNG","498":"MDA","499":"MNE","500":"MSR","504":"MAR","508":"MOZ","512":"OMN","516":"NAM","520":"NRU","524":"NPL","527":"XXA","528":"NLD","530":"ANT","531":"CUW","532":"NAA","533":"ABW","534":"MAF","535":"BES","536":"XXE","540":"NCL","548":"VUT","554":"NZL","558":"NIC","562":"NER","566":"NGA","568":"XXC","570":"NIU","574":"NFK","577":"XXA","578":"NOR","580":"MNP","581":"XXB","582":"PCI","583":"FSM","584":"MHL","585":"PLW","586":"PAK","591":"PAN","592":"PCZ","598":"PNG","600":"PRY","604":"PER","608":"PHL","612":"PCN","616":"POL","620":"PRT","624":"GNB","626":"TLS","630":"PRI","634":"QAT","636":"XXC","637":"XXD","638":"REU","642":"ROU","643":"RUS","646":"RWA","652":"BLM","654":"SHN","658":"KNA","660":"AIA","662":"LCA","666":"SPM","670":"VCT","674":"SMR","678":"STP","682":"SAU","686":"SEN","688":"SRB","690":"SYC","694":"SLE","697":"XXD","699":"IND","702":"SGP","703":"SVK","704":"VNM","705":"SVN","706":"SOM","710":"ZAF","716":"ZWE","720":"YMD","724":"ESP","728":"SSD","729":"SDN","732":"ESH","740":"SUR","744":"SJM","748":"SWZ","752":"SWE","756":"CHE","760":"SYR","762":"TJK","764":"THA","768":"TGO","772":"TKL","776":"TON","780":"TTO","784":"ARE","788":"TUN","792":"TUR","795":"TKM","796":"TCA","798":"TUV","800":"UGA","804":"UKR","807":"MKD","810":"SUN","818":"EGY","826":"GBR","833":"IMN","834":"TZA","837":"XXG","838":"XXF","839":"XXI","840":"USA","850":"VIR","854":"BFA","858":"URY","860":"UZB","862":"VEN","872":"WAK","876":"WLF","879":"XXA","882":"WSM","886":"YAR","887":"YEM","890":"YUG","894":"ZMB","899":"XXH","024":"AGO","072":"BWA","012":"DZA","010":"ATA","074":"BVT","080":"XXA","004":"AFG","051":"ARM","031":"AZE","050":"BGD","048":"BHR","096":"BRN","064":"BTN","086":"IOT","008":"ALB","020":"AND","040":"AUT","056":"BEL","070":"BIH","058":"BLX","028":"ATG","044":"BHS","084":"BLZ","060":"BMU","052":"BRB","092":"VGB","016":"ASM","036":"AUS","090":"SLB","032":"ARG","068":"BOL","076":"BRA"};
meichnerb commented 7 years ago

hey so I'm still a little stuck (I've always had trouble with mapping). This is the code I have now to try to map the IDs and alpha codes

`featureCollection.features.map(function(row, index){

if(!row["id"])return false;

    if(row["id"])

      return row["id"] = d3.values(num2alpha);

});`

but obviously that's not the way to go about it, so I'm thinking there's something similar to d3.values that maybe I should be using instead?

Thanks!

davelandry commented 7 years ago

You're still going to need to use featureCollection.features as the data for the country paths, so you should create an Object to look up the data based on the country code.

You're on the right track by storing the data into a global variable (AllData), but instead of making it an Array, make it an Object where the key is the country code. First, get rid of line 65 where you initialize it as an array, and simply initialize it as an empty object on line 56:

var AllData = {};

Then, instead of pushing into the Array on line 103, add it as a key of the object:

AllData[row['Country Code']] = {name: row['Country Name'], years: yearlyData, code: row['Country Code']};

Now, in your getCircleColor, which gets passed the data point from featureCollection.features, you can grab the greenhouse data like this:

var countryCode = num2alpha[d.id];
var greenhouseData = AllData[countryCode];

Hope that helps, let me know where that gets you. I'll be online tomorrow morning if you have any more questions.

meichnerb commented 7 years ago

hey, so new question. I'm trying to output the greenhouseData.years object into a div so that it'll look like this: 1990: somenumber 1991: somenumber 1992: somenumber etc. I'm trying to get it to work with this code but it's coming up undefined: .on("click", function(d) { var countryCode = num2alpha[d.id]; var greenhouseData = AllData[countryCode]; var countryData = JSON.stringify(greenhouseData.years); function print(){ var printThis; for(var i = 0; i < 48; i++){ printThis = "\n" + greenhouseData.years[i]; } return printThis; // <-- to be printed to the div } console.log("click"); document.getElementById('Name').innerHTML = greenhouseData.name; document.getElementById('code').innerHTML = greenhouseData.code; document.getElementById('data').innerHTML = print(); });

What am I doing wrong?

Thanks, Bayley

davelandry commented 7 years ago

The keys in the greenhouseData.years Object are actual years, not numbers between 0 and 48. Try this out:

.on("click", function(d) {
  var countryCode = num2alpha[d.id];
  var greenhouseData = AllData[countryCode];
  function print() { 
    var printThis;
    for(var i = 1990; i <= 1999; i++){ 
      printThis = "\n" + greenhouseData.years[i]; 
    } 
    return printThis; // <-- to be printed to the div 
  } 
  console.log("click");
  d3.select('#Name').html(greenhouseData.name);      
  d3.select('#code').html(greenhouseData.code);   
  d3.select('#data').html(print()); 
});
meichnerb commented 7 years ago

Unfortunately still not working. I just pushed all the recent changes, not sure if you want to take a look. I'm going to keep googling things.

Thanks, Bayley

On Thu, Dec 1, 2016 at 6:09 PM, Dave Landry notifications@github.com wrote:

The keys in the greenhouseData.years Object are actual years, not numbers between 0 and 48. Try this out:

.on("click", function(d) { var countryCode = num2alpha[d.id]; var greenhouseData = AllData[countryCode]; function print() { var printThis; for(var i = 1990; i <= 1999; i++){ printThis = "\n" + greenhouseData.years[i]; } return printThis; // <-- to be printed to the div } console.log("click"); d3.select('#Name').html(greenhouseData.name); d3.select('#code').html(greenhouseData.code); d3.select('#data').html(print()); });

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/meichnerb/meichnerb.github.io/issues/1#issuecomment-264324168, or mute the thread https://github.com/notifications/unsubscribe-auth/AVJ6v9kCe4Lm5_C6Cj1otVqwcBKSbxcIks5rD1O6gaJpZM4K29Xk .

meichnerb commented 7 years ago

I've hardcoded it in for now, it works but if you know of an easier way I'd love to know!

On Thu, Dec 1, 2016 at 8:14 PM, Bayley Meichner meichner.b@husky.neu.edu wrote:

Unfortunately still not working. I just pushed all the recent changes, not sure if you want to take a look. I'm going to keep googling things.

Thanks, Bayley

On Thu, Dec 1, 2016 at 6:09 PM, Dave Landry notifications@github.com wrote:

The keys in the greenhouseData.years Object are actual years, not numbers between 0 and 48. Try this out:

.on("click", function(d) { var countryCode = num2alpha[d.id]; var greenhouseData = AllData[countryCode]; function print() { var printThis; for(var i = 1990; i <= 1999; i++){ printThis = "\n" + greenhouseData.years[i]; } return printThis; // <-- to be printed to the div } console.log("click"); d3.select('#Name').html(greenhouseData.name); d3.select('#code').html(greenhouseData.code); d3.select('#data').html(print()); });

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/meichnerb/meichnerb.github.io/issues/1#issuecomment-264324168, or mute the thread https://github.com/notifications/unsubscribe-auth/AVJ6v9kCe4Lm5_C6Cj1otVqwcBKSbxcIks5rD1O6gaJpZM4K29Xk .

davelandry commented 7 years ago

That's my bad. The code I posted was overwritting the printThis variable each time through the for loop instead of adding to it. Try this:

.on("click", function(d) {
  var countryCode = num2alpha[d.id];
  var greenhouseData = AllData[countryCode];
  function print() { 
    var printThis = "";
    for(var i = 1990; i <= 1999; i++){ 
      printThis += i + ":  " + greenhouseData.years[i] + "<br/>"; 
    } 
    return printThis; // <-- to be printed to the div 
  } 
  console.log(print());
  d3.select('#Name').html(greenhouseData.name);      
  d3.select('#code').html(greenhouseData.code);   
  d3.select('#data').html(print()); 
});
meichnerb commented 7 years ago

Perfect! Thank you!

On Sun, Dec 4, 2016 at 9:16 PM, Dave Landry notifications@github.com wrote:

That's my bad. The code I posted was overwritting the printThis variable each time through the for loop instead of adding to it. Try this:

.on("click", function(d) { var countryCode = num2alpha[d.id]; var greenhouseData = AllData[countryCode]; function print() { var printThis = ""; for(var i = 1990; i <= 1999; i++){ printThis += i + ": " + greenhouseData.years[i] + "
"; } return printThis; // <-- to be printed to the div } console.log(print()); d3.select('#Name').html(greenhouseData.name); d3.select('#code').html(greenhouseData.code); d3.select('#data').html(print()); });

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/meichnerb/meichnerb.github.io/issues/1#issuecomment-264753221, or mute the thread https://github.com/notifications/unsubscribe-auth/AVJ6v3EGgsDI_vI7gTaEy0LA5BGLa9Bwks5rE3QQgaJpZM4K29Xk .