Script to 'apply elevation values from terrain layers to selected features'

I've been reading through the global mapper scripting documents and can't seem to see a way that I can do what I want it to do. Ideally, I would already have DEMs loaded into GM, then my script would import my (control) points and apply elevation values from the DEMs to the point features and output a CSV of the point data. 

My question is, is this possible?

Best Answers

  • bmg_bobbmg_bob Global Mapper Programmer Posts: 1,686
    Answer ✓
    Hello,

    I saw the data that you sent to Blue Marble Geographics support, and was able to reproduce your result.  The cause is that your point data has an ELEVATION attribute (with value of 0), and that is where Global Mapper is getting the elevation value it is putting in the CSV file.  A workaround is to add the following script command before the export:
    EDIT_VECTOR ATTR_TO_DELETE="ELEVATION"
    That will delete the ELEVATION attribute, and force the elevation values in the grid to be used.  It might not be a bad idea to add a way to force the script to use the grid, even if the features happen to have an ELEVATION attribute.  We will take a look a that.

    Cheers,
    Bob
  • bmg_bobbmg_bob Global Mapper Programmer Posts: 1,686
    Answer ✓
    Hello,

    When you are exporting a shapefile, the INC_ELEV_ATTR does not work the same was as the EXPORT_ELEV parameter, which is supported only for text file exports.  INC_ELEV_ATTR will export an elevation parameter (typically called ELEVATION) if it exists.  It does not apply elevations from elevation grid layers.

    You will need to change your EDIT_VECTOR command to remove the DELETE_ATTR parameter, and add APPLY_ELEVS=YES.  This will apply elevations from your elevation grid in a parameter called ELEVATION, and enable the INC_ELEV_ATTR parameter on the EXPORT_VECTOR command to operate correctly.

    Cheers,
    Bob

Answers

  • eddie416eddie416 Posts: 39
    Anybody? Does anyone know if I can add elevation values from a grid/DEM to point features?
  • mcsmoothermcsmoother Global Mapper User Posts: 18
    You can apply the elevations on export to the CSV, using EXPORT_VECTOR command with EXPORT_ELEV=YES
  • You can apply the elevations on export to the CSV, using EXPORT_VECTOR command with EXPORT_ELEV=YES
    The result of this is an elevation field with values that are all 0
  • mcsmoothermcsmoother Global Mapper User Posts: 18
    you need to have an elevation grid that the values are going to be pulled from and make sure it (it's layer) is visible during export
  • I'm not sure what I'm doing wrong. I have an elevation grid loaded and visible, yet when I do this I get elevation values of 0. Even when doing this through the export functions of global mapper (not through script) and select 'export elevations' I get elevation values of all 0. What could be going wrong?
  • MykleMykle Global Mapper User Posts: 382Trusted User
    What kind of elevation grid file have you loaded?  A TIF file, for example, can contain color information, or it can contain elevation data, so HOW you load such a file is important.  A shapefile may contain contour lines, but the elevation values may not be directly accessible.  A file may contain elevations, but might not provide an elevation grid without additional steps.  So the answer to your question may not be simple. 
  • It is an ascii grid file. So it is an elevation grid that displays elevations. Here is an image displaying the elvation grid with the points I wish to append the elevations to.
  • bmg_bobbmg_bob Global Mapper Programmer Posts: 1,686
    edited October 2016
    Hello,

    Using Global  Mapper 17.2 (b091316), I ran the following script, and the output file contains elevations:
    GLOBAL_MAPPER_SCRIPT VERSION="1.00"
    
    EXPORT_VECTOR TYPE="CSV" FILENAME="P:\Output\fromScript.csv" EXPORT_ELEV=YES 
    I also used File | Export | Export Vector File... in the UI, and successfully produced a CSV that includes elevation values.  I noticed that if I started in the Control Center, and right-clicked on my point layer to export, I had to select both the point layer and DEM layer in order for the CSV export to work.  

    When you are running the script via the File | Run Script... command, make sure that you have a check next to "Run Script in the Context of the Main View (i.e., Use Already Loaded Data)".

    What version of Global Mapper are you using?  Do you have the latest release?

    Cheers,
    Bob
  • I tried using your exact script (with a different output of course) and the CSV elevations all still read 0. Same with the other methods of exporting that you described. I am using the same version as well (Global  Mapper 17.2 (b091316)).

    To be clear, this function should append the elevation from the gridded surface to the point, correct? I don't know why this is not working. Could it be the ascii files?
  • MykleMykle Global Mapper User Posts: 382Trusted User
    Is your elevation file available to the public?
  • It is not. Maybe I'll try converting it to a GM Grid file
  • Tried the GMG file. Still all 0 elevations
  • bmg_bobbmg_bob Global Mapper Programmer Posts: 1,686
    eddie416 said:
    To be clear, this function should append the elevation from the gridded surface to the point, correct? I don't know why this is not working. Could it be the ascii files?
    Yes, that is what should happen.  When you say the elevations are zero, I assume that you are looking at the CSV output file.  Am I correct?

    Cheers,
    Bob
  • MykleMykle Global Mapper User Posts: 382Trusted User
    Is the name of the original file "ascii.png"?  I would expect an ascii file to be a text file, not an image.  An image file may show elevations as zero. 
  • bmg_bob said:
    eddie416 said:
    To be clear, this function should append the elevation from the gridded surface to the point, correct? I don't know why this is not working. Could it be the ascii files?
    Yes, that is what should happen.  When you say the elevations are zero, I assume that you are looking at the CSV output file.  Am I correct?

    Cheers,
    Bob
    Yes I am looking at the CSV output file. 

    Mykle said:
    Is the name of the original file "ascii.png"?  I would expect an ascii file to be a text file, not an image.  An image file may show elevations as zero. 
    It is an ASCII Grid. You'll see this format in the export options for Elevation Grid formats 
  • MykleMykle Global Mapper User Posts: 382Trusted User
    Okay, I see several formats that are described as ASCII.  I have exported an elevation file of mine to "Arc ASCII grid", and opened it in a text editor.  I see a few lines of parameters, followed by elevation values separated by a space character.  Are your values as you would expect your elevation values to be? 

    Of more interest may be where these values came from.  It sounds like you loaded the data from elsewhere, and I'm still trying to find some solid data that I can stand on and say something relevant. 
  • MykleMykle Global Mapper User Posts: 382Trusted User
    If you have View | Status Bar selected, you should see elevation values as you move your cursor, displayed on the status bar at the bottom of the window, on the left end.  
  • eddie416eddie416 Posts: 39
    edited October 2016
    I don't see anything wrong with the files after investigation. I have also tried this with a global mapper grid and a USGS DEM file and the result is the same. I have no idea why my global mapper is not cooperating or what it is I'm doing wrong but I keep getting 0 for all point elevations. I'm quite savvy with this stuff usually, I'm at a loss as to what is happening.

    I've also contacted Blue Marble directly and followed their instructions, to no avail. 
  • MykleMykle Global Mapper User Posts: 382Trusted User
    Well, we have been talking mostly about the results of exporting data files.  I would be more interested in the source of your data.  For example, you can look at the metadata for your elevation data layer.  Select the layer in Control Center, right-click and select Metadata (or press M as you double-click on your layer).  At the bottom of the list are values for min and max elevation, as well as pixel width and height).  The Histogram tab shows your elevations as a series of ranges. 

    Global Mapper (and GIS in general) are not simple, but they aren't that complex.  Many times the data look like one thing, but they are really something else.  Sorting out the details is occasionally surprising.  All of this leads us to be more and more sensitive to methods for cross-checking the data (and our assumptions).  In that respect, your problem is interesting! 
  • PROJ_UNITS=meters
    EPSG_CODE=EPSG:26910
    COVERED AREA=66.422 sq mi
    LOAD TIME=3.06 s
    NUM COLUMNS=7429
    NUM ROWS=5791
    PIXEL WIDTH=2 meters
    PIXEL HEIGHT=2 meters
    MIN ELEVATION=685 meters
    MAX ELEVATION=2089 meters
    ELEVATION UNITS=meters
    BIT DEPTH=24
    An example of some of the metadata. The rest of the metadata and the histogram looks as I would expect it to



  • MykleMykle Global Mapper User Posts: 382Trusted User
    Okay, your file is UTM zone 10.  My sample file is zone 12.  My bit depth is 32, but I don't see a problem with your 24 bit data. 

    Do your elevation values look reasonable, as displayed on the status bar at the bottom? 

    This is now looking like an issue with settings.  Your workflow looks fine. 
  • Ah thank you! That ought to fix it. I know when you 'apply elevations from terrain layers to selected features' it overwrites the current elevation field. Didn't cross my mind that that might be the issue. Thank you! 
  • eddie416eddie416 Posts: 39
    I seem to be having some trouble lately Bob. My script that worked to do this automatically in GM17, no longer works in GM18. 

    //import all control points
    IMPORT FILENAME=H:\Control_points\2016\Mascot_Spirit_GPS_UTM.shp TYPE="SHAPEFILE"

    //define export Location/filename and select the project area
    DEFINE_VAR NAME=PROJECT_CONTROL PROMPT="DIR" VALUE=D:\ PROMPT_TEXT="Select Control shapefile and CSV output location"
    DEFINE_VAR NAME=PROJECT_AREA PROMPT=FILE VALUE="D:\.shp" PROMPT_TEXT="Select Project Area Shapefile" FILE_MUST_EXIST="YES"

    //delete elevation field
    EDIT_VECTOR FILENAME=H:\Control_points\2016\Mascot_Spirit_GPS_UTM.shp SHAPE_TYPE="POINTS" ATTR_TO_DELETE="ELEVATION" 
    //export all control within defined polygon
    EXPORT_VECTOR FILENAME=%PROJECT_CONTROL%Control.shp TYPE=SHAPEFILE GEN_PRJ_FILE=YES SHAPE_TYPE=POINTS POLYGON_CROP_FILE=%PROJECT_AREA% POLYGON_CROP_USE_ALL=YES GEN_3D_FEATURES=YES INC_ELEV_ATTR=YES OVERWRITE_EXISTING=YES 


    //Unload all control points
    UNLOAD_LAYER FILENAME=H:\Control_points\2016\Mascot_Spirit_GPS_UTM.shp

    //import project relevant control points
    IMPORT FILENAME=%PROJECT_CONTROL%Control.shp TYPE="SHAPEFILE"



    //Calculate difference between grid elevation and control points. Select the control point layer before running
    CALC_ATTR_FORMULA FILENAME="%PROJECT_CONTROL%Control.shp" NEW_ATTR="Diff" CALC_MODE=NUMERIC FORMULA="ELEVATION - Control"

    //Calculate absolute difference
    CALC_ATTR_FORMULA FILENAME="%PROJECT_CONTROL%Control.shp" NEW_ATTR="Abs_Diff" CALC_MODE=NUMERIC FORMULA="ABS(ELEVATION - Control)"

    //Output CSV
    EXPORT_VECTOR FILENAME=%PROJECT_CONTROL%Control.csv TYPE=CSV COORD_DELIM=COMMA EXPORT_ELEV=NO OVERWRITE_EXISTING="YES"



    The script will run, but it will not take elevation values from the terrain I have loaded. This exact script works perfectly in GM17 (I still have it installed), but it does not work in GM18.2. I can't figure out what is not working
  • bmg_bobbmg_bob Global Mapper Programmer Posts: 1,686
    Hello,

    Can you be more specific about what you are trying to do and what part of the output is not correct?

    Cheers,
    Bob
  • eddie416eddie416 Posts: 39
    The goal is to load in a shapefile of control points (the control elevations are under a field called 'control'), then export the control points in a specific area with the elevations from the loaded terrain. So the export should have 2 sets of elevations, one under the field 'control' and one under the field 'elevation' (exported from the currently loaded terrain). 

    The result is a shapefile that has the 'control' field, but no 'elevation' field with values from the terrain I have loaded. I am definitely clicking 'run script in context of main menu' so I'm not sure what exactly is wrong. As I said, it still works as expected in GM17
  • eddie416eddie416 Posts: 39
    That was the problem, thank you very much Bob
Sign In or Register to comment.