Actions

Fix population

From Dragon Eye Atlas

Save this as fix_population.php and use it as outlined in part 3 of the video tutorial, feeding it the burgs.csv file from Azgaar's FMG.

<source> <?php

$fp = fopen("php://stdin", "r") or die("cannot open stdin");

$input_data = file_get_contents("php://stdin");

$json = json_decode($input_data);

foreach ($json->features as &$feature) {

   preg_match('/([0-9\.]+K?) \(([0-9\.]+K?) rural, urban ([0-9\.]+K?)\)/', $feature->properties->population, $matches);
   if ($matches[1] > 0) {
       $feature->properties->population = k_to_number($matches[1]);
       $feature->properties->rural = k_to_number($matches[2]);
       $feature->properties->urban = k_to_number($matches[3]);
   }

}

function k_to_number($data) {

   if (preg_match('/([0-9]+)(\.([0-9])+)?K/', $data, $matches)) {
       if (isset($matches[3])) {
           return $matches[1].$matches[3].'00';
       } else {
           return $matches[1].'000';
       }
   } else {
       return $data;
   }

}

echo json_encode($json, JSON_PRETTY_PRINT);

?> </source>