Adding tile cache and preprocess.
This commit is contained in:
parent
598dda2aae
commit
711f1dc845
@ -235,15 +235,17 @@ export default class Conquer {
|
||||
// }
|
||||
//})
|
||||
olProj.useGeographic()
|
||||
const osm = new OSM()
|
||||
osm.setUrls([`${window.location.protocol}//${window.location.hostname}:${window.location.port}/conquer/tile/{z}/{x}/{y}.png`])
|
||||
this.map = new Map({
|
||||
target: conquerContainer,
|
||||
layers: [
|
||||
new TileLayer({
|
||||
source: new OSM()
|
||||
source: osm
|
||||
})
|
||||
],
|
||||
view: new View({
|
||||
zoom: 21,
|
||||
zoom: 19,
|
||||
maxZoom: 22,
|
||||
}),
|
||||
})
|
||||
|
@ -94,6 +94,7 @@ sub startup ($self) {
|
||||
$r->put('/conquer/user')->to('UserConquer#create');
|
||||
$r->get('/conquer/user')->to('UserConquer#get_self');
|
||||
$r->post('/conquer/user/login')->to('UserConquer#login');
|
||||
$r->get('/conquer/tile/<zoom>/<x>/<y>.png')->to('ConquerTile#tile');
|
||||
$r->get('/search.json')->to('Search#search');
|
||||
$r->get('/farmacia-guardia.json')->to('FarmaciaGuardia#current');
|
||||
$r->get('/<:category>.rss')->to('Page#category_rss');
|
||||
|
52
lib/BurguillosInfo/Controller/ConquerTile.pm
Normal file
52
lib/BurguillosInfo/Controller/ConquerTile.pm
Normal file
@ -0,0 +1,52 @@
|
||||
package BurguillosInfo::Controller::ConquerTile;
|
||||
|
||||
use v5.34.1;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use utf8;
|
||||
|
||||
use Mojo::Base 'Mojolicious::Controller', '-signatures';
|
||||
use Path::Tiny;
|
||||
use Mojo::UserAgent;
|
||||
|
||||
my $cache_files_dir = path(__FILE__)->parent->parent->parent->parent->child('cache/tiles/');
|
||||
|
||||
sub tile($self) {
|
||||
my $zoom = $self->stash('zoom');
|
||||
my $x = $self->stash('x');
|
||||
my $y = $self->stash('y');
|
||||
my $candidate_file = $cache_files_dir->child("$zoom-$x-$y.png");
|
||||
if (-f $candidate_file) {
|
||||
return $self->_render_png($candidate_file);
|
||||
}
|
||||
if (!defined $self->current_user) {
|
||||
return $self->render(status => 401, text => '¡¡No estás loggeado, no puedes cargar mapa nuevo.!!');
|
||||
}
|
||||
my $file_to_write = $candidate_file;
|
||||
my $ua = Mojo::UserAgent->new;
|
||||
my $png_tile = $ua->get("https://tile.openstreetmap.org/$zoom/$x/$y.png")->result->body;
|
||||
open my $fh, '|-', 'convert', '/dev/stdin', '-channel', 'RGB', '-negate', $file_to_write;
|
||||
print $fh $png_tile;
|
||||
close $fh;
|
||||
$self->_render_png($file_to_write);
|
||||
$self->_delete_extra_files();
|
||||
}
|
||||
|
||||
sub _delete_extra_files($self) {
|
||||
my @files = $cache_files_dir->children;
|
||||
if (scalar @files < 20001) {
|
||||
return;
|
||||
}
|
||||
@files = sort { -M $a <=> -M $b } @files;
|
||||
for (my $i = 0; $i < (scalar @files) - 20000; $i++) {
|
||||
system 'rm', '-v', $files[$i];
|
||||
}
|
||||
}
|
||||
|
||||
sub _render_png($self, $file) {
|
||||
system 'touch', $file;
|
||||
return $self->render(data => $file->slurp_raw, status => 200, format => 'png');
|
||||
}
|
||||
1;
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user