hypertidy / ceramic

read image server tiles direct with GDAL, or download image server tiles to a local cache
https://hypertidy.github.io/ceramic/
92 stars 6 forks source link

Minimal styles causing issues due to file size #34

Closed zacdav closed 1 year ago

zacdav commented 4 years ago

I've been playing with ceramic and so far enjoying it as a replacement for ggmap.

This issue arises when trying to use a "label only" style so that I can have labels on top of polygons. This style produces tiles that are very very small file sizes which are getting rejected due to a file size check in the package. I've been able to circumvent this by adjusting the file size check in a fork of the repo.

This is the file size check I adjusted: https://github.com/hypertidy/ceramic/blob/089f1f2df69f51af214c22aa6c81762cd64e4a5c/R/get_tiles.R#L94

Example: working example using forked repo, fails in current development version.

library(ceramic)
library(tidyverse)
library(sf)
library(raster)

# polygon
poi <- st_point(c(151.199940, -33.909384)) %>%
  st_sfc() %>%
  st_set_crs(4326) %>%
  st_transform(3577) %>%
  st_buffer(2000) %>%
  st_transform(3857)

# custom tiles
custom_tiles_base <- "https://api.mapbox.com/styles/v1/zacdav/ckbj2wkm90v3q1iobgqrjmbkn/tiles/512/{zoom}/{x}/{y}"
custom_tiles_labels <- "https://api.mapbox.com/styles/v1/zacdav/ckbjh5np511vp1iqjpjbujpq0/tiles/512/{zoom}/{x}/{y}"

get_map_raster_df <- function(x, ...) {
  # adapted from https://github.com/hypertidy/ceramic/issues/4
  tiles <- ceramic::cc_location(x, ...)
  tiles_df <- raster::as.data.frame(tiles, xy = TRUE)
  if ("layer.4" %in% names(tiles_df)) {
    names(tiles_df) <- c("x", "y", "red", "green", "blue", "alpha")
    tiles_df <- dplyr::filter(tiles_df, alpha > 0)
  } else {
    names(tiles_df) <- c("x", "y", "red", "green", "blue")
    tiles_df <- dplyr::mutate(tiles_df, alpha = 1)
  }
  tiles_df$hex <- grDevices::rgb(tiles_df$red, tiles_df$green, tiles_df$blue, maxColorValue = 255)
  tiles_df

}

base <- get_map_raster_df(poi, base_url = custom_tiles_base)
labels <- get_map_raster_df(poi, base_url = custom_tiles_labels)

plt <- ggplot() +
  geom_raster(
    data = base,
    aes(x, y, fill = hex),
    inherit.aes = FALSE
  ) +
  geom_sf(
    data = poi,
    lwd = 0,
    fill = "#FF0000",
    alpha = 0.7,
    inherit.aes = FALSE
  ) +
  geom_tile(
    data = labels,
    aes(x, y, fill = hex),#, alpha = alpha / 255),
    inherit.aes = FALSE
  ) +
  coord_sf() +
  scale_fill_identity() + 
  theme_void() + 
  theme(
    legend.position = "none"
  )

plt

desired result

Is there a more robust check that can take place? I am not sure for what situations the current check is for.

Thanks

mdsumner commented 4 years ago

Oh good one, that was a totally arbitrary check I put in. I think file size is a poor thing to use. That size came from a download resulting in a text file with an error message that was a constant size - I'll see if I can make it use the jpeg/png magic number detection instead.

ceramic needs an update soonish for the new mapbox api anyway, so glad it's working for you using the gh source

zacdav commented 4 years ago

Ah that makes sense, magic number seems like a good idea.

mdsumner commented 4 years ago

This is the reason, the dud files were 34 bytes:

"{\"message\":\"Tile does not exist\"}"

i.e. https://github.com/hypertidy/ceramic/blob/bc3e337f762035795a765d04e270326e0431fd9e/tests/testthat/test-dud-clobber.R

mdsumner commented 4 years ago

That is really tiny, can you share one of these minimal tiles? I'm getting a bit confused

zacdav commented 4 years ago

Let me get the files, the example above was giving the error.

zacdav commented 4 years ago

Well this is odd - I can't seem to get the behaviour on either CRAN version or an older commit. I wonder if somehow I had a bad file stuck in the cache that was the same as the dud one?

I'll keep investigating to see if i can recreate it again, It does seem weird that removing the check made it work last night.

mdsumner commented 4 years ago

I may have fixed in dev before today and not realized. Thanks for following up!

Maybe you saw an example just under 100b? I'll change this to 35, can you try? Committed from

Here was another test on file size: https://github.com/hypertidy/ceramic/blob/089f1f2df69f51af214c22aa6c81762cd64e4a5c/R/cache.R#L65

zacdav commented 4 years ago

I have tried a few more times now and haven't had any issues on dev branch. I think it must have been related to me updating the same style and having something stuck in cache.

Thanks for helping and being so prompt!

mdsumner commented 1 year ago

note that cc_location no longer interacts with the tiles, you can use custom styles with get_tiles() but we no longer support reading them atm