]> Raphaël G. Git Repositories - packbundle/blob - Controller/MapController.php
Add map controller
[packbundle] / Controller / MapController.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys PackBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\PackBundle\Controller;
13
14 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
16 use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
17 use Symfony\Component\Filesystem\Filesystem;
18 use Symfony\Component\HttpFoundation\BinaryFileResponse;
19 use Symfony\Component\HttpFoundation\Request;
20 use Symfony\Component\HttpFoundation\Response;
21 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
22 use Symfony\Component\Routing\RequestContext;
23 use Symfony\Contracts\Service\ServiceSubscriberInterface;
24
25 use Rapsys\PackBundle\Util\MapUtil;
26 use Rapsys\PackBundle\Util\SluggerUtil;
27
28 /**
29 * {@inheritdoc}
30 */
31 class MapController extends AbstractController implements ServiceSubscriberInterface {
32 /**
33 * The cache path
34 */
35 protected string $cache;
36
37 /**
38 * The ContainerInterface instance
39 *
40 * @var ContainerInterface
41 */
42 protected $container;
43
44 /**
45 * The stream context instance
46 */
47 protected mixed $ctx;
48
49 /**
50 * The MapUtil instance
51 */
52 protected MapUtil $map;
53
54 /**
55 * The public path
56 */
57 protected string $public;
58
59 /**
60 * The SluggerUtil instance
61 */
62 protected SluggerUtil $slugger;
63
64 /**
65 * The tile server url
66 */
67 protected string $url;
68
69 /**
70 * Creates a new osm util
71 *
72 * @param ContainerInterface $container The ContainerInterface instance
73 * @param MapUtil $map The MapUtil instance
74 * @param SluggerUtil $slugger The SluggerUtil instance
75 * @param string $cache The cache path
76 * @param string $public The public path
77 * @param string $url The tile server url
78 */
79 function __construct(ContainerInterface $container, MapUtil $map, SluggerUtil $slugger, string $cache = '../var/cache/map', string $public = './bundles/rapsyspack/map', string $url = MapUtil::osm) {
80 //Set cache
81 $this->cache = $cache;
82
83 //Set container
84 $this->container = $container;
85
86 //Set ctx
87 $this->ctx = stream_context_create(
88 [
89 'http' => [
90 #'header' => ['Referer: https://www.openstreetmap.org/'],
91 'max_redirects' => 5,
92 'timeout' => (int)ini_get('default_socket_timeout'),
93 #'user_agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36',
94 'user_agent' => (string)ini_get('user_agent')?:'rapsys_pack/2.0.0',
95 ]
96 ]
97 );
98
99 //Set map
100 $this->map = $map;
101
102 //Set public
103 $this->public = $public;
104
105 //Set slugger
106 $this->slugger = $slugger;
107
108 //Set url
109 $this->url = $url;
110 }
111
112 /**
113 * Return map image
114 *
115 * @param Request $request The Request instance
116 * @param string $hash The hash
117 * @param int $updated The updated timestamp
118 * @param float $latitude The latitude
119 * @param float $longitude The longitude
120 * @param int $zoom The zoom
121 * @param int $width The width
122 * @param int $height The height
123 * @return Response The rendered image
124 */
125 public function map(Request $request, string $hash, int $updated, float $latitude, float $longitude, int $zoom, int $width, int $height): Response {
126 //Without matching hash
127 if ($hash !== $this->slugger->serialize([$updated, $latitude, $longitude, $zoom, $width, $height])) {
128 //Throw new exception
129 throw new NotFoundHttpException(sprintf('Unable to match map hash: %s', $hash));
130 }
131
132 //Set map
133 $map = $this->public.'/'.$zoom.'/'.$latitude.'/'.$longitude.'/'.$width.'x'.$height.'.jpeg';
134
135 //With map up to date file
136 if (is_file($map) && ($mtime = stat($map)['mtime']) && $mtime >= $updated) {
137 //Read map from cache
138 //TODO: handle modified, etag, cache, etc ???
139 return new BinaryFileResponse($map);
140 }
141
142 //Without existing map
143 if (!is_dir($dir = dirname($map))) {
144 //Create filesystem object
145 $filesystem = new Filesystem();
146
147 try {
148 //Create path
149 //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
150 //XXX: on CoW filesystems execute a chattr +C before filling
151 $filesystem->mkdir($dir, 0775);
152 } catch (IOExceptionInterface $e) {
153 //Throw error
154 throw new \Exception(sprintf('Output path "%s" do not exists and unable to create it', $dir), 0, $e);
155 }
156 }
157
158 //Create image instance
159 $image = new \Imagick();
160
161 //Add new image
162 $image->newImage($width, $height, new \ImagickPixel('transparent'), 'jpeg');
163
164 //Create tile instance
165 $tile = new \Imagick();
166
167 //Get tile xy
168 $centerX = $this->map->longitudeToX($longitude, $zoom);
169 $centerY = $this->map->latitudeToY($latitude, $zoom);
170
171 //Calculate start xy
172 $startX = floor(floor($centerX) - $width / MapUtil::tz);
173 $startY = floor(floor($centerY) - $height / MapUtil::tz);
174
175 //Calculate end xy
176 $endX = ceil(ceil($centerX) + $width / MapUtil::tz);
177 $endY = ceil(ceil($centerY) + $height / MapUtil::tz);
178
179 for($x = $startX; $x <= $endX; $x++) {
180 for($y = $startY; $y <= $endY; $y++) {
181 //Set cache path
182 $cache = $this->cache.'/'.$zoom.'/'.$x.'/'.$y.'.png';
183
184 //Without cache image
185 if (!is_file($cache)) {
186 //Set tile url
187 $tileUri = str_replace(['{Z}', '{X}', '{Y}'], [$zoom, $x, $y], $this->url);
188
189 //Without cache path
190 if (!is_dir($dir = dirname($cache))) {
191 //Create filesystem object
192 $filesystem = new Filesystem();
193
194 try {
195 //Create path
196 //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
197 $filesystem->mkdir($dir, 0775);
198 } catch (IOExceptionInterface $e) {
199 //Throw error
200 throw new \Exception(sprintf('Output directory "%s" do not exists and unable to create it', $dir), 0, $e);
201 }
202 }
203
204 //Store tile in cache
205 file_put_contents($cache, file_get_contents($tileUri, false, $this->ctx));
206 }
207
208 //Set dest x
209 $destX = intval(floor($width / 2 - MapUtil::tz * ($centerX - $x)));
210
211 //Set dest y
212 $destY = intval(floor($height / 2 - MapUtil::tz * ($centerY - $y)));
213
214 //Read tile from cache
215 $tile->readImage($cache);
216
217 //Compose image
218 $image->compositeImage($tile, \Imagick::COMPOSITE_OVER, $destX, $destY);
219
220 //Clear tile
221 $tile->clear();
222 }
223 }
224
225 //Add imagick draw instance
226 //XXX: see https://www.php.net/manual/fr/imagick.examples-1.php#example-3916
227 $draw = new \ImagickDraw();
228
229 //Set text antialias
230 $draw->setTextAntialias(true);
231
232 //Set stroke antialias
233 $draw->setStrokeAntialias(true);
234
235 //Set text alignment
236 $draw->setTextAlignment(\Imagick::ALIGN_CENTER);
237
238 //Set gravity
239 $draw->setGravity(\Imagick::GRAVITY_CENTER);
240
241 //Set fill color
242 $draw->setFillColor('#cff');
243
244 //Set stroke color
245 $draw->setStrokeColor('#00c3f9');
246
247 //Set stroke width
248 $draw->setStrokeWidth(2);
249
250 //Draw circle
251 $draw->circle($width/2 - 5, $height/2 - 5, $width/2 + 5, $height/2 + 5);
252
253 //Draw on image
254 $image->drawImage($draw);
255
256 //Strip image exif data and properties
257 $image->stripImage();
258
259 //Add latitude
260 //XXX: not supported by imagick :'(
261 $image->setImageProperty('exif:GPSLatitude', $this->map->latitudeToSexagesimal($latitude));
262
263 //Add longitude
264 //XXX: not supported by imagick :'(
265 $image->setImageProperty('exif:GPSLongitude', $this->map->longitudeToSexagesimal($longitude));
266
267 //Add description
268 //XXX: not supported by imagick :'(
269 #$image->setImageProperty('exif:Description', $caption);
270
271 //Set progressive jpeg
272 $image->setInterlaceScheme(\Imagick::INTERLACE_PLANE);
273
274 //Set compression quality
275 //TODO: ajust that
276 $image->setImageCompressionQuality(70);
277
278 //Save image
279 if (!$image->writeImage($map)) {
280 //Throw error
281 throw new \Exception(sprintf('Unable to write image "%s"', $path));
282 }
283
284 //Return response
285 //TODO: générer l'image ici à partir du cache :p
286 #return new Response($image->getImageBlob());
287 return new BinaryFileResponse($map);
288 }
289
290 /**
291 * Return multi map image
292 *
293 * @param Request $request The Request instance
294 * @param string $hash The hash
295 * @param int $updated The updated timestamp
296 * @param float $latitude The latitude
297 * @param float $longitude The longitude
298 * @param string $coordinates The coordinates
299 * @param int $zoom The zoom
300 * @param int $width The width
301 * @param int $height The height
302 * @return Response The rendered image
303 */
304 public function multiMap(Request $request, string $hash, int $updated, float $latitude, float $longitude, string $coordinates, int $zoom, int $width, int $height): Response {
305 //Without matching hash
306 if ($hash !== $this->slugger->serialize([$updated, $latitude, $longitude, $coordinate = $this->slugger->hash($coordinates), $zoom, $width, $height])) {
307 //Throw new exception
308 throw new NotFoundHttpException(sprintf('Unable to match multi map hash: %s', $hash));
309 }
310
311 //Set multi
312 $map = $this->public.'/'.$zoom.'/'.$latitude.'/'.$longitude.'/'.$coordinate.'/'.$width.'x'.$height.'.jpeg';
313
314 //With multi up to date file
315 if (is_file($map) && ($mtime = stat($map)['mtime']) && $mtime >= $updated) {
316 //Read multi from cache
317 //TODO: handle modified, etag, cache, etc ???
318 return new BinaryFileResponse($map);
319 }
320
321 //Without existing multi
322 if (!is_dir($dir = dirname($map))) {
323 //Create filesystem object
324 $filesystem = new Filesystem();
325
326 try {
327 //Create path
328 //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
329 //XXX: on CoW filesystems execute a chattr +C before filling
330 $filesystem->mkdir($dir, 0775);
331 } catch (IOExceptionInterface $e) {
332 //Throw error
333 throw new \Exception(sprintf('Output path "%s" do not exists and unable to create it', $dir), 0, $e);
334 }
335 }
336
337 //Create image instance
338 $image = new \Imagick();
339
340 //Add new image
341 $image->newImage($width, $height, new \ImagickPixel('transparent'), 'jpeg');
342
343 //Create tile instance
344 $tile = new \Imagick();
345
346 //Get tile xy
347 $centerX = $this->map->longitudeToX($longitude, $zoom);
348 $centerY = $this->map->latitudeToY($latitude, $zoom);
349
350 //Calculate start xy
351 $startX = floor(floor($centerX) - $width / MapUtil::tz);
352 $startY = floor(floor($centerY) - $height / MapUtil::tz);
353
354 //Calculate end xy
355 $endX = ceil(ceil($centerX) + $width / MapUtil::tz);
356 $endY = ceil(ceil($centerY) + $height / MapUtil::tz);
357
358 for($x = $startX; $x <= $endX; $x++) {
359 for($y = $startY; $y <= $endY; $y++) {
360 //Set cache path
361 $cache = $this->cache.'/'.$zoom.'/'.$x.'/'.$y.'.png';
362
363 //Without cache image
364 if (!is_file($cache)) {
365 //Set tile url
366 $tileUri = str_replace(['{Z}', '{X}', '{Y}'], [$zoom, $x, $y], $this->url);
367
368 //Without cache path
369 if (!is_dir($dir = dirname($cache))) {
370 //Create filesystem object
371 $filesystem = new Filesystem();
372
373 try {
374 //Create path
375 //XXX: set as 0775, symfony umask (0022) will reduce rights (0755)
376 $filesystem->mkdir($dir, 0775);
377 } catch (IOExceptionInterface $e) {
378 //Throw error
379 throw new \Exception(sprintf('Output directory "%s" do not exists and unable to create it', $dir), 0, $e);
380 }
381 }
382
383 //Store tile in cache
384 file_put_contents($cache, file_get_contents($tileUri, false, $this->ctx));
385 }
386
387 //Set dest x
388 $destX = intval(floor($width / 2 - MapUtil::tz * ($centerX - $x)));
389
390 //Set dest y
391 $destY = intval(floor($height / 2 - MapUtil::tz * ($centerY - $y)));
392
393 //Read tile from cache
394 $tile->readImage($cache);
395
396 //Compose image
397 $image->compositeImage($tile, \Imagick::COMPOSITE_OVER, $destX, $destY);
398
399 //Clear tile
400 $tile->clear();
401 }
402 }
403
404 //Add imagick draw instance
405 //XXX: see https://www.php.net/manual/fr/imagick.examples-1.php#example-3916
406 $draw = new \ImagickDraw();
407
408 //Set text antialias
409 $draw->setTextAntialias(true);
410
411 //Set stroke antialias
412 $draw->setStrokeAntialias(true);
413
414 //Set text alignment
415 $draw->setTextAlignment(\Imagick::ALIGN_CENTER);
416
417 //Set gravity
418 $draw->setGravity(\Imagick::GRAVITY_CENTER);
419
420 //Convert to array
421 $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);
422
423 //Iterate on locations
424 foreach($coordinates as $id => $coordinate) {
425 //Set dest x
426 $destX = intval(floor($width / 2 - MapUtil::tz * ($centerX - $this->map->longitudeToX(floatval($coordinate['longitude']), $zoom))));
427
428 //Set dest y
429 $destY = intval(floor($height / 2 - MapUtil::tz * ($centerY - $this->map->latitudeToY(floatval($coordinate['latitude']), $zoom))));
430
431 //Set fill color
432 $draw->setFillColor($this->map->fill);
433
434 //Set font size
435 $draw->setFontSize($this->map->fontSize);
436
437 //Set stroke color
438 $draw->setStrokeColor($this->map->stroke);
439
440 //Set circle radius
441 $radius = $this->map->radius;
442
443 //Set stroke width
444 $stroke = $this->map->strokeWidth;
445
446 //With matching position
447 if ($coordinate['latitude'] === $latitude && $coordinate['longitude'] == $longitude) {
448 //Set fill color
449 $draw->setFillColor($this->map->highFill);
450
451 //Set font size
452 $draw->setFontSize($this->map->highFontSize);
453
454 //Set stroke color
455 $draw->setStrokeColor($this->map->highStroke);
456
457 //Set circle radius
458 $radius = $this->map->highRadius;
459
460 //Set stroke width
461 $stroke = $this->map->highStrokeWidth;
462 }
463
464 //Set stroke width
465 $draw->setStrokeWidth($stroke);
466
467 //Draw circle
468 $draw->circle($destX - $radius, $destY - $radius, $destX + $radius, $destY + $radius);
469
470 //Set fill color
471 $draw->setFillColor($draw->getStrokeColor());
472
473 //Set stroke width
474 $draw->setStrokeWidth($stroke / 4);
475
476 //Get font metrics
477 $metrics = $image->queryFontMetrics($draw, strval($id));
478
479 //Add annotation
480 $draw->annotation($destX - $radius, $destY + $stroke, strval($id));
481 }
482
483 //Draw on image
484 $image->drawImage($draw);
485
486 //Strip image exif data and properties
487 $image->stripImage();
488
489 //Add latitude
490 //XXX: not supported by imagick :'(
491 $image->setImageProperty('exif:GPSLatitude', $this->map->latitudeToSexagesimal($latitude));
492
493 //Add longitude
494 //XXX: not supported by imagick :'(
495 $image->setImageProperty('exif:GPSLongitude', $this->map->longitudeToSexagesimal($longitude));
496
497 //Add description
498 //XXX: not supported by imagick :'(
499 #$image->setImageProperty('exif:Description', $caption);
500
501 //Set progressive jpeg
502 $image->setInterlaceScheme(\Imagick::INTERLACE_PLANE);
503
504 //Set compression quality
505 //TODO: ajust that
506 $image->setImageCompressionQuality(70);
507
508 //Save image
509 if (!$image->writeImage($map)) {
510 //Throw error
511 throw new \Exception(sprintf('Unable to write image "%s"', $path));
512 }
513
514 //Return response
515 //TODO: générer l'image ici à partir du cache :p
516 #return new Response($image->getImageBlob());
517 return new BinaryFileResponse($map);
518 }
519 }