X-Git-Url: https://git.rapsys.eu/packbundle/blobdiff_plain/4b3b3f7b79348080ea7d78bcbf9921797a0caa34..0ae04c58e6b885c2a5ec71a79252bda1058104a6:/Util/MapUtil.php?ds=inline diff --git a/Util/MapUtil.php b/Util/MapUtil.php index f736a07..b7d2232 100644 --- a/Util/MapUtil.php +++ b/Util/MapUtil.php @@ -14,7 +14,7 @@ namespace Rapsys\PackBundle\Util; use Symfony\Component\Routing\RouterInterface; /** - * Helps manage map + * Manages map */ class MapUtil { /** @@ -55,7 +55,7 @@ class MapUtil { const highStroke = '#3333c3'; /** - * The high stroke size + * The high stroke width */ const highStrokeWidth = 4; @@ -87,7 +87,7 @@ class MapUtil { const stroke = '#00c3f9'; /** - * The stroke size + * The stroke width */ const strokeWidth = 2; @@ -109,107 +109,82 @@ class MapUtil { const zoom = 17; /** - * The RouterInterface instance - */ - protected RouterInterface $router; - - /** - * The SluggerUtil instance + * Creates a new map util + * + * @param RouterInterface $router The RouterInterface instance + * @param SluggerUtil $slugger The SluggerUtil instance */ - protected SluggerUtil $slugger; + function __construct(protected RouterInterface $router, protected SluggerUtil $slugger, protected string $fill = self::fill, protected int $fontSize = self::fontSize, protected string $highFill = self::highFill, protected int $highFontSize = self::highFontSize, protected int $highRadius = self::highRadius, protected string $highStroke = self::highStroke, protected int $highStrokeWidth = self::highStrokeWidth, protected int $radius = self::radius, protected string $stroke = self::stroke, protected int $strokeWidth = self::strokeWidth) { + } /** - * The fill color + * Get fill color */ - public string $fill; + function getFill() { + return $this->fill; + } /** - * The font size + * Get font size */ - public int $fontSize; + function getFontSize() { + return $this->fontSize; + } /** - * The high fill color + * Get high fill color */ - public string $highFill; + function getHighFill() { + return $this->highFill; + } /** - * The font size + * Get high font size */ - public int $highFontSize; + function getHighFontSize() { + return $this->highFontSize; + } /** - * The radius size + * Get high radius size */ - public int $highRadius; + function getHighRadius() { + return $this->highRadius; + } /** - * The high stroke color + * Get high stroke color */ - public string $highStroke; + function getHighStroke() { + return $this->highStroke; + } /** - * The stroke size + * Get high stroke width */ - public int $highStrokeWidth; + function getHighStrokeWidth() { + return $this->highStrokeWidth; + } /** - * The stroke color + * Get radius size */ - public string $stroke; + function getRadius() { + return $this->radius; + } /** - * The stroke size + * Get stroke color */ - public int $strokeWidth; + function getStroke() { + return $this->stroke; + } /** - * The radius size + * Get stroke width */ - public int $radius; - - /** - * Creates a new map util - * - * @param RouterInterface $router The RouterInterface instance - * @param SluggerUtil $slugger The SluggerUtil instance - */ - function __construct(RouterInterface $router, SluggerUtil $slugger, string $fill = self::fill, int $fontSize = self::fontSize, string $highFill = self::highFill, int $highFontSize = self::highFontSize, int $highRadius = self::highRadius, string $highStroke = self::highStroke, int $highStrokeWidth = self::highStrokeWidth, int $radius = self::radius, string $stroke = self::stroke, int $strokeWidth = self::strokeWidth) { - //Set router - $this->router = $router; - - //Set slugger - $this->slugger = $slugger; - - //Set fill - $this->fill = $fill; - - //Set font size - $this->fontSize = $fontSize; - - //Set highFill - $this->highFill = $highFill; - - //Set high font size - $this->highFontSize = $highFontSize; - - //Set high radius size - $this->highRadius = $highRadius; - - //Set highStroke - $this->highStroke = $highStroke; - - //Set high stroke size - $this->highStrokeWidth = $highStrokeWidth; - - //Set radius size - $this->radius = $radius; - - //Set stroke - $this->stroke = $stroke; - - //Set stroke size - $this->strokeWidth = $strokeWidth; + function getStrokeWidth() { + return $this->strokeWidth; } /** @@ -234,8 +209,8 @@ class MapUtil { //Return array return [ 'caption' => $caption, - 'link' => $this->router->generate('rapsys_pack_map', ['hash' => $link, 'updated' => $updated, 'latitude' => $latitude, 'longitude' => $longitude, 'zoom' => $zoom + 1, 'width' => $width * 2, 'height' => $height * 2]), - 'src' => $this->router->generate('rapsys_pack_map', ['hash' => $src, 'updated' => $updated, 'latitude' => $latitude, 'longitude' => $longitude, 'zoom' => $zoom, 'width' => $width, 'height' => $height]), + 'link' => $this->router->generate('rapsyspack_map', ['hash' => $link, 'updated' => $updated, 'latitude' => $latitude, 'longitude' => $longitude, 'zoom' => $zoom + 1, 'width' => $width * 2, 'height' => $height * 2]), + 'src' => $this->router->generate('rapsyspack_map', ['hash' => $src, 'updated' => $updated, 'latitude' => $latitude, 'longitude' => $longitude, 'zoom' => $zoom, 'width' => $width, 'height' => $height]), 'width' => $width, 'height' => $height ]; @@ -246,15 +221,33 @@ class MapUtil { * * @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 array The multi map data */ - public function getMultiMap(string $caption, int $updated, float $latitude, float $longitude, $coordinates = [], int $zoom = self::zoom, int $width = self::width, int $height = self::height): array { + public function getMultiMap(string $caption, int $updated, array $coordinates, int $width = self::width, int $height = self::height): array { + //Without coordinates + if (empty($coordinates)) { + //Return empty array + return []; + } + + //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)); @@ -270,8 +263,8 @@ class MapUtil { //Return array return [ 'caption' => $caption, - 'link' => $this->router->generate('rapsys_pack_multimap', ['hash' => $link, 'updated' => $updated, 'latitude' => $latitude, 'longitude' => $longitude, 'coordinates' => $coordinate, 'zoom' => $zoom + 1, 'width' => $width * 2, 'height' => $height * 2]), - 'src' => $this->router->generate('rapsys_pack_multimap', ['hash' => $src, 'updated' => $updated, 'latitude' => $latitude, 'longitude' => $longitude, 'coordinates' => $coordinate, 'zoom' => $zoom, 'width' => $width, 'height' => $height]), + 'link' => $this->router->generate('rapsyspack_multimap', ['hash' => $link, 'updated' => $updated, 'latitude' => $latitude, 'longitude' => $longitude, 'coordinates' => $coordinate, 'zoom' => $zoom + 1, 'width' => $width * 2, 'height' => $height * 2]), + 'src' => $this->router->generate('rapsyspack_multimap', ['hash' => $src, 'updated' => $updated, 'latitude' => $latitude, 'longitude' => $longitude, 'coordinates' => $coordinate, 'zoom' => $zoom, 'width' => $width, 'height' => $height]), 'width' => $width, 'height' => $height ]; @@ -288,12 +281,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 getMultiZoom(float $latitude, float $longitude, array $coordinates = [], int $zoom = self::zoom, int $width = self::width, int $height = self::height): 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 @@ -398,13 +391,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'); @@ -419,13 +413,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');