[R-sig-Geo] error occured when combining st_sample() with raster::extract()
Xiang Ye
x|@ngye @end|ng |rom bu||@|o@edu
Mon Jun 24 05:40:44 CEST 2024
Dear Ákos,
Thank you so much for the swift reply! Your answer helped me to solve this issue perfectly.
Have a great one!
叶翔 YE, Xiang
THINKING SPATIALLY<http://www.linkedin.com/in/spatialyexiang>.
Ph.D. in Spatial Statistics
________________________________
From: R-sig-Geo <r-sig-geo-bounces using r-project.org> on behalf of Bede-Fazekas Ákos <bfalevlist using gmail.com>
Sent: Monday, June 24, 2024 2:25
To: r-sig-geo using r-project.org <r-sig-geo using r-project.org>
Subject: Re: [R-sig-Geo] error occured when combining st_sample() with raster::extract()
Dear Xiang,
I guess that the reason for the error is that zion_points is an sf
object, while sampled_sites is an sfc object. You should create a
0-column sf object from the sfc using function st_as_sf().
Please try the following code:
extract(srtm, st_as_sf(sampled_sites))
HTH,
Ákos
__________
Ákos Bede-Fazekas
Centre for Ecological Research, Hungary
2024. 06. 23. 19:24 keltezéssel, Xiang Ye via R-sig-Geo írta:
> Dear community,
>
> I am exploring the functionality of raster package by following the online textbook Geocomputation with R, 1st ed. (I know the newer version of the textbook is online, I just want to finish what I left). The problem I encountered is associated with the example from Section 5.4.2, raster extraction:
> https://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fbookdown.org%2Frobinlovelace%2Fgeocompr%2Fgeometric-operations.html%23raster-extraction&data=05%7C02%7Cxiangye%40buffalo.edu%7Cb2edba793f3d475bd6c108dc93b1ec03%7C96464a8af8ed40b199e25f6b50a20250%7C0%7C0%7C638547639583101457%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=D7s8aHWKp3D%2FOtHtBbP5m2Yc%2BXbkzFFqV3zYChIJQd8%3D&reserved=0<https://bookdown.org/robinlovelace/geocompr/geometric-operations.html#raster-extraction>
> When I want to combine it with st_sample() from Section 5.2.3, i.e. extract the pixel values from say 15 randomly chosen locations, I met an error.
>
> Here comes the script:
>
> library(sf)
> library(raster)
>
> filename=system.file('raster/srtm.tif', package='spDataLarge')
> srtm=raster(filename)
> filename=system.file('vector/zion.gpkg', package='spDataLarge')
> zion=st_read(filename)
> zion=st_transform(zion, projection(srtm))
>
> data('zion_points', package='spDataLarge')
> extract(srtm, zion_points) # textbook example, work as expected
>
> sampled_sites=st_sample(zion, 15)
> extract(srtm, sampled_sites) # error occurred.
>
> The error message says "����: ������extract����ǩ��x = "RasterLayer", y = "sfc_POINT"���Ҳ����̳з���"
> There is Chinese text embedded because my OS is in Chinese. Translated into English, it reads "ERROR: function 'extract' labelling ��x = "RasterLayer", y = "sfc_POINT"�� cannot find the inherited methods".
>
> My question: what's the reason of this problem, and how to fix it?
>
> Thank you in advance!
>
> Ҷ�� YE, Xiang
> THINKING SPATIALLY<https://nam12.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.linkedin.com%2Fin%2Fspatialyexiang&data=05%7C02%7Cxiangye%40buffalo.edu%7Cb2edba793f3d475bd6c108dc93b1ec03%7C96464a8af8ed40b199e25f6b50a20250%7C0%7C0%7C638547639583112207%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=JAsnBDapAm4NYJb6zN5zdpralWRhHD1HA0M46sgwsQ4%3D&reserved=0<http://www.linkedin.com/in/spatialyexiang>>.
> Ph.D. in Spatial Statistics
>
> [[alternative HTML version deleted]]
>
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-sig-geo&data=05%7C02%7Cxiangye%40buffalo.edu%7Cb2edba793f3d475bd6c108dc93b1ec03%7C96464a8af8ed40b199e25f6b50a20250%7C0%7C0%7C638547639583115271%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=FQ04KwpzFewolFlX0%2B6rufWKda6jBlcFYugRiiN4N%2BQ%3D&reserved=0<https://stat.ethz.ch/mailman/listinfo/r-sig-geo>
_______________________________________________
R-sig-Geo mailing list
R-sig-Geo using r-project.org
https://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-sig-geo&data=05%7C02%7Cxiangye%40buffalo.edu%7Cb2edba793f3d475bd6c108dc93b1ec03%7C96464a8af8ed40b199e25f6b50a20250%7C0%7C0%7C638547639583118082%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=9OB10OER9p16VLRylMER5KlbA%2F7fwN11eEBbFYztkWc%3D&reserved=0<https://stat.ethz.ch/mailman/listinfo/r-sig-geo>
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list