0

I am a beginner in raster object, and I have a particular problem.

Suppose that I have a raster which is focus on Mexico.

Besides, I have created a SpatialPoint Object of coordinates that are accurate of different locations that stand for let's say houses.

I would like to extract the houses within the positive values of the raster. How should I do ?

class       : RasterLayer 
dimensions  : 72, 122, 8784  (nrow, ncol, ncell)
resolution  : 0.25, 0.25  (x, y)
extent      : -117.125, -86.625, 14.625, 32.625  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
data source : in memory
names       : layer 
values      : 54.00137, 126.3991  (min, max)


Formal class 'SpatialPoints' [package "sp"] with 3 slots
  ..@ coords     : num [1:459382, 1:2] -103.4 -98.2 -114.8 -110.9 -110 ...
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:459382] "1026" "1027" "1028" "1029" ...
  .. .. ..$ : chr [1:2] "Longitude" "Latitude"
  ..@ bbox       : num [1:2, 1:2] -117.1 14.7 -86.7 32.7
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:2] "Longitude" "Latitude"
  .. .. ..$ : chr [1:2] "min" "max"
  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
  .. .. ..@ projargs: chr "+init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"

1 Answer 1

0

You said the positive values of the raster... You raster has only positive values, you mean the positive coordinates? I will create a raster with positive/negative values to show how to do it (if the first case is the mean of your answer):

library(raster)
library(sp)

set.seed(123)

# raster creation
r <- raster()

r <- setValues(r, rnorm(ncell(r)))

# point creation
pts <- spsample(as(r@extent,"SpatialPolygons"),100,type='random')

plot(r)
plot(pts, add=T)

enter image description here

# positive and negative values

head(extract(r,pts))
## [1]  0.10838254 -0.63933663 -0.25773183  2.34038342 -1.12571713  0.08899983    

To extract only positive values, create a vector with position of point which have positive values:

# set test
positive <- which(extract(r >= 0,pts) == 1)

# just positives
head(extract(r,pts[positive,]))
[1] 0.10838254 2.34038342 0.08899983 1.98150871 0.55436178 0.23737944

# to extract the corresponding points 
ptsPositives <- pts[positive,]
ptsPositives
## class       : SpatialPoints 
## features    : 49 
## extent      : -166.7576, 164.8217, -89.23281, 85.95573  (xmin, xmax, ymin, ymax)
## coord. ref. : NA 
1
  • 1
    Thanks for your answer ! My raster has only positive values, the others are NA values. Therefore, I have some random patterns that carries those positive values. My goal is to only keep the latitude/longitude from the SpatialPoint, that overlay the pattern, namely the positive values within my raster. Your approach works very well ! Thank you
    – Sarah75013
    Aug 11, 2017 at 6:33

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.