Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in png::readPNG(img) : file is not in PNG format #13

Closed
gremms1 opened this issue Feb 14, 2022 · 5 comments
Closed

Error in png::readPNG(img) : file is not in PNG format #13

gremms1 opened this issue Feb 14, 2022 · 5 comments

Comments

@gremms1
Copy link

gremms1 commented Feb 14, 2022

Hello,

I am trying to get the following code to work, but it's not working out:

library(sf)
library(maptiles)

p = st_point(c(110784.2,494605.3))
p = st_sfc(p)
p = st_set_crs(p, 28992)
bb = st_bbox(st_buffer(p, 300))

url <- "https://geodata.nationaalgeoregister.nl/tiles/service/wmts/opentopo/EPSG:28992/{z}/{x}/{y}.png"
mffp <- list(src="OpenTopo", q=url, sub=NA, cit='')

t <- get_tiles(bb, provider=mffp, crop= T, forceDownload = T, zoom=5)

I get the following error: Error in png::readPNG(img) : file is not in PNG format
The strange this is that entering the following url in my browser does yield the correct tile:
https://geodata.nationaalgeoregister.nl/tiles/service/wmts/opentopo/EPSG:28992/5/16/12.png
So the error that it throws me seems strange.

@rCarto rCarto closed this as completed in 2e00f00 Feb 15, 2022
@rCarto
Copy link
Member

rCarto commented Feb 15, 2022

Hello,
I've tried to save this tile (right-click, save):
https://geodata.nationaalgeoregister.nl/tiles/service/wmts/opentopo/EPSG:28992/5/16/12.png
and saw that the file is actually a jpeg despite its png extension.

So I tried this url for the tile server:

url <- "https://geodata.nationaalgeoregister.nl/tiles/service/wmts/opentopo/EPSG:28992/{z}/{x}/{y}.jpeg"

but maptiles does not understand jpeg extension, only jpg and png.
So I've update maptiles to allow jpeg extension and you can try this (using the dev version of the package):

remotes::install_github('riatelab/maptiles')
library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.2, PROJ 7.1.0; sf_use_s2() is TRUE
library(maptiles)

p = st_point(c(328452.9,267552.5))
p = st_sfc(p)
p = st_set_crs(p, 28992)
bb = st_bbox(st_buffer(p, 300000))

url <- "https://geodata.nationaalgeoregister.nl/tiles/service/wmts/opentopo/EPSG:28992/{z}/{x}/{y}.jpeg"
mffp <- list(src="OpenTopo", q=url, sub=NA, cit='')

t <- get_tiles(bb, provider=mffp, crop= T, forceDownload = T, zoom=7)
plot_tiles(t, adjust = TRUE)

Created on 2022-02-15 by the reprex package (v2.0.1)

I have modified point coordinates and zoom level from your original example to show something not on the sea :-)

@gremms1
Copy link
Author

gremms1 commented Feb 15, 2022

Hi, I came to the same conclusion (concerning the jpeg) yesterday evening! Thanks for acting so promptly. Getting the tiles now works, the only thing I can´t figure out is what is going wrong with the projection. Using the following code

library(sf)
library(maptiles)
library(stars)
library(tmap)
p = st_point(c(328452.9,267552.5))
p = st_sfc(p)
p = st_set_crs(p, 28992)
bb = st_bbox(st_buffer(p, 300000))
url <- "https://geodata.nationaalgeoregister.nl/tiles/service/wmts/opentopo/EPSG:28992/{z}/{x}/{y}.jpeg"
mffp <- list(src="OpenTopo", q=url, sub=NA, cit='')
t <- get_tiles(bb, provider=mffp, crop= T, forceDownload = T, zoom=7)
plot_tiles(t, adjust = TRUE)
map <- stars::st_as_stars(t)
tmap_mode("plot")
tm_shape(map) +
  tm_rgb() +
tm_graticules(lines=F)

shows that the map is way bigger than it actually should be.

@rCarto
Copy link
Member

rCarto commented Feb 15, 2022

I suspected something odd with the projection stuff.
I think it has something to do with this part of the url (and the way tiles are delivered): EPSG:28992
Most of tiles providers ask for lon & lat in EPSG:4326 and offer tiles in EPSG:3857 (web mercator) and this projs are actually hardcoded in maptiles.
Can you direct me to the API documentation just to be sure of what input/output proj are expected?

@gremms1
Copy link
Author

gremms1 commented Feb 15, 2022

I think you're right. this is the KVP WMTS GetCapabilities page. If you ctrl+f and search for opentopo you can see EPSG:28992. Thanks for your help!

@gremms1
Copy link
Author

gremms1 commented Feb 15, 2022

Additionally, here a working example with an interactive tmap (requires dev-version):

remotes::install_github('r-tmap/tmap')
library(tmap)
library(leaflet)
epsg28992 <- leafletCRS(
  crsClass = "L.Proj.CRS"
  ,code = "EPSG:28992"
  ,proj4def = "+proj=sterea +lat_0=52.15616055555555 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +towgs84=565.417,50.3319,465.552,-0.398957,0.343988,-1.8774,4.0725 +units=m +no_defs"
  ,origin=c(-285401.92,903401.92)
  ,resolutions = c(3251.206502413005,1625.6032512065026,812.8016256032513,406.40081280162565,203.20040640081282,101.60020320040641, 50.800101600203206,25.400050800101603,12.700025400050801,6.350012700025401,3.1750063500127004,1.5875031750063502,0.7937515875031751,0.39687579375158755,0.19843789687579377,0.09921894843789689,0.04960947421894844)
)

url <- "https://geodata.nationaalgeoregister.nl/tiles/service/wmts/opentopo/EPSG:28992/{z}/{x}/{y}.jpeg"


data(World)
tmap_mode("view")

tm_shape(World) +
  tm_polygons() +
  tm_basemap(NULL) +
  tm_view(projection = epsg28992, set.view = c(5.092098,52.093992,4)) +
  tm_tiles(server = url) +
  tm_mouse_coordinates() 

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

No branches or pull requests

2 participants