Tapatalk

help with calculation

help with calculation

2
NewbieNewbie
2

    Oct 16, 2007#1

    I am using UE 13.10a+1. I'm trying to change coordinate values digital minutes to digital degrees.

    From the following:

    Code: Select all

     -87 1.5319959 32 24.3091365 624.8 599999.5 0
     -87 1.4278910 32 24.3534171 624.8 600600.4 0
     -87 1.3237846 32 24.3976961 624.8 600599.8 0
     -87 1.2196764 32 24.4419736 624.8 600599.8 0
     -87 0.8032268 32 24.6190693 624.8 600.0 0
    The first line should read:

    Code: Select all

    -87.0255333,32.4051523,624.8
    Longitude = 87 + (1.5319959 / 60), latitude = 32 + (24.3091365 / 60)
    I must keep the output values to 7 decimal places and rounding the 7th place.

    Here is the code I'm using. The calculation is taking place in the second loop (which is totally wrong). This is my first attempt at JavaScript and I've tried several different ways, but I just can't seem to get calculations to work.

    Code: Select all

    UltraEdit.insertMode();
    if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
    else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
    UltraEdit.activeDocument.hexOff();
    UltraEdit.ueReOn();
    UltraEdit.activeDocument.findReplace.regExp = true;
    UltraEdit.activeDocument.findReplace.replaceAll = false;
    
    UltraEdit.activeDocument.bottom();
    if (UltraEdit.activeDocument.isColNumGt(1)) UltraEdit.activeDocument.insertLine();
    
    UltraEdit.activeDocument.top();
    var runn = "%^(RUN [0-9]+^) +^(-[0-9]+ [0-9]+.[0-9]+ [0-9]+ [0-9]+.[0-9]+^) +^(-[0-9]+ [0-9]+.[0-9]+ [0-9]+ [0-9]+.[0-9]+^) ^([0-9]+^) [0-9]*$";
    
    while ( (UltraEdit.activeDocument.findReplace.find(runn)) && ! UltraEdit.activeDocument.isEof() ) {
       UltraEdit.activeDocument.findReplace.replace(runn,"^1" + "   Total Points = " + "^4");
    }
    UltraEdit.activeDocument.top();
    var coors = "% ^(-[0-9]+^) ^([0-9]+.[0-9]+^) ^([0-9]+^) ^([0-9]+.[0-9]+^) ^([0-9]*^) [0-9]*$";
    
    while ( (UltraEdit.activeDocument.findReplace.find(coors)) && ! UltraEdit.activeDocument.isEof() ) {
    
       UltraEdit.activeDocument.findReplace.replace(coors, ("^1" + ("^2" / 60)) + "," + "("^3" + ("^4" /60))" + "," + "^5");
    }
    UltraEdit.activeDocument.top();
    Thanks, Paul

    262
    MasterMaster
    262

      Oct 16, 2007#2

      Hello Paul

      You cannot do calculations directly in a replace(...). You will have to do the calculations with JavaScript code. I have changed the second part of your script to do these calculations. I haven't touched to first half as I do not have any example data for that.

      Code: Select all

      UltraEdit.insertMode();
      if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
      else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
      UltraEdit.activeDocument.hexOff();
      UltraEdit.ueReOn();
      UltraEdit.activeDocument.findReplace.regExp = true;
      UltraEdit.activeDocument.findReplace.replaceAll = false;
      
      UltraEdit.activeDocument.bottom();
      if (UltraEdit.activeDocument.isColNumGt(1)) UltraEdit.activeDocument.insertLine();
      
      UltraEdit.activeDocument.top();
      var runn = "%^(RUN [0-9]+^) +^(-[0-9]+ [0-9]+.[0-9]+ [0-9]+ [0-9]+.[0-9]+^) +^(-[0-9]+ [0-9]+.[0-9]+ [0-9]+ [0-9]+.[0-9]+^) ^([0-9]+^) [0-9]*$";
      
      while ( (UltraEdit.activeDocument.findReplace.find(runn)) && ! UltraEdit.activeDocument.isEof() ) {
         UltraEdit.activeDocument.findReplace.replace(runn,"^1" + "   Total Points = " + "^4");
      }
      
      // Changes to script beyond this point:
      
      // Switch to Perl regexp so UE and JavaScript regexps is alike
      UltraEdit.perlReOn();
      
      // Helper function to make a JavaScript regExp ready for UE use:
      RegExp.prototype.toUEregexpString = function() { return this.toString().replace(/^\/|\/$/g, ""); }; /* remove starting and ending / */
      
      
      UltraEdit.activeDocument.top();
      
      // Perl regexp finding lines with coors
      var coors = /^\s(-?\d+)\s(\d+\.\d+)\s(\d+)\s(\d+\.\d+)\s([\d.]+).*$/;
      
      while ( UltraEdit.activeDocument.findReplace.find(coors.toUEregexpString()) && ! UltraEdit.activeDocument.isEof() ) {
      
       var coorLine = UltraEdit.activeDocument.selection; // Retrieve coor line into JavaScript
       var coorLineArr = coorLine.match(coors); // perform Perl Regexp match on line
       
       var longitude = convertToDigitalDegrees(Number(coorLineArr[1]),Number(coorLineArr[2]));
       var latitude = convertToDigitalDegrees(Number(coorLineArr[3]),Number(coorLineArr[4]));
       
       var resultLine = longitude+","+latitude+","+coorLineArr[5];
      
        UltraEdit.activeDocument.write(resultLine); // original line is selected. Write on top of that
      }
      UltraEdit.activeDocument.top();
      
      // local function to change coordinate values digital minutes to digital degrees.
      function convertToDigitalDegrees(coor1,coor2) {
       var signVal = 1;
       if (coor1<0) {
       signVal = -1;
       }
       coor1 = coor1 * signVal; // remove sign before calculations
       
       var coor = coor1 + (coor2 / 60);
       
       coor = coor.toFixed(7); // round to 7 decimal places
       coor = coor * signVal;  // change sign
       
       return String(coor); // return as string
      }
      
      When run on your sample data it becomes:

      Code: Select all

      -87.0255333,32.4051523,624.8
      -87.0237982,32.4058903,624.8
      -87.0220631,32.4066283,624.8
      -87.0203279,32.4073662,624.8
      -87.0133871,32.4103178,624.8
      

      2
      NewbieNewbie
      2

        Oct 16, 2007#3

        Thanks jorrasdk. That works great!

        The first half reformats the header for each run and pulls the total point count and is working okay. So no change was needed there.

        Thanks for the help, Paul