Grid over lidar point cloud using LiDR - r

I there a way to put a grid over point cloud data using the LiDR package in R.
I want to place a 30x30m grid over a lidar data that I have and then run some statistics on each 10x10 square within the grid. Does anyone know if this is possible and what function I would use?

You can use grid_metrics() with a RasterLayer as layout. From the manual:
res numeric. The resolution of the output Raster. Can optionally be a RasterLayer. In that case the RasterLayer is used as the layout.

Related

Spatial operations with curvilinear data in program stars for R

I'm new to the package stars for R and am trying to do basic spatial operations with curvilinear data. I am using netCDF climate data. I am able to read the netcdf into r along with a shapefile I would like to use to specify the area in which I want to conduct analyses. I have tried to crop the file directly using st_crop() but receive the following error:
Warning message:
In st_crop.stars(test, wrst) : crop only crops regular grids
I then tried to warp the stars object using code like this:
warp <- test %>% st_set_crs(3338) %>% st_warp(st_as_stars(st_bbox(), dx = 2))
but I get this error:
Error in colrow_from_xy(pts, x, NA_outside = TRUE) :
colrow_from_xy not supported for curvilinear objects
Do I need to 'flatten' my curvilinear grid in order to conduct analyses in a given region? If so, how do I do that? Or, conversely, if I am able to conduct operations like st_crop() or the equivalent of raster operations calc() or stackApply() using a curvilinear grid, can someone point me in the right direction? Thanks so much.
Well I figured it out and it was quite simple. I was able to subset the stars object using the shapefile with this simple code: test[wrst]. No warping or resampling necessary.

Line density with R and spatstat

I use R and the spatstat package to create a density map of vessels' trajectories using the pixellate function (spatstat). I'm able to create a map and plot the trajectories density.
Now I would like to improve the design of my map and do something like the maps created by Niels Willems (Google Scholar). The image below give an example of what I'm looking to achieve. Do you know what I could do to get this kind of 3D effects ?
Thanks for your help,
Arnaud
If the original trajectories were represented as an object of class psp (line segment pattern) then you could have used spatstat::density.psp to compute this kind of smoothed image. But if you have already converted them to a pixel image using pixellate.psp (with pixel values that tell you how much trajectory length was in each pixel) then you could use spatstat::blur to get a smoothed image.

Changing the extent and resolution of raster layers in R for successful stacking

I am trying to create a species distribution model in R. I have created raster layers in ArcMap and have imported them into R. They cannot be stacked unless the extents are exactly the same and they all have the same number of rows and columns.
However, when I alter these factors to successfully stack them they lose all their values and my stacked data frame is just filled with NAs.
Does anyone know how I can alter the extent and resolution of my raster layers so they can be successfully stacked -- so I can then attach environmental info to presence points.
Cheers
One way to do this is to choose a raster that has the projection and extent that you want and use that as a template for the others
For example, if you have rasterA and rasterB. You can use projectRaster() to make a new version of rasterA with the same extent and resolution as rasterB. You should then be able to stack new.rasterA & rasterB.
new.rasterA <- projectRaster(rasterB, rasterA) # define the projection and extent
r.stack <- stack(new.rasterA, rasterB) # add them to a raster stack object
I had the same issue and I solved this in arcgis by snapping each raster to a mask of my study area.
This can be done by clicking geoprocessing -> environments -> processing extent - then select a layer you want to snap to in the snap raster box. I did this before I extracted (clipped) each layer and it worked perfectly. You can check the extent in properties when you are done for each layer you do to double check before you upload them into R.

R clip raster with multiple bands

I want to create a subset of an image with four bands. Therefore I am using the crop function in R.
A<-raster("L8_stacked.tif")
subset<-extent(c(639451, 660104, 5469254, 5489566))
B<-crop(A,subset)
As a result I get a raster with only one band in the .tif file. Do I have to define other options to get a subset image with 4 bands?
As the others already pointed out in the comments, the raster() function returns a (single) RasterLayer object. If you want a multilayer raster object you need to use the stack() or brick() function load the image into R. I.e.:
A <- stack("L8_stacked.tif")
If you then apply your extent with the crop() function, the result should be a raster stack containing all the bands from the original image.
To learn more on the raster package, read this document.

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