Global Mapper Pro

Use only a part of a PROMPT="DIR" variable

RobertR
RobertR GlobalMapper Fan!Trusted User
edited October 2013 in GM Script Language
Hello,

What I would find very useful (and maybe it's already there) is to be able to use only a part of a PROMPT="DIR" path. To somehow parse it.

Let's say I have data regarding city_area and city_population (the folders with a purple color are always present, these are the ones which stay in-place);

C:/workspace/city1/areas/
C:/workspace/city2/areas/
C:/workspace/city3/areas/
C:/workspace/other/county/city1/population/
C:/workspace/other/county/city2/population/
C:/workspace/other/county/city3/population/

I am using NAME="AREAS" PROMPT="DIR" to determine the full path to the areas folder (it is very useful as maybe one has the data on the D:/ drive or any other partition, and the containing folder may be other than "workspace"); this is why I am not using a simple PROMPT=YES to get the city? name;

Once I know the path ("C:/workspace/city1/areas/") I would find it very useful to be able to work on that specific variable within the script in order to deduce the population folder (without prompting the user for another path/value);

In this case, I would need to extract from the %AREAS% variable the "C:/workspace/" and "city1" parts to work on the population task.

Currently I would need 3 prompts to make this happen:
a prompt to find out the root drive towards the city shapefile (as it can vary from user to user)
a prompt=yes for the the city name (to be able to use this variable in the following stages - export part)
a prompt=dir for the population path.

Do I have a solution to use only one prompt in this case?

Comments

  • global_mapper
    global_mapper Administrator
    edited June 2013
    Robert,

    I think I have a solution that will work now. Prompt for the directory 'C:/workspace/city1'. Then to extract out the 'city1' part use a DIR_LOOP_START...DIR_LOOP_END to define a variable from the parent folder value of 'city1'. As follows:

    DEFINE_VAR VAR_NAME="city_folder" PROMPT_DIR=YES
    DIR_LOOP_START DIRECTORY="%city_folder%" FILENAME_MASKS=".."
    // We should only get here once with the .. mask, define a variable
    DEFINE_VAR VAR_NAME="city_name" VAR_VALUE="%PARENT_DIR%"
    DIR_LOOP_END

    At this point the %city_name% should contain 'city1' in your example. To build the path to the population use:

    "%city_folder%/../other/county/%city_name%/population/"

    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 June 2013
    Oh, ook.,,

    So Mike, can the '../' command be used to browse through the folders? I did not know this.

    Basically, I can do %variable%../../folder/ to go back 2 folders and access the files located there?
  • global_mapper
    global_mapper Administrator
    edited June 2013
    Robert,

    Yes, Windows allows you to back-up through a path with ..\, so you can use however many of those to back up the folder tree.

    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 June 2013
    Just a quick follow up.

    I thought of a way to be able and use any part of the initial path but it does not work, although I do not think it is very hard to fix:
    DEFINE_VAR NAME="COUNTY_DIR" PROMPT="DIR" PROMPT_TEXT="Insert path to county directory" 
    		
    DEFINE_VAR NAME="STATE_FULLDIR" VALUE="%COUNTY_DIR%..\"
    

    output.log:
    Defined variable %COUNTY_DIR% to value C:\ca\butte\
    Defined variable %STATE_FULLDIR% to value Z:\ca\butte\..\
    

    My goal was to obtain STATE_FULLDIR="C:\ca\"

    If the "..\" would work with the VALUE param, one could virtually extract any part of the initial path for later use. Furthermore, using a DIR_LOOP as you mentioned, you can extract a value like 'ca'.

    You would just prompt the user for the longest path in the dir tree and then extract whatever you need from the branch (assuming you know the structure of the tree); My eg is with a really short path but imagine how useful would be with a long one.

    Any thoughts?
  • global_mapper
    global_mapper Administrator
    edited June 2013
    Robert,

    Do you need the path just to specify a filename or do you need to include it as text somewhere? The smarts for working with ..\ come from Windows, so you can define the path to a file with ..\ in it to move around the folder structure, so it would work for an IMPORT or EXPORT_* command. If you need it as text somewhere though then Global Mapper would have to add some special support.

    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 June 2013
    Hello Mike,

    Again, I am going to use fictional Tiger data as an example because it is so common and it has a standard which is kept year by year plus the way of representing the state (we have 'California' for some data, 'ca' for other and '06' for several more);

    Let's say I need to extract some data from one or more shapefiles and bring a few attributes in another.

    task 1 C:testing\data\tiger\data\allcounties\ca\butte\td_06_001_landuse.shp
    task 2 C:testing\data\tiger\population\06\population.shp
    task 3 C:testind\data\tiger\cities\CALIFORNIA.shp


    Instead of having several prompts, I would need only one (the first one);

    If I would have the option of using the example from the previous post (define_var..value = %***%..\..\)
    1. I could extract for example the path "
    C:\testing\data\tiger\data\allcounties\ca\"
    2. Apply a dir loop - %parent_dir% and extract the value "ca".
    3.1 Then, I can do a define table (inline) in which I state that... 'ca' corresponds to 06 (here we solved task 2 as I can use it to import the population.shp)
    3.2 ...'ca' corresponds to 'CALIFORNIA' (solved task 3)
    4. Furthermore using the same var"define..value = %***%..\..\..\..\" I could extract "C:\testing\data\tiger" and place it in a nice variable so my script would look clean.

    To sum up, from the first prompt, I already know that I have tiger data and it is for California, I would not need any other info from the user. To bring in state data for that specific county/city, I need the state abbreviation (from the path) in a text format so I can include it in comparisons, inline tables and so on.

    Would you find this useful? Maybe it's just me :)
  • global_mapper
    global_mapper Administrator
    edited June 2013
    Robert,

    Rather than get tricky, I just added support for %PARENT_DIR% to have a number at the end saying how far up to go. So %PARENT_DIR0% is the same as now, %PARENT_DIR1% is the grand-parent, etc. Here is a sample script that works:

    DEFINE_VAR NAME="city_folder" PROMPT=DIR
    DIR_LOOP_START DIRECTORY="%city_folder%" FILENAME_MASKS=".."
    // We should only get here once with the .. mask, define a variable
    DEFINE_VAR NAME="parent" VALUE="%PARENT_DIR%"
    DEFINE_VAR NAME="grandparent" VALUE="%PARENT_DIR1%"
    DEFINE_VAR NAME="super_grandparent" VALUE="%PARENT_DIR2%"
    DIR_LOOP_END

    I have placed a new build at http://www.bluemarblegeo.com/downloads/global-mapper/global_mapper14.zip with the latest changes for you to try. Simply download that file and extract the contents into your existing v14.xx installation folder to give it a try. If you are using the 64-bit v14 version there is a new build at http://www.bluemarblegeo.com/downloads/global-mapper/global_mapper14_64bit.zip .

    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 June 2013
    Mike,

    True that. It does what I needs, I'm glad you got my idea.

    I hope many will find this useful. I will add a 'user script' sample so others can see its usability.

    Cheers,
  • RobertR
    RobertR GlobalMapper Fan! Trusted User
    edited October 2013
    Hello,

    I tried to run a .gms file from command prompt but it just flashes the command prompt quickly 2 times and nothing happens.

    I would expect it to pop-up the "prompt_dir" box.

    I ran "C:\Program Files\GlobalMapper15_64bit>global_mapper.exe "C:\my_script.gms""

    It should execute the following:
    DEFINE_VAR NAME="COUNTY_DIR" PROMPT="DIR" PROMPT_TEXT="Insert path to directory" ABORT_ON_CANCEL=YES
    

    I wonder why is this happening?
  • global_mapper
    global_mapper Administrator
    edited October 2013
    Robert,

    What does the whole script look like? If you run it via File->Run Script does it work? You might also add a SET_LOG_FILE command at the top so you get a log of what happens so you can see any errors that might provide clues.

    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 October 2013
    Hello,
    Yes, it works via File->Run Script

    The log file says:
    Global Mapper v15.0 (b101713) [64-bit]
    ERROR: No VALUE provided for DEFINE_VAR command. Please use as follows:
    

    The line where the error occurs is the one mentioned above:
    DEFINE_VAR NAME="COUNTY_DIR" PROMPT="DIR" PROMPT_TEXT="Insert path to directory" ABORT_ON_CANCEL=YES
    

    So I would expect a prompt box where I can specify the path.
  • global_mapper
    global_mapper Administrator
    edited October 2013
    Robert,

    I took a look and discovered that prompts were intentionally turned off when running in batch mode! I think this was needed just for the original prompt type, so I re-enabled the prompts if no default value is provided or the prompt is for a file or directory. Now your command line runs fine. 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 .

    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 October 2013
    Great! Thanks Mike!
Sign In or Register to comment.