Geographic coordinates to utm converter

Discussions about CSEntry
Post Reply
vgonzalez
Posts: 27
Joined: March 12th, 2020, 4:19 pm
Location: VENEZUELA

Geographic coordinates to utm converter

Post by vgonzalez »

Dear colleagues

I am writing to you on this occasion to be advised by the guru developers in this prestigious tool in order to develop a logic that allows me to respond to a requirement from the CSPro logic.

The requirement that they ask me after reading the geographic coordinates is to convert the geographic coordinates to UTM coordinates. :?:

So far the equations and functions I have used to do this use the trigonometric functions of Sine, Cosine, and Tangent. :geek:

Could someone advise me how to perform the logic without these functions? :!:

Or are there equivalent functions that allow you to do these calculations using a CSPro logic? :idea:
Gregory Martin
Posts: 1796
Joined: December 5th, 2011, 11:27 pm
Location: Washington, DC

Re: Geographic coordinates to utm converter

Post by Gregory Martin »

If your project goes into production in a few months, you can use JavaScript in CSPro 8.0 and use a library like the code here: https://www.npmjs.com/package/utm-latlng?activeTab=code

However, in the meantime, if you want to program this in CSPro, you can use these math functions as Taylor series approximations of the math functions that you need that are not available in CSPro logic:
// because these math functions don't exist in CSPro, we need to approximate them using Taylor series

function fm_fact(val)

    numeric result = 1;

    do val = val while val >= 2 by -1
       
result = result * val;
    enddo;

    fm_fact = result;

end;


numeric fm_pi = 3.14159265359;


function fm_deg2rad(angle)
    fm_deg2rad = angle * fm_pi / 180;
end;

function fm_rad2deg(angle)
    fm_rad2deg = angle / fm_pi * 180;
end;


function fm_sin(angle)

    numeric result,idx = 1;

    while 1 do

        numeric
calculation = ( angle ^ idx ) / fm_fact(idx);

        if calculation = default then
            break
;
        endif;

        if idx % 4 = 1 then
           
result = result + calculation;
        else
           
result = result - calculation;
        endif;

        inc(idx,2);

    enddo;

    fm_sin = result;

end;


function fm_cos(angle)

    numeric result = 1, idx = 2;

    while 1 do

        numeric
calculation = ( angle ^ idx ) / fm_fact(idx);

        if calculation = default then
            break
;
        endif;

        if idx % 4 = 2 then
           
result = result - calculation;
        else
           
result = result + calculation;
        endif;

        inc(idx,2);

    enddo;

    fm_cos = result;

end;


function fm_asin(sin)

    numeric result = sin, idx = 3;

    numeric numNumer = 1,numDenom = 2;

    while 1 do

        numeric
calculation = ( numNumer / numDenom ) * ( sin ^ idx / idx );

        if calculation = default then
            break
;
        endif;

        inc(result, calculation);

        numNumer = numNumer * idx;
        numDenom = numDenom * ( idx + 1 );

        inc(idx,2);

    enddo;

    fm_asin = result;

end;


function fm_atan(tan)

    fm_atan = fm_asin(tan / sqrt(tan ^ 2 + 1));

end;


function fm_atan2(y,x)

    if x > 0 then                   fm_atan2 = fm_atan(y / x);
    elseif y >= 0 and x < 0 then    fm_atan2 = fm_pi + fm_atan(y / x);
    elseif y < 0 and x < 0 then     fm_atan2 = -1 * fm_pi + fm_atan(y / x);
    elseif y > 0 and x = 0 then     fm_atan2 = fm_pi / 2;
    elseif y < 0 and x = 0 then     fm_atan2 = -1 * fm_pi / 2;
    elseif y = 0 and x = 0 then     fm_atan2 = default;
    endif;

end;
htuser
Posts: 632
Joined: December 19th, 2011, 6:26 pm
Location: Silver Spring Area, MD, USA

Re: Geographic coordinates to utm converter

Post by htuser »

Thanks Greg for helping us in having trigonometric functions in CSPro logic. More than 4 years ago, I requested this: viewtopic.php?p=10688 so right now, I'll be able to perform some calculations directly in CSPro logic.

However, I would also like to know if the JS-CSPro logic bidirectional API of CSPro 8.0 will support Node's CommonJS modules?
(It seems that the JS lib you pointed use CommonJS instead of ESM modules var utmObj = require('utm-latlng');)
Best,
G.VOLNY, a CSProuser from Haiti, since 2004
vgonzalez
Posts: 27
Joined: March 12th, 2020, 4:19 pm
Location: VENEZUELA

Re: Geographic coordinates to utm converter

Post by vgonzalez »

Thanks Gregory for the functions, I managed to make an approximation to the expected value of UTM, not perfect but useful, however I note that I had to modify a function and add another to give the expected results.

Code: Select all

function fm_cos2(angle)
 // Função quadrado do ângulo cosseno
    numeric result = 0;

    result = (1 + fm_cos(2 * angle)) / 2;
	
    fm_cos2 = result;
end;


function LogNatural(NUMERIC numLog)
//
// Calcular logaritmo natural
// ln (número) = log(número)/log(2,718281828459).
//
	NUMERIC result = 0;
	
// Equivalent to Ln(number) = Log(number) / Log(Euler)
	result = Log(numLog) / Log(numEuler);
	if not special(result) then
		LogNatural = result;
	else
		LogNatural = 0;
	endif;
end;
In the same way, I attach an example of how to calculate the UTM coordinates from the geographic coordinates through the process
of Coticchia - Surace
Attachments
Geo_To_UTM.rar
(61.89 KiB) Downloaded 59 times
Post Reply