ggmap stamen watercolor png error
Asked Answered
S

3

8

I would really appreciate some help / ideas about a problem I am having with ggmap and stamen watercolor.

I keep getting the same error message every time I try to create a stamen watercolor map:

"Error in readPNG(destfile) : file is not in PNG format"

Here are some examples of simple code that is returning this error:

qmap("new-york", zoom=13, source="stamen", maptype="watercolor")

or

get_map(location='Auckland', source="stamen", maptype="watercolor", zoom=13)

I am using r version: [Default] [64-bit] C:\Program Files\R\R-3.0.2 under windows 8

I realize a few people have posted about this same problem - but only a few - and I haven't seen any explanations / suggestions. I'm stumped and frustrated and am really hoping someone with more experience than me has run into (and solved) this problem. Thanks in advance or any help you can provide

Schizomycete answered 6/5, 2014 at 7:4 Comment(0)
M
14

As a temporary fix you can do the change yourself. Type

get_stamenmap 

at the R terminal. This will dump out the code for loading the maps. You will need to edit this code and replace the function in the namespace.

Copy the code to a text editor and make if a function again by changing the first line:

get_stamenmap <- function (bbox = 

Then you need to switch over to loading jpegs. Search on png and change the text to jpg. I had two instances which looked like the text you need in the file extension, they were on line 64 and 71 for me.

64: urls <- paste(urls, ".jpg", sep = "")
71: destfile <- paste(filename, "jpg", sep = ".")

On line 75 there is the function readPNG, which you need to change to readJPEG.

tile <- readJPEG(destfile)

You will also need to ensure to load the jpeg package, i.e. library(jpeg) since ggmap does not otherwise realise this is now needed. I also needed library(plyr) but I did not figure out why - I did this because I got a later error message about ldply() which I found in that package.

Now paste this "all new" function back into the terminal. Afterwards, you need to overwrite the function embedded in the package, which is different from the local copy you just pasted into the terminal, so you need to type this:

assignInNamespace("get_stamenmap",get_stamenmap,ns="ggmap")

Now you should be ready to use qmap again. This procedure worked for me and was simpler than recompiling the package with the same changes or downloading the latest source which has these fixes and compiling.

Mace answered 19/6, 2014 at 7:55 Comment(0)
I
2

I have had the same issue. Note that if you run

get_map(location='Auckland', source="stamen", maptype="watercolor", zoom=13)

you will get the URL(s) for the tiles you're trying to load in R. When I visit one of the URLs given, it redirects to a .jpg, instead of a .png. Hence the error is accurate - the tiles being served are not in PNG format - they're JPGs.

Looks like this is a bug in ggmap introduced by a change in Stamen's API. It appears that version 2.4 will address this; see the GitHub commit here: https://github.com/dkahle/ggmap/commit/c7c48947360351f2e86ba13d0457aa3894b51d46.

Indamine answered 10/6, 2014 at 16:25 Comment(0)
P
1

An alternative until the 2.4 is published in the library is to load the functions available in the repository and replace the ones already present on the ggmap package (as Michael answered). I just loaded the content of get_stamenmap.R and filedrawer.R (needed for url_lookup()) as was good to go with watercolor maps.

Perplex answered 29/8, 2014 at 13:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.