Skip to content

Instantly share code, notes, and snippets.

@mdsumner
Created February 17, 2025 21:42
Show Gist options
  • Save mdsumner/600f751bee6698cf5ca90fe26203433f to your computer and use it in GitHub Desktop.
Save mdsumner/600f751bee6698cf5ca90fe26203433f to your computer and use it in GitHub Desktop.
library(maptiles)
library(sf)
nc = st_read(system.file("shape/nc.shp", package = "sf"))

## let's say we have target raster
rr <-  get_tiles(
    sf::st_transform(nc, "EPSG:3857"),
    provider = "OpenStreetMap",
    zoom = 7,
    crop = TRUE,
    forceDownload = TRUE,
    project = TRUE
  )
rr <- rr * 0

## the tile server configured like this

dsn <- "<GDAL_WMS><Service name=\"TMS\"><ServerUrl>https://tile.openstreetmap.org/${z}/${x}/${y}.png</ServerUrl></Service><DataWindow><UpperLeftX>-20037508.34</UpperLeftX><UpperLeftY>20037508.34</UpperLeftY><LowerRightX>20037508.34</LowerRightX><LowerRightY>-20037508.34</LowerRightY><TileLevel>18</TileLevel><TileCountX>1</TileCountX><TileCountY>1</TileCountY><YOrigin>top</YOrigin></DataWindow><Projection>EPSG:3857</Projection><BlockSizeX>256</BlockSizeX><BlockSizeY>256</BlockSizeY><BandsCount>3</BandsCount><UserAgent>RUser</UserAgent><Cache /></GDAL_WMS>"
im <- terra::project(terra::rast(dsn), rr, by_util = TRUE, method = "bilinear")
terra::plotRGB(im)
plot(sf::st_transform(nc, "EPSG:3857"), add = TRUE, col = NA)
@mdsumner
Copy link
Author

now see that we get a degraded image (though the size isn't very different), so I think it's a real problem for tmap

tm_shape(im) + tm_rgb() + tm_crs(3857) + tm_shape(nc) + tm_lines()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment