how to convert RgoogleMaps PNG to SpatialGridDataFrame in R? - r

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?

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...

Related

How to get raster file from a nested raster list produced by landscapemetrics package in R?

Package landscapemetrics can calculate area of each patch for a given raster file, shape of that patch and so on. I want to have not only tibble-frame with patch metrics calculated, but a new raster where each pixel within specific patch will have a value of the area of that patch, shape indicator and so on. We can do it with function spatialize_lsm() (it produces a Large list nested object with probably RasterObject objects within):
library(landscapemetrics)
plot(podlasie_ccilc) # this raster data is provided with package
podlasie.metrics.area <- spatialize_lsm(podlasie_ccilc, what = 'lsm_p_area') # creates a list
plot(podlasie.metrics.area) # produces an error...
How to get a desirable raster file with patch metrics from that list? I guess it is a question of raster package or something else, since landscapemetrics documentation tells nothing about this step.
I not that this data and new raster do not have resolution of the pixel like in meters (30, 30 for Landsat satellite image, for example). So we cannot plot the new raster produced:
podlasie.metrics.area[[1]]
plot(podlasie.metrics.area[[1]])
So I guess landscapemetrics cannot deal with such rasters, we can even use its function to check a suitability of the prior raster for patch discovering:
check_landscape(podlasie_ccilc)
Upd. I did it for the Landsat dataset with resolution 30, 30 and it produced patch area raster, but again I cannot open/show/save as raster it, because of the same error.
Package maintainer helps to solve a problem (yes, it is just related to the structure of list):
plot(podlasie.metrics.area[[1]]$lsm_p_area)

Easy way to convert sfc_POINT into RasterLayer?

I am using the library sf for spatial data in R. I want to use the SpaDES::splitRaster() method to split up a grid made with sf::st_make_grid() into many tiles. As far as I know, neither the sf or the raster library support such an operation.
However, st_make_grid() returns an object of sfc_POINT, but splitRaster() requires an object of type RasterLayer (for example).
Is there a method in sf (or raster, but I'd prefer to stay within sf) that I can use to quickly convert sfc_POINT into a RasterLayer, ideally without loading rgdal and rgeos?

Custom background image with ggmap

I'm using R and ggmap to analyse some geographic data. I would like to use my own background map (a tif file) instead of the ones provided by ggmap (google maps, osm, stamen, etc.).
Is there a way to load my own raster file and display it using ggmap ? I really want to keep ggmap as most of my analysis use ggmap's functions (geom_density2d, geom_tile, etc.). For example a wrapper that transform a raster file reads by the raster package to a ggmap get_map object ?
Thanks

Convert Raster object into Shapefile

Basically, I have a spLines Object with a bunch of lines, and after I am rasterizing it - rast2 <- rasterize(spLines, rast, fun='count') - in order to get a grid frequency map. However, I'd like to convert and save that rast2 Object into a Shapefile. Is that possible?
Thanks in advance!
this raster package will do the trick but there's a much faster way to do it if you have python installed by using gdal_polygonise.py

How to convert from shape into polygon in R? There was shape2poly(shapefiles) but this function have been removed

How to convert from shape into polygon in R? There was shape2poly(shapefiles) but this function have been removed, are shapefiles, maptools, spdep still packages for handling maps in R?
I tend to use the OGR stuff, as it lets me work with data from a range of sources (geodatabases, kml, etc).
library(rgdal)
mylayer <- readOGR(dsn="/path/to/folder/containing/shapefile",
layer="shapefilename-minus-dot-shp")

Resources