如何将 Javascript 中的地理坐标转换为 C# 中的地理坐标

发布于 2024-08-14 01:16:07 字数 1155 浏览 1 评论 0原文

我有一些 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 网格代码

编辑: 谢谢大家远方寻求你的帮助;我猜第二个要求是链接中代码的提醒可以转换。该片段的重点是匿名类型。

I have got some javascript code and I'd like to convert this to C#. I have no idea of the best way to structure this or if there is an easy way to convert the code.

A sample of this code is shown below.

// 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;
}

The full code can be downloaded from:
Javascript Grid Code

EDIT: Thank you all so far for your help; I guess the second requirement is that the reminder of the code in the link can be converted. The focus of the snippet was on the Anonymous Types.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

草莓味的萝莉 2024-08-21 01:16:07

您的 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
    }
};

Your JavaScript snippet is creating anonymous types. You can do the same thing in 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
    }
};
初熏 2024-08-21 01:16:07

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   } };

..代表省略号。您可以在 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   }
};

但是您可能需要字典方法来代替 Ellipses 类,如下所示:

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 转换类采取相同的方法。

The javascript uses anonymous types. You could do the same in C# but it would be clearer to use named types.

for example, the javascript like this:

// 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 ellipses. You might do this in C# like so:

// 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   }
};

But in place of the Ellipses class, you might want a dictionary approach, something like this:

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   });

You would take the same approach with the helmert transform classes.

画中仙 2024-08-21 01:16:07

我认为没有任何真正简单的方法来转换代码,因为 javascript 是一种更宽松的语言。更重要的是,在您的代码中,您使用了 javascript 的动态对象生成功能,而在 C# 中则无法使用。

您可能需要声明在 JS 中使用的相同类(但不是被迫这样做)并重写代码,我认为没有简单的方法。

从我个人的角度来看,用 C# 从头开始​​重写代码可能会有所帮助。如果您是经验丰富的 C# 开发人员,您可能会找到更好的算法或更简单的代码设计,但如果您是新手,它会帮助您学习该语言。

I don't think there's any real easy way to convert the code as javascript is a much looser language. Whatsmore in your code you're using javascript's on-the-fly object generation that you couldn't in C#.

You probably need to declare the same classes you use in JS (but weren't forced to do so) and rewrite the code, I don't think there's a simple way.

From my personal perspective though it would probably be helpful to rewrite the code from scratch in C#. If you're an expreienced C# developer you might find a better algorithm or simpler design for the code, if you're a newbie though it would help you learn the language.

饮湿 2024-08-21 01:16:07

最好的希望是将 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 } };

The best hope is to use Dictionary combined with a new class 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;

Here's how you can use it:

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 } };
许久 2024-08-21 01:16:07

有人写了c#类来将WGS84转换为OSGB36,经纬度转换为NE,你可以下载它此处
我已经检查过了,它似乎工作得很好。

Someone has written a c# class to convert WGS84 to OSGB36 and lat lon to NE, you can download it here
I've checked it and it seems to work just fine.

静赏你的温柔 2024-08-21 01:16:07

我工作的公司刚刚开源了一个库来做到这一点:http://code.google .com/p/geocoordconversion/

The company I work for have just open sourced a library to do exactly this: http://code.google.com/p/geocoordconversion/

初见终念 2024-08-21 01:16:07

我使用的是 M2H 的在线转换器。

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

I use this online convertor by M2H.

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

他夏了夏天 2024-08-21 01:16:07
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");

}
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");

}
妳是的陽光 2024-08-21 01:16:07
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;
}
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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文