I am trying to extract values of raster cells on points using extract
from raster
package in R (similar to 'Extract Values to Points' in ArcGIS-10.2). After doing so in order to check integrity, I computed the total value of all the raster cells (i.e. sum) ~66,000 and total extracted values on points ~49,000. We have tried both techniques simple
and bilinear
interpolation techniques. Data loss gets only slightly reduced with bilinear
technique, i.e. ~50,000 points are extracted.
There is a huge data loss, if anyone can provide a way forward or has experienced the same.
-
If you are only sampling 49,000 points then obviously they are going to sum to less than 66,000 cells. What did you expect, and can you give us a reproducible example maybe using random data on a much smaller grid?– SpacedmanCommented Nov 20, 2014 at 12:00
-
@Spacedman, he means the sum of point values, as a proxy for how different they are, I guess.– user21313Commented Nov 20, 2014 at 13:49
-
@CincoSauces yes. But how can the sum of the values of a small number of points sampled over a grid ever be anything but less than the sum of the grid cells? We really need to see some code and some clarification. I don't understand why this Q has been upvoted.– SpacedmanCommented Nov 20, 2014 at 15:01
-
@Spacedman : I did not mean 66K cells or 49K cells, To brief you more: 66 K is the total road length(adding cell by cell) in the raster file and 49 K is the total road length (adding extracted raster value on points) after the raster file is extracted on Points.– TishaCommented Nov 25, 2014 at 4:35
2 Answers
You might want to give a try to the GMT tool grdtrack. You can start by reconverting to your raster to a NetCDF file using gdal_translate:
gdal_translate -of NetCDF myraster.tif myraster.grd
and then put your x,y locations in a text file:
x1, y1
x2, y2
.., ..
then call grdtrack on that NetCDF grid based on your table of locations:
grdtrack mylocations.xy -Gmyraster.grd > myvalues.txt
You will get a new table with the sampled points. This procedure should work well and it is relatively fast. Good luck!
-
How this answers the question?– user32309Commented Nov 21, 2014 at 5:40
-
Thanks for your inquiry @Pascal, this answer addresses the question by providing a simple yet effective method of solving the problem.– user21313Commented Nov 21, 2014 at 14:01
You said:
I am trying to extract values of raster cells on points using extract from raster package in R (similar to 'Extract Values to Points' in ArcGIS-10.2).
Lets set up a test raster:
require(raster)
r = raster(ncol=100,nrow=100,xmn=0,xmx=1,ymn=0,ymx=1)
r[]=runif(100*100)
You said:
After doing so in order to check integrity, I computed the total value of all the raster cells (i.e. sum) ~66,000 and total extracted values on points ~49,000.
Okay, lets do that:
pts = cbind(runif(100),runif(100))
vr = extract(r,pts)
sum(vr)
[1] 55.41762
sum(values(r))
[1] 5003.098
Obviously the sum of 100 points sampled at random across that raster is going to be approximately 0.5*100 = 50. The sum of the whole raster is 0.5*100*100 because there's 100*100 cells with an average value of 0.5.
You said:
We have tried both techniques simple and bilinear interpolation techniques. Data loss gets only slightly reduced with bilinear technique, i.e. ~50,000 points are extracted. There is a huge data loss, if anyone can provide a way forward or has experienced the same.
I do not understand why you think this is a good idea for some kind of "integrity test" unless I misunderstand what you are doing. If this answer doesn't help please clarify your question with some sample code like mine. Otherwise this question is unclear.
-
I appreciate your inputs but the problem is little more deep. I have a raster which consists of road length i.e. each cell represents a value of total road length present per sq Km on earth in kilometers and I need to extract this raster on Points (~ at 30 arc seconds which is 928 m). So after the extraction, i try to compute the Total Road Length on raster cells and points and found a whooping difference of 17K in kilometers. I suspect there will be some loss due to sampling but to this degree was unaware.– TishaCommented Nov 25, 2014 at 4:42
-
Edit your question to set up an example like I've done that illustrates the problem. Is it just a case that your new sample cell areas are different, and you've forgotten that the original raster is km/**per unit area** and you need to scale up by the change in cell area? I don't know, because you haven't given us any code. Commented Nov 25, 2014 at 8:35