Easy way to convert sfc_POINT into RasterLayer? - r

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?

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)

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")

Overlaying SpatialLines by SpatialPolygons in R

I have a SpatialLines object representing roads and a SpatialPolygons object
containing cities.
I'd like to know how to overlay a SpatialLines object by a SpatialPolygons
object in R.
I'd like to know the Lines that passes over the two cities and which cities in?
Is it possible?
You need the rgeos package which wraps the GEOS library of geometry operations.
Then probably gIntersection or gIntersects will do what you want.
In general, overlays of spatial objects are handled by the over function from the sp package. In the Methods section of the documentation of over (?over) there is a list of methods for over, which does not include the combination of Lines and Polygons. Luckily, the documentation of over says that by installing the rgeos package these kind of methods are made available.

how to convert RgoogleMaps PNG to SpatialGridDataFrame in 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...

Autokriging spatial data

I'm trying to use a kriging function to create vertical maps of chemical parameters in an ocean transect, and I'm having a hard time getting started.
My data look like this:
horiz=rep(1:5, 5)
depth=runif(25)
value = horiz+runif(25)/5
df <- data.frame(horiz, depth, value)
The autoKrige function in the automap package looks like it should do the job for me but it takes an object of class SpatialPointsDataFrame. As far as I can tell, the function spTransform in package rgdal creates SpatialPointsDataFrame objects, but there are two problems:
OSX binaries of this aren't available from CRAN, and my copy of RStudio running on OXS 10.7 doesn't seem to be able to install it, and
This function seems to work on lat/long data and correct distance values for the curvature of the Earth. Since I'm dealing with a vertical plane (and short distances, scale of hundreds of meters) I don't want to correct my distances.
There's an excellent discussion of kriging in R here, but due to the issues listed above I don't quite understand how to apply it to my specific problem.
I want a matrix or dataframe describing a grid of points with interpolated values for my chemical parameters, which I can then plot (ideally using ggplot2). I suspect that the solution to my problem is considerably easier than I'm making it out to be.
So there a a few question you want answered:
The spTransform function does not create SPDF's, but transforms between projections. To create a SPDF you can use a simple data.frame as a start. To transform df to a SPDF:
coordinates(df) = c("horiz", "depth")
OS X binaries of rgdal can be found at http://www.kyngchaos.com. But I doubt if you need rgdal.
spTransform can operate on latlong data, but also on projected data. But I do not think you need rgdal, or spTransform, see also point 1.
After you create the SPDF using point 1, you can use the info at the post you mentioned to go on.

Resources