Raster to ASCII (Conversion)

Summary

Converts a raster dataset to an ASCII file representing raster data.

Usage

  • The input raster dataset can be any valid raster dataset.

  • The structure of the ASCII file consists of header information containing a set of keywords, followed by cell values in row-major order.

    The format of the file in general is:

        NCOLS xxx
        NROWS xxx
        XLLCORNER xxx
        YLLCORNER xxx
        CELLSIZE xxx
        NODATA_VALUE xxx
        row 1
        row 2
        .
        .
        row n

    The definitions of the keywords are as follows:

    • NCOLS and NROWS are the number of columns and rows in the raster defined by the ASCII file.

    • XLLCORNER and YLLCORNER are the coordinates of the lower left corner of the lower left cell.

    • CELLSIZE is the cell size of the raster.

    • NODATA_VALUE is the value that is to represent NoData cells.

    • In the data stream of cell values, row 1 of the data is the top of the raster, row 2 is just under row 1, and so on.

  • An example of an ASCII raster file is:

        NCOLS 480
        NROWS 450
        XLLCORNER 378922
        YLLCORNER 4072345
        CELLSIZE 30
        NODATA_VALUE -32768
        43 2 45 7 3 56 2 5 23 65 34 6 32 54 57 34
        35 45 65 34 2 6 78 4 2 6 89 3 2 7 45 23 5 ...

    NODATA_VALUE in the example above is the value in the ASCII file that will represent cells that are NoData in the input raster. This value is normally reserved for those cells whose true value is unknown.

  • The end of each row of data from the raster is terminated with a carriage return character in the file.

  • Both integer and floating-point rasters can be converted to an ASCII raster file.

  • This tool only writes the origin as the lower left corner of the lower left cell. The Copy Raster tool also supports the origin as the center of the lower left cell.

  • At ArcGIS Pro 3.2 and later, the default tool output is an ASCII file with an .asc extension, not a .txt extension. For existing ASCII files with a .txt extension, renaming the file using the .asc extension is recommended.

Parameters

LabelExplanationData Type
Input raster

The input raster dataset.

The raster can be integer or floating-point type.

Raster Layer
Output ASCII raster file

The output ASCII raster file.

File

arcpy.conversion.RasterToASCII(in_raster, out_ascii_file)
NameExplanationData Type
in_raster

The input raster dataset.

The raster can be integer or floating-point type.

Raster Layer
out_ascii_file

The output ASCII raster file.

File

Code sample

RasterToASCII example 1 (Python window)

Converts a raster dataset to an ASCII file representing raster data.

import arcpy
arcpy.env.workspace = "c:/data"
arcpy.conversion.RasterToASCII("elevation", "c:/output/sa500.asc")
RasterToASCII example 2 (stand-alone script)

Converts a raster dataset to an ASCII file representing raster data.

# Name: RasterToASCII_Ex_02.py
# Description: Converts a raster dataset to an ASCII file representing 
#    raster data. 
# Requirements: None

# Import system modules
import arcpy

# Set environment settings
arcpy.env.workspace = "C:/data"

# Set local variables
inRaster = "elevation"
outASCII = "c:/output/elevation.asc"

# Run RasterToASCII
arcpy.conversion.RasterToASCII(inRaster, outASCII)

Licensing information

  • Basic: Yes
  • Standard: Yes
  • Advanced: Yes

Related topics