@@ -1322,6 +1322,40 @@ async fn get_tile(s: String, z: u8, x: u32, y: u32) -> Result<(ContentType, Vec<
13221322 }
13231323}
13241324
1325+ async fn get_cached_proxy_tile (
1326+ provider : & str ,
1327+ z : u8 ,
1328+ x : u32 ,
1329+ y : u32 ,
1330+ extension : & str ,
1331+ upstream_url : String ,
1332+ content_type : ContentType ,
1333+ ) -> Result < ( ContentType , Vec < u8 > ) , Status > {
1334+ let path = format ! ( "data/tiles_{provider}/{z}/{x}/{y}.{extension}" ) ;
1335+ if let Some ( bytes) = load_png_from_disk ( path. clone ( ) ) {
1336+ return Result :: Ok ( ( content_type, bytes) ) ;
1337+ }
1338+
1339+ let response = reqwest_client ( )
1340+ . get ( & upstream_url)
1341+ . send ( )
1342+ . await
1343+ . map_err ( |e| {
1344+ println ! ( "{e}" ) ;
1345+ Status :: InternalServerError
1346+ } ) ?;
1347+ let bytes = response. bytes ( ) . await . map_err ( |e| {
1348+ println ! ( "{e}" ) ;
1349+ Status :: InternalServerError
1350+ } ) ?;
1351+
1352+ fs:: create_dir_all ( format ! ( "data/tiles_{provider}/{z}/{x}" ) )
1353+ . map_err ( |_| Status :: InternalServerError ) ?;
1354+ fs:: write ( & path, & bytes) . map_err ( |_| Status :: InternalServerError ) ?;
1355+
1356+ Result :: Ok ( ( content_type, bytes. to_vec ( ) ) )
1357+ }
1358+
13251359#[ get( "/opentopomap/<s>/<z>/<x>/<y_p>" ) ]
13261360async fn get_opentopomap_tile (
13271361 s : String ,
@@ -1334,6 +1368,27 @@ async fn get_opentopomap_tile(
13341368 get_tile ( s, z, x, y) . await
13351369}
13361370
1371+ #[ get( "/openstreetmap/<s>/<z>/<x>/<y_p>" ) ]
1372+ async fn get_openstreetmap_tile (
1373+ s : String ,
1374+ z : u8 ,
1375+ x : u32 ,
1376+ y_p : String ,
1377+ ) -> Result < ( ContentType , Vec < u8 > ) , Status > {
1378+ let y: u32 = y_p. split ( "." ) . next ( ) . unwrap ( ) . parse ( ) . unwrap ( ) ;
1379+ let url = format ! ( "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" ) ;
1380+ get_cached_proxy_tile ( "openstreetmap" , z, x, y, "png" , url, ContentType :: PNG ) . await
1381+ }
1382+
1383+ #[ get( "/satellite/<z>/<y>/<x_p>" ) ]
1384+ async fn get_satellite_tile ( z : u8 , y : u32 , x_p : String ) -> Result < ( ContentType , Vec < u8 > ) , Status > {
1385+ let x: u32 = x_p. split ( "." ) . next ( ) . unwrap ( ) . parse ( ) . unwrap ( ) ;
1386+ let url = format ! (
1387+ "https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}"
1388+ ) ;
1389+ get_cached_proxy_tile ( "satellite" , z, x, y, "jpg" , url, ContentType :: JPEG ) . await
1390+ }
1391+
13371392#[ derive( Serialize ) ]
13381393struct Stats {
13391394 webp_cache_size : usize ,
@@ -1397,6 +1452,8 @@ fn rocket() -> _ {
13971452 . mount ( "/" , routes ! [ get_height_image] )
13981453 . mount ( "/" , routes ! [ get_kml] )
13991454 . mount ( "/" , routes ! [ get_opentopomap_tile] )
1455+ . mount ( "/" , routes ! [ get_openstreetmap_tile] )
1456+ . mount ( "/" , routes ! [ get_satellite_tile] )
14001457 . mount ( "/" , routes ! [ get_stats] )
14011458 . mount ( "/" , routes ! [ get_height_map] )
14021459 . mount ( "/" , routes ! [ get_height_map_meta] )
0 commit comments