help with calculation

Help with writing and running scripts

help with calculation

Postby pbnav1 » Tue Oct 16, 2007 12:53 pm

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;
-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 crack 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();
UltraEdit.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
User avatar
pbnav1
Newbie
 
Posts: 2
Joined: Mon Oct 15, 2007 11:00 pm

Re: help with calculation

Postby jorrasdk » Tue Oct 16, 2007 2:23 pm

Hello Paul

You cannot do calculations directly in a replace(...). You will have to do the calculations inside the javascript. I have changed the 2nd part of your script to do these calculations. I haven't touched to 1st half as I do not have any example data for that.

Code: Select all
UltraEdit.insertMode();
UltraEdit.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 returns:

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
User avatar
jorrasdk
Master
Master
 
Posts: 275
Joined: Mon Mar 19, 2007 11:00 pm
Location: Denmark

Re: help with calculation

Postby pbnav1 » Tue Oct 16, 2007 2:58 pm

thanks jorrasdk that works great...

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

thanks for the help
Paul
User avatar
pbnav1
Newbie
 
Posts: 2
Joined: Mon Oct 15, 2007 11:00 pm


Return to Scripts

cron