3

History: Extracted raster data from the static Google map png, loaded it on the R device through ggimage.

library (png)
library (ggmap)

rasterArray <- readPNG ("My.png")

x = c (40.702147,40.718217,40.711614)
y = c (-74.012318,-74.015794,-73.998284)

myData <- data.frame (x, y)

print (ggimage (rasterArray, fullpage = TRUE, coord_equal = FALSE) 
    + geom_point (aes (x = x, y = y), data = myData, colour = I("green"), 
      size = I(5), fill = NA))

I did run dput on the rasterArray but the output is of 20 MBs, can't post here.
BTW, this is the URL of that static map:

Question: For plotting "GPS coordinates" on the R device containing the map in pixels, do I need to scale the data.frame?

I saw this page: http://www-personal.umich.edu/~varel/rdatasets/Langren1644.html Do I need to do scaling the way they have shown here?

If yes, then what else other than the man page of scale function do I need to understand to get this done?

Am I barking at the wrong tree?

www
  • 38,575
  • 12
  • 48
  • 84
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
  • If the question is still broad or unclear please let me know how to improve it. – Aquarius_Girl Jul 25 '12 at 11:39
  • 1
    ggimage knows nothing about coordinates, it has no clue about the spatial origin of the image. Its just an image. Have you considered using get_map from package:ggmap? It will get a google maps image, and then you add geom_points to it. The coordinates are lat-long. – Spacedman Jul 25 '12 at 12:53
  • @Spacedman the problem is that this is to be done "offline". Is it possible to store the value of get_map for later use? – Aquarius_Girl Jul 25 '12 at 12:55
  • 1
    Of course - the save() function will serialise anything to disk. – Spacedman Jul 25 '12 at 13:02
  • @Spacedman I have to do this "in memory" not on the png. See this question of mine: http://stackoverflow.com/questions/11543620/how-to-display-coordinates-on-the-static-map-png-file-as-they-are-received-from What's the way out? Please help. – Aquarius_Girl Jul 25 '12 at 13:03
  • 2
    What does 'in memory' mean? get_map doesn't leave anything on disk, it creates an object with the image data in it. You can work with that. I think we're missing the big picture here, and the big picture is not really something SO is the right place for. – Spacedman Jul 25 '12 at 13:35
  • @Spacedman There is no hidden picture here. By "in memory" I meant that I didn't want to constantly keep on writing and reading on/from the "png". And since this has to be done offline, so, I am thinking of saving the data received from `get_map` and supplying that data to `geom_point` from a "file"! Does this sound sensible? Please confirm. – Aquarius_Girl Jul 25 '12 at 14:51

1 Answers1

22

I think your mistake was the following:

  • Trying to plot geographic data on an image, where that image doesn't have any awareness of the map coordinates
  • Possibly transposing your latitude and longitudes in the data frame

Here is how you should do it instead, in two steps:

  1. Get the map with get_map() and save it to disk using save()
  2. Plot the data with ggmap()

First, get the map.

library (ggmap)


# Read map from google maps and save data to file

mapImageData <- get_googlemap(
  c(lon=-74.0087986666667, lat=40.7106593333333), 
  zoom=15
)
save(mapImageData, file="savedMap.rda")

Then, in a new session:

# Start a new session (well, clear the workspace, to be honest)

rm(list=ls())

# Load the saved file

load(file="savedMap.rda")

# Set up some data

myData <- data.frame(
    lat = c (40.702147, 40.718217, 40.711614),
    lon = c (-74.012318, -74.015794, -73.998284)
)


# Plot

ggmap(mapImageData) +
    geom_point(aes(x=lon, y=lat), data=myData, colour="red", size=5)

enter image description here

Andrie
  • 176,377
  • 47
  • 447
  • 496
  • PS, I have no idea why the colours are so weird on my machine. – Andrie Jul 25 '12 at 15:18
  • All I can say is that I am grateful for your help and patience. I am not a Maths student, that's a big problem when dealing with R. I'll try your code tomorrow. – Aquarius_Girl Jul 25 '12 at 15:20
  • @AnishaKaul This isn't about maths. It's about carefully understanding each bit of the puzzle, reading the help files, and trying the examples. That's what I did during the past hour to get to this answer. – Andrie Jul 25 '12 at 15:22
  • @Andrie, will this topic be covered in your next R book? :) – Roman Luštrik Jul 25 '12 at 15:32
  • Andrie, the title you have added can be easily solved through `RGoogleMaps` package. This thread is about doing the same without the png, without reading and writing the disk, doing it offline. Kindly edit the title accordingly. – Aquarius_Girl Jul 25 '12 at 15:35
  • @AnishaKaul Feel free to edit the title to reflect the problem. – Andrie Jul 25 '12 at 15:51
  • 1
    Andrie's solution has two parts: first get the map and save it. Then you can unplug your network connection and go 'offline'. You can even quit and restart R, then load the map from the saved .rda file. This is still offline. Then add your points with ggmap. Repeat. – Spacedman Jul 25 '12 at 16:00
  • 1
    @AnishaKaul I managed to solve the colour issue by using `get_googlemap` directly rather than `get_map`. – Andrie Jul 26 '12 at 06:49
  • Andrie, the colour problem is indeed solved with the new function. thanks for everything. – Aquarius_Girl Jul 26 '12 at 07:34
  • Now, I am not able to see any "start bounty" link in this thread. – Aquarius_Girl Jul 26 '12 at 07:35
  • @AnishaKaul You can usually only start a bounty 48 hours after asking the question.# – Andrie Jul 26 '12 at 07:49
  • Andrie, how do you describe then "loaded in ram"? What are the proper words? Actually this is quite important and different than doing it normally by reading and writing a png (since this will crash the disk). – Aquarius_Girl Jul 26 '12 at 07:53
  • @AnishaKaul Everything you do happens in RAM. I think the word "offline" is sufficient. And whatever you do, you need to read the image once from disk (as you did in your initial code). Writing to disk will only happen if you save the plot. The title should reflect the problem in as general a way as possible, so that future users can find it and recognize it as the same problem they may have. – Andrie Jul 26 '12 at 08:06
  • @Andrie, this solution is widely good. There are some user that PNG Errors, like the ones mentioned here and other places. I had readPNG error i needed to work with offline data, and i could not. So after three hours, the ggmap started to work again! So now, i'm using this simple but usefull answer – Sergio Feb 18 '16 at 05:33