gdal输出带坐标信息的tif图像

string datafilepath = @"D:\CAMVS\CAMVS\bin\Debug\Data\20110804\NumbericFile\wrfout_d01_2011-08-24_18_00_00.nc";
            // string datafilepath1 = @"D:\CAMVS\CAMVS\bin\Debug\Data\20110804\NumbericFile\wrfout_d01_2011-08-24_18_00_temp.nc";

            string var = "T";
            string varpath = "NETCDF:\"" + datafilepath + "\":" + var;
            string varpath1 = "ss.tif";
            Gdal.AllRegister();
            OSGeo.OGR.Ogr.RegisterAll();
            Dataset ds = Gdal.Open(varpath, Access.GA_ReadOnly);

//从shp数据中获取投影坐标信息
            OSGeo.OGR.Driver poDriver = OSGeo.OGR.Ogr.GetDriverByName("ESRI Shapefile");
            OSGeo.OGR.DataSource src = poDriver.Open(@"D:\lambert\country2.shp", 0);
            OSGeo.OSR.SpatialReference sprfc = src.GetLayerByIndex(0).GetSpatialRef();
            string wktstr = "";
            sprfc.ExportToWkt(out wktstr);


            string lambert = wktstr;
            GeographicCoordinateSystem fromCS = GeographicCoordinateSystem.WGS84;
            IProjectedCoordinateSystem toCS = CoordinateSystemWktReader.Parse(lambert) as IProjectedCoordinateSystem;

//计算左上角的空间地理坐标信息
            double[] xy = new double[] { 110.399994, 39.900005 };
            List<double[]> pts = new List<double[]>();
            pts.Add(xy);
            List<double[]> results = new List<double[]>();
            PtsTransform.PtsToPts(fromCS, toCS, pts, out results);
            double x = 0;
            double y = 0;
            x = results[0][0];
            y = results[0][1];
            double[] bound = ComputeBound(x, y, 387, 250, 36000);
            x = bound[0];
            y = bound[3];
            double[] arg = new double[6] { x, 36000, 0.0, y, 0.0, 36000 };

           // ds.SetGeoTransform(arg);
           // ds.SetProjection(wktstr);
            Band band = ds.GetRasterBand(1);
//数据的类型如果设置成float生成的图像,可能会导致在设置投影时出错,导致arcgis也不能够识别这个图像
            double[] buffer = new double[band.XSize * band.YSize];
            NumberFile.GetVarBandData(datafilepath, "T", 1, band.XSize, band.YSize, buffer);
            string[] option = { "INTERLEAVE=PIXEL" };
            Dataset todataset = Gdal.GetDriverByName("GTiff").Create(varpath1, band.XSize, band.YSize, 1, DataType.GDT_Float32, option);
         //设置起始点坐标信息及栅格大小和投影坐标系
           todataset.SetGeoTransform(arg);
            todataset.SetProjection(wktstr);
            todataset.GetRasterBand(1).WriteRaster(0, 0, band.XSize, band.YSize, buffer, band.XSize, band.YSize, 0, 0);
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值