Skip to content

Instantly share code, notes, and snippets.

@sdennler
Last active April 26, 2017 10:40
Show Gist options
  • Save sdennler/be34fef82afb37c6e42bc2779fa42308 to your computer and use it in GitHub Desktop.
Save sdennler/be34fef82afb37c6e42bc2779fa42308 to your computer and use it in GitHub Desktop.
Ugly script to calculate the areas for the TB Swiss Hiker (TB56N6P)
<?php
/**
* Ugly script to calculate the areas for the TB Swiss Hiker and similar
* Usage:
* 1. Download KML file from GC.com (View in Google Earth)
* 1. Put in same directory as script and name it tb.kml
* 3. Get a geonames username and put bellow
* 4. Run script (php tb.php |less)
* @see https://coord.info/TB56N6P
* @see https://coord.info/TB2HMZ6
*/
$geonames_username = ''; // Get one: http://www.geonames.org/login
$file_name = 'tb.kml';
if(!$geonames_username) die('geonames_username missing');
$xml = simplexml_load_file($file_name) or die("coult not read file $file_name");
$route_raw = $xml->Document->Placemark->LineString->coordinates;
unset($xml);
$count = 0;
$skipped = 0;
$unknown = 0;
$collection = array();
$collection['Switzerland'] = array();
$collection['Switzerland']['Aargau'] = 0;
$collection['Switzerland']['Appenzell Ausserrhoden'] = 0;
$collection['Switzerland']['Appenzell Innerrhoden'] = 0;
$collection['Switzerland']['Basel-City'] = 0;
$collection['Switzerland']['Basel-Landschaft'] = 0;
$collection['Switzerland']['Bern'] = 0;
$collection['Switzerland']['Fribourg'] = 0;
$collection['Switzerland']['Geneva'] = 0;
$collection['Switzerland']['Glarus'] = 0;
$collection['Switzerland']['Grisons'] = 0;
$collection['Switzerland']['Jura'] = 0;
$collection['Switzerland']['Lucerne'] = 0;
$collection['Switzerland']['Neuchatel'] = 0;
$collection['Switzerland']['Nidwalden'] = 0;
$collection['Switzerland']['Obwalden'] = 0;
$collection['Switzerland']['Saint Gallen'] = 0;
$collection['Switzerland']['Schaffhausen'] = 0;
$collection['Switzerland']['Schwyz'] = 0;
$collection['Switzerland']['Solothurn'] = 0;
$collection['Switzerland']['Thurgau'] = 0;
$collection['Switzerland']['Ticino'] = 0;
$collection['Switzerland']['Uri'] = 0;
$collection['Switzerland']['Vaud'] = 0;
$collection['Switzerland']['Valais'] = 0;
$collection['Switzerland']['Zug'] = 0;
$collection['Switzerland']['Zurich'] = 0;
foreach(preg_split("/((\r?\n)|(\r\n?))/", trim($route_raw)) as $route){
$missing = false;
$point = explode(',', $route);
if(!is_numeric($point[0])){
$skipped++;
continue;
}
$count++;
$url = "http://api.geonames.org/countrySubdivision?lat=${point[1]}&lng=${point[0]}&username=$geonames_username";
echo "Read ($count): $url\n";
$geonames = simplexml_load_file($url);
$country = (string) $geonames->countrySubdivision->countryName;
$adminName = (string) $geonames->countrySubdivision->adminName1;
if(!$country) {
echo "Unknown Country!\n";
$country = 'Unknown Country';
$missing = true;
}
if(!$adminName) {
echo "Unknown Adminname!\n";
$adminName = 'Unknown';
$missing = true;
}
if($missing){
$unknown++;
var_dump($geonames);
}
if(!isset($collection[$country])) $collection[$country] = array();
if(!isset($collection[$country][$adminName])) $collection[$country][$adminName] = 1;
else $collection[$country][$adminName] += 1;
#if($count > 10) break;
}
#var_dump($collection);
if($skipped) echo "\n\nGot $skipped errors!\n\n";
if($unknown) echo "\n\nGot $unknown unknown locations!\n\n";
ksort($collection);
foreach($collection as $country_name => $country){
echo "\n**$country_name:**\n";
arsort($country);
foreach($country as $adminName => $times){
$fomat = $times?'':'***';
echo " $fomat$adminName: $times$fomat\n";
}
}
#ksort($collection['Switzerland']);
#echo implode("\n", array_keys($collection['Switzerland']));
echo "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment