2009-12-10 88 views
5

我有一些JavaScript代碼,我想將其轉換爲C#。我不知道如何構建這個結構的最佳方式,或者是否有簡單的方法來轉換代碼。如何將JavaScript中的地理座標轉換爲C中的地理座標#

此代碼示例如下所示。

// ellipse parameters 
var e = { WGS84: { a: 6378137,  b: 6356752.3142, f: 1/298.257223563 }, 
      Airy1830: { a: 6377563.396, b: 6356256.910, f: 1/299.3249646 }, 
      Airy1849: { a: 6377340.189, b: 6356034.447, f: 1/299.3249646 } }; 

// helmert transform parameters 
var h = { WGS84toOSGB36: { tx: -446.448, ty: 125.157, tz: -542.060, // m 
          rx: -0.1502, ry: -0.2470, rz: -0.8421, // sec 
          s: 20.4894 },        // ppm 
      OSGB36toWGS84: { tx: 446.448, ty: -125.157, tz: 542.060, 
          rx: 0.1502, ry: 0.2470, rz: 0.8421, 
          s: -20.4894 } }; 


function convertOSGB36toWGS84(p1) { 
    var p2 = convert(p1, e.Airy1830, h.OSGB36toWGS84, e.WGS84); 
    return p2; 
} 

完整的代碼可以從以下地址下載: Javascript Grid Code

編輯:謝謝大家,到目前爲止對你的幫助;我想第二個要求是,鏈接中的代碼提醒可以被轉換。片段的焦點在匿名類型上。

+0

我認爲這是機械的,但不是自動的。 – Cheeso 2009-12-10 15:14:32

+0

如果我們的答案中的任何一個能幫助解決您的問題,請將其標記爲答案。 – 2009-12-16 17:02:33

+0

幫助但未解決。似乎沒有人看過我想轉換的完整源代碼。 – Coolcoder 2009-12-17 08:39:04

回答

0

的我工作的公司剛剛開放的源庫來做到這一點:http://code.google.com/p/geocoordconversion/

+0

這仍然是一個開源庫嗎?有權限設置阻止我查看項目。 – Web 2011-06-21 15:04:48

+0

不知道那裏發生了什麼,應該可以再次使用。 – 2011-06-22 12:25:42

+6

該解決方案過於具體,無法匹配大多數來此的人的頭銜。 – 2013-03-16 11:46:09

1

我不認爲有任何真正簡單的方法來轉換代碼,因爲JavaScript是一個更鬆散的語言。在你的代碼中,你正在使用JavaScript的即時對象生成方式,而這在C#中是無法實現的。

您可能需要聲明您在JS中使用的相同類(但不是強制這樣做)並重寫代碼,我不認爲有一種簡單的方法。

從我個人的角度來看,儘管在C#中重寫代碼可能會有幫助。如果你是一個C#開發人員,你可能會發現一個更好的算法或更簡單的代碼設計,如果你是一個新手,雖然它會幫助你學習語言。

1

最好的希望是使用Dictionary加上一個新的類def。

public class abf 
{ 
    public double a 
    {get;set;} 
    public double b 
    {get;set;} 
    public double f 
    {get;set;} 
} 

public class txtytz 
{ 
    //repeat for tx ty tz etc 
} 

// 

public Dictionary<string, abf> e; 
public Dictionary<string, txtytz> h; 

這裏是你如何使用它:

abf wgs84Result=e["WGS84"]; // will yield { a: 6378137,  b: 6356752.3142, f: 1/298.257223563 } 

txtytz OSGB36toWGS84Result=h["OSGB36toWGS84"]; // will yield { tx: 446.448, ty: -125.157, tz: 542.060, 
          rx: 0.1502, ry: 0.2470, rz: 0.8421, 
          s: -20.4894 } }; 
7

JavaScript的使用匿名類型。你可以在C#中做同樣的事情,但使用命名類型會更清晰。

例如,JavaScript的是這樣的:

// ellipse parameters 
var e = { WGS84: { a: 6378137,  b: 6356752.3142, f: 1/298.257223563 }, 
      Airy1830: { a: 6377563.396, b: 6356256.910, f: 1/299.3249646 }, 
      Airy1849: { a: 6377340.189, b: 6356034.447, f: 1/299.3249646 } }; 

..represents橢圓。你可以這樣做在C#像這樣:

// ellipse class 
public class EllipseParameters { 
    public double a {get; set;} 
    public double b {get; set;} 
    public double f {get; set;} 
} 

public Ellipses { 
    public EllipseParameters WGS84 {get;set;} 
    public EllipseParameters Airy1830 {get;set;} 
    public EllipseParameters Airy1849 {get;set;} 
} 

Ellipses e = new Ellipses { 
    WGS84 = new EllipseParameters { a = 6378137,  b= 6356752.3142, f = 1/298.257223563 }, 
    Airy1830 = new EllipseParameters { a= 6377563.396, b= 6356256.910, f= 1/299.3249646 }, 
    Airy1849 = new EllipseParameters { a= 6377340.189, b= 6356034.447, f= 1/299.3249646 } 
}; 

但代替省略號類的,你可能要一本字典的方法,像這樣:

var e = new Dictionary<String,EllipseParameters>(); 
e.Add("WGS84", new EllipseParameters { a = 6378137,  b= 6356752.3142, f = 1/298.257223563 }); 
e.Add("Airy1830", new EllipseParameters { a= 6377563.396, b= 6356256.910, f= 1/299.3249646 }); 
e.Add("Airy1849", new EllipseParameters { a= 6377340.189, b= 6356034.447, f= 1/299.3249646 }); 

你會採取與同樣的方法helmert轉換類。

9

您的JavaScript片段正在創建匿名類型。你可以做同樣的事情在C#:

var e = new 
{ 
    WGS84 = new { a = 6378137, b = 6356752.3142, f = 1/298.257223563 }, 
    Airy1830 = new { a = 6377563.396, b = 6356256.910, f = 1/299.3249646 }, 
    Airy1849 = new { a = 6377340.189, b = 6356034.447, f = 1/299.3249646 } 
}; 

var h = new 
{ 
    WGS84toOSGB36 = new 
    { 
     tx = -446.448, ty = 125.157, tz = -542.060, // m 
     rx = -0.1502, ry = -0.2470, rz = -0.8421, // sec 
     s = 20.4894         // ppm 
    },        
    OSGB36toWGS84 = new 
    { 
     tx = 446.448, 
     ty = -125.157, 
     tz = 542.060, 
     rx = 0.1502, 
     ry = 0.2470, 
     rz = 0.8421, 
     s = -20.4894 
    } 
}; 
1

有人寫C#類WGS84轉換爲OSGB36和緯度經度到NE,您可以下載它here 我檢查了它,它似乎只是工作精細。

0

我通過M2H使用這個在線轉換器。

http://www.m2h.nl/files/js_to_c.php

+0

該工具適用於UnityScript(雖然統一調用JavaScript有時候JavaScript並非真正的JavaScript) – 2015-11-02 22:16:30

0
function Update() { 

    GetComponent.<Renderer>().material.color.r = red; 
    GetComponent.<Renderer>().material.color.b = blue; 
    GetComponent.<Renderer>().material.color.g = green; 
    GetComponent.<Renderer>().material.color.a = alpha; 
    GetComponent.<Renderer>().material.mainTextureOffset = Vector2(parseFloat(xOffset),parseFloat(yOffset));mipazirad 

    GetComponent.<Renderer>().material.mainTextureScale = Vector2(parseFloat(xTiling),parseFloat(yTiling)); 
    moshkhas mikonim 
    if(selectedShader == 0) 
     GetComponent.<Renderer>().material.shader = Shader.Find("Diffuse"); 
    else if(selectedShader == 1) 
     GetComponent.<Renderer>().material.shader = Shader.Find("Bumped Diffuse"); 
    else if(selectedShader == 2) 
     GetComponent.<Renderer>().material.shader = Shader.Find("Bumped Specular"); 

} 
0
function Start() { 
    var theMesh : Mesh; 

    theMesh = this.transform.GetComponent(MeshFilter).mesh as Mesh; 
    var theUVs : Vector2[] = new Vector2[theMesh.uv.Length]; 

    theUVs = theMesh.uv; 
    theUVs[4] = Vector2(0.5, 1.0); 
    theUVs[5] = Vector2(1.0, 1.0); 
    theUVs[8] = Vector2(0.5, 0.5); 
    theUVs[9] = Vector2(1.0, 0.5); 
    theMesh.uv = theUVs; 
} 
+0

雖然此代碼可能回答此問題,但提供有關如何解決問題和/或爲何解決問題的其他上下文會提高答案的長期價值。 – 2017-02-05 09:17:24