標籤:style blog http color io os ar java for
import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;public class Gpstest { final static int TABLESIZE=660*450; //#define ID(i, j) ((i) + 660 * (j)) static double[] TableX=new double[TABLESIZE]; static double[] TableY=new double[TABLESIZE]; static boolean bInitTable = false; static void InitTable() throws IOException { File filename = new File("C://Mars2Wgsnew.txt"); long lX1, lY1; String tempString; String[] tem; bInitTable = false; BufferedReader reader = new BufferedReader(new FileReader(filename)); for(int i=0;;i++){ tempString = reader.readLine(); if(tempString==null){ break; } // System.out.println(tempString); tempString=tempString.replaceAll(" ",""); tem=tempString.split(","); TableX[i] = ((double) Long.valueOf(tem[0])) / 100000.0; TableY[i] = ((double) Long.valueOf(tem[1])) / 100000.0; } reader.close(); bInitTable = true; } // Result:// 0 - ok// -1 - Table is not initialized// -2 - Latitude or Longitude is invalid static int Mars2Wgs( double x_mars, double y_mars ) { int i, j, k; double x1, y1, x2, y2, x3, y3, x4, y4, xtry, ytry, dx, dy,x_wgs,y_wgs; double t, u; if(!bInitTable) return -1; xtry = x_mars; ytry = y_mars; for( k=0; k<10; ++k ) { // 只對中國國境內資料轉換 if( xtry < 72 || xtry > 137.9 || ytry < 10 || ytry > 54.9) { return -2; } i = (int) ((xtry - 72.0) * 10.0); j = (int) ((ytry - 10.0) * 10.0); x1 = TableX[i+660*j]; y1 = TableY[i+660*j]; x2 = TableX[(i+1)+660*j]; y2 = TableY[(i+1)+660*j]; x3 = TableX[(i+1)+660*(j+1)]; y3 = TableY[(i+1)+660*(j+1)]; x4 = TableX[i+660*(j+1)]; y4 = TableY[i+660*(j+1)]; t = (xtry - 72.0 - 0.1 * i) * 10.0; u = (ytry - 10.0 - 0.1 * j) * 10.0; dx = (1.0-t)*(1.0-u)*x1 + t*(1.0-u)*x2 + t*u*x3 + (1.0-t)*u*x4 - xtry; dy = (1.0-t)*(1.0-u)*y1 + t*(1.0-u)*y2 + t*u*y3 + (1.0-t)*u*y4 - ytry; xtry = (xtry + x_mars - dx)/2.0; ytry = (ytry + y_mars - dy)/2.0; } x_wgs = xtry; y_wgs = ytry; System.out.println(x_wgs+";"+y_wgs); return 0; } public static void main(String[] args) throws IOException { double x_mars, y_mars, x_wgs, y_wgs; File fname = new File("C://2.txt"); String tempString; String[] tem; BufferedReader reader = new BufferedReader(new FileReader(fname)); int result; for(int i=0;;i++){ tempString = reader.readLine(); if(tempString==null){ break; } // System.out.println(tempString); tempString=tempString.replaceAll(" ",""); tem=tempString.split(","); x_mars = ((double) Double.valueOf(tem[0])); y_mars = ((double) Double.valueOf(tem[1])); InitTable(); result = Mars2Wgs(x_mars, y_mars); } }}
本例子程式改編自其它網友,其功能是將火星02座標轉換為WGS84座標
需要一個資料檔案Mars2Wgsnew.txt。轉換後存在幾米到十幾米的誤差
http://files.cnblogs.com/casicyuan/Mars2Wgsnew.rar
火星02座標轉換為WGS84座標