


PURPOSE: assign to a set of points the values sampled at an underlying
raster
-------------------------------------------------------------------------
USAGE: P = assign2points(PS,raster,xcoords,ycoords);
where: [PS] is the set of points as an array in the form [xcoord, ycoord]
[raster] is the raster to be sampled%
[xcoords],[ycoords] are coordinate vectors of the raster
[delNaN] (optional) flag to delete all points with NaN in
valuefield. 1= delete NaNs; 0= leave NaNs in pointset.
-------------------------------------------------------------------------
OUTPUTS:
[P] is the original point set with an additional third column
holding values of the raster sampled at the coordinates. The output
array has the form [xcoord, ycoord, value]
-------------------------------------------------------------------------
NOTES: ycoords(1) and xcoords(1) should have the llc coords of raster.
if target coord lies exactely on the border of two cells,
lower left cell is sampled.
See also: sample_center()
Felix Hebeler, Geography Dept., University Zurich, April 2006.


0001 function PS = assign2points(PS,raster,xcoords,ycoords,delNaN) 0002 % PURPOSE: assign to a set of points the values sampled at an underlying 0003 % raster 0004 % ------------------------------------------------------------------------- 0005 % USAGE: P = assign2points(PS,raster,xcoords,ycoords); 0006 % where: [PS] is the set of points as an array in the form [xcoord, ycoord] 0007 % [raster] is the raster to be sampled% 0008 % [xcoords],[ycoords] are coordinate vectors of the raster 0009 % [delNaN] (optional) flag to delete all points with NaN in 0010 % valuefield. 1= delete NaNs; 0= leave NaNs in pointset. 0011 % ------------------------------------------------------------------------- 0012 % OUTPUTS: 0013 % [P] is the original point set with an additional third column 0014 % holding values of the raster sampled at the coordinates. The output 0015 % array has the form [xcoord, ycoord, value] 0016 % ------------------------------------------------------------------------- 0017 % NOTES: ycoords(1) and xcoords(1) should have the llc coords of raster. 0018 % if target coord lies exactely on the border of two cells, 0019 % lower left cell is sampled. 0020 % 0021 % 0022 % See also: sample_center() 0023 % 0024 % Felix Hebeler, Geography Dept., University Zurich, April 2006. 0025 0026 PS(:,3)=NaN; 0027 for i=1:size(PS,1) 0028 PS(i,3)=s_c(PS(i,1),PS(i,2),xcoords,ycoords,raster); 0029 end 0030 0031 % awkward way to delete NaNs... its getting late... 0032 if exist('delNaN','var') 0033 if delNaN==1; 0034 i=1; 0035 while i<size(PS,1) 0036 if isnan(PS(i,3)) 0037 PS(i,:)=[]; 0038 else 0039 i=i+1; 0040 end 0041 end 0042 end 0043 end 0044 0045 0046 function value = s_c(sx,sy,xcoord,ycoord,grid) 0047 % the sample_center function 0048 % calculate source cellsize 0049 xcs= abs(xcoord(2)-xcoord(1)); ycs= abs(ycoord(2)-ycoord(1)); 0050 0051 % get distance on x and y axis 0052 dx=abs(xcoord(1)- sx); 0053 dy=abs(ycoord(1)- sy); 0054 if sx>max(xcoord) || sy>max(ycoord) || sx<min(xcoord) || sy<min(ycoord) 0055 value = NaN; 0056 return 0057 end 0058 value = grid(size(raster,1)-(floor(dy/ycs)+1)+1, (floor(dx/xcs)+1));