File Exchange

image thumbnail

arcgridwrite

version 1.1 (2.93 KB) by

Write gridded data set in Arc ASCII Grid Format

4.625
9 Ratings

25 Downloads

Updated

arcgridwrite(fileName,X,Y,Z)- converts data in a matlab
grid into a text file in Arc ASCII Grid Format.

INPUTS
fileName: output filename including extension
X: X coordinates (vector 1 x N)
Y: Y coordinates (vector m x 1)
Z: gridded data (m x n)

SYNTAX AND OPTIONS
arcgridwrite('D:\tools\bathyGrid.asc',X,Y,Z)

arcgridwrite(...,'precision',5) - changes default output
from 3 to 5 decimal places.

arcgridwrite(...,'nodata',-32768) - changes no data value
from -9999 (default) to -32768.

arcgridwrite(...,'grid_mapping','center') - changes the
grid spatial reference from 'corner' (default) to 'center'.
This is useful when combining output from the mapping
toolbox's PIXCENTERS function.

EXAMPLE 1 - create a raster grid of the peaks function
[X,Y,Z]=peaks(100);
arcgridwrite('peaksArc.asc',X(1,:),Y(:,1),Z,'precision',5)

NOTES
Because the Arc ASCII format has only one parameter for cell size,
both X and Y must have the same, non-varying grid spacing.

Comments and Ratings (18)

Very nice Andrew.
This enables me to use my gridded mat DEMs in your DemViewer tool, which is also great.

Jack

five stars once i included flipud and the modification recommended by Marc

Andrew,

Can you please update this code on file exchange to reflect the updates to this code which is available through OpenEarthTools.

Thankyou.

Yugarshi

in Marc's code:

replace:

for i=1:mz

with

for i=mz:-1:1

this fixes the flipud problem

Marc

Marc (view profile)

Thanks for this useful submission. However the performance of your code is bad (at least on my (standard) pc). Writing of a 3500 by 6000 matrix would take days on my desktop machine. I looked into your code and replaced your nested for loop (which is a real bottleneck!) containing the fprintf statement by a single one, taking advantage of the list behaviour of the format specifier the fprintf command can take.
specifically i replaced:

%%%START OF YOUR ORIGINAL CODE%%%
%write data
for i=1:mz
for j=1:nz

if j==nz
fprintf(fid,[dc,'\n'],Z(i,j));
else
fprintf(fid,[dc,'\t'],Z(i,j));
end
end
%update waitbar
waitbar(i/mz,h,['Writing file: ',[fname,ext],...
sprintf(' %d%% complete...',round(i/mz*100))])
end

%%%END OF YOUR ORIGINAL CODE%%%

with:

%%%START OF MY MODIFIED VERSION%%%%
test=repmat([dc,'\t'],1,nz);
test(end-1:end)='\n';
%write data
for i=1:mz
fprintf(fid,test,Z(i,:));
%update waitbar
waitbar(i/mz,h,['Writing file: ',[fname,ext],...
sprintf(' %d%% complete...',round(i/mz*100))])
end
%%%END OF MY MODIFIED VERSION%%%

My modified Version takes approx. 10 minutes to write my large array. Maybe you can integrate this in your submission... By the way in ESRI arcmap10 i still have to flipud the imported asc file to display correctly...
Best regards
Marc

Pasha

Pasha (view profile)

Like Edward, I need to flipud my Z, but otherwise works perfectly!

Dan

Dan (view profile)

I really like this file and it works great on my Matlab desktop, but I am now trying to run it over a UNIX platform, which is a -nodisplay platform. I get an error saying that this functionality is no longer supported under -nodisplay.

Do you know if there is a solution where I could use arcgridwrite with -nodisplay?

Thanks,
Dan

A Westergaard

The above mentioned matrix has 350 rows and 300 columns. Values are double.

A Westergaard

Andrew,
Thank you for sharing your work. As I am new to MatLab, I hope you or others have time for some guidance: In Matlab I have a matrix with values [-1:1]. I want to export this matrix to ArcMap. What would the syntax be for this? Thank you in advance.

Andrew Stevens

Edgar,

The default precision for the function is %0.3f, so you should get 0.075 as the cell spacing. I tried the following code to verify:

%generate synthetic data with desired %cell spacing
xi=0:0.075:1;
yi=0:0.075:1;
[xm,ym]=meshgrid(xi,yi);
zm=peaks(numel(xi));

%call arcgridwrite
arcgridwrite('test.asc',xm,ym,zm);

%look at output
type('test.asc')

The results look correct to me. Let me know (by email) if you have further questions.

Edgar

Edgar (view profile)

Andrew,

Thanks for the script. However, I found the script is not able to write high resolution cellsizes. My grid has a resolution of 0.075. However, the scripts writes: "0.000". How can I solve this?? Thanks.

EM

Edward Bair

Thanks for the util. It may be the way I'm building the grid, but ARCGRIDWRITE creates rasters that are flipped vertically for me. If Z is my raster, I need to do the following to have it written correctly:

[iv,jv]=ndgrid(1:size(Z,1),1:size(Z,2));
arcgridwrite('VegGrid_small.asc',jv,iv,flipud(Z));

Daniya

Daniya (view profile)

Andrew Stevens

Colin,

Thanks for your comments. I found after playing with a couple examples, I determined that if I simply change the output filename from .txt to .asc that the file can be read directly into arc without doing the raster conversion.

I would also be careful with your modification of the way the header is written in the output file. Your modification forces the cell-size to be an integer which would produce a large error in many of my datasets.

Andrew

This is a nice file and works well. Modification of the header section write format will allow the file to be read directly by ESRI Arc-GIS. My modification is:

%write header
fprintf(fid,'%s','ncols ');
fprintf(fid,' %d\n',nz);
fprintf(fid,'%s','nrows ');
fprintf(fid,' %d\n',mz);
fprintf(fid,'%s','xllcorner ');
fprintf(fid,[dc,'\n'],minX);
fprintf(fid,'%s','yllcorner ');
fprintf(fid,[dc,'\n'],minY);
fprintf(fid,'%s','cellsize ');
fprintf(fid,'%-6.0d\n',dx);
fprintf(fid,'%s','NODATA_value ');
fprintf(fid,'%-5.0d\n',-9999);

This place the numeric values starting in column 15 and removes the trailing zeros from the last two values.

Updates

1.1

Finally incorporated lots of comments from everyone. Thanks!
Linked to github repository

1.1

changed description, help and example

bug fixed in conversion to raster portion of program

MATLAB Release
MATLAB 8.5 (R2015a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video