Global Mapper v25.0

IF/ELSE_IF/ELSE/END_IF help

MorganRS
MorganRS Global Mapper UserTrusted User
I'm trying to have a script check for an attribute value and write a log message depending on if the value is correct. The script should be checking to make sure a field is populated, and if it is sending out one message. If it's not, sending out a different one. Here's how I've been trying to get it to work correctly:


EDIT_MAP_CATALOG FILENAME="G:\Users\Morgan\DATA_STATE\%state%\%state%_Admin_Bounds.GMC" CREATE_IF_EMPTY=YES ADD_FILE="G:\Users\Morgan\DATA_STATE\%state%\Administrative_Boundaries\2013_complete\*.SHP" ZOOM_DISPLAY="PERCENT,0.75,0"
IF COMPARE_STR="MP_TYPE=0x*"
LOG_MESSAGE **********************************************MP_TYPE FIELD POPULATED**********************************************
LOG_MESSAGE ------------------------%state% Admin Bounds Catalog Created----------------------------------
LOG_MESSAGE ------------------------Import took %TIME_SINCE_LAST_LOG%
ELSE COMPARE_STR="MP_TYPE!=0x*"
LOG_MESSAGE /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\MP_TYPE FIELD is EMPTY/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
END_IF



I'm not sure what I'm doing wrong here. When it checks the MP_TYPE field, the * should count as a wildcard. I tried this script using two datasets, one where I removed the MP_TYPE field, and the other with the field being correct. But I'm getting the /\/\/\MP_TYPE FIELD IS EMPTY/\/\/\/\/\ with both datasets.

Thanks


Tagged:

Answers

  • bmg_bob
    bmg_bob Global Mapper Programmer
    The IF command only works with script variables, not attribute names -- there is no way to determine which feature to get the attribute value from.  You might be able to use DEFINE_VAR with the VALUE_ATTR parameter to get a value that you can test with IF.   That would look something like this:

    DEFINE VAR NAME="MP_TYPE" VALUE_ATTR="MP_TYPE" FILENAME="*"
    IF COMPARE_STR="%MP_TYPE% != 0x*"
    ...
    END_IF


  • MorganRS
    MorganRS Global Mapper User Trusted User
    Well I tried it this way, but I'm not getting the results I was hoping for.
    GLOBAL_MAPPER_SCRIPT VERSION=1.00
    DEFINE_VAR NAME="state" VALUE="MT"
    SET_LOG_FILE FILENAME="G:\Users\Morgan\DATA_STATE\%state%\0_CatalogBuild.log"
    LOG_MESSAGE <%SCRIPT_FILENAME%> Started at %DATE% %TIME%

    LOG_MESSAGE ----------------------- Preparing to build %state% Admin Bounds Catalog ---------------------------------
    IMPORT_DIR_TREE DIRECTORY="G:\Users\Morgan\DATA_STATE\%state%\Administrative_Boundaries\2013_complete" FILENAME_MASKS="*.SHP" RECURSE_DIR=NO
    DEFINE_VAR NAME="mp_type" VALUE_ATTR="MP_TYPE" FILENAME="*"

    IF COMPARE_STR="%mp_type% != 0x*"
    LOG_MESSAGE ************************************************************************************************MP_TYPE FIELD EMPTY************************************************************************************************
    ELSE
    EDIT_MAP_CATALOG FILENAME="G:\Users\Morgan\DATA_STATE\%state%\%state%_Admin_Bounds.GMC" CREATE_IF_EMPTY=YES ADD_FILE="G:\Users\Morgan\DATA_STATE\%state%\Administrative_Boundaries\2013_complete\*.SHP" ZOOM_DISPLAY="PERCENT,0.75,0"
    LOG_MESSAGE ************************************************************ MP_TYPE FIELD POPULATED ************************************************************
    LOG_MESSAGE ------------------------%state% Admin Bounds Catalog Created----------------------------------
    LOG_MESSAGE ------------------------Import took %TIME_SINCE_LAST_LOG%------------------------
    END_IF
    UNLOAD_ALL
    LOG_MESSAGE ----------------------- Preparing to build %state% Bathymetry Catalog ----------------------
    IMPORT_DIR_TREE DIRECTORY="G:\Users\Morgan\DATA_STATE\%state%\Bathymetry\complete" FILENAME_MASKS="*.SHP" RECURSE_DIR=NO
    DEFINE_VAR NAME="mp_type1" VALUE_ATTR="MP_TYPE" FILENAME="*"

    IF COMPARE_STR="%mp_type1% != 0x*"
    LOG_MESSAGE ************************************************************************************************MP_TYPE FIELD EMPTY************************************************************************************************
    ELSE
    EDIT_MAP_CATALOG FILENAME="G:\Users\Morgan\DATA_STATE\%state%\%state%_BATHYMETRY.GMC" CREATE_IF_EMPTY=YES ADD_FILE="G:\Users\Morgan\DATA_STATE\%state%\Bathymetry\complete\*.SHP" ZOOM_DISPLAY="PERCENT,0.75,0"
    LOG_MESSAGE ************************************************************ MP_TYPE FIELD POPULATED ************************************************************
    LOG_MESSAGE ------------------------%state% Bathymetry Catalog Created----------------------------------
    LOG_MESSAGE ------------------------Import took %TIME_SINCE_LAST_LOG%------------------------
    END_IF
    UNLOAD_ALL

    In the state admin directory there is a file where the MP_TYPE field is empty, and I get the correct LOG_MESSAGE. But in the bathymetry directory, all of the MP_TYPE field is correctly populated. The compare doesn't see this correctly and spits out the LOG_MESSAGE when it should be creating the map catalog. Unless anyone can see something I'm doing wrong.






  • bmg_bob
    bmg_bob Global Mapper Programmer
    I suggest logging the value you have for %MP_TYPE% so you know what you are working with.  

    The VALUE_ATTR parameter causes GM to find the first feature that contains a non-blank value for the specified attribute and return that value.  If no feature contains a value for the attribute, it will return an empty string.

    What are you trying to do?  It looks like you might be trying to determine whether or not your data has any features where MP_TYPE is empty.  Is this correct?
  • MorganRS
    MorganRS Global Mapper User Trusted User
    edited January 2016
    Yes, I'm trying to verify that the MP_TYPE field is populated, and if it is I want GM to go ahead with the catalog creation, and if it isn't, I want it to log a message and move on. Some files will be using many different mp types, I just want to know if there are any objects that don't have one. I'd like to do this check during this phase of my work flow to save time later on. GM now gives me an error during my next phase, which is the MP export. But it would save me a ton of time if I could ensure the field was populated before my second script was started.
  • bmg_bob
    bmg_bob Global Mapper Programmer
    Global Mapper doesn't have a single script command that does exactly what you want, but I think something like this would work:

    EDIT_VECTOR COMPARE_STR="MP_TYPE=" ATTR_MISSING="MP_TYPE" \
       COMPARE_OP=ANY COPY_TO_NEW_LAYER NEW_LAYER_NAME="MPT"
    QUERY_LAYER_METADATA METADATA_LAYER="MPT" \
       METADATA_ATTR="AREA COUNT" RESULT_VAR="MPT_COUNT"
    UNLOAD_LAYER FILENAME="MPT"
    IF COMPARE_OP="%MPT_COUNT%=0"
    // All features have MP_TYPE
    ELSE
    // There is at least one feature that does not have MP_TYPE
    END_IF


  • MorganRS
    MorganRS Global Mapper User Trusted User
    I've been trying this variation and other combinations of it with no luck. I get an IF error stating the the command is not valid because there is to COMPARE_STR parameter, and that the METADATA_LAYER can not be found.

    I also tried:
    EDIT_VECTOR COMPARE_STR="MP_TYPE=0x*"  ATTR_VAL="MISSING_MP=NO"
    EDIT_VECTOR COMPARE_STR="MP_TYPE!=0x*" ATTR_VAL="MISSING_MP=YES"
    DEFINE_VAR NAME="MISSING_MP_ATTR" VALUE="MISSING_MP"

    IF COMPARE_STR="%MISSING_MP_ATTR%!=YES"
    //LOG_MESSAGE ************************************************************ MP_TYPE FIELD POPULATED ************************************************************
    EDIT_MAP_CATALOG FILENAME="G:\Users\Morgan\DATA_STATE\%state%\%state%_BATHYMETRY.GMC" \
    CREATE_IF_EMPTY=YES ADD_FILE="G:\Users\Morgan\DATA_STATE\%state%\Bathymetry\complete\*.SHP" ZOOM_DISPLAY="PERCENT,0.75,0" ZOOM_DISPLAY="PERCENT,0.75,0"
    LOG_MESSAGE ------------------------%state% Bathymetry Catalog Created----------------------------------
    LOG_MESSAGE ------------------------Import took %TIME_SINCE_LAST_LOG%------------------------

    ELSE_IF COMPARE_STR="%MISSING_MP%=YES"
    LOG_MESSAGE ************************************************************************************************MP_TYPE FIELD EMPTY************************************************************************************************
    UNLOAD_ALL


    END_IF

    with no luck.


  • bmg_bob
    bmg_bob Global Mapper Programmer
    I had a typo in my example.  The IF command should have had a parameter of COMPARE_STR instead of COMPARE_OP.
    EDIT_VECTOR COMPARE_STR="MP_TYPE=" ATTR_MISSING="MP_TYPE" \
       COMPARE_OP=ANY COPY_TO_NEW_LAYER NEW_LAYER_NAME="MPT"
    QUERY_LAYER_METADATA METADATA_LAYER="MPT" \
       METADATA_ATTR="AREA COUNT" RESULT_VAR="MPT_COUNT"
    UNLOAD_LAYER FILENAME="MPT"
    IF COMPARE_STR="%MPT_COUNT%=0"
    // All features have MP_TYPE
    ELSE
    // There is at least one feature that does not have MP_TYPE
    END_IF
    The EDIT_VECTOR command will fine all loaded features where the MP_TYPE attribute is empty or missing, and copy those features to a new layer called MPT.

    The QUERY_LAYER_METADATA will return the number of area features in the "MPT" layer that we just created, and puts the value in a variable called MPT_COUNT.  Note that if you need lines or points, you can change the METADATA_ATTR to specify that.  Look at the layer metadata for a vector layer to see the names for lines and points.

    The UNLOAD_LAYER command gets rid of the "MPT" layer so it does not impact subsequent commands.

    At the end of this, you will have the MPT_COUNT variable, which tells you how many features have an empty or missing MP_TYPE attribute.

    The IF command tests the value of MPT_COUNT, and performs the appropriate commands.
  • MorganRS
    MorganRS Global Mapper User Trusted User
    I tried it this way too, and GM is creating the catalogs no matter what is in the MP_TYPE. My code looks like :
    GLOBAL_MAPPER_SCRIPT VERSION=1.00
    DEFINE_VAR NAME="state" VALUE="MT"
    DEFINE_VAR NAME="year" VALUE="2016"
    SET_LOG_FILE FILENAME="G:\Users\Morgan\DATA_STATE\%state%\0_CatalogBuild.log"
    LOG_MESSAGE <%SCRIPT_FILENAME%> Started at %DATE% %TIME%
    LOG_MESSAGE ----------------------- Preparing to build %state% Admin Bounds Catalog 1---------------------------------
    IMPORT_DIR_TREE DIRECTORY="G:\Users\Morgan\DATA_STATE\%state%\Administrative_Boundaries\2013_complete" FILENAME_MASKS="*.SHP" RECURSE_DIR=NO
    EDIT_VECTOR COMPARE_STR="MP_TYPE=" ATTR_MISSING="MP_TYPE" \
    COMPARE_OP=ANY COPY_TO_NEW_LAYER NEW_LAYER_NAME="MPT"
    QUERY_LAYER_METADATA METADATA_LAYER="MPT" \
    METADATA_ATTR="AREA COUNT" RESULT_VAR="MPT_COUNT"
    UNLOAD_LAYER FILENAME="MPT"
    IF COMPARE_STR="%MPT_COUNT%=0"
    LOG_MESSAGE ************************************************************ MP_TYPE FIELD POPULATED ************************************************************
    EDIT_MAP_CATALOG FILENAME="G:\Users\Morgan\DATA_STATE\%state%\%state%_Admin_Bounds1.GMC" \
    CREATE_IF_EMPTY=YES ADD_FILE="G:\Users\Morgan\DATA_STATE\%state%\Administrative_Boundaries\2013_complete\*.SHP" \
    ZOOM_DISPLAY="PERCENT,0.75,0"
    LOG_MESSAGE ------------------------%state% Admin Bounds Catalog 1 Created----------------------------------
    LOG_MESSAGE ------------------------Import took %TIME_SINCE_LAST_LOG%------------------------
    ELSE
    LOG_MESSAGE ************************************************************************************************MP_TYPE FIELD EMPTY************************************************************************************************
    END_IF
    UNLOAD_ALL
    LOG_MESSAGE ----------------------- Preparing to build %state% Bathymetry Catalog 1----------------------
    IMPORT_DIR_TREE DIRECTORY="G:\Users\Morgan\DATA_STATE\%state%\Bathymetry\complete" FILENAME_MASKS="*.SHP" RECURSE_DIR=NO
    EDIT_VECTOR COMPARE_STR="MP_TYPE=" ATTR_MISSING="MP_TYPE" \
    COMPARE_OP=ANY COPY_TO_NEW_LAYER NEW_LAYER_NAME="MPT"
    QUERY_LAYER_METADATA METADATA_LAYER="MPT" \
    METADATA_ATTR="AREA COUNT" RESULT_VAR="MPT_COUNT"
    UNLOAD_LAYER FILENAME="MPT"
    IF COMPARE_STR="%MPT_COUNT%=0"
    LOG_MESSAGE ************************************************************ MP_TYPE FIELD POPULATED ************************************************************
    EDIT_MAP_CATALOG FILENAME="G:\Users\Morgan\DATA_STATE\%state%\%state%_BATHYMETRY1.GMC" \
    CREATE_IF_EMPTY=YES ADD_FILE="G:\Users\Morgan\DATA_STATE\%state%\Bathymetry\complete\*.SHP" ZOOM_DISPLAY="PERCENT,0.75,0"
    LOG_MESSAGE ------------------------%state% Bathymetry Catalog 1 Created----------------------------------
    LOG_MESSAGE ------------------------Import took %TIME_SINCE_LAST_LOG%------------------------
    ELSE
    LOG_MESSAGE ************************************************************************************************MP_TYPE FIELD EMPTY************************************************************************************************
    END_IF
    UNLOAD_ALL

     and my results are

    ----------------------- Preparing to build MT Admin Bounds Catalog 1---------------------------------
    Importing directory <G:\Users\Morgan\DATA_STATE\MT\Administrative_Boundaries\2013_complete>...
    Editing vector features...
    Updated 1 features.
    Query layer metadata set variable MPT_COUNT to value 0
    Unloading layer MPT
    ************************************************************ MP_TYPE FIELD POPULATED ************************************************************
    Creating/Editing map catalog...
    Added 8 maps to the map catalog.
    ------------------------MT Admin Bounds Catalog 1 Created----------------------------------
    ------------------------Import took 4 s------------------------
    Removed all loaded overlays.
    ----------------------- Preparing to build MT Bathymetry Catalog 1----------------------
    Importing directory <G:\Users\Morgan\DATA_STATE\MT\Bathymetry\complete>...
    Editing vector features...
    Updated 0 features.
    ERROR: The METADATA_LAYER MPT specified in QUERY_LAYER_METADATA can not be found.
    Unloading layer MPT
    WARNING: Unable to unload overlay <MPT>, no matches found to unload. Be sure to specify the full path or description of the loaded layer that you want to unload.
    ************************************************************ MP_TYPE FIELD POPULATED ************************************************************
    Creating/Editing map catalog...
    Added 1 maps to the map catalog.
    ------------------------MT Bathymetry Catalog 1 Created----------------------------------
    ------------------------Import took 26 s------------------------
    Removed all loaded overlays.


    The first dataset imported has one file in the directory with no MP_TYPE. The script notes that and  updates the feature, and copies it to a new layer, as it should. But then the script unloads the new layer, and the IF condition queries that layer, it looks like to me anyways, and either doesn't notice the MPT_COUNT or ignores it, and makes the catalog anyways.
    Dataset two has the field populated, so it doesn't make the new layer, which causes a few errors but GM keeps on going. At least that's how I'm reading this. I've tried a few variations for the IF condition to look for, but it ignores them all. Not sure what the issue is.

  • bmg_bob
    bmg_bob Global Mapper Programmer
    Answer ✓
    The QUERY_METADATA command is getting the number of area features that were copied.  Your output indicates that 1 feature was copied, but the MPT_COUNT gets set to 0.  Are there other types of features in the data (i.e., lines or points) that might be getting copied?  This would explain why, even though 1 feature was copied, the MPT_COUNT variable was set to 0.  I ran a test using some of my own data, and I find that the QUERY_METADATA command puts the correct value in the variable.

    I suggest that you create a smaller script for testing that contains only the IMPORT_DIR_TREE, EDIT_VECTOR, and QUERY_METADATA commands.  When you run this in the UI, you will be able to look at the metadata for the layer that was created, and the layer iteself, to see what types of features are in there.

    Note that the IF command is not querying data in the layer.  It is only testing the value of a variable (MPT_COUNT, in this case.)  The QUERY_METADATA command queries information about the layer.  Once that information has been stored in the variable, then it is safe to unload the layer.

  • MorganRS
    MorganRS Global Mapper User Trusted User
    I ran the script in the UI and checked the metadata, and the contents of the layer. I can't believe I didn't think about it, but my directory that was imported was importing both lines and areas, and a line feature was missing the attr value. Script worked when I changed it to look for the line count.
    Thanks for all the help!
  • MorganRS
    MorganRS Global Mapper User Trusted User
    After doing a bunch of tests, and reorganizing the way the script runs I've found that this method works only about 75% of the time. I'll have to try and find another way for GM to notify me if a field is missing or empty