riatelab / maptiles

Download, compose and display map tiles with R
97 stars 12 forks source link

Fix bug where you cannot download maps spanning tilesets across powers of 10 #17

Closed rlh1994 closed 2 years ago

rlh1994 commented 2 years ago

Small issue I found but was easy enough to fix on a local clone so wanted to just contribute it - it might not be the most elegant way but it solves the issue using a base function.

The issue is that apply coerces everything to a character for the OSM maps when the x value is passed into the dl_t function which causes a failure when you are downloading a tileset that spans (on the x or y columns) values with a different number of units such as in the below example.

library(maptiles)
#> Warning: package 'maptiles' was built under R version 4.1.3
library(sf)
#> Warning: package 'sf' was built under R version 4.1.3
#> Linking to GEOS 3.10.2, GDAL 3.4.1, PROJ 7.2.1; sf_use_s2() is TRUE

a = st_sf(a = 1:2, geom = st_sfc(st_point(c(-8.177926, 49.8847)), st_point(c(1.763016, 60.84555))), crs = 4326)
get_tiles(x = a, crop = TRUE, cachedir = 'Tiles', zoom = 5, verbose = TRUE)
#> http://b.tile.openstreetmap.org/5/15/ 9.png => Tiles/OpenStreetMap/OpenStreetMap_5_15_ 9.png
#> Error in curl::curl_download(url = q, destfile = outfile): HTTP error 400.

Created on 2022-08-01 by the reprex package (v2.0.1)

This change fixes this issue and correctly completes the filename

library(maptiles) # locally built version with change.
library(sf)
#> Warning: package 'sf' was built under R version 4.1.3
#> Linking to GEOS 3.10.2, GDAL 3.4.1, PROJ 7.2.1; sf_use_s2() is TRUE

a = st_sf(a = 1:2, geom = st_sfc(st_point(c(-8.177926, 49.8847)), st_point(c(1.763016, 60.84555))), crs = 4326)
get_tiles(x = a, crop = TRUE, cachedir = 'Tiles', zoom = 5, verbose = TRUE)
#> http://a.tile.openstreetmap.org/5/15/9.png => Tiles/OpenStreetMap/OpenStreetMap_5_15_9.png
#> http://c.tile.openstreetmap.org/5/16/9.png => Tiles/OpenStreetMap/OpenStreetMap_5_16_9.png
#> http://c.tile.openstreetmap.org/5/15/10.png => Tiles/OpenStreetMap/OpenStreetMap_5_15_10.png
#> http://b.tile.openstreetmap.org/5/16/10.png => Tiles/OpenStreetMap/OpenStreetMap_5_16_10.png
#> Zoom:5
#> Data and map tiles sources:
#> © OpenStreetMap contributors
#> class       : SpatRaster 
#> dimensions  : 336, 307, 3  (nrow, ncol, nlyr)
#> resolution  : 0.0357, 0.0357  (x, y)
#> extent      : -8.6796, 2.2803, 49.37146, 61.36666  (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326) 
#> source      : memory 
#> colors RGB  : 1, 2, 3 
#> names       : red, green, blue 
#> min values  :   4,     4,    4 
#> max values  : 252,   252,  250

Created on 2022-08-01 by the reprex package (v2.0.1)

I updated the news but left the version number increased on the DESCRIPTION as this already didn't match so was unsure if that needed changing.

Hopefully this helps!

codecov[bot] commented 2 years ago

Codecov Report

Merging #17 (dbf5129) into main (259daa4) will increase coverage by 0.07%. The diff coverage is 100.00%.

:exclamation: Current head dbf5129 differs from pull request most recent head 36fd243. Consider uploading reports for the commit 36fd243 to get more accurate results

@@            Coverage Diff             @@
##             main      #17      +/-   ##
==========================================
+ Coverage   86.33%   86.41%   +0.07%     
==========================================
  Files           4        4              
  Lines         183      184       +1     
==========================================
+ Hits          158      159       +1     
  Misses         25       25              
Impacted Files Coverage Δ
R/get_tiles.R 87.91% <100.00%> (+0.08%) :arrow_up:

Help us with your feedback. Take ten seconds to tell us how you rate us.

rCarto commented 2 years ago

Thank you very much, that's useful!