Skip to content
Snippets Groups Projects
Commit 993366d6 authored by Mario Cosma Damiano Di Lorenzo's avatar Mario Cosma Damiano Di Lorenzo
Browse files

fixed eduGAIN_map.php

parent c205ea5e
No related branches found
No related tags found
No related merge requests found
......@@ -3,34 +3,26 @@
class eduGAIN_map {
static function make_maps($active_only = 0) {
// Debugging message to indicate map creation has started
Utils::debug(4, "making maps");
// Set paths for maps and map source files
$maps_dir = TECHNICAL_HOME."/web/maps/";
$maps_source = TECHNICAL_HOME."/lib/Map_source/";
// Map files for Europe and World
$europe_in = "eduGAIN-Europe.svg";
$world_in = "worldHigh.svg";
// Define the color mapping based on the status values
$Colours = [
0 => "#d0d0d0", // Default color (grey)
1 => "#1e73be", // Color for status 1 (blue)
4 => "#81d742", // Color for status 4 (green)
5 => "#d93", // Color for status 5 (orange)
6 => "#d93", // Color for status 6 (orange)
0 => "#d0d0d0",
1 => "#1e73be",
4 => "#81d742",
5 => "#d93",
6 => "#d93",
];
// Special colors for specific country codes
$Colour = [];
$Colour['va'] = '#c0c0c0';
$Colour['im'] = '#c0c0c0';
$Colour['gg'] = '#c0c0c0';
$Colour['je'] = '#c0c0c0';
// Fetch data for federations based on whether we want only active ones or all
if ($active_only == 1) {
$json = json_decode(file_get_contents(ROOT_URL."/api.php?action=list_federation_map_codes&opt=1"));
$file_suffix = '_active';
......@@ -38,60 +30,48 @@ class eduGAIN_map {
$json = json_decode(file_get_contents(ROOT_URL."/api.php?action=list_federation_map_codes"));
$file_suffix = '';
}
// Initialize array to store the status of countries
$Status = [];
// Loop through the JSON data to process the country status
foreach ($json as $key => $element) {
$countries = $element->country_code;
if (empty($countries)) {
if (!is_array($countries)) {
$countries = [$countries]; // Convert string to array
}
if (empty($countries)) {
continue;
}
foreach ($countries as $country) {
// Get the current status or default to 0
$c = isset($Status[$country]) ? $Status[$country] : 0;
// Update the status for this country
$s = $Status[$country] = max($c, $element->status);
// Set the color for the country based on status
$Colour[$country] = empty($Colours[$s]) ? 'none' : $Colours[$s];
}
}
// Generate Europe Map
// Generate Europe
$svg_if = fopen($maps_source.$europe_in, "r");
$png_out = 'europe'.$file_suffix.'.png';
$svg_out = 'europe'.$file_suffix.'.svg';
// Initialize empty SVG content
$svg = '';
$path = 0;
// Read the SVG file and apply colors based on the status of each country
while ($buffer = fgets($svg_if)) {
if (preg_match('/^ *<path/', $buffer)) {
if (preg_match('/^ *<path/', $buffer))
$path = 1;
}
if (preg_match('/^ +id="([\w]+).*"$/', $buffer, $m)) {
if (preg_match('/^ +id="([\w]+).*"$/', $buffer, $m))
$id = $m[1];
}
$out = $buffer;
// Apply fill color based on status
if (preg_match('/^ +style=".*fill:#[^;]+;.*\/>/', $buffer)) {
if ($path) {
if ($id == "Large") {
$c = '#ffffff'; // Color for "Large" id
} else {
$c = empty($Colour[$id]) ? "#c0c0c0" : $Colour[$id]; // Use country color or default
}
if ($id == "Large")
$c = '#ffffff';
else
$c = empty($Colour[$id]) ? "#c0c0c0" : $Colour[$id];
$out = preg_replace('/(style=".*fill:)#[^;]+;/', '$1'.$c.';', $buffer);
}
$path = 0;
}
$svg .= $out;
}
// Convert SVG to PNG format and save the file
$png = new Imagick();
$png->setBackgroundColor(new ImagickPixel("transparent"));
$png->readImageBlob($svg);
......@@ -104,25 +84,20 @@ class eduGAIN_map {
return(false);
};
// Generate World Map
// Generate world
$png_out = 'world_s'.$file_suffix.'.png';
$svg_out = 'world'.$file_suffix.'.svg';
$png_out_l = 'world_l'.$file_suffix.'.png';
// Load the world map SVG
$xml = simplexml_load_file($maps_source.$world_in);
// Loop through the countries and update the fill color based on their status
for ($i = 0; $i < count($xml->g->path); $i++) {
$buffer = $xml->g->path[$i];
$id = strtolower($buffer->attributes()->id);
if (isset($Status["$id"]) && isset($Colours[$Status["$id"]])) {
// Apply the color style for each country
$xml->g->path[$i]->attributes()->style = "fill: ".$Colours[$Status["$id"]].";";
}
}
// Convert updated world SVG to XML and PNG
$svg = $xml->asXML();
$png = new Imagick();
$png->setBackgroundColor(new ImagickPixel("transparent"));
......@@ -135,13 +110,10 @@ class eduGAIN_map {
if (file_put_contents($maps_dir.$svg_out, $svg) === false) {
return(false);
}
// Resize the world map PNG for smaller version
$png->scaleImage(420, 350, true);
if (file_put_contents($maps_dir.$png_out, $png) === false) {
return(false);
}
return(true);
}
}
\ No newline at end of file
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment