简体   繁体   中英

Offline plotting of map coordinates on static maps of Google

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?

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)

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM