<?php
// regions.php
// JSON parse https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_50m_geography_regions_polys.geojson via http://geojson.xyz/ as "geography regions polys"
// RJM Programming
// January, 2023
set_time_limit(900);

$dolatlong=false;
$longoff=180.0;
$latoff=90.0;
$factor=1.0;
$abit="";

if (isset($_GET['zoom'])) {
  if (strlen(str_replace('+','',urldecode($_GET['zoom']))) > 0) {
    $factor=str_replace('+','',urldecode($_GET['zoom']));
  } 
}  

if (isset($_GET['uselatlaong'])) {  
  $dolatlong=true;  
  $longoff=0.0;
  $latoff=0.0;
} else if (file_exists('regions.html')) {
  if ($factor != 1.0) {
    echo str_replace("function onl() {", "function onl() { \n document.body.style.zoom=((izoom == 1.0) ? ('' + eval(izoom * 100.0) + '%') : '" . (100.0 * $factor) . "%'); \n ", file_get_contents('regions.html'));
    exit;
  } else {
    echo file_get_contents('regions.html');
    exit;
  }
}

// Create a transparent image thanks to https://www.php.net/manual/en/function.imagecolortransparent.php

$im = imagecreatetruecolor((360.0 * $factor), (180.0 * $factor));
$black = imagecolorallocate($im, 0, 0, 0);

// Make the background transparent

imagecolortransparent($im, $black);

// Save the image

$udirnameprebimg='/tmp/imagecolortransparent.png';
imagepng($im, $udirnameprebimg);
$duis='data:image/' . str_replace('jpg','jpeg',strtolower(explode('.',$udirnameprebimg)[-1 + sizeof(explode('.',$udirnameprebimg))])) . ';base64,' . base64_encode(file_get_contents($udirnameprebimg));
imagedestroy($im);
unlink($udirnameprebimg); 

$htmlis="<html><head><title>Display GeoJSON Coastline</title>
<scr" . "ipt type='text/javascript'>
  var izoom=location.search.split('zoom=')[1] ? eval(decodeURIComponent(location.search.split('zoom=')[1].split('&')[0])) : 1.0;
  
  function onl() {
    if (izoom != 1.0) document.body.style.zoom=('' + eval(izoom * 100.0) + '%');
    var elem = document.getElementById('mycanvas');
    elem.style.cursor='progress';
    var context = elem.getContext('2d');
    context.strokeStyle = '#000000';
  }
</scr" . "ipt>
</head><body onload='onl();' style='background-color: yellow;'><img name=myimg id=myimg style='height:" . (180.0 * $factor) . "px;z-index:56;position:absolute;top:0px;left:0px;width:" . (360.0 * $factor) . "px;' src='" . $duis . "' usemap='#mymap'></img>
<map name='mymap' style='z-index:56;'>
</map><canvas id=mycanvas height='" . (180.0 * $factor) . "' width='" . (360.0 * $factor) . "' style='background-color: white;z-index:1;display:inline-block;border-bottom:1px dotted gray;border-right:1px dotted gray;position:absolute;top:0px;left:0px;'></canvas>&nbsp;<h1 style='position:absolute;top:0px;left:" . (360.0 * $factor + 25) . "px;'>GeoJSON World Coastline</h1><h3  style='position:absolute;top:150px;left:" . (360.0 * $factor + 25) . "px;'>RJM Programming - January, 2023</h3><h3  style='position:absolute;top:300px;left:" . (360.0 * $factor + 25) . "px;'>Thanks to <a target=_blank title=https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_110m_coastline.geojson href='https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_110m_coastline.geojson'>https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_110m_coastline.geojson</a></h3></body></html>";

$geojsonis=file_get_contents('coastline.geojson');
$parts=explode("]]", $geojsonis);

for ($i=0; $i<(-1 + sizeof($parts)); $i++) {
  $coords=str_replace('[','',str_replace(']','',explode('[[' , $parts[$i])[-1 + sizeof(explode('[[' , $parts[$i]))]));
  $newcoords=$coords;
  if (!$dolatlong || 1 == 1) {
  $lls=explode(",", $coords);
  $newcoords="";
  for ($j=0; $j<sizeof($lls); $j+=2) {
  $lls[$j]=($lls[$j] + $longoff) * $factor;
  $lls[$j + 1]=($lls[$j + 1] + $latoff) * $factor;
  if ($newcoords != "") { $newcoords.=",";  $htmlis=str_replace("}", " context.lineTo(" . $lls[$j] . ',' . ((180.0 * $factor) - $lls[$j + 1]) . "); \n}", $htmlis);  } else { $htmlis=str_replace("}", " context.stroke(); \n context.beginPath(); \n context.moveTo(" . $lls[$j] . ',' . ((180.0 * $factor) - $lls[$j + 1]) . "); \n}", $htmlis); }
  $newcoords.='' . $lls[$j] . ',' . ((180.0 * $factor) - $lls[$j + 1]);
  }
  }
  $abit.="<area onclick=\"alert('Longitude,Latitude coordinates are " . $coords . "');\" shape='poly' coords='" . $newcoords . "'></area>\n";
  //echo "<area type='poly' coords='" . $newcoords . "'></area>\n";
}

if (!file_exists('regions.html') && strpos(('' . $_SERVER['QUERY_STRING']), 'zoom=') === false && strpos(('' . $_SERVER['QUERY_STRING']), 'uselatlaong=') === false) {
  file_put_contents('regions.html', str_replace("}", " context.stroke(); \n  elem.style.cursor='pointer';  \n}", str_replace("</map>", $abit . "</map>", $htmlis)));
}
echo str_replace("}", " context.stroke(); \n  elem.style.cursor='pointer';  \n}", str_replace("</map>", $abit . "</map>", $htmlis));
?>
