Scripting trig formulas - can it be done?

kbelliskbellis Global Mapper UserPosts: 518Trusted User
edited August 2018 in GM Script Language
Given:

Prerequisite Formula
goundHeight = RelativeAltitude * sensorHeight / FocalLength

Formula Example 1

ForwardPointNorthing = (COS FlightYawDegree) * groundHeight/2) + PrinciplePointNorthing


The documentation is lacking, so I started poking at this in CALC_ATTR; wherein, the Function Descriptions are a bit confusing, as too, the Status / Sample result:


Changing Calculation Mode to Automatic had no effect.

Eventually, I'm hopeful that I might be able to define variables in a Global Mapper Script, but at this point, I'm stuck.

Thanks for any replies.

Best Answer

  • JeffH@BMGJeffH@BMG Global Mapper Developer Posts: 322
    Answer ✓
    With respect to scripting attribute formulas, you'd want to use the CALC_ATTR_FORMULA script command. In this case, a command something like the following should work:
    CALC_ATTR_FORMULA NEW_ATTR="TEST" FORMULA="cos(degtorad(FLIGHTYAWDEGREE))"

Answers

  • JeffH@BMGJeffH@BMG Global Mapper Developer Posts: 322Trusted User
    Hi Kelly,

    For the formula "COS FlightYawDegree", you had it right: you would indeed use cos(degtorad(FLIGHTYAWDEGREE)). You'll want to assign the result to a different attribute than FLIGHTYAWDEGREE; otherwise you'll destroy the original value (that's the attribute named in the "Select existing or create new attribute..." control up at the top of the dialog) when you press "Calculate". Better to assign it to something else with a name that has meaning to you, or a dummy name like "Test" -- you can always rename it later on. Then you can run the calculation, and examine the results in a new attribute column.

    Using a Calculation Mode of Numeric is fine; Automatic should work as well.

    The Sample result shown is always based on the attributes in the first feature in your layer; a result of 0 may very well be valid for that. If you want to see what it is, you can just type in a formula that's just an attribute name (e.g. FLIGHTYAWDEGREE). The Status will tell you if it's not found. Or if you to test your formula out while you're developing it, you can use numbers instead of attributes that you know will give you known results; e.g., a formula of cos(degtorad(0)) should return a result of 1.

    I hope that this helps. Feel free to ask more questions.

    Best regards,

    ~Jeff
  • kbelliskbellis Global Mapper User Posts: 518Trusted User
    Thanks Jeff! I very much appreciate your response and I plan on looking at this further. Over the weekend, I ended up doing the whole series of calculations in OpenOffice Calc before finally doing a data merge using the joined database and Writer.While this workflow outside of Global Mapper allowed for the automated creation of the 130+ .gcp files, a more elegant solution I think may lie in Global Mapper scripting.
  • JeffH@BMGJeffH@BMG Global Mapper Developer Posts: 322Trusted User
     
    Thanks Jeff! I very much appreciate your response and I plan on looking at this further. Over the weekend, I ended up doing the whole series of calculations in OpenOffice Calc before finally doing a data merge using the joined database and Writer.While this workflow outside of Global Mapper allowed for the automated creation of the 130+ .gcp files, a more elegant solution I think may lie in Global Mapper scripting.
    We'd like to think so, too. :) 

    Anyway, this sort of operation is exactly what we had in mind when we expanded the attribute calculation facilities. The formula language is mainly modeled on MS Excel (which is probably pretty similar to OpenOffice Calc), with a twist of SQL. If you have suggestions as to how to make things better, we'd like to hear them.
  • kbelliskbellis Global Mapper User Posts: 518Trusted User
    The original question has been answered, but the forum software continues nagging me to accept any reply as having answered the question or not. How's come?

    But getting back to the original issue, I'm looking forward to developing Nadir Collage (TM) using Global Mapper scripting - more on this soon! And thank you Jeff for your kind assistance.

  • kbelliskbellis Global Mapper User Posts: 518Trusted User
    edited August 2018
    JeffH@BMG said:
     
    If you have suggestions as to how to make things better, we'd like to hear them.
    1) The initial Status / Sample result: is always wrong, that is, the incorrect values are getting displayed. Consider showing a tabular display of real numbers for the first half dozen or so of the layer being operated upon.



    2) Make available previous formulas used during the entirety of the project; i.e., not just on one editing session.
  • JeffH@BMGJeffH@BMG Global Mapper Developer Posts: 322Trusted User
    Hi Kelly,

    With respect to #1: The status result reflects the calculation as performed on the first feature of the layer it's invoked on (the order may depend on how the layers features have been sorted in the Attribute Editor). When you say it's always wrong, do you mean it doesn't match up with the results you think they should be? That would be a bug worth looking into if so. 

    Rather than display the first <n> results, my inclination would be to also show the values for the attributes that are actually used in the formula; that way you can see the relationship between the values and the result. So maybe something like the following would make this more useful:
    Formula OKSample Result: "3"Where FLIGHTYAWDEGREE = "32", GROUNDHEIGHT = "43.5", PRINCIPLEPOINTEASTING = "111"

    With respect to #2: we do persist formulas to GM workspace files, indexed by the attribute name created or modified, one formula per attribute. That is, if we create a new attribute "Temp", then the workspace will contain an entry for "temp" and the last formula used to modify it (it's in a DEFINE_PREVIOUS_ATTR_FORMULAS section). You'd see this by opening up the attribute calculator and selecting "temp" as the target attribute; the formula would be shown at the bottom of the calculator dialog. I don't believe that these are persisted in the GM project files, though.

    ~Jeff
  • kbelliskbellis Global Mapper User Posts: 518Trusted User
    edited August 2018
    1) Yes, that's correct, the answer is wrong. **
    2) I will look for that when next I'm in Calculator. Thank you!

    ** This wrong answer was happening in Global v19.1, but isn't happening in v20 Beta! :)
  • kbelliskbellis Global Mapper User Posts: 518Trusted User
    Also, in v20, I'm now seeing the Previous Formula information.

    One note worth mentioning, this morning I uninstalled v19 and v20, and then deleted the entirety of the Windows registry folder; i.e., HKEY_CURRENT_USER > Software > Global Mapper, and Delete Global Mapper folder. And then reinstalled v20.

Sign In or Register to comment.