Nested Loops

I'm having trouble figuring out how to nest loops properly. I've read they should be nested like this:
DIR_LOOP_START
   VAR_LOOP_START
          Do stuff
   VAR_LOOP_END
DIR_LOOP_END
But that results in the processing getting stuck inside the VAR_LOOP and not progressing the DIR_LOOP. Is there another way to do this so that both the DIR and the VAR loops progress together?

Best Answer

  • bmg_bobbmg_bob Global Mapper Programmer Posts: 1,688
    Answer ✓
    eddie416 said:
    I actually want to load 1 lidar file, then 1 corresponding shapefile then export 1 time. Then I want it to move onto the next lidar file and shapefile. I know there are a few ways I could do this, but I'm wondering if nested loops would work in this fashion?
    OK.  Nested loops won't work for this case.  You will need to update the ID value without using a VAR_LOOP.  It would look something like this: 
    DEFINE_VAR NAME="ID" VALUE="1"
    
    DIR_LOOP_START for LAZ files
    IMPORT the LAZ file
    DEFINE_VAR NAME="TILE" VALUE_TABLE="TABLE" VALUE_COLUMN="sheet" COMPARE_STR="id=%ID%" IMPORT the %TILE% shapefile EXPORT_ELEVATION UNLOAD_LAYER %TILE% UNLOAD_LAYER generated grid
    UNLOAD_LAYER the LAZ file DEFINE_VAR NAME="ID" FORUMULA="ID + 1"
    DIR_LOOP_END
    Now the ID value will be updated once for each LAZ file.

    Cheers,
    Bob

Answers

  • bmg_bobbmg_bob Global Mapper Programmer Posts: 1,688
    Hello,

    Using Global Mapper 18.2, I ran the test script below, and it worked exactly as expected.
    DIR_LOOP_START DIRECTORY="P:\CSV\" FILENAME_MASKS="*.CSV" RECURSE_DIR=NO
    VAR_LOOP_START VAL_START=1 VAL_STOP=3 VAL_STEP=1 VAR_NAME="%LOOP_VAL%"
    LOG_MESSAGE Dir Loop %FNAME% -- Var Loop %LOOP_VAL%
    VAR_LOOP_END
    DIR_LOOP_END
    What version of Global Mapper are you using?  Can you post more of your script so we can see if you are using a different option that is causing a problem.

    Cheers,
    Bob
  • eddie416eddie416 Posts: 39
    //Define table location
    DEFINE_VAR_TABLE NAME="TABLE" FILENAME="D:\2_Classification_In_Progress\Classified_Grd_final_check\test\TABLE.csv"
    END_VAR_TABLE 

    //LiDAR file folder define
    DEFINE_VAR NAME=LIDAR VALUE="D:\2_Classification_In_Progress\Classified_Grd_final_check\test"

    DIR_LOOP_START DIRECTORY="%LIDAR%" FILENAME_MASKS="*.laz" RECURSE_DIR=NO

          //LOAD SHAPEFILE FOR CLIP

         //Loop over table IDs
         VAR_LOOP_START VAL_START=1 VAL_STOP=4 VAL_STEP=1 VAR_NAME="ID"
         DEFINE_VAR NAME="TILE" VALUE_TABLE="TABLE" VALUE_COLUMN="sheet" COMPARE_STR="id=%ID%"
        //import tile layer
        IMPORT FILENAME="%TILE%" TYPE=SHAPEFILE

        IMPORT FILENAME="%FNAME_W_DIR%" TYPE="LIDAR_LAS" LIDAR_FILTER="NONE,2"          LIDAR_RETURN_FILTER="NONE,3"
       GENERATE_ELEV_GRID ELEV_UNITS=METERS SPATIAL_RES_METERS=5.0               LAYER_BOUNDS=%FNAME_W_DIR% GRID_FILL_TO_BOUNDS=YES 

        //EXPORT GRID
        EXPORT_ELEVATION     FILENAME="D:\2_Classification_In_Progress\Classified_Grd_final_check\test\CLIPPED\%FNAME_WO_EXT%.dem" EXPORT_LAYER="GENERATED ELEVATION GRID" \
    TYPE=USGS_DEM ELEV_UNITS=CENTIMETERS SPATIAL_RES_METERS=5.0 LAYER_BOUNDS=%TILE%

       UNLOAD_LAYER FILENAME=%FNAME_W_DIR%
       UNLOAD_LAYER FILENAME=%TILE%
       UNLOAD_LAYER FILENAME="GENERATED ELEVATION GRID"

       VAR_LOOP_END
    DIR_LOOP_END


    The result is that it loads the same .laz file 4 times while cycling through the 4 shapefiles, then it moves onto the next .laz file.



  • eddie416eddie416 Posts: 39
    Sorry, I am also using 18.2
  • bmg_bobbmg_bob Global Mapper Programmer Posts: 1,688
    Hello,

    You are importing your Lidar data during every iteration of the VAR_LOOP.  Is that what you intended?  

    I suspect that you want to load it once, then export 4 times (once using each shapefile).  If that is the case, you need to move the IMPORT command for the LIDAR_LAS file before the VAR_LOOP_START command, and move the corresponding UNLOAD_LAYER command after the VAR_LOOP_END command.  The general outline is as follows:
    DIR_LOOP_START for LAZ files
      IMPORT the LAZ file
    VAR_LOOP_START
    IMPORT the tiles shapefile EXPORT_ELEVATION UNLOAD_LAYER tiles UNLOAD_LAYER generated grid
    VAR_LOOP_END UNLOAD_LAYER the LAZ file
    DIR_LOOP_END
    Cheers,
    Bob
  • eddie416eddie416 Posts: 39
    I actually want to load 1 lidar file, then 1 corresponding shapefile then export 1 time. Then I want it to move onto the next lidar file and shapefile. I know there are a few ways I could do this, but I'm wondering if nested loops would work in this fashion?
  • eddie416eddie416 Posts: 39
    Good idea, thanks Bob
Sign In or Register to comment.