views:

433

answers:

6

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.

+1  A: 

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.

Gergely Orosz
+1  A: 

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 } };
Ngu Soon Hui
+2  A: 

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.

Cheeso
+3  A: 

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
    }
};
Judah Himango
+1  A: 

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.

Andrew Hancox
A: 

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

Andrew Hancox