5
function power($kw) {
if ($kw >= 100) return 5;
if ($kw >= 10) return 4;
if ($kw >= 1) return 3;
if ($kw >= .1) return 2;
if ($kw >= .01) return 1;
return 0;
}
function renderJS($stations) {
global $HEADER;
header('Content-type: application/javascript');
echo "/* ".$HEADER." */\n";
echo "var STATIONS = ".json_encode($stations);
}
function renderCSV($stations) {
header('Content-type: text/csv');
$keys = array_keys($stations[0]);
echo join(',', $keys)."\n";
foreach ($stations as $station) {
$values = array();
foreach ($keys as $key) {
array_push($values, $station[$key]);
}
echo join(',', $values)."\n";
}
}
function kmlPlacemark($parent, $name, $desc, $kw, $lat, $lon) {
// Get document for creating elements
$dom = $parent->ownerDocument;
// Create the Placemark
$pm = $parent->appendChild($dom->createElement('Placemark'));
// Create name, description
$pm->appendChild($dom->createElement('name',htmlentities($name)));
$pm->appendChild($dom->createElement('description', $desc));
// Set the icon style
$pm->appendChild($dom->createElement('styleUrl', '#style_'.power($kw)));
// Create point
$point = $pm->appendChild($dom->createElement('Point'))->appendChild(
$dom->createElement('coordinates', $lon.','.$lat)
);
}
function renderKML($stations) {
header('Content-type: application/vnd.google-earth.kml+xml');
//header('Content-type: text/plain');
// Creates the Document.
$dom = new DOMDocument('1.0', 'UTF-8');
// Creates the root KML element and appends it to the root document.
$kml = $dom->appendChild(
$dom->createElementNS('http://earth.google.com/kml/2.1', 'kml')
);
// Add atom namespace
$kml->setAttribute('xmlns:atom', 'http://www.w3.org/2005/Atom');
// Creates a KML Document element and append it to the KML element.
$docNode = $kml->appendChild(
$dom->createElement('Document')
);
// Add author info
$author = $docNode->appendChild(
$dom->createElement('atom:author')
);
$author->appendChild(
$dom->createElement('atom:name', 'http://www.broofa.com/Toys/NPRStations')
);
$link = $docNode->appendChild(
$dom->createElement('atom:link', 'http://www.broofa.com/Toys/NPRStations')
);
// Create style elements
for ($i = 0; $i <= 5; $i++) {
$styleNode = $dom->createElement('Style');
$styleNode->setAttribute('id', 'style_'.$i);
$iconStyleNode = $dom->createElement('IconStyle');
$iconStyleNode->setAttribute('id', 'icon_'.$i);
$iconNode = $dom->createElement('Icon');
$iconNode->appendChild(
$dom->createElement('href', powerImage($i))
);
$iconStyleNode->appendChild($iconNode);
$styleNode->appendChild($iconStyleNode);
$docNode->appendChild($styleNode);
}
// Process each station
$maxKw = 0;
$desc = array();
for ($i = 0; $i < count($stations); $i++) {
$station = $stations[$i];
$sign = $station['sign'];
$freq = $station['freq'];
$kw = $station['kw'];
$city = $station['city'];
$state = $station['state'];
$lat = $station['lat'];
$lon = $station['lon'];
// Combine stations in the same city
$maxKw = max($maxKw, $kw);
$power = power($kw);
array_push($desc,
'
'.$sign.' - '.$freq.' ('.($kw > 0 ? $kw : '?').' kw)'
);
// If this is the last entry for this city, generate a placemark for it
$next = $stations[$i+1];
if ($next == null || $city != $next['city'] || $state != $next['state']) {
// Write placemark (if we have placemark data)
if (count($desc) > 0) {
kmlPlacemark($docNode,
$city.", ".$state,
join('
', $desc),
$maxKw,
$lat,
$lon
);
}
// Reset the properties we accumulate for each city
$maxKw = 0;
$desc = array();
}
}
$kmlOutput = $dom->saveXML();
$kmlOutput = preg_replace('/>', ">\n<", $kmlOutput);
echo $kmlOutput;
}
switch ($_REQUEST['format']) {
case 'js':
case 'json': renderJS($STATIONS); break;
case 'kml': renderKML($STATIONS); break;
case 'csv':
default: renderCSV($STATIONS); break;
}
?>