RasterPrintXYZ5.sml

  Download

More scripts: Raster

Syntax Highlighing:

comments, key words, predefined symbols, class members & methods, functions & classes
            
 # SML created 24 June 2004
 # Cindy Robbins
 # exports a raster to an X, Y, Z text file, formatted so each line of the
 # text file corresponds to one cell in the raster
 clear();
 raster R;
 class Georef geoR;
 class POINT2D coords;
 class FILE outFile;
 GetInputRaster(R);
 geoR = GetLastUsedGeorefObject(R);
 #outFile = GetOutputTextFile("c:/rasterToXYZ.txt", "Select text file for output", "txt");
if (fexists("c:/rasterToXYZ.txt"))
	DeleteFile("c:/rasterToXYZ.txt");
 outFile = fopen("c:/rasterToXYZ.txt", "a");
 # loop through each cell in the raster,
 # getting the cell value and writing to text file.
 numeric row, col;
 for row = 1 to R.$Info.NumLins {
     for col = 1 to R.$Info.NumCols  {
#   get the cell value
             numeric cellVal = R[row, col];
#		if (cellVal == null) cellVal = 0;    # uncomment if you want to set null value to 0.
          	coords = ObjectToMap(R,  col - .5, row - .5, geoR);
             fprintf(outFile, "%f%s%f%s%f%s", coords.x, " ", coords.y , " ", cellVal, " ");   
				 fprintf(outFile, "\n");
    }   #close 'for col...' 
}  # close 'for row...'
 
 CloseRaster(R);
print("done");
 ##########################################################################