Showing results for 
Search instead for 
Do you mean 

Split String at NewLine character (snippet)

by Technical Evangelist on ‎11-25-2019 09:44 AM - edited on ‎02-24-2020 04:55 AM by Community Manager (97 Views)

Download model and sample data


The Split String operator splits based on single characters. So splitting a text file which has CR/LF NewLine characters (\n) is not easy. This snippet shows how to achieve it.

Here's an example of an ascii text file (a .hdr header that came with a generic binary DEM) which contains Newline characters (which you can't generally see, but the text appears on new lines, so they must be present!):

NCOLS 3001
NROWS 3001
CELLSIZE 4.16666666666667E-5


If I want to parse this text to get at variables such as the lower-left X,Y coordinate values, I need to split the entire text string based on the Newline character. But a Newline is generally denoted as \n, which is two characters. The Spatial Modeler's Split String operator splits the input string based on the individual characters provided, so if I entered \n it would split on any occurrence of  \ and also on any occurrences of n, which is going to create a mess. So I need a way force a split on \n.



The Spatial Model snippet constructs a JSON file containing the \n character and uses the Data Input operator to read that JSON file and pass the character directly to the Split String operator. The Report operator is used to define the content of the JSON file, like this:

   "Type" : "IMAGINE.String",
   "Value" : "\n"

The Report writes out to a temporary file which is named by the Create Temp Filename operator. This must happen before the Data Input operator attempts to read that JSON filename and so Flow Control is used to make sure the Report executes prior to the Data Input (otherwise the JSON file may not be created before the Data Input attempts to read it). By using the Create Temp Filename operator the JSON file will be cleaned up when the Spatial Model is destroyed. 

In this manner the Split String operator correctly splits the input text string based on the \n Newline characters, as can be seen by the successful extraction of the 2nd and 3rd components of the split string representing the desired LLX and Y components. 

Note that the generally required Input1 port on the Report operator is depicted as "Not Used". This is because the desired Report text was defined within the report operator itself, not fed in via the Input1 port. To avoid confusion of an unconnected red (required) input port the text "Not Used" was added by entering "(String)Not Used" in the Properties panel. This is simply a "nicety" and is not required for the functioning of the Spatial Model



Example data:

n49e009h4dsm.hdr – An ascii text file header containing key name / value pairs. This type of header is commonly used in conjunction with Generic Binary files of raster pixel values to denote the number of rows and columns present, the number of bands, the bit depth, NoData values, etc. Many variants of this type of .hdr header can be directly read and used by ERDAS IMAGINE, but some variants cannot and these must be visually interpreted by the user.