3

I have derived a 'static map' using the GetMap() function from the RgoogleMaps package. I can save it (MyMap) to my harddrive as a PNG. However, then it looses the spatial reference.

Has anybody succeeded in creating a spatial object (in the sense of a GDAL-readable data format) from such a PNG?

mdsumner
  • 29,099
  • 6
  • 83
  • 91
Jens
  • 2,363
  • 3
  • 28
  • 44
  • 1
    I can do it from a ggmap object, which gets its map data from an OpenStreetMap source (and hence is legally reusable under the terms of the license). Have you looked at the structure of the Rgooglemaps objects? Try str(thing). – Spacedman Jul 02 '12 at 20:54
  • Try using dismo package instead with its gmap() function, you get the data directly in R as a 'raster' package object without a file. Another way that might work is to use the URLs constructed by GetMap (or gmap) directly with readGDAL in a build of rgdal with the HTML wrapper driver, or just try the TMS support with GDAL anyway: http://www.gdal.org/frmt_wms.html – mdsumner Jul 03 '12 at 00:00
  • ggmap and gmap, I will try out those versions two. It is always good to have alternatives. many thanks! – Jens Jul 03 '12 at 19:28

1 Answers1

7

Get your RGoogleMaps object as MyMap. Make it download the tile to MyTile1.png Use the raster package.

bb = MyMap$BBOX
t = stack("MyTile.png")
extent(t)=extent(bb$ll[,2],bb$ur[,2],bb$ll[,1],bb$ur[,1])

Now t is a raster stack. Do plotRGB(t) and you should see it. Now you can try writeRaster to create a GDAL data source. GeoTIFF perhaps?

And watch out for that pesky Google image usage agreement...

Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • yes, that was exactly what I had in mind. Many thanks! Of course, everything only for personl use. – Jens Jul 03 '12 at 19:25
  • Fine, but it's not a good practice to call any variable "t", since that's a predefined function used to transpose tables (change rows for cols). – Rodrigo Feb 05 '15 at 14:49
  • @Rodrigo if my `t` was a function, yes, but when R needs a function it will look for a function. `t=99; t(matrix(1:4,2,2))` still works fine. – Spacedman Feb 05 '15 at 17:14
  • Yes, that's true. But "good practices" come from the fact that 1) once you do it, others may learn it from you; 2) you may, from pure habit, one day confuse one for the other (perhaps if older or for some reason distracted). This in a big project may be a very hard to find bug. – Rodrigo Feb 05 '15 at 19:28
  • So you are saying that one should know all the names of all the functions and not call any of your variables by those names in case it triggers a highly improbable big? Impractical. – Spacedman Feb 05 '15 at 20:57