# Extract very accurate lat lon coordinates from a raster of a given resolution

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

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

``````# positive and negative values

## [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