From 42a99324bb26ad7305e738882ac2de332ce5cc38 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Wed, 15 Sep 2021 16:58:45 +0200 Subject: [PATCH] Add map controller --- Controller/MapController.php | 519 +++++++++++++++++++++++++++++++++++ 1 file changed, 519 insertions(+) create mode 100644 Controller/MapController.php diff --git a/Controller/MapController.php b/Controller/MapController.php new file mode 100644 index 0000000..7bd6fe7 --- /dev/null +++ b/Controller/MapController.php @@ -0,0 +1,519 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Rapsys\PackBundle\Controller; + +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\Filesystem\Exception\IOExceptionInterface; +use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\HttpFoundation\BinaryFileResponse; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; +use Symfony\Component\Routing\RequestContext; +use Symfony\Contracts\Service\ServiceSubscriberInterface; + +use Rapsys\PackBundle\Util\MapUtil; +use Rapsys\PackBundle\Util\SluggerUtil; + +/** + * {@inheritdoc} + */ +class MapController extends AbstractController implements ServiceSubscriberInterface { + /** + * The cache path + */ + protected string $cache; + + /** + * The ContainerInterface instance + * + * @var ContainerInterface + */ + protected $container; + + /** + * The stream context instance + */ + protected mixed $ctx; + + /** + * The MapUtil instance + */ + protected MapUtil $map; + + /** + * The public path + */ + protected string $public; + + /** + * The SluggerUtil instance + */ + protected SluggerUtil $slugger; + + /** + * The tile server url + */ + protected string $url; + + /** + * Creates a new osm util + * + * @param ContainerInterface $container The ContainerInterface instance + * @param MapUtil $map The MapUtil instance + * @param SluggerUtil $slugger The SluggerUtil instance + * @param string $cache The cache path + * @param string $public The public path + * @param string $url The tile server url + */ + function __construct(ContainerInterface $container, MapUtil $map, SluggerUtil $slugger, string $cache = '../var/cache/map', string $public = './bundles/rapsyspack/map', string $url = MapUtil::osm) { + //Set cache + $this->cache = $cache; + + //Set container + $this->container = $container; + + //Set ctx + $this->ctx = stream_context_create( + [ + 'http' => [ + #'header' => ['Referer: https://www.openstreetmap.org/'], + 'max_redirects' => 5, + 'timeout' => (int)ini_get('default_socket_timeout'), + #'user_agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36', + 'user_agent' => (string)ini_get('user_agent')?:'rapsys_pack/2.0.0', + ] + ] + ); + + //Set map + $this->map = $map; + + //Set public + $this->public = $public; + + //Set slugger + $this->slugger = $slugger; + + //Set url + $this->url = $url; + } + + /** + * Return map image + * + * @param Request $request The Request instance + * @param string $hash The hash + * @param int $updated The updated timestamp + * @param float $latitude The latitude + * @param float $longitude The longitude + * @param int $zoom The zoom + * @param int $width The width + * @param int $height The height + * @return Response The rendered image + */ + public function map(Request $request, string $hash, int $updated, float $latitude, float $longitude, int $zoom, int $width, int $height): Response { + //Without matching hash + if ($hash !== $this->slugger->serialize([$updated, $latitude, $longitude, $zoom, $width, $height])) { + //Throw new exception + throw new NotFoundHttpException(sprintf('Unable to match map hash: %s', $hash)); + } + + //Set map + $map = $this->public.'/'.$zoom.'/'.$latitude.'/'.$longitude.'/'.$width.'x'.$height.'.jpeg'; + + //With map up to date file + if (is_file($map) && ($mtime = stat($map)['mtime']) && $mtime >= $updated) { + //Read map from cache + //TODO: handle modified, etag, cache, etc ??? + return new BinaryFileResponse($map); + } + + //Without existing map + if (!is_dir($dir = dirname($map))) { + //Create filesystem object + $filesystem = new Filesystem(); + + try { + //Create path + //XXX: set as 0775, symfony umask (0022) will reduce rights (0755) + //XXX: on CoW filesystems execute a chattr +C before filling + $filesystem->mkdir($dir, 0775); + } catch (IOExceptionInterface $e) { + //Throw error + throw new \Exception(sprintf('Output path "%s" do not exists and unable to create it', $dir), 0, $e); + } + } + + //Create image instance + $image = new \Imagick(); + + //Add new image + $image->newImage($width, $height, new \ImagickPixel('transparent'), 'jpeg'); + + //Create tile instance + $tile = new \Imagick(); + + //Get tile xy + $centerX = $this->map->longitudeToX($longitude, $zoom); + $centerY = $this->map->latitudeToY($latitude, $zoom); + + //Calculate start xy + $startX = floor(floor($centerX) - $width / MapUtil::tz); + $startY = floor(floor($centerY) - $height / MapUtil::tz); + + //Calculate end xy + $endX = ceil(ceil($centerX) + $width / MapUtil::tz); + $endY = ceil(ceil($centerY) + $height / MapUtil::tz); + + for($x = $startX; $x <= $endX; $x++) { + for($y = $startY; $y <= $endY; $y++) { + //Set cache path + $cache = $this->cache.'/'.$zoom.'/'.$x.'/'.$y.'.png'; + + //Without cache image + if (!is_file($cache)) { + //Set tile url + $tileUri = str_replace(['{Z}', '{X}', '{Y}'], [$zoom, $x, $y], $this->url); + + //Without cache path + if (!is_dir($dir = dirname($cache))) { + //Create filesystem object + $filesystem = new Filesystem(); + + try { + //Create path + //XXX: set as 0775, symfony umask (0022) will reduce rights (0755) + $filesystem->mkdir($dir, 0775); + } catch (IOExceptionInterface $e) { + //Throw error + throw new \Exception(sprintf('Output directory "%s" do not exists and unable to create it', $dir), 0, $e); + } + } + + //Store tile in cache + file_put_contents($cache, file_get_contents($tileUri, false, $this->ctx)); + } + + //Set dest x + $destX = intval(floor($width / 2 - MapUtil::tz * ($centerX - $x))); + + //Set dest y + $destY = intval(floor($height / 2 - MapUtil::tz * ($centerY - $y))); + + //Read tile from cache + $tile->readImage($cache); + + //Compose image + $image->compositeImage($tile, \Imagick::COMPOSITE_OVER, $destX, $destY); + + //Clear tile + $tile->clear(); + } + } + + //Add imagick draw instance + //XXX: see https://www.php.net/manual/fr/imagick.examples-1.php#example-3916 + $draw = new \ImagickDraw(); + + //Set text antialias + $draw->setTextAntialias(true); + + //Set stroke antialias + $draw->setStrokeAntialias(true); + + //Set text alignment + $draw->setTextAlignment(\Imagick::ALIGN_CENTER); + + //Set gravity + $draw->setGravity(\Imagick::GRAVITY_CENTER); + + //Set fill color + $draw->setFillColor('#cff'); + + //Set stroke color + $draw->setStrokeColor('#00c3f9'); + + //Set stroke width + $draw->setStrokeWidth(2); + + //Draw circle + $draw->circle($width/2 - 5, $height/2 - 5, $width/2 + 5, $height/2 + 5); + + //Draw on image + $image->drawImage($draw); + + //Strip image exif data and properties + $image->stripImage(); + + //Add latitude + //XXX: not supported by imagick :'( + $image->setImageProperty('exif:GPSLatitude', $this->map->latitudeToSexagesimal($latitude)); + + //Add longitude + //XXX: not supported by imagick :'( + $image->setImageProperty('exif:GPSLongitude', $this->map->longitudeToSexagesimal($longitude)); + + //Add description + //XXX: not supported by imagick :'( + #$image->setImageProperty('exif:Description', $caption); + + //Set progressive jpeg + $image->setInterlaceScheme(\Imagick::INTERLACE_PLANE); + + //Set compression quality + //TODO: ajust that + $image->setImageCompressionQuality(70); + + //Save image + if (!$image->writeImage($map)) { + //Throw error + throw new \Exception(sprintf('Unable to write image "%s"', $path)); + } + + //Return response + //TODO: générer l'image ici à partir du cache :p + #return new Response($image->getImageBlob()); + return new BinaryFileResponse($map); + } + + /** + * Return multi map image + * + * @param Request $request The Request instance + * @param string $hash The hash + * @param int $updated The updated timestamp + * @param float $latitude The latitude + * @param float $longitude The longitude + * @param string $coordinates The coordinates + * @param int $zoom The zoom + * @param int $width The width + * @param int $height The height + * @return Response The rendered image + */ + public function multiMap(Request $request, string $hash, int $updated, float $latitude, float $longitude, string $coordinates, int $zoom, int $width, int $height): Response { + //Without matching hash + if ($hash !== $this->slugger->serialize([$updated, $latitude, $longitude, $coordinate = $this->slugger->hash($coordinates), $zoom, $width, $height])) { + //Throw new exception + throw new NotFoundHttpException(sprintf('Unable to match multi map hash: %s', $hash)); + } + + //Set multi + $map = $this->public.'/'.$zoom.'/'.$latitude.'/'.$longitude.'/'.$coordinate.'/'.$width.'x'.$height.'.jpeg'; + + //With multi up to date file + if (is_file($map) && ($mtime = stat($map)['mtime']) && $mtime >= $updated) { + //Read multi from cache + //TODO: handle modified, etag, cache, etc ??? + return new BinaryFileResponse($map); + } + + //Without existing multi + if (!is_dir($dir = dirname($map))) { + //Create filesystem object + $filesystem = new Filesystem(); + + try { + //Create path + //XXX: set as 0775, symfony umask (0022) will reduce rights (0755) + //XXX: on CoW filesystems execute a chattr +C before filling + $filesystem->mkdir($dir, 0775); + } catch (IOExceptionInterface $e) { + //Throw error + throw new \Exception(sprintf('Output path "%s" do not exists and unable to create it', $dir), 0, $e); + } + } + + //Create image instance + $image = new \Imagick(); + + //Add new image + $image->newImage($width, $height, new \ImagickPixel('transparent'), 'jpeg'); + + //Create tile instance + $tile = new \Imagick(); + + //Get tile xy + $centerX = $this->map->longitudeToX($longitude, $zoom); + $centerY = $this->map->latitudeToY($latitude, $zoom); + + //Calculate start xy + $startX = floor(floor($centerX) - $width / MapUtil::tz); + $startY = floor(floor($centerY) - $height / MapUtil::tz); + + //Calculate end xy + $endX = ceil(ceil($centerX) + $width / MapUtil::tz); + $endY = ceil(ceil($centerY) + $height / MapUtil::tz); + + for($x = $startX; $x <= $endX; $x++) { + for($y = $startY; $y <= $endY; $y++) { + //Set cache path + $cache = $this->cache.'/'.$zoom.'/'.$x.'/'.$y.'.png'; + + //Without cache image + if (!is_file($cache)) { + //Set tile url + $tileUri = str_replace(['{Z}', '{X}', '{Y}'], [$zoom, $x, $y], $this->url); + + //Without cache path + if (!is_dir($dir = dirname($cache))) { + //Create filesystem object + $filesystem = new Filesystem(); + + try { + //Create path + //XXX: set as 0775, symfony umask (0022) will reduce rights (0755) + $filesystem->mkdir($dir, 0775); + } catch (IOExceptionInterface $e) { + //Throw error + throw new \Exception(sprintf('Output directory "%s" do not exists and unable to create it', $dir), 0, $e); + } + } + + //Store tile in cache + file_put_contents($cache, file_get_contents($tileUri, false, $this->ctx)); + } + + //Set dest x + $destX = intval(floor($width / 2 - MapUtil::tz * ($centerX - $x))); + + //Set dest y + $destY = intval(floor($height / 2 - MapUtil::tz * ($centerY - $y))); + + //Read tile from cache + $tile->readImage($cache); + + //Compose image + $image->compositeImage($tile, \Imagick::COMPOSITE_OVER, $destX, $destY); + + //Clear tile + $tile->clear(); + } + } + + //Add imagick draw instance + //XXX: see https://www.php.net/manual/fr/imagick.examples-1.php#example-3916 + $draw = new \ImagickDraw(); + + //Set text antialias + $draw->setTextAntialias(true); + + //Set stroke antialias + $draw->setStrokeAntialias(true); + + //Set text alignment + $draw->setTextAlignment(\Imagick::ALIGN_CENTER); + + //Set gravity + $draw->setGravity(\Imagick::GRAVITY_CENTER); + + //Convert to array + $coordinates = array_reverse(array_map(function ($v) { $p = strpos($v, ','); return ['latitude' => floatval(substr($v, 0, $p)), 'longitude' => floatval(substr($v, $p + 1))]; }, explode('-', $coordinates)), true); + + //Iterate on locations + foreach($coordinates as $id => $coordinate) { + //Set dest x + $destX = intval(floor($width / 2 - MapUtil::tz * ($centerX - $this->map->longitudeToX(floatval($coordinate['longitude']), $zoom)))); + + //Set dest y + $destY = intval(floor($height / 2 - MapUtil::tz * ($centerY - $this->map->latitudeToY(floatval($coordinate['latitude']), $zoom)))); + + //Set fill color + $draw->setFillColor($this->map->fill); + + //Set font size + $draw->setFontSize($this->map->fontSize); + + //Set stroke color + $draw->setStrokeColor($this->map->stroke); + + //Set circle radius + $radius = $this->map->radius; + + //Set stroke width + $stroke = $this->map->strokeWidth; + + //With matching position + if ($coordinate['latitude'] === $latitude && $coordinate['longitude'] == $longitude) { + //Set fill color + $draw->setFillColor($this->map->highFill); + + //Set font size + $draw->setFontSize($this->map->highFontSize); + + //Set stroke color + $draw->setStrokeColor($this->map->highStroke); + + //Set circle radius + $radius = $this->map->highRadius; + + //Set stroke width + $stroke = $this->map->highStrokeWidth; + } + + //Set stroke width + $draw->setStrokeWidth($stroke); + + //Draw circle + $draw->circle($destX - $radius, $destY - $radius, $destX + $radius, $destY + $radius); + + //Set fill color + $draw->setFillColor($draw->getStrokeColor()); + + //Set stroke width + $draw->setStrokeWidth($stroke / 4); + + //Get font metrics + $metrics = $image->queryFontMetrics($draw, strval($id)); + + //Add annotation + $draw->annotation($destX - $radius, $destY + $stroke, strval($id)); + } + + //Draw on image + $image->drawImage($draw); + + //Strip image exif data and properties + $image->stripImage(); + + //Add latitude + //XXX: not supported by imagick :'( + $image->setImageProperty('exif:GPSLatitude', $this->map->latitudeToSexagesimal($latitude)); + + //Add longitude + //XXX: not supported by imagick :'( + $image->setImageProperty('exif:GPSLongitude', $this->map->longitudeToSexagesimal($longitude)); + + //Add description + //XXX: not supported by imagick :'( + #$image->setImageProperty('exif:Description', $caption); + + //Set progressive jpeg + $image->setInterlaceScheme(\Imagick::INTERLACE_PLANE); + + //Set compression quality + //TODO: ajust that + $image->setImageCompressionQuality(70); + + //Save image + if (!$image->writeImage($map)) { + //Throw error + throw new \Exception(sprintf('Unable to write image "%s"', $path)); + } + + //Return response + //TODO: générer l'image ici à partir du cache :p + #return new Response($image->getImageBlob()); + return new BinaryFileResponse($map); + } +} -- 2.41.0