174 lines
6.4 KiB
PHP
174 lines
6.4 KiB
PHP
<?php
|
|
|
|
class Mapbox_StaticImageApi {
|
|
|
|
public static function getImageFileContent(Array $params) : ?string {
|
|
$access_token = $params["access_token"];
|
|
$gps_lat = $params["gps_lat"];
|
|
$gps_long = $params["gps_long"];
|
|
$zoom = $params["zoom"];
|
|
$size_x = $params["size_x"];
|
|
$size_y = $params["size_y"];
|
|
$style = $params["style"];
|
|
$pin = $params["pin"];
|
|
$paths = $params["paths"];
|
|
|
|
$pin_part = "";
|
|
|
|
// https://api.mapbox.com/styles/v1/mapbox/satellite-streets-v12/static/pin-l-embassy+ee9900(15.4200370610,46.9599897293)/15.4200370610,46.9599897293,19/640x640?access_token=pk.eyJ1Ijoic2tuZXR3b3J4IiwiYSI6ImNqdWw3eXoyMzBieWU0M284OTU2eWNxMTMifQ.GrgBYKMXVt3TiwU53OBllQ
|
|
$url = "https://api.mapbox.com/styles/v1/mapbox/$style/static";
|
|
if(is_array($pin) && $pin["gps_lat"] && $pin["gps_long"] && $pin["size"]) {
|
|
$pin_gps_lat = $pin["gps_lat"];
|
|
$pin_gps_long = $pin["gps_long"];
|
|
$pin_size = $pin["size"];
|
|
$pin_color = $pin["color"];
|
|
$pin_icon = $pin["icon"];
|
|
|
|
$pin_part .= "pin-$pin_size";
|
|
if($pin_icon) $pin_part .= "-$pin_icon";
|
|
$pin_part .= "+$pin_color($pin_gps_long,$pin_gps_lat)";
|
|
}
|
|
|
|
$initial_path_count = 0;
|
|
$path_count = 0;
|
|
$path_parts = [];
|
|
if(is_array($paths) && count($paths)) {
|
|
$path_stroke_width = $paths["line_width"];
|
|
$path_stroke_color = $paths["line_color"];
|
|
$path_stroke_opacity = $paths["line_opacity"];
|
|
$path_fill_color = $paths["line_fill_color"];
|
|
$path_fill_opacity = $paths["line_fill_opacity"];
|
|
|
|
|
|
|
|
foreach($paths["coords"] as $path) {
|
|
$initial_path_count++;
|
|
//mfLoghandler::singleton()->debug(print_r($paths, true));
|
|
|
|
$skip_path = false;
|
|
// skip if coordinates are outside of 111 meters of middle point
|
|
// because mapbox only allows 100 paths max
|
|
foreach($path as $coords) {
|
|
$lat_less = $gps_lat;
|
|
$lat_more = $coords[0];
|
|
$long_less = $gps_long;
|
|
$long_more = $coords[1];
|
|
|
|
if($coords[0] < $gps_lat) {
|
|
$lat_less = $coords[0];
|
|
$lat_more = $gps_lat;
|
|
}
|
|
if($coords[1] < $gps_long) {
|
|
$long_less = $coords[1];
|
|
$long_more = $gps_long;
|
|
}
|
|
|
|
mfLoghandler::singleton()->debug(__METHOD__.": lat_more($lat_more) - lat_less($lat_less) = ".($lat_more - $lat_less));
|
|
mfLoghandler::singleton()->debug(__METHOD__.": long_more($long_more) - long_less($long_less) = ".($long_more - $long_less));
|
|
|
|
if($lat_more - $lat_less > 0.0005 || $long_more - $long_less > 0.0005) {
|
|
mfLoghandler::singleton()->debug(__METHOD__.": Skipping path");
|
|
$skip_path = true;
|
|
}
|
|
|
|
}
|
|
if($skip_path) continue;
|
|
|
|
if(!is_array($path) || !count($path)) {
|
|
mfLoghandler::singleton()->debug("path not array or empty: ".print_r($path, true));
|
|
continue;
|
|
}
|
|
|
|
if($path_count > 99) break;
|
|
$path_count++;
|
|
|
|
$path_enc_polyline = urlencode(self::encodeCoordArrayToPolyline($path));
|
|
//$path_enc_polyline = self::encodeCoordArrayToPolyline($path);
|
|
//if(!$path_enc_polyline) continue;
|
|
//$pp = "path-$path_stroke_width+$path_stroke_color-$path_stroke_opacity+$path_fill_color-$path_fill_opacity($path_enc_polyline)"
|
|
$pp = "path-$path_stroke_width+$path_stroke_color-$path_stroke_opacity($path_enc_polyline)";
|
|
$path_parts[] = $pp;
|
|
}
|
|
}
|
|
|
|
mfLoghandler::singleton()->debug(__METHOD__.": path count: $path_count | initial path count: $initial_path_count");
|
|
|
|
// build url
|
|
$url_opt_parts = [];
|
|
if($pin_part) $url_opt_parts[] = $pin_part;
|
|
|
|
if(count($path_parts)) {
|
|
foreach($path_parts as $path_part) {
|
|
$url_opt_parts[] = $path_part;
|
|
}
|
|
}
|
|
|
|
if(is_array($url_opt_parts) && count($url_opt_parts)) {
|
|
$url .= "/".implode(",", $url_opt_parts);
|
|
}
|
|
|
|
$url .= "/$gps_long,$gps_lat,$zoom/{$size_x}x{$size_y}?access_token=$access_token";
|
|
//$url .= "/auto/{$size_x}x{$size_y}?access_token=$access_token";
|
|
|
|
mfLoghandler::singleton()->debug($url);
|
|
//exit;
|
|
|
|
$ctx_opts = [
|
|
'http' => [
|
|
'method' => 'GET',
|
|
'header' => 'accept: application/json'
|
|
]
|
|
];
|
|
|
|
$ctx = stream_context_create($ctx_opts);
|
|
$response = file_get_contents($url, false, $ctx);
|
|
//fLoghandler::singleton()->debug(print_r($response, true));
|
|
|
|
if($response === false) {
|
|
return null;
|
|
}
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
public static function encodeCoordArrayToPolyline($coords, $precision = 5) {
|
|
//if(!is_array($coords)) return false;
|
|
|
|
$points = [];
|
|
array_walk_recursive(
|
|
$coords,
|
|
function ($current) use (&$points) {
|
|
$points[] = $current;
|
|
}
|
|
);
|
|
|
|
mfLoghandler::singleton()->debug(__METHOD__.": flattened path: ".print_r($points, true));
|
|
|
|
$encodedString = '';
|
|
$index = 0;
|
|
$previous = array(0,0);
|
|
foreach ( $points as $number ) {
|
|
$number = (float)($number);
|
|
$number = (int)round($number * pow(10, $precision));
|
|
$diff = $number - $previous[$index % 2];
|
|
$previous[$index % 2] = $number;
|
|
$number = $diff;
|
|
$index++;
|
|
$number = ($number < 0) ? ~($number << 1) : ($number << 1);
|
|
$chunk = '';
|
|
while ( $number >= 0x20 ) {
|
|
$chunk .= chr((0x20 | ($number & 0x1f)) + 63);
|
|
$number >>= 5;
|
|
}
|
|
$chunk .= chr($number + 63);
|
|
$encodedString .= $chunk;
|
|
}
|
|
|
|
mfLoghandler::singleton()->debug(__METHOD__.": encoded polyline: $encodedString");
|
|
|
|
|
|
//return str_replace("?", "%3f", $encodedString);
|
|
return $encodedString;
|
|
}
|
|
} |