简体   繁体   中英

How can I extract spatial df altitude values from a raster file?

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

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM