0
votes

I need to extract altitude values from a raster using spatial data frame points.

Crossing two objects (raster and vectorial) I have just obtained NA.

> lat41_42_sp
class      : RasterLayer
dimensions : 1201, 10801, 12972001  (nrow, ncol, ncell)
resolution : 0.0008333333, 0.0008333333  (x, y)
extent     : -9.000417, 0.0004166633, 40.99958, 42.00042  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
source     : memory
names      : layer
values     : -12, 2307  (min, max)

> spdf
class       : SpatialPointsDataFrame
features    : 1757
extent      : -46.58056, 71.00404, -158.0419, 174.9681  (xmin, xmax, ymin, ymax)
crs         : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
variables   : 3
names       : WINDFARM_NAME,  LATITUDE,  LONGITUDE
min values  :   'A_Farrapa', -46.58056, -158.04192
max values  :     'Zuromin',  71.00404,   174.9681

Spatial Data Frame has a longer extent, but there are points into RasterLayer.

But it does not work :(.

> my<-extract(lat41_42_sp,spdf)
> my[1:10]
 [1] NA NA NA NA NA NA NA NA NA NA
> head(my)
[1] NA NA NA NA NA NA
> tail(my)
[1] NA NA NA NA NA NA
2
Could you provide a sample of your data that can be copied and pasted?NelsonGon

2 Answers

0
votes

This is probably because all the points are outside the raster. I cannot know that for sure, but the information provided shows that the spatial extent of the points is much larger than that of the raster.

You could check if there are any points on the raster by doing

plot(lat41_42_sp)
points(spdf)

But clearly your spdf is not correct: you have latitudes (y) between -158.0419 and 174.9681; surely these must be latitudes. That suggests that you made a mistake when you created spdf, that is, that you used ("latitude", "longitude") instead of ("longitude", "latitude"). You can also see that from the range of LONGITUDE and LATITUDE that you show.

0
votes

Try select only the points in raster:

library(raster)
spdf_in_lat41_42_sp = crop(spdf ,lat41_42_sp)
my<-extract(lat41_42_sp, spdf_in_lat41_42_sp)