Skip to content

Instantly share code, notes, and snippets.

@mheadd
Created February 15, 2012 20:56
Show Gist options
  • Select an option

  • Save mheadd/1838957 to your computer and use it in GitHub Desktop.

Select an option

Save mheadd/1838957 to your computer and use it in GitHub Desktop.
Put a JSON facade on top of of Hampton Roads Transit Data
<?php
// FTP URI to transit data.
define("TRANSIT_FTP_URI", "ftp://216.54.15.3/Anrd/hrtrtf.txt");
// Parse a string and return specific element.
function parseString($string, $num) {
$pieces = explode(" ", trim($string));
return $pieces[$num];
}
// Get the transit data.
$data = file_get_contents(TRANSIT_FTP_URI);
$rows = explode("\n", $data);
// An array to hold vehicle locations.
$positons = array();
// Iterate over each record in the file and parse out vehicle info.
foreach($rows as $row) {
$record = explode(" ", $row);
$type = parseString($record[0], 3);
$vehicle_id = parseString($record[0], 1);
if($type == "MT_LOCATION") {
$coordinates = parseString($record[1], 0);
}
else {
$coordinates = parseString($record[1], 8);
}
// Assumes a specific level of precision.
$lat = substr($coordinates, (strpos($coordinates, ':')+1), 9);
$lon = substr($coordinates, (strpos($coordinates, '/-')+1), 9); ;
array_push($positons, array("type" => $type, "id" => $vehicle_id, "lat" => $lat, "lon" => $lon));
}
// Make some JSON and return it.
$positions_json = json_encode($positons);
header('Content-type: applicaiton/json');
echo $positions_json;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment