0

I have a DEM raster and a list of SpatialPointsDataFrames (data available here https://drive.google.com/open?id=1ERFdsqDGLH1a_FbxwawE_gPm0Au0Q9vT).

EDIT: (with regard to @Spacedman's comment)

I want to calculate the smallest N for each data point such that the raster values in an NxN square around each SpatialPoint's square have an elevation range (max-min) >= 15metres. It should look something like the picture below, where the brown raster cell is the one with a SpatialPoint inside and the blue line represents the extent of the NxN square.

enter image description here

I've looked at the help for the raster:focal function as proposed by @JeffreyEvans, but I don't see any possibilities to input my SPDF as source for the focal points.

  • 1
    So if you have N points you want N polygons? Even for cells with multiple points? What about adjacent cells with points in? If not, then you are really only creating a number of squares centred on the cell centres with a various offsets and you can do this without buffering at all... – Spacedman Jul 10 '19 at 11:16
  • The squares can overlap, that should not be the problem. In the end, I want to calculate the difference in elevation between the cells of each square and automatically enlarge every square until a certain value (for example 15 meters) is reached. The solution provided by @Sam seems to fit, I will try that. – Florian Mlehliv Jul 10 '19 at 13:09
  • You should probably reword your question, because the answer doesn't give you a polygon vector at all - it returns a data frame of the cell indexes of the neighbours. Try and ask about what you want to achieve rather than how you think you are going to do it. – Spacedman Jul 10 '19 at 13:42
  • 2
    I think that you are simply looking for the raster::focal function. You would then just pull the resulting focal raster values for your points. – Jeffrey Evans Jul 10 '19 at 15:13
  • 1
    This is your problem - "I want to calculate the difference in elevation between the DEM cells inside my "buffer" and enlarge the "buffer" until a certain threshold is passed (15 m elevation difference). ". Can we concentrate on that? You want to find the smallest N for each data point such that the raster values in the NxN square around that point's square has an elevation range (max-min) > 15metres? – Spacedman Jul 11 '19 at 6:49
1

Try the raster::adjacent function. This function can extract raster cells surrounding a given cell. You don't need to bother buffering the points.

I'm not sure what level of visualisation you want from your final product, but this produces a data frame with cell numbers, data and xy coordinates;

require(raster)

# make a dummy raster
r <- raster(xmn=0,xmx=10,ymn=0,ymx=10,res=1)
r[] <- sample(1:5,ncell(r), replace=T)

# make some dummy points
points <- data.frame(x=round(runif(10, 0.0, 10.0),1), y=round(runif(10, 0.0, 10.0),1))

# create a SpatialPointsDataFrame
coords <- cbind(points$x, points$y)
pts <- SpatialPointsDataFrame(coords, points)

# Add an attribute to SPDF that is the cell number of the raster, per point
pts$cellno <- extract(r,pts,cellnumbers=T)[,"cells"]

# use the 'adjacent' function to extract queen's case raster cells, per point
# this requires the cell numbers we extracted above
li <- lapply(pts$cellno, function(x) adjacent(r,x, directions = 8, include=T))

# collapse to one data frame
df <- do.call(rbind.data.frame, li)

# add a column that is the values of the cells, per point
df$val <- extract(r,df[,2])

# if you want, merge in the XY data
df.all <- merge(df, pts@data, by.x = "from", by.y = "cellno")
|improve this answer|||||
  • So, where I appreciate your ingenuity here, this kind of over complicates the solution. You can accomplish exactly the same thing by simply running a focal function and then taking the "at cell" values of the points. – Jeffrey Evans Jul 10 '19 at 14:14
  • @JeffreyEvans yes you're right, and i also found quicker solutions myself but i thought this a little more transparent for OP. Which is perhaps patronizing. But yes, quicker ways and less complicated also. – Sam Jul 10 '19 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.