Permalink
Browse files

Added utility files to rasterize the mesh

  • Loading branch information...
1 parent 29535f3 commit a40925a1335756538201c04812b97e4e6134d12b @Chrismarsh committed Oct 28, 2011
@@ -0,0 +1,128 @@
+function SaveAsciiRaster(varname, header,fname);
+ % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
+ % %
+ % Produced by Giuliano Langella %
+ % e-mail:gyuliano@libero.it %
+ % March 2008 %
+ % %
+ % Last Updated: 01 June, 2009 %
+ % %
+ % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
+
+
+%§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§
+%
+% ----- SYNTAX -----
+% SaveAsciiRaster(varname, header);
+% SaveAsciiRaster(varname); % in case varname is an xyz matrix
+%
+%
+% ----- DESCRIPTION -----
+% This function saves a spatial matrix into an Arc-Info ascii raster. Two file extension '.asc'
+% or '.txt' are supported.
+% FIRST CASE
+% USE: SaveAsciiRaster(varname, header);
+% It requires two inputs: (1) the z-values to be exported ('varname'
+% variable), and (2) the 'header' vector with the spatial information of
+% the grid. 'varname' can be a 1-D vector or a 2-D spatial grid.
+% SECOND CASE
+% USE: SaveAsciiRaster(varname);
+% If an xyz matrix (with [x_coord,y_coord,z_values]) is given as
+% 'varname', no 'header' has to be defined, since the function will
+% extract all the required header information from the xyz table. The
+% first row contains the x_coord, y_coord and z_value of the most
+% north-western cell; the last row refers to the most south-eastern
+% pixel. Elements in xyz are sorted column-by-column from the
+% geographical grid
+% (geographical_grid=[1st_col,2nd_col,3rd_col,...,last_col];
+% xyz=[1st_col;2nd_col;3rd_col;...;last_col]).
+% The xy coordinates have to refer to the center of the cells.
+%
+%
+% ----- INPUT -----
+% +'varname'(mandatory) : the MatLab matrix to be exported in ascii raster.
+% It can be passed:
+% (1) a 2-D z-values matrix of size equal to the spatial grid extent;
+% (2) a 1-D vector with z-values sorted as [1st_col;2nd_col;...];
+% (3) an xyz table with characteristics described in "SECOND CASE".
+%
+%
+% +'header'(facultative): the geospatial reference matrix.
+% If varname is an xyz matrix then header have not to be given.
+% The header matrix as the .hdr file in Arc-Info Binary Raster.
+% The 'header' MatLab variable is created when importing an ascii
+% raster with function ImportAsciiRaster (author: Giuliano Langella).
+%
+%
+% ----- EXAMPLE1 -----
+% [Z h] = ImportAsciiRaster(NaN, 'h'); % import DEM and Aspect
+% filt_gau05 = fspecial('gaussian', 3, 0.5); % create a low-pass spatial filter
+% Z_g5 = imfilter(Z, filt_gau05, 'symmetric'); % apply the filter
+% SaveAsciiRaster(Z_g5, h); % save the filtered DEM
+% ----- EXAMPLE2 -----
+% SaveAsciiRaster(xyz); % save the xyz matrix as an ascii grid
+%§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§
+
+
+
+% Save path
+% [FileName, PathName] = uiputfile({'*.asc','Arc-Info ASCII Raster (*.asc)'; ...
+% '*.txt','Text ASCII Raster (*.txt)'; ...
+% '*.*', 'All Files (*.*)'}, 'Save Ascii Grid', pwd);
+
+% OPEN file
+fid = fopen(fname,'w');
+
+% if I loaded an xyz matrix as 'varname'
+if nargin == 1
+ cellsize = abs(varname(1,2)-varname(2,2));
+ hor = max(varname(:,1)) - min(varname(:,1)) + cellsize;
+ ver = max(varname(:,2)) - min(varname(:,2)) + cellsize;
+ ncols = ceil(hor/cellsize);
+ nrows = ceil(ver/cellsize);
+ raster = zeros(nrows,ncols);
+ %create the header variable [default no data value is -9999]
+ header = [ncols; nrows; min(varname(:,1))-0.5*cellsize; min(varname(:,2))-0.5*cellsize; cellsize; -9999];
+ Zvar = reshape(varname(:,3),header(2),header(1));
+ varname=[];
+ varname=Zvar;
+end
+
+% WRITE HEADER
+fprintf(fid,'%s','ncols '); %1
+fprintf(fid,'%12.0f\n', header(1,1));
+fprintf(fid,'%s','nrows '); %2
+fprintf(fid,'%12.0f\n', header(2,1));
+fprintf(fid,'%s','xllcorner '); %3
+fprintf(fid,'%f\n', header(3,1));
+fprintf(fid,'%s','yllcorner '); %4
+fprintf(fid,'%f\n', header(4,1));
+fprintf(fid,'%s','cellsize '); %5
+fprintf(fid,'%f\n', header(5,1));
+fprintf(fid,'%s','NODATA_value '); %6
+fprintf(fid,'%f\n', header(6,1));
+
+% WRITE MATRIX
+%substitute to NaN the NODATA_value written in header
+varname(find(isnan(varname))) = header(6,1);
+%start loop
+ncols = header(1,1);
+nrows = header(2,1);
+handle = waitbar(0, mfilename);
+for CurrRow = 1:nrows;
+ waitbar(CurrRow/nrows);
+ % if varname is a vector instead of a 2-D array
+ if size(varname,2) == 1;
+ fprintf(fid,'% f ',varname( ((CurrRow-1)*ncols + 1) : (CurrRow*ncols) )' );
+ fprintf(fid,'%s\n', ' ');
+ % if varname is a 2-D array
+ else
+ fprintf(fid,'%f ',varname(CurrRow,:));
+ fprintf(fid,'%s\n', ' ');
+ end
+end
+fclose(fid);
+fclose('all');
+
+waitbar(CurrRow/nrows, handle, 'Done!');
+close(handle)
@@ -0,0 +1,84 @@
+function Z = gridtrimesh(F,V,X,Y,faceValue)
+%GRIDTRIMESH Fitting a square grid onto a triangular mesh.
+%
+% Z = GRIDTRIMESH(F,V,X,Y) fits a surface of the form Z = F(X,Y) to the
+% triangular mesh defined by F and V.
+%
+% The triangles of the triangular mesh are defined in the m-by-3 face
+% matrix F. Each row of F defines a single triangular face by indexing
+% into the n-by-3 matrix V that contains X, Y and Z coordinates of the
+% vertices.
+%
+% It is assumed that X and Y are produced by [X,Y] = meshgrid(x,y),
+% where x and y are monotonically increasing vectors.
+%
+% The function Z = MXGRIDTRIMESH(F,V,X,Y) gives exactly the same result
+% but is coded in C and compiled using MEX resulting in slightly faster
+% execution.
+%
+% Example: (assume the file example.mat contains F and V)
+% load example
+% nx = 10; ny = 10;
+% x = linspace(min(V(:,1)),max(V(:,1)),nx);
+% y = linspace(min(V(:,2)),max(V(:,2)),ny);
+% [X,Y] = meshgrid(x,y);
+% Z = gridtrimesh(F,V,X,Y);
+% surf(X,Y,Z)
+%
+%
+% Author: Willie Brink [ w.brink@shu.ac.uk ]
+% April 2007
+
+
+
+m = size(F,1);
+
+% size of grid
+nx = size(X,2);
+ny = size(X,1);
+
+% initialise output
+Z = NaN(size(X));
+
+% consider every triangle projected to the x-y plane and determine whether gridpoints lie inside
+for i = 1:m,
+ v1x = V(F(i,1),1); v1y = V(F(i,1),2); v1z = V(F(i,1),3);
+ v2x = V(F(i,2),1); v2y = V(F(i,2),2); v2z = V(F(i,2),3);
+ v3x = V(F(i,3),1); v3y = V(F(i,3),2); v3z = V(F(i,3),3);
+ % we'll use the projected triangle's bounding box: of the form (minx,maxx) x (miny,maxy)
+ minx = min([v1x v2x v3x]);
+ maxx = max([v1x v2x v3x]);
+ % find smallest x-grid value > minx, and largest x-grid value < maxx
+ east = NaN; west = NaN;
+ j = 1; while (j <= nx && X(1,j) < minx), j = j + 1; end; if (j <= nx), west = j; end
+ j = nx; while (j >= 1 && X(1,j) > maxx), j = j - 1; end; if (j >= 1), east = j; end
+ % if there are gridpoints strictly inside bounds (minx,maxx), continue
+ if ~isnan(east) && ~isnan(west) && east - west >= 0,
+ miny = min([v1y v2y v3y]);
+ maxy = max([v1y v2y v3y]);
+ % find smallest y-grid value > miny, and largest y-grid value < maxy
+ north = NaN; south = NaN;
+ j = 1; while (j <= ny && Y(j,1) < miny), j = j + 1; end; if (j <= ny), north = j; end
+ j = ny; while (j >= 1 && Y(j,1) > maxy), j = j - 1; end; if (j >= 0), south = j; end
+ % if, further, there are gridpoints strictly inside bounds (miny,maxy), continue
+ if ~isnan(north) && ~isnan(south) && south - north >= 0,
+ % we now know that there might be gridpoints bounded by (west,east) x (north,south)
+ % that lie inside the current triangle, so we'll test each of them
+ for j = west:east,
+ for k = north:south,
+ % calculate barycentric coordinates of gridpoint w.r.t. current (projected) triangle
+ A = [v1x v2x v3x; v1y v2y v3y; 1 1 1];
+ if rcond(A) > eps, w = A\[X(k,j); Y(k,j); 1]; else w = [1; 1; 1]/3; end
+ if min(w) > 0,
+ % use barycentric coordinates to calculate z-value
+ z = w(1)*v1z + w(2)*v2z + w(3)*v3z;
+ if isnan(Z(k,j)) || z > Z(k,j)
+ Z(k,j) = faceValue(i);
+% Z(k,j) = z;
+ end
+ end
+ end
+ end
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit a40925a

Please sign in to comment.