Skip to content

Instantly share code, notes, and snippets.

@pjaudiomv
Created September 8, 2022 19:11
Show Gist options
  • Save pjaudiomv/0dc724240d07c2db68af66890d083436 to your computer and use it in GitHub Desktop.
Save pjaudiomv/0dc724240d07c2db68af66890d083436 to your computer and use it in GitHub Desktop.
BMLT JSON 2 KML
<?php
$bmlt_server = "https://latest.aws.bmlt.app/main_server";
$search_query = "&services=1010&weekdays=2";
$query_url = urldecode($bmlt_server . "/client_interface/json/?switcher=GetSearchResults" . $search_query);
$result = '';
if (str_contains($query_url, "/client_interface/json/?switcher=GetSearchResults")) {
$meetings = file_get_contents($query_url);
$meetings = json_decode($meetings, true);
if (is_array($meetings) && count($meetings)) {
$result .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
$result .= '<kml xmlns="http://www.opengis.net/kml/2.2">';
$result .= '<Document>';
foreach ($meetings as $meeting) {
$desc = prepareSimpleLine($meeting);
$address = prepareSimpleLine($meeting, false);
$name = htmlspecialchars(trim(stripslashes($meeting['meeting_name'])));
if (!$name) {
$name = "NA Meeting";
}
$lng = floatval($meeting['longitude']);
$lat = floatval($meeting['latitude']);
if ($lng || $lat) {
$result .= '<Placemark>';
$result .= '<name>'.htmlspecialchars($name).'</name>';
if ($address) {
$result .= '<address>'.$address.'</address>';
}
if ($desc) {
$result .= '<description>'.$desc.'</description>';
}
$result .= '<Point>';
$result .= '<coordinates>';
$result .= htmlspecialchars($lng).','.htmlspecialchars($lat).',0';
$result .= '</coordinates>';
$result .= '</Point>';
$result .= '</Placemark>';
}
}
$result .= '</Document>';
$result .= '</kml>';
}
}
echo $result;
function prepareSimpleLine(
$meeting, /**< An associative array of meeting data */
$withDate = true /**< If false (default is true), the weekday and time will not be added. */
) {
$location_borough = array_key_exists('location_city_subsection', $meeting) ? htmlspecialchars(trim(stripslashes($meeting['location_city_subsection']))) : "";
$location_neighborhood = array_key_exists('location_neighborhood', $meeting) ? htmlspecialchars(trim(stripslashes($meeting['location_neighborhood']))) : "";
$location_province = array_key_exists('location_province', $meeting) ? htmlspecialchars(trim(stripslashes($meeting['location_province']))) : "";
$location_nation = array_key_exists('location_nation', $meeting) ? htmlspecialchars(trim(stripslashes($meeting['location_nation']))) : "";
$location_postal_code_1 = array_key_exists('location_postal_code_1', $meeting) ? htmlspecialchars(trim(stripslashes($meeting['location_postal_code_1']))) : "";
$location_text = array_key_exists('location_text', $meeting) ? htmlspecialchars(trim(stripslashes($meeting['location_text']))) : "";
$street = array_key_exists('location_street', $meeting) ? htmlspecialchars(trim(stripslashes($meeting['location_street']))) : "";
$info = array_key_exists('location_info', $meeting) ? htmlspecialchars(trim(stripslashes($meeting['location_info']))) : "";
$town = array_key_exists('location_municipality', $meeting) ? htmlspecialchars(trim(stripslashes($meeting['location_municipality']))) : "";
$desc = $withDate ? '' : $location_text;
if ($location_borough) {
$town = $location_borough;
}
if ($location_province) {
$town = "$town, $location_province";
}
if ($location_postal_code_1) {
$town = "$town, $location_postal_code_1";
}
if ($location_nation) {
$town = "$town, $location_nation";
}
if ($withDate && $location_neighborhood) {
$town = "$town ($location_neighborhood)";
}
if ($street) {
if ($desc) {
$desc .= ", ";
}
$desc .= $street;
}
if ($town) {
if ($desc) {
$desc .= ", ";
}
$desc .= $town;
}
if ($withDate && $info) {
if ($desc) {
$desc .= " ($info)";
} else {
$desc = $info;
}
}
$weekday = intval(trim(stripslashes($meeting['weekday_tinyint'])));
$time = date('g:i A', strtotime($meeting['start_time']));
$weekday_strings = array('All', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
$weekday = $weekday_strings[$weekday];
$ret = null;
if ($withDate && $weekday) {
$ret = $weekday;
}
if ($withDate && $time) {
if ($ret) {
$ret .= ', ';
}
$ret .= $time;
}
if ($ret) {
$ret .= ', ';
}
$ret .= $desc;
return $ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment