-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpolateGeolocatedRaster.m
72 lines (68 loc) · 2.17 KB
/
interpolateGeolocatedRaster.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
function B = interpolateGeolocatedRaster(X,Y,A,Xq,Yq,method)
%interpolate raster and keep NaNs from propagating
%Input
% X,Y - coordinates of input values
% A - input values
% Xq,Yq - coordinates of interpolated values
% method - interpolation method
%Output
% B - interpolated raster
%memory check
RasterReprojectionMemoryCheck((numel(Xq)+numel(Yq))*8*size(A,3));
% coordinate check
CheckInputCoordinates(X,Y,Xq,Yq);
%should have enough memory, go ...
if ismatrix(A)
noNaN = ~isnan(X) & ~isnan(Y) & ~isnan(A);
if nnz(noNaN)
F = scatteredInterpolant(X(noNaN),Y(noNaN),A(noNaN),method,'none');
else
F = scatteredInterpolant(X(:),Y(:),A(:),method,'none');
end
B = F(Xq,Yq);
elseif ndims(A)==3
for k=1:size(A,3)
V = A(:,:,k);
% allocate on first pass
if k==1
noNaN = ~isnan(X) & ~isnan(Y) & ~isnan(V);
if nnz(noNaN)
F = scatteredInterpolant(X(noNaN),Y(noNaN),V(noNaN),method,'none');
else
F = scatteredInterpolant(X(:),Y(:),V(:),method,'none');
end
B1 = F(Xq,Yq);
%memory check
if ispc
RasterReprojectionMemoryCheck(8*size(A,3)*(numel(B1)));
end
%allocate output space
B = zeros(size(B1,1),size(B1,2),size(A,3));
else
if nnz(noNaN)
F.Values = V(noNaN);
else
F.Values = V(:);
end
B1 = F(Xq,Yq);
end
B(:,:,k) = B1;
end
else
error('arrays of more than 3 dimensions not supported')
end
if all(isnan(B(:)))
error('all interpolated values are NaN, check input and output coordinates')
end
end
function CheckInputCoordinates(X,Y,Xq,Yq)
inputX = [min(X(:)) max(X(:))];
inputY = [min(Y(:)) max(Y(:))];
% polygon
xv = [min(inputX) max(inputX) max(inputX) min(inputX) min(inputX)];
yv = [min(inputY) min(inputY) max(inputY) max(inputY) min(inputY)];
overlap = inpolygon(Xq,Yq,xv,yv);
if nnz(overlap)==0
error('all the output coordinates are outside the quadrangle of the input coordinates')
end
end