riatelab / maptiles

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

resolution of fetched tiles #27

Closed brry closed 1 year ago

brry commented 1 year ago

The images are fuzzy, compared to OpenStreetMap (to be taken off CRAN next week). I guess it uses a different tile fetching mechanism, because at a particular zoom level, the images have better resolution.

This may be related to #5, but I don't think so, as the downloaded tiles in the cache folder have the same fuzzyness.

Is it worth looking into how Ian's code does this? It does look a lot nicer in larger images.

rosm has the same resolution, by the way.

Here is the comparison:

maps1

maps2

And here's the code generating it:

d <- read.table(sep=",", header=TRUE, text=
"lat, long
52.514464, 13.350137
52.370000, 13.120000
55.685143, 12.580008
50.106452, 14.419989
48.847003, 2.3372130
51.505364,-0.1647520")

png("maps%d.png", width=2, height=2, units="in", res=200)

# OpenStreetMap ----
OSMscale::pointsMap(lat, long, data=d, fx=0, scale=FALSE, pch=NA, zoom=5)
title(main="OpenStreetMap", line=-1)

# maptiles ----
bounds <- sf::st_bbox(sf::st_as_sf(d, coords=c("long", "lat"), crs=4326))
map <- maptiles::get_tiles(bounds, crop=TRUE, zoom=5, project=FALSE)
maptiles::plot_tiles(map, adjust=FALSE) 
title(main="maptiles", line=-1)

dev.off()
rCarto commented 1 year ago

Hello, I think it has to do with the smooth parameter in terra::plotRGB() which doesn't seem to be taken into account. I'll look at it more in depth tomorrow.

rCarto commented 1 year ago

OpenStreetMap is using raster to plot RGB images. maptiles (and rosm I think) are using terra. The smooth argument in terra seems to be broken when compared to the interpolate argument in raster.

library(terra)
#> terra 1.7.46
library(raster)
#> Loading required package: sp
#> The legacy packages maptools, rgdal, and rgeos, underpinning the sp package,
#> which was just loaded, were retired in October 2023.
#> Please refer to R-spatial evolution reports for details, especially
#> https://r-spatial.org/r/2023/05/15/evolution4.html.
#> It may be desirable to make the sf package available;
#> package maintainers should consider adding sf to Suggests:.
r.spatraster <- terra::rast(system.file("ex/logo.tif", package="terra"))   
r.rasterbrick <- raster::brick(r.spatraster)
par(mfrow = c(2,2))
terra::plotRGB(r.spatraster, smooth = FALSE)
terra::plotRGB(r.spatraster, smooth = TRUE)
raster::plotRGB(r.rasterbrick, interpolate = FALSE)
#> Warning in .couldBeLonLat(x, warnings = warnings): CRS is NA. Assuming it is
#> longitude/latitude
raster::plotRGB(r.rasterbrick, interpolate = TRUE)
#> Warning in .couldBeLonLat(x, warnings = warnings): CRS is NA. Assuming it is
#> longitude/latitude

Created on 2023-10-11 with reprex v2.0.2

rCarto commented 1 year ago

The problem is solved in terra.

# remotes::install_github('rspatial/terra')
d <- read.table(sep=",", header=TRUE, text=
                  "lat, long
52.514464, 13.350137
52.370000, 13.120000
55.685143, 12.580008
50.106452, 14.419989
48.847003, 2.3372130
51.505364,-0.1647520")

# maptiles ----
png("maps%d.png", width=2, height=2, units="in", res=200)
bounds <- sf::st_bbox(sf::st_as_sf(d, coords=c("long", "lat"), crs=4326))
map <- maptiles::get_tiles(bounds, crop=TRUE, zoom=5, project=FALSE)
maptiles::plot_tiles(map, adjust=FALSE) 
title(main="maptiles", line=-1)
dev.off()
#> png 
#>   2

Created on 2023-10-13 with reprex v2.0.2

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.3.1 (2023-06-16) #> os Debian GNU/Linux 12 (bookworm) #> system x86_64, linux-gnu #> ui X11 #> language en #> collate fr_FR.UTF-8 #> ctype fr_FR.UTF-8 #> tz Europe/Paris #> date 2023-10-13 #> pandoc 3.1.1 @ /usr/lib/rstudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> class 7.3-22 2023-05-03 [1] CRAN (R 4.3.1) #> classInt 0.4-10 2023-09-05 [1] CRAN (R 4.3.1) #> cli 3.6.1 2023-03-23 [1] CRAN (R 4.3.0) #> codetools 0.2-19 2023-02-01 [1] CRAN (R 4.3.1) #> curl 5.1.0 2023-10-02 [1] CRAN (R 4.3.1) #> DBI 1.1.3 2022-06-18 [1] CRAN (R 4.3.0) #> digest 0.6.33 2023-07-07 [1] CRAN (R 4.3.1) #> dplyr 1.1.3 2023-09-03 [1] CRAN (R 4.3.1) #> e1071 1.7-13 2023-02-01 [1] CRAN (R 4.3.0) #> evaluate 0.22 2023-09-29 [1] CRAN (R 4.3.1) #> fansi 1.0.5 2023-10-08 [1] CRAN (R 4.3.1) #> fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.3.0) #> fs 1.6.3 2023-07-20 [1] CRAN (R 4.3.1) #> generics 0.1.3 2022-07-05 [1] CRAN (R 4.3.0) #> glue 1.6.2 2022-02-24 [1] CRAN (R 4.3.0) #> htmltools 0.5.6.1 2023-10-06 [1] CRAN (R 4.3.1) #> KernSmooth 2.23-22 2023-07-10 [1] CRAN (R 4.3.1) #> knitr 1.44 2023-09-11 [1] CRAN (R 4.3.1) #> lifecycle 1.0.3 2022-10-07 [1] CRAN (R 4.3.0) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.3.0) #> maptiles 0.7.0.0 2023-09-14 [1] local #> pillar 1.9.0 2023-03-22 [1] CRAN (R 4.3.0) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.3.0) #> png 0.1-8 2022-11-29 [1] CRAN (R 4.3.0) #> proxy 0.4-27 2022-06-09 [1] CRAN (R 4.3.0) #> purrr 1.0.2 2023-08-10 [1] CRAN (R 4.3.1) #> R.cache 0.16.0 2022-07-21 [1] CRAN (R 4.3.0) #> R.methodsS3 1.8.2 2022-06-13 [1] CRAN (R 4.3.0) #> R.oo 1.25.0 2022-06-12 [1] CRAN (R 4.3.0) #> R.utils 2.12.2 2022-11-11 [1] CRAN (R 4.3.0) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.3.0) #> Rcpp 1.0.11 2023-07-06 [1] CRAN (R 4.3.1) #> reprex 2.0.2 2022-08-17 [1] CRAN (R 4.3.0) #> rlang 1.1.1 2023-04-28 [1] CRAN (R 4.3.0) #> rmarkdown 2.25 2023-09-18 [1] CRAN (R 4.3.1) #> rstudioapi 0.15.0 2023-07-07 [1] CRAN (R 4.3.1) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.3.0) #> sf 1.0-14 2023-07-11 [1] CRAN (R 4.3.1) #> slippymath 0.3.1 2019-06-28 [1] CRAN (R 4.3.0) #> styler 1.10.2 2023-08-29 [1] CRAN (R 4.3.1) #> terra 1.7-53 2023-10-13 [1] Github (rspatial/terra@d85f8b1) #> tibble 3.2.1 2023-03-20 [1] CRAN (R 4.3.0) #> tidyselect 1.2.0 2022-10-10 [1] CRAN (R 4.3.0) #> units 0.8-4 2023-09-13 [1] CRAN (R 4.3.1) #> utf8 1.2.3 2023-01-31 [1] CRAN (R 4.3.0) #> vctrs 0.6.3 2023-06-14 [1] CRAN (R 4.3.0) #> withr 2.5.1 2023-09-26 [1] CRAN (R 4.3.1) #> xfun 0.40 2023-08-09 [1] CRAN (R 4.3.1) #> yaml 2.3.7 2023-01-23 [1] CRAN (R 4.3.0) #> #> [1] /home/tim/Documents/R/4.3 #> [2] /usr/local/lib/R/site-library #> [3] /usr/lib/R/site-library #> [4] /usr/lib/R/library #> #> ────────────────────────────────────────────────────────────────────────────── ```

maps1

brry commented 1 year ago

Thank you so much!