GDAL创建坐标系与坐标转换

using OSGeo.GDAL;
using OSGeo.OSR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chapter02
{
    class Program
    {
        static void Main(string[] args)
        {
            SetGCS();

            Console.WriteLine("\n" + "坐标转换:");

            CoordinateTransform();


            Console.ReadKey();
        }

       static void SetGCS()
        {
            SpatialReference osr = new SpatialReference("");

            //设置地理坐标系
            //osr.SetGeogCS("My GCS",
            //    "WGS_1984",
            //    "My WGS84 Spheroid",
            //    Osr.SRS_WGS84_SEMIMAJOR,
            //    Osr.SRS_WGS84_INVFLATTENING,
            //    "Greenwich", 0.0,
            //    "degree",
            //    0.017453925199433);

            osr.SetWellKnownGeogCS("WGS84");

            //osr.SetWellKnownGeogCS("EPSG:4326");

            //osr.SetFromUserInput("");

            string strWkt;
            osr.ExportToWkt(out strWkt);
            Console.WriteLine("普通WKT:");
            Console.WriteLine(strWkt);
            Console.WriteLine();

            string strPrettyWkt;
            osr.ExportToPrettyWkt(out strPrettyWkt, 0);
            Console.WriteLine("美观WKT:");
            Console.WriteLine(strPrettyWkt);
        }

        static void CoordinateTransform()
       {
           //string strXian80 = "+proj=tmerc+lat_0=0+lon_0=117 +k=1+ x_0=39500000+y_0=0 +ellps=";
           //strXian80 += "IAU76+towgs84=34.65192983,-69.97976937,-69.52875538,-0.56104022,";
           //strXian80 += "-1.34050334,1.9067841,-0.27446825 +units=m +no_defs";

           string filePath = @"E:\TEMP\project\123.tif";
           Gdal.AllRegister();
           Dataset ds = Gdal.Open(filePath, Access.GA_ReadOnly);
           string temp = ds.GetProjectionRef();
           Console.WriteLine(temp);


           SpatialReference oXian80 = new SpatialReference(temp);
           SpatialReference oLatLong;
            
           //构造投影坐标系
           //oXian80.SetFromUserInput(strXian80);

            //获取该投影坐标系统中的地理坐标系
           oLatLong = oXian80.CloneGeogCS();

            //构造一个从UTM投影坐标系统到地理坐标系统的转换关系
           CoordinateTransformation ct = new CoordinateTransformation(oXian80, oLatLong);
            if(ct==null)
            {
                Console.WriteLine("构造坐标转换关系失败");
                return;
            }

            double[] dX = new double[2] {39464667.861,39458907.868 };
            double[] dY = new double[2] { 4441766.356, 4444406.349 };
            double[] dZ = new double[2] { 0, 0 };

            Console.WriteLine("转换前:");
            Console.WriteLine("1:({0},{1})", dX[0], dY[0]);
            Console.WriteLine("2:({0},{1})", dX[1], dY[1]);
            
            ct.TransformPoints(2, dX, dY, dZ);

            Console.WriteLine("转换后:");
            Console.WriteLine("1:({0},{1})", dX[0], dY[0]);
            Console.WriteLine("2:({0},{1})", dX[1], dY[1]);

        
        }
    }
}

效果:

GDAL创建坐标系与坐标转换_第1张图片


你可能感兴趣的:(C/C++/C#与RS,GIS技术)