樹心幽徑

20170424將WGS84台灣經緯度座標轉TWD97二度分帶座標Java Script
2017/04/24,16:31
<title>WGS84台灣經緯度座標轉TWD97二度分帶座標</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<body bgcolor=lightgreen>

<script language="JavaScript">

function lonlat2twd97twd67(lon,lat)
{
var x97,y97;
var a,b,long0,k0,dx;
var e,e2,n,nu,p;
var A,B,C,D,E,S;
var K1,K2,K3;
var K4,K5;
        a = 6378137.0;
        b = 6356752.314245;
        long0 = 121.0*Math.PI/180.0;
        k0 = 0.9999;
        dx = 250000;


        e = Math.pow(1-(b*b)/(a*a),0.5);
        e2 = Math.pow(e,2)/(1-Math.pow(e,2));
        n = (a-b)/(a+b);
        nu = a/Math.pow(1-Math.pow(e,2)*Math.pow(Math.sin(lat),2),0.5);
        p = lon-long0;
        A = a*(1 - n + (5/4.0)*(Math.pow(n,2) - Math.pow(n,3)) + (81/64.0)*(Math.pow(n,4)  - Math.pow(n,5)));
        B = (3*a*n/2.0)*(1 - n + (7/8.0)*(Math.pow(n,2) - Math.pow(n,3)) + (55/64.0)*(Math.pow(n,4) - Math.pow(n,5)));
        C = (15*a*Math.pow(n,2)/16.0)*(1 - n + (3/4.0)*(Math.pow(n,2) - Math.pow(n,3)));
        D = (35*a*Math.pow(n,3)/48.0)*(1 - n + (11/16.0)*(Math.pow(n,2) - Math.pow(n,3)));
        E = (315*a*Math.pow(n,4)/51.0)*(1 - n);
        S = A*lat - B*Math.sin(2*lat) + C*Math.sin(4*lat) - D*Math.sin(6*lat) + E*Math.sin(8*lat);
        K1 = S*k0;
        K2 = k0*nu*Math.sin(2*lat)/4.0;
        K3 = (k0*nu*Math.sin(lat)*Math.pow(Math.cos(lat),3)/24.0) * (5.0 - Math.pow(Math.tan(lat),2) + 9.0*e2*Math.pow(Math.cos(lat),2) + 4.0*Math.pow(e2,2)*Math.pow(Math.cos(lat),4));
        y97 = K1 + K2*Math.pow(p,2) + K3*Math.pow(p,4);

        K4 = k0*nu*Math.cos(lat);
        K5 = (k0*nu*Math.pow(Math.cos(lat),3)/6.0) * (1 - Math.pow(Math.tan(lat),2) + e2*Math.pow(Math.cos(lat),2));
        x97 = K4*p + K5*Math.pow(p,3) + dx;

        document.write("TWD97 (" + x97 +"," + y97 + ")<br>" );

	x67=x97-807.8-0.00001549*x97-0.000006521*y97;
        y67=y97+248.6-0.00001549*y97-0.000006521*x97;

        document.write("TWD67 (" + x67 +"," + y67 + ")<br>" );


}


function change_wgs84()
{
	var lon,lat,londeg,latdeg;
	var query=document.frm.wgs84.value;
	var res = query.split(",");

        document.write("(" +res[0]+"," +res[1]+")<br>");
	londeg=parseFloat(res[0]);
	latdeg=parseFloat(res[1]);
	lon=londeg*Math.PI/180.0; 
	lat=latdeg*Math.PI/180.0;
	lonlat2twd97twd67(lon,lat)

}

</script>
<form name='frm' METHOD=POST ACTION='' onsubmit='return validation();'>
<div align="center">

將WGS84經緯度座標轉TWD97,TWD67<br>
台灣虎子山經緯度WGS84=120.982025,23.973875 <br>
其二度分帶座標T<br>
TWD97=248170.787,2652129.936  <br>
TWD67=247341.925, 2652335.811<br>
輸入要轉換WGS84經緯度座標(lon,lat):<input type=text name=wgs84 size=40 value="120.982025,23.973875"  onchange="change_wgs84()"> <br>

</form>
<br>
<script language="JavaScript">
var lon,lat,londeg,latdeg,x,y;

londeg=120.982025;
latdeg=23.973875;

document.write("WGS84台灣經緯度座標: (" + londeg +"," + latdeg + ")<br>" );

lon=londeg*Math.PI/180.0; 
lat=latdeg*Math.PI/180.0;


lonlat2twd97twd67(lon,lat)


document.write("本頁最後更新日期:"+document.lastModified+"<br>");

</script>
</div>
</body>
C GPX Reader Project STEP1: 20170423將WGS84經緯度座標轉TWD97(TWD67)二度分帶座標
2017/04/17,14:00

dog@wutingan-All-Series:~/lake$ g++ i.c

dog@wutingan-All-Series:~/lake$ ./a.out
將WGS84經緯度座標轉TWD97,TWD67
台灣虎子山經緯度WGS84=120.982025,23.973875 其二度分帶座標TWD97=248170.787,2652129.936  TWD67=247341.925, 2652335.811
請輸入經度(例如虎子山lon=120.982025)=120.982025
請輸入緯度(例如虎子山lat=23.973875)=23.973875
轉換結果如下
WGS84(120.982025,23.973875)
TWD97(248170.825725,2652129.977372)
TWD67(247341.887019,2652335.877557)


dog@wutingan-All-Series:~/lake$ cat i.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
int lonlat2twd97(double lon,double lat,double *x, double *y)
{
double a,b,long0,k0,dx;
double e,e2,n,nu,p;
double A,B,C,D,E,S;
double K1,K2,K3;
double K4,K5;
        a = 6378137.0;
        b = 6356752.314245;
        long0 = 121.0*M_PI/180.0;
        k0 = 0.9999;
        dx = 250000;
        e = pow(1-(b*b)/(a*a),0.5);
        e2 = pow(e,2)/(1-pow(e,2));
        n = (a-b)/(a+b);
        nu = a/pow(1-pow(e,2)*pow(sin(lat),2),0.5);
        p = lon-long0;
        A = a*(1 - n + (5/4.0)*(pow(n,2) - pow(n,3)) + (81/64.0)*(pow(n,4)  - pow(n,5)));
        B = (3*a*n/2.0)*(1 - n + (7/8.0)*(pow(n,2) - pow(n,3)) + (55/64.0)*(pow(n,4) - pow(n,5)));
        C = (15*a*pow(n,2)/16.0)*(1 - n + (3/4.0)*(pow(n,2) - pow(n,3)));
        D = (35*a*pow(n,3)/48.0)*(1 - n + (11/16.0)*(pow(n,2) - pow(n,3)));
        E = (315*a*pow(n,4)/51.0)*(1 - n);
        S = A*lat - B*sin(2*lat) + C*sin(4*lat) - D*sin(6*lat) + E*sin(8*lat);
        K1 = S*k0;
        K2 = k0*nu*sin(2*lat)/4.0;
        K3 = (k0*nu*sin(lat)*pow(cos(lat),3)/24.0) * (5.0 - pow(tan(lat),2) + 9.0*e2*pow(cos(lat),2) + 4.0*pow(e2,2)*pow(cos(lat),4));
        *y = K1 + K2*pow(p,2) + K3*pow(p,4);

        K4 = k0*nu*cos(lat);
        K5 = (k0*nu*pow(cos(lat),3)/6.0) * (1 - pow(tan(lat),2) + e2*pow(cos(lat),2));
        *x = K4*p + K5*pow(p,3) + dx;
}
int lonlat2twd67(double lon,double lat,double *x, double *y)
{
    double x97,y97;
     lonlat2twd97(lon,lat,&x97,&y97);   
    *x=x97-807.8-0.00001549*x97-0.000006521*y97;
    *y=y97+248.6-0.00001549*y97-0.000006521*x97;
}


int main()
{
    double lon,lat,londeg,latdeg;
    printf("將WGS84經緯度座標轉TWD97,TWD67\n");
        printf("台灣虎子山經緯度WGS84=120.982025,23.973875 其二度分帶座標TWD97=248170.787,2652129.936  TWD67=247341.925, 2652335.811\n");
    printf("請輸入經度(例如虎子山lon=120.982025)=");scanf("%lf",&londeg);
    printf("請輸入緯度(例如虎子山lat=23.973875)=");scanf("%lf",&latdeg);
    lon=londeg*M_PI/180.0; lat=latdeg*M_PI/180.0;

    double x97,y97,x67,y67;
     lonlat2twd97(lon,lat,&x97,&y97);   
     lonlat2twd67(lon,lat,&x67,&y67);   

    printf("轉換結果如下\n");
    printf("WGS84(%f,%f)\n",londeg,latdeg);
    printf("TWD97(%f,%f)\n",x97,y97);
    printf("TWD67(%f,%f)\n",x67,y67);

}

//參考程式設計遇上小提琴BLOG: http://blog.ez2learn.com/2009/08/15/lat-lon-to-twd97/

 

http://www.kmvs.km.edu.tw/lf/index.php?op=ViewAlbum&albumId=247&blogId=70

 

 
Accessible and Valid XHTML 1.0 Strict and CSS Powered by LifeType