Global Mapper v25.0

Batch Combine/compare terrain layers

Hello,

I know the combine_terrain function is available, but I'm wondering how I could subtract many files in one script. For example I have many files (x1,x2,x3,...) that I want to subtract from a group of other files (y1,y2,y3,....). How would I do this with the combine_terrain command? From what I'm seeing I need to provide the exact filename with this command. Can I set up variables to go through all the files? Thank you

Answers

  • bmg_bob
    bmg_bob Global Mapper Programmer
    Answer ✓
    Hello,

    You can use variables to go through the files.

    Here are a couple of ideas.  

    Idea 1)

    Depending on how your files are set up in folders, you might be able to use the DIR_LOOP_START and DIR_LOOP_END commands to iterate over the files, especially if the file names are similar (for example, one file is \folder1\grid.tif and the other is \folder2\grid_1.tif.  

    Idea 2)

    If your file names or file structure is not that easy to work with, you could create a CSV file that looks something like this:
    id,file1,file2<br>1,\folder1\grid1.tif,\folder2\other_grid1.tif<br>2,\folder2\grid2.tif,\folder2\other_grid2.tif<br>...
    You would then use the DEFINE_VAR_TABLE command to import the CSV file.  Then use the VAR_LOOP_START and VAR_LOOP_END commands to iterate over the id values, and use DEFINE_VAR to get the file names out of the table based on the id value.

    Cheers,
    Bob
  • Thanks Bob, I'm going to have to go with Idea 2 here. This may seem like a silly question, but how do I iterate over the id values using VAR_LOOP? I'm struggling with the reference guide a bit
  • bmg_bob
    bmg_bob Global Mapper Programmer
    Answer ✓
    eddie416 said:
    Thanks Bob, I'm going to have to go with Idea 2 here. This may seem like a silly question, but how do I iterate over the id values using VAR_LOOP? I'm struggling with the reference guide a bit
    Here is a sample script that implements the loop:

    <div>GLOBAL_MAPPER_SCRIPT VERSION=1.00</div><div><br></div><div><br>// Set up table of file names.</div><br><div>DEFINE_VAR_TABLE NAME="file_names"</div><br><div>id,file1,file2</div><br><div>1,\folder1\grid1.tif,\folder1\other_grid1.tif</div><br><div>2,\folder2\grid2.tif,\folder2\other_grid2.tif</div><br><div>3,\folder3\grid3.tif,\folder3\other_grid3.tif</div><br><div>END_VAR_TABLE</div><div><br></div><div>// Loop over IDs 1-3</div><br><div>VAR_LOOP_START VAL_START=1 VAL_STOP=3 VAL_STEP=1 VAR_NAME="ID"</div><br><div>&nbsp; &nbsp; DEFINE_VAR NAME="FILE1" VALUE_TABLE="file_names" VALUE_COLUMN="file1" COMPARE_STR="id=%ID%"</div><br><div>&nbsp; &nbsp; DEFINE_VAR NAME="FILE2" VALUE_TABLE="file_names" VALUE_COLUMN="file2" COMPARE_STR="id=%ID%"</div><div><br></div><div>&nbsp; &nbsp; // Do your work using %FILE1% and %FILE2% as your file names.</div><div><br></div><div>VAR_LOOP_END<br></div>

    The VAR_LOOP_START/VAR_LOOP_END command pair will repeat the enclosed set of commands once for each numeric value from VAL_START to VAL_STOP.  The value for each iteration will be placed in the variable called %ID%.  The DEFINE_VAR commands get the file names from the table that area associated with %ID% and assign them to variables called %FILE1% and %FILE2%.

    I hope this helps.

    Cheers,
    Bob
  • Thank you Bob that works wonderfully!
  • Hi,
    Having the same problem as described in this thread. I am trying to use an assigned layer description upon import for defining Layer 1 and 2 for the calculation. I also tried defining the path and filename, however this did not work. Example: \\cafs02\Geoscience\GIS\COL\culture\LiDAR\Putumayo\Data\Area1\Priority_area\DEM\%FNAME_W_DIR%

    Any help would be greatly appreciated @bmg_bob

    Script: 
    //Batch script to calculate CHM using combine terrain calculator. Desired workflow is to load two terrain tiles, perform subtraction, and export new grid
    GLOBAL_MAPPER_SCRIPT VERSION=1.00
    UNLOAD_ALL
    // IMPORT DSM
    DIR_LOOP_START DIRECTORY="\\cafs02\Geoscience\GIS\COL\culture\LiDAR\Putumayo\Data\Area1\Priority_area\DSM" RECURSE_DIR="NO" FILENAME_MASKS="*.ASC"
    IMPORT_ASCII FILENAME="%FNAME_W_DIR%" LAYER_DESC="DSM"
    // IMPORT DEM
    DIR_LOOP_START DIRECTORY="\\cafs02\Geoscience\GIS\COL\culture\LiDAR\Putumayo\Data\Area1\Priority_area\DEM" RECURSE_DIR="NO" FILENAME_MASKS="*.ASC"
    IMPORT_ASCII FILENAME="%FNAME_W_DIR%" LAYER_DESC="DEM"
    // CALCULATE CHM
    COMBINE_TERRAIN COMBINE_OP="SUBTRACT_UNSIGNED" ELEV_UNITS="METERS" LAYER1_FILENAME="LAYER_DESC=DSM" LAYER2_FILENAME="LAYER_DESC=DEM" SPATIAL_RES_METERS="1.0,1.0" LAYER_DESC=%FNAME_WO_EXT%_CHM_1m
    //EXPORT CHM
    EXPORT_ELEVATION FILENAME="\\cafs02\Geoscience\GIS\COL\culture\LiDAR\Putumayo\Data\Area1\AREA 1_LIDAR PRODUCTS_KYLE\CHM_1m_TILES\%FNAME_WO_EXT%_CHM_1m.ASC" GEN_PRJ_FILE="YES" TYPE="SURFERGRID" SPATIAL_RES_METERS="1.0,1.0"
    // UNLOAD DATA
    UNLOAD_ALL
    // END LOOP
    DIR_LOOP_END

    Script Results:
    Removed all loaded overlays.
    Starting loop over files in directory <\\cafs02\Geoscience\GIS\COL\culture\LiDAR\Putumayo\Data\Area1\Priority_area\DSM\> that match '*.ASC'...
    Importing ASCII file \\cafs02\Geoscience\GIS\COL\culture\LiDAR\Putumayo\Data\Area1\Priority_area\DSM\BN084_DSM.asc...
    Starting loop over files in directory <\\cafs02\Geoscience\GIS\COL\culture\LiDAR\Putumayo\Data\Area1\Priority_area\DEM\> that match '*.ASC'...
    Importing ASCII file \\cafs02\Geoscience\GIS\COL\culture\LiDAR\Putumayo\Data\Area1\Priority_area\DEM\BN084_DEM.asc...
    Combining Terrain Layers to new layer: BN084_DEM...
    ERROR: No layers matching <LAYER_DESC=DSM> for COMBINE_TERRAIN
    Be sure to provide the full path and filename or the description for the loaded file to use, or * for all files
    ERROR: No elevation layers with the specified filename <LAYER_DESC=DSM> were found.
    Exporting elevation data to file <\\cafs02\Geoscience\GIS\COL\culture\LiDAR\Putumayo\Data\Area1\AREA 1_LIDAR PRODUCTS_KYLE\CHM_1m_TILES\BN084_DEM.asc>...
    ERROR: Elevation data must be loaded for EXPORT_ELEVATION command.
    Removed all loaded overlays.

  • bmg_bob
    bmg_bob Global Mapper Programmer
    Hello,

    The IMPORT_ASCII command needs to include the parameter TYPE=ELEVATION. This means that all lines in the file with 3 coordinate values (x,y, and elevation) will be used to create an elevation grid. There is a good chance that Global Mapper is loading your data as point features, which does not seem to be what you want. The best way to figure out what to put in an IMPORT statement is to load one of your files into Global Mapper, then save a workspace. A workspace file is a script, so you can open the workspace file in a text editor and copy the IMPORT command from the workspace to your script (you will likely need to tweak the file name to match your DIR_LOOP_START).

    On the COMBINE_TERRAIN command, the file name parameters should be LAYER1_FILENAME="DSM" and LAYER2_FILENAME="DEM".

    The script contains nested DIR_LOOP_START commands. This is OK, but it means that, in your case, for each file you read from the DSM directory, all of the files in the DEM directory will be combined with it. Is that what you want?

    Also, if you have two DIR_LOOP_START commands, you also need two DIR_LOOP_ENDs.

    Cheers,
    Bob


  • Hi @bmg_bob
    Thank you for the feedback. 

    The script contains nested DIR_LOOP_START commands. This is OK, but it means that, in your case, for each file you read from the DSM directory, all of the files in the DEM directory will be combined with it. Is that what you want?

    No this is not what I was trying to achieve, thank you for catching that bust. I am trying to iterate over files located in two separate folders: 

    folder 1- file 1 and folder 2- file 1......folder 1- file 2 and folder 2- file 2.......folder 1- file 3 and folder2- file 3.....etc.

    What do you suggest to modify the DIR_LOOP_START commands to achieve this? Thank you again for the support, it is greatly appreciated. 

    Kyle
  • bmg_bob
    bmg_bob Global Mapper Programmer
    Hello Kyle,

    Do the files that you want to combine from the two folders have the same name? If so, you can use a single DIR_LOOP_START and import the two files. The first file is from the folder you used in DIR_LOOP_START, and uses %FILENAME_W_DIR% and the second uses the DEM folder name with the %FILENAME_WO_DIR% appended to it.

    Cheers,
    Bob
  • Hi Bob,
    The files have a similar name, but not the exact name. Example: CC031_DSM and CC031_DEM

    Tried your suggestions with not luck:

    DIR_LOOP_START DIRECTORY="\\cafs02\Geoscience\GIS\COL\culture\LiDAR\Putumayo\Data\Area1\Priority_area" RECURSE_DIR="YES" FILENAME_MASKS="*.ASC"
    IMPORT_ASCII FILENAME="\\cafs02\Geoscience\GIS\COL\culture\LiDAR\Putumayo\Data\Area1\Priority_area\DSM\%FILENAME_W_DIR%" TYPE="ELEVATION" LAYER_DESC="DSM"
    IMPORT_ASCII FILENAME="\\cafs02\Geoscience\GIS\COL\culture\LiDAR\Putumayo\Data\Area1\Priority_area\DEM\%FILENAME_WO_DIR%" TYPE="ELEVATION" LAYER_DESC="DEM"

    Should a single DIR_LOOP_START work this this close of filenames? Thanks for the help. 

    Kyle