public static void Main(string[] args) { try { /* -------------------------------------------------------------------- */ /* Initialize srs */ /* -------------------------------------------------------------------- */ SpatialReference src = new SpatialReference(""); src.ImportFromProj4("+proj=latlong +datum=WGS84 +no_defs"); Console.WriteLine( "SOURCE IsGeographic:" + src.IsGeographic() + " IsProjected:" + src.IsProjected() ); SpatialReference dst = new SpatialReference(""); dst.ImportFromProj4("+proj=somerc +lat_0=47.14439372222222 +lon_0=19.04857177777778 +x_0=650000 +y_0=200000 +ellps=GRS67 +units=m +no_defs"); Console.WriteLine( "DEST IsGeographic:" + dst.IsGeographic() + " IsProjected:" + dst.IsProjected() ); /* -------------------------------------------------------------------- */ /* making the transform */ /* -------------------------------------------------------------------- */ CoordinateTransformation ct = new CoordinateTransformation(src, dst); double[] p = new double[3]; p[0] = 19; p[1] = 47; p[2] = 0; ct.TransformPoint(p); Console.WriteLine("x:" + p[0] + " y:" + p[1] + " z:" + p[2]); ct.TransformPoint(p, 19.2, 47.5, 0); Console.WriteLine("x:" + p[0] + " y:" + p[1] + " z:" + p[2]); } catch (Exception e) { Console.WriteLine("Error occurred: " + e.Message); System.Environment.Exit(-1); } }
/// <summary> /// Simple coordinate reprojection /// </summary> /// <param name="EpsgIn"></param> /// <param name="EpsgOut"></param> /// <returns></returns> public static double[] ReprojectCoordinates(int EpsgIn, int EpsgOut, double x, double y, double z) { var src = new OSGeo.OSR.SpatialReference(EPSG2WKT(EpsgIn)); logger.Trace($"SOURCE IsGeographic:" + src.IsGeographic() + " IsProjected:" + src.IsProjected()); var dst = new OSGeo.OSR.SpatialReference(EPSG2WKT(EpsgOut)); logger.Trace("DEST IsGeographic:" + dst.IsGeographic() + " IsProjected:" + dst.IsProjected()); var ct = new OSGeo.OSR.CoordinateTransformation(src, dst); double[] p = new double[] { x, y, z }; logger.Trace("From: x:" + p[0] + " y:" + p[1] + " z:" + p[2]); ct.TransformPoint(p); logger.Trace("To: x:" + p[0] + " y:" + p[1] + " z:" + p[2]); return(p); }