X-Git-Url: https://git.rapsys.eu/packbundle/blobdiff_plain/b31612d28cf2a5617667dd9dae62379c90dbaf1c..72fbddfee92e8573c8ea87b7e321b404d94ed566:/Util/MapUtil.php diff --git a/Util/MapUtil.php b/Util/MapUtil.php index 5de2967..62e6b30 100644 --- a/Util/MapUtil.php +++ b/Util/MapUtil.php @@ -60,9 +60,14 @@ class MapUtil { const highStrokeWidth = 4; /** - * The map length + * The map width */ - const length = 640; + const width = 640; + + /** + * The map height + */ + const height = 640; /** * The osm tile server @@ -208,7 +213,7 @@ class MapUtil { } /** - * Return map url + * Get map data * * @param string $caption The caption * @param int $updated The updated timestamp @@ -217,14 +222,14 @@ class MapUtil { * @param int $zoom The zoom * @param int $width The width * @param int $height The height - * @return int The zoom + * @return array The map data */ - public function mapUrl(string $caption, int $updated, float $latitude, float $longitude, int $zoom = self::zoom, int $width = self::length, int $height = self::length): array { + public function getMap(string $caption, int $updated, float $latitude, float $longitude, int $zoom = self::zoom, int $width = self::width, int $height = self::height): array { //Set link hash - $link = $this->slugger->serialize([$updated, $latitude, $longitude, $zoom + 1, $width * 2, $height * 2]); + $link = $this->slugger->hash([$updated, $latitude, $longitude, $zoom + 1, $width * 2, $height * 2]); //Set src hash - $src = $this->slugger->serialize([$updated, $latitude, $longitude, $zoom, $width, $height]); + $src = $this->slugger->hash([$updated, $latitude, $longitude, $zoom, $width, $height]); //Return array return [ @@ -237,19 +242,31 @@ class MapUtil { } /** - * Return multi map url + * Get multi map data * * @param string $caption The caption * @param int $updated The updated timestamp - * @param float $latitude The latitude - * @param float $longitude The longitude * @param array $coordinates The coordinates array - * @param int $zoom The zoom * @param int $width The width * @param int $height The height - * @return int The zoom + * @return array The multi map data */ - public function multiMapUrl(string $caption, int $updated, float $latitude, float $longitude, $coordinates = [], int $zoom = self::zoom, int $width = self::length, int $height = self::length): array { + public function getMultiMap(string $caption, int $updated, array $coordinates, int $width = self::width, int $height = self::height): array { + //Set latitudes + $latitudes = array_map(function ($v) { return $v['latitude']; }, $coordinates); + + //Set longitudes + $longitudes = array_map(function ($v) { return $v['longitude']; }, $coordinates); + + //Set latitude + $latitude = round((min($latitudes)+max($latitudes))/2, 6); + + //Set longitude + $longitude = round((min($longitudes)+max($longitudes))/2, 6); + + //Set zoom + $zoom = $this->getMultiZoom($latitude, $longitude, $coordinates, $width, $height); + //Set coordinate $coordinate = implode('-', array_map(function ($v) { return $v['latitude'].','.$v['longitude']; }, $coordinates)); @@ -257,10 +274,10 @@ class MapUtil { $hash = $this->slugger->hash($coordinate); //Set link hash - $link = $this->slugger->serialize([$updated, $latitude, $longitude, $hash, $zoom + 1, $width * 2, $height * 2]); + $link = $this->slugger->hash([$updated, $latitude, $longitude, $hash, $zoom + 1, $width * 2, $height * 2]); //Set src hash - $src = $this->slugger->serialize([$updated, $latitude, $longitude, $hash, $zoom, $width, $height]); + $src = $this->slugger->hash([$updated, $latitude, $longitude, $hash, $zoom, $width, $height]); //Return array return [ @@ -273,7 +290,7 @@ class MapUtil { } /** - * Return multi map zoom + * Get multi zoom * * Compute a zoom to have all coordinates on multi map * Multi map visible only from -($width / 2) until ($width / 2) and from -($height / 2) until ($height / 2) @@ -283,12 +300,12 @@ class MapUtil { * @param float $latitude The latitude * @param float $longitude The longitude * @param array $coordinates The coordinates array - * @param int $zoom The zoom * @param int $width The width * @param int $height The height + * @param int $zoom The zoom * @return int The zoom */ - public function multiMapZoom(float $latitude, float $longitude, array $coordinates = [], int $zoom = self::zoom, int $width = self::length, int $height = self::length): int { + public function getMultiZoom(float $latitude, float $longitude, array $coordinates, int $width, int $height, int $zoom = self::zoom): int { //Iterate on each zoom for ($i = $zoom; $i >= 1; $i--) { //Get tile xy @@ -393,13 +410,14 @@ class MapUtil { */ public static function latitudeToSexagesimal(float $latitude): string { //Set degree - $degree = $latitude % 60; + //TODO: see if round or intval is better suited to fix the Deprecated: Implicit conversion from float to int loses precision + $degree = round($latitude) % 60; //Set minute - $minute = ($latitude - $degree) * 60 % 60; + $minute = round(($latitude - $degree) * 60) % 60; //Set second - $second = ($latitude - $degree - $minute / 60) * 3600 % 3600; + $second = round(($latitude - $degree - $minute / 60) * 3600) % 3600; //Return sexagesimal longitude return $degree.'°'.$minute.'\''.$second.'"'.($latitude >= 0 ? 'N' : 'S'); @@ -414,13 +432,14 @@ class MapUtil { */ public static function longitudeToSexagesimal(float $longitude): string { //Set degree - $degree = $longitude % 60; + //TODO: see if round or intval is better suited to fix the Deprecated: Implicit conversion from float to int loses precision + $degree = round($longitude) % 60; //Set minute - $minute = ($longitude - $degree) * 60 % 60; + $minute = round(($longitude - $degree) * 60) % 60; //Set second - $second = ($longitude - $degree - $minute / 60) * 3600 % 3600; + $second = round(($longitude - $degree - $minute / 60) * 3600) % 3600; //Return sexagesimal longitude return $degree.'°'.$minute.'\''.$second.'"'.($longitude >= 0 ? 'E' : 'W');