Global Mapper Pro

run GM script and pass variable from cmd.exe

RobertR
RobertR GlobalMapper Fan!Trusted User
edited March 2014 in GM Script Language
Hello,
I am using GM v15.1;

Basically, what I want is:
C:\>global_mapper.exe my_script.gms --some_name

"some_name" is a variable needed in the gm script in order for it to function correctly. I would like to pass it from the command line. In this way, I could automate a process even more as I could call cmd.exe with global_mapper.exe, my_script.gms and the --some_name as parameters from a Python script. "some_name" is a variable which will derive from the python script so this process will go even faster.

Comments

  • Jasmine_BMG
    Jasmine_BMG BMG Staff
    edited February 2014
    Hello RobertR,

    Thank you for your suggestion, I have submitted your request to add the variable 'some_name' to Global Mapper scripting (workorder # 13204). A member of our support or development team will be in touch with any developments.

    Regards,

    Jasmine
  • global_mapper
    global_mapper Administrator
    edited February 2014
    Robert,

    You can pass variable values on the command line to scripts. See Global Mapper Scripting Reference for details on how to define a named variable in the command line parameters.

    Thanks,

    Mike
    Global Mapper Guru
    geohelp@bluemarblegeo.com
    Global Mapper
  • RobertR
    RobertR GlobalMapper Fan! Trusted User
    edited February 2014
    Thank you Sir!
  • RobertR
    RobertR GlobalMapper Fan! Trusted User
    edited February 2014
    If I declare:
    [COLOR=#000000]C:\>global_mapper.exe my_script.gms -some_name john[/COLOR]
    

    How should the DEFINE_VAR look? I tried several options but it did not get the value from the command line:
    DEFINE_VAR NAME="some_name"
    and
    DEFINE_VAR NAME="some_name" VALUE=""
    
  • RobertR
    RobertR GlobalMapper Fan! Trusted User
    edited February 2014
    I figured out that I do not need to specify it as a DEFINE_VAR in the script.
    It was failing because:
    IMPORT_DIR_TREE DIRECTORY="%PATH%" FILENAME_MASKS="*.shp" RECURSE_DIR=YES
    

    It seems like RECURSE_DIR is working only in DIR_LOOP?
  • RobertR
    RobertR GlobalMapper Fan! Trusted User
    edited February 2014
    I forgot to mention that what I want is to import all the .shp files from %PATH% and every folder located in %PATH%
  • RobertR
    RobertR GlobalMapper Fan! Trusted User
    edited February 2014
    I managed to make it work. It would be nice to have a delete button in order to remove those extra posts.

    I see in the log file that my command:
    RUN_COMMAND COMMAND_LINE="cmd RMDIR /S /Q '%PATH%'"
    
    is returned as:
    Running command cmd RMDIR /S /Q "Z:\testing\all_people\names\extra"
    
    but it does not delete the folder.

    If I run the exact same command
    RMDIR /S /Q "Z:\testing\all_people\names\extra"
    
    from the cmd.exe window, it does the job. Can you think of why this would not work from the .gms file?
  • RobertR
    RobertR GlobalMapper Fan! Trusted User
    edited February 2014
    Hello again,

    I still could not fix the issue stated above.
    RobertR wrote: »
    I managed to make it work. It would be nice to have a delete button in order to remove those extra posts.

    I see in the log file that my command:

    RUN_COMMAND COMMAND_LINE="cmd RMDIR /S /Q '%PATH%'"
    
    is returned as:
    Running command cmd RMDIR /S /Q "Z:\testing\all_people\names\extra"
    

    but it does not delete the folder.

    If I run the exact same command
    RMDIR /S /Q "Z:\testing\all_people\names\extra"
    
    from the cmd.exe window, it does the job. Can you think of why this would not work from the .gms file?

    Beside this, another thing that I did not figure out yet, is how to use the imported file name as being the name used in EXPORT_VECTOR FILENAME="???"
    DEFINE_VAR NAME="FNAME" PROMPT="FILE" PROMPT_TEXT="Select the csv file for processing" ABORT_ON_CANCEL=YES
    

    I would like to use the name of the file as the name of the exported vector file.
  • global_mapper
    global_mapper Administrator
    edited February 2014
    Robert,

    I missed the question in the earlier message with the 'rmdir' not working. If you add /C after the 'cmd', like the following, does that work?

    RUN_COMMAND COMMAND_LINE="cmd /C RMDIR /S /Q '%PATH%'"

    If not, does the rmdir work on a local directory vs. a network one? It's hard to tell what the error is since you can't easily get that back, maybe you can add ' > c:\log.txt' to your command line to log the results?

    I will add something to DEFINE_VAR to extract pieces of a filename from a value so you can get just the path, filename, or extension from a variable. I'll let you know when that's available.

    Thanks,

    Mike
    Global Mapper Guru
    geohelp@bluemarblegeo.com
    Blue Marble Geographics for Coordinate Conversion, Image Reprojection and Vector Translation
  • global_mapper
    global_mapper Administrator
    edited February 2014
    I've added support for adding FILENAME_PIECE to the DEFINE_VAR script command to specify a piece of the filename value to extract into the new variable. I have placed a new build at http://www.bluemarblegeo.com/downloads/global-mapper/global_mapper15.zip with the latest changes for you to try. Simply download that file and extract the contents into your existing v15.xx installation folder to give it a try. If you are using the 64-bit v15 version there is a new build at http://www.bluemarblegeo.com/downloads/global-mapper/global_mapper15_64bit.zip .

    Here is the documentation of the new parameter and an example:

    FILENAME_PIECE - specifies that only a piece of the specified value should be used. Used if the value specifies a full path and filename and you want to define the variable to just a piece of that. So you might use one DEFINE_VAR with a PROMPT=FILE to select a filename, then another DEFINE_VAR with that previous variable value as the value and FILENAME_PIECE added requesting one of the pieces listed below (samples based on 'C:\path\to\my\data\my_file.dem'):

    DIR - full path to current file (value is 'C:\path\to\my\data\')
    FNAME - filename of current file (value is 'my_file.dem')
    FNAME_WO_EXT - filename of current file without extension (value is 'my_file')
    PARENT_DIR - name of parent directory of file (value is 'data')
    PARENT_DIRN - name of some level of parent directory, where 'N' is level. For example, PARENT_DIR1 value is 'my' and %PARENT_DIR2% is 'to'.

    Example to prompt the user for a filename, then define new variables that hold pieces of the selected filename.

    // Prompt use for file. Script aborts on cancel
    DEFINE_VAR NAME="FULL_FNAME" PROMPT=FILE ABORT_ON_CANCEL=YES

    // Define a variable with just the filename, another with parent folder
    DEFINE_VAR NAME="FNAME_ONLY" VALUE="%FULL_FNAME%" FILENAME_PIECE="FNAME_WO_EXT"
    DEFINE_VAR NAME="FNAME_PARENT_DIR" VALUE="%FULL_FNAME%" FILENAME_PIECE="PARENT_DIR"
    DEFINE_VAR NAME="FNAME_PARENT_DIR1" VALUE="%FULL_FNAME%" FILENAME_PIECE="PARENT_DIR1"

    Thanks,

    Mike
    Global Mapper Guru
    geohelp@bluemarblegeo.com
    Blue Marble Geographics for Coordinate Conversion, Image Reprojection and Vector Translation
  • RobertR
    RobertR GlobalMapper Fan! Trusted User
    edited March 2014
    Hello,
    Somehow on the same idea, I have a question:

    As I said, all my workflow starts in Python and at the end of this script, the .gsm file is called with a parameter passed through command line. Until now, all worked very well.

    What I would like to do, is to be able and decide the "LOAD_PROJECTION PROJ=" value based on the parameter passed through the cmd line. This is needed because I need local projections which are different from county to county.

    I would need something like:
    if x in [county1, county2 ] to use one "PROJ=****" else use "PROJ=####".

    I my case we are talking about only 2 projections, and I was wondering if I can do this in .gms.

    Later edit: I just realized that I can pass two values from Python instead of one :) But maybe this can be done in the .gms file as well
  • global_mapper
    global_mapper Administrator
    edited March 2014
    Robert,

    I think you can use the new DEFINE_VAR_TABLE command to define the table of county codes/names and the projection code to use for each. See the thread at http://www.globalmapperforum.com/forums/gm-script-language/10370-determine-variables-parameters-based-excel-csv-dbf-file.html for a description of that command.

    Thanks,

    Mike
    Global Mapper Guru
    geohelp@bluemarblegeo.com
    Blue Marble Geographics for Coordinate Conversion, Image Reprojection and Vector Translation
Sign In or Register to comment.