calculating centroid of raster
Asked Answered
E

1

4

I have a list (s) containing information on the probable locations of many animals in South America. For example, this is the type of stored information and what it looks like when plotted for the first individual.

Example:

> s[1]
[[1]]
class       : RasterLayer 
dimensions  : 418, 313, 130834  (nrow, ncol, ncell)
resolution  : 0.16666, 0.16666  (x, y)
extent      : -86.333, -34.16842, -55.91633, 13.74755  (xmin, xmax, ymin, ymax)
coord. ref. : NA 
data source : in memory
names       : layer 
values      : 0, 1  (min, max)
> plot(s[[1]])

enter image description here

Note: the green areas are all "likely" locations and the grey areas are "unlikely" locations.

I would like to calculate the centroid of this probable location (i.e., centroid of the green area).

Below @dww suggested the following solution (which works for the simulated data), but leads to an error message with my data.

colMeans(xyFromCell(s, which(s[]==1)))

Error in xyFromCell(s[1], which(s[] == 1)) : 
trying to get slot "extent" from an object of a basic class ("list") with no slots
Ether answered 4/10, 2018 at 17:31 Comment(6)
You might try searching and asking here: gis.stackexchange.com your answer is essentially looking for a tool and that site is more oriented towards that sort of thingCinemascope
gis.stackexchange.com/search?q=centroid+raster+%5Br%5DCinemascope
I'd recommend against doing this. If your green blobs look like this: O O (two blobs with a gap in the middle), then the centroid is not in either blob. The centroid is not the most likely location.Loggins
@Cinemascope thank-you for the helpful suggestion. I had looked there previously, but obviously did not use the correct search terms since I only saw questions for ArcGISEther
@Loggins I'm also wary about this approach, but thought I would visually check the results to make sure that the centroid does fall within the probable values.Ether
@moody_mudskipper I believe there's been pretty big edit since I first weighed inCinemascope
J
6

To find the centroid of the cells where a raster r has the value 1, you can use

colMeans(xyFromCell(r, which(r[]==1)))

Essentially, the centroid is at the mean of the latitudes/longitudes of the subsetted locations.

Here's some reproducible dummy data to test on:

r = raster(matrix(sample(0:1, 10000,T), nrow=100,ncol=100))
Jamal answered 4/10, 2018 at 17:45 Comment(4)
Thanks for this suggestion. This seems to work with the sample data you've provided, however, when I attempt it on my data, I get the following error message: Error in xyFromCell(s, which(s[] == 1)) : trying to get slot "extent" from an object of a basic class ("list") with no slots Any thoughts on why this is not working?Ether
You did not provide example data, so I can't be 100% certain. But I think almost certainly that s is a list of raster* objects, rather than a raster* itself. You need to perform the raster analysis on the elements of the list rather than on the list.Jamal
Any thoughts on how to do that? I've tried: colMeans(xyFromCell(s[1], which(s[1]==1))) so that it just pulls the first object in the list. but I still get the same error message. aside from this, I don't know how else I could just work with one raster at a time.Ether
there really isn't a way for me to produce all or part of my data set, it's a long multi-step process to get to this point.Ether

© 2022 - 2024 — McMap. All rights reserved.