Using ProjNET for point reprojection

By Bill Thorp

Are you – yes, you– firing up ArcObjects code just to do simple point reprojection?  Check out Proj.NET.  Grab the EPSG-code reader and point projection code from the very helpful FAQs and you’ll get something like this: [edit: fixed, thanks Morten ... definitely read the comments, this code was meant to show how easy things could be... its definitely not the ProjNet best practices]

public static double[] projectFromWgs84(double X, double Y, int destinationEpsgCode)
{
   double[] input = new double[] { X, Y };
   SC.ICoordinateSystem destProj = SridReader.GetCSbyID(destinationEpsgCode);
   SC.ICoordinateSystem sourceProj = SridReader.GetCSbyID(4236);
   SCT.CoordinateTransformationFactory ctf = new SCT.CoordinateTransformationFactory();
   SCT.IMathTransform xForm = ctf.CreateFromCoordinateSystems(sourceProj, destProj).MathTransform;
   return xForm.Transform(input);
}

One Response to “Using ProjNET for point reprojection”

  1. Morten Says:

    Just a note on this. The GetCSbyID is a very expensive call. It has to go through a big CSV file to find the Well-Known text string and then afterwards parse it into a CS object.
    The above code would be very ineffective if you were to transform many points.
    An alternative could be to create a static sourceProj object and parse in destProj as a parameter instead:

    private static SC.ICoordinateSystem sourceProj;
    public static double[] projectFromWgs84(double X, double Y, SC.ICoordinateSystem destProj)
    {
    double[] input = new double[] { X, Y };
    if(sourceProj==null)
    sourceProj = SridReader.GetCSbyID(4236);
    SCT.CoordinateTransformationFactory ctf = new SCT.CoordinateTransformationFactory();
    SCT.IMathTransform xForm = ctf.CreateFromCoordinateSystems(sourceProj, destProj).MathTransform;
    return xForm.Transform(input);
    }

    Or even better, parse in the IMathTransform as a parameter.
    (btw, your sample doesn’t compile. You forgot to rename source and googProj)

Leave a Reply