/// <summary> /// 栅格重建,srPath输入,dsPath输出,1为1=>1, 2为1=>4, 3为1=>9, /// </summary> /// <param name="srPath"></param> /// <param name="dsPath"></param> /// <param name="level"></param> public static void ImgReProject(string srPath, string dsPath, int level = 2) { Console.WriteLine("ReprojectImage开始,lever 2"); double[] geoTran = new double[6]; double[] geoTran2 = new double[6]; //读 OSGeo.GDAL.Gdal.AllRegister(); Dataset ds = Gdal.Open(srPath, Access.GA_ReadOnly); ds.GetGeoTransform(geoTran); geoTran2 = geoTran; geoTran2[1] = geoTran[1] / level; geoTran2[5] = geoTran[5] / level; //建 OSGeo.GDAL.Driver gdalDriver = Gdal.GetDriverByName("HFA"); Dataset ods = gdalDriver.Create(dsPath, ds.RasterXSize * level, ds.RasterYSize * level, 1, DataType.GDT_Float32, null); ods.SetProjection(ds.GetProjection()); ods.SetGeoTransform(geoTran2); ods.GetRasterBand(1).SetNoDataValue(-999999); //写 Gdal.ReprojectImage(ds, ods, null, null, ResampleAlg.GRA_NearestNeighbour, 0, 0.02, null, null); ds.Dispose(); ods.Dispose(); Console.WriteLine("ReprojectImage完成"); }
private static void StartClippingProcess(Layer layer, string rasterName, double rasterCellSize, out Dataset outAlignedRaster) { DriverUtils.RegisterGdalOgrDrivers(); //Extrac srs from input feature string inputShapeSrs; SpatialReference spatialRefrence = layer.GetSpatialRef(); spatialRefrence.ExportToWkt(out inputShapeSrs); Envelope envelope = new Envelope(); layer.GetExtent(envelope, 0); //Compute the out raster cell resolutions int x_res = Convert.ToInt32((envelope.MaxX - envelope.MinX) / rasterCellSize); int y_res = Convert.ToInt32((envelope.MaxY - envelope.MinY) / rasterCellSize); Dataset oldRasterDataset = Gdal.Open(rasterName, Access.GA_ReadOnly); //No of bands in older dataset int rasterBands = oldRasterDataset.RasterCount; //Create new tiff in disk string tempRaster = "tempValueRaster.tif"; if (File.Exists(tempRaster)) { File.Delete(tempRaster); } OSGeo.GDAL.Driver outputDriver = Gdal.GetDriverByName("GTiff"); outAlignedRaster = outputDriver.Create(tempRaster, x_res, y_res, rasterBands, DataType.GDT_Float32, null); //Geotransform double[] argin = new double[] { envelope.MinX, rasterCellSize, 0, envelope.MaxY, 0, -rasterCellSize }; outAlignedRaster.SetGeoTransform(argin); //Set no data for (int rasBand = 1; rasBand <= rasterBands; rasBand++) { Band band = outAlignedRaster.GetRasterBand(rasBand); band.Fill(GdalUtilConstants.NoDataValue, 0.0); } //band.SetNoDataValue(GdalUtilConstants.NoDataValue); outAlignedRaster.SetProjection(inputShapeSrs); string[] reprojectOptions = { "NUM_THREADS = ALL_CPUS", "WRITE_FLUSH = YES" }; Gdal.ReprojectImage(oldRasterDataset, outAlignedRaster, null, inputShapeSrs, ResampleAlg.GRA_NearestNeighbour, 0.0, 0.0, null, null, reprojectOptions); //flush cache oldRasterDataset.FlushCache(); oldRasterDataset.Dispose(); }
/// <summary> /// Scales down query dataset to the tile dataset. /// </summary> /// <param name="queryDataset">Source dataset.</param> /// <param name="tileDataset">Destination dataset.</param> /// <param name="resampling">Resampling method.</param> private static void ScaleQueryToTile(Dataset queryDataset, Dataset tileDataset, ResampleAlg resampling) { queryDataset.SetGeoTransform(new[] { 0.0, Convert.ToSingle(tileDataset.RasterXSize) / queryDataset.RasterXSize, 0.0, 0.0, 0.0, Convert.ToSingle(tileDataset.RasterXSize) / queryDataset.RasterXSize }); tileDataset.SetGeoTransform(new[] { 0.0, 1.0, 0.0, 0.0, 0.0, 1.0 }); Gdal.ReprojectImage(queryDataset, tileDataset, null, null, resampling, 0.0, 0.0, null, null, null); }
// 将给定Tif转换为Web Mercator投影 public void TransformTiff(Dataset ds, double[] VerticeX, double[] VerticeY, string FilePath, ResampleAlg eResampleMethod) { //获取Web墨卡托坐标系 SpatialReference Mercator = new SpatialReference(""); Mercator.ImportFromEPSG(3857); // Web Mercator Mercator.SetMercator(0d, 0d, 1d, 0d, 0d); string MercatorWkt; Mercator.ExportToWkt(out MercatorWkt); //原栅格坐标信息 SpatialReference Raster_spf = new SpatialReference(ds.GetProjectionRef()); //影像四个顶点投影转换 CoordinateTransformation coordinateTrans = Osr.CreateCoordinateTransformation(Raster_spf, Mercator); coordinateTrans.TransformPoints(4, VerticeX, VerticeY, null); //VerticeX和VerticeY存储的是影像四个顶点坐标 coordinateTrans.Dispose(); //计算重投影后栅格顶点坐标 double dbMinx = 0; double dbMaxx = 0; double dbMiny = 0; double dbMaxy = 0; dbMinx = Math.Min(Math.Min(Math.Min(VerticeX[0], VerticeX[1]), VerticeX[2]), VerticeX[3]); dbMaxx = Math.Max(Math.Max(Math.Max(VerticeX[0], VerticeX[1]), VerticeX[2]), VerticeX[3]); dbMiny = Math.Min(Math.Min(Math.Min(VerticeY[0], VerticeY[1]), VerticeY[2]), VerticeY[3]); dbMaxy = Math.Max(Math.Max(Math.Max(VerticeY[0], VerticeY[1]), VerticeY[2]), VerticeY[3]); //计算新的仿射变换参数 double[] newTransform = new double[6]; newTransform[0] = dbMinx; //左上角点x坐标 newTransform[3] = dbMaxy; //左上角点y坐标 newTransform[1] = 100; //像素宽度 newTransform[5] = -100; //像素高度 //计算大小 int width = (int)Math.Ceiling(Math.Abs(dbMaxx - dbMinx) / 100.0); int height = (int)Math.Ceiling(Math.Abs(dbMaxy - dbMiny) / 100.0); //创建新的栅格影像 OSGeo.GDAL.Driver pGDalDriver = Gdal.GetDriverByName("GTiff"); Dataset poDataset = pGDalDriver.Create(FilePath, width, height, 1, DataType.GDT_Float32, null); poDataset.SetGeoTransform(newTransform); poDataset.SetProjection(MercatorWkt); //重投影 Gdal.ReprojectImage(ds, poDataset, ds.GetProjectionRef(), MercatorWkt, eResampleMethod, 0, 0, new Gdal.GDALProgressFuncDelegate(ProgressFunc), null, null); //设置NoData值 Band band = poDataset.GetRasterBand(1); band.SetNoDataValue(-10000000); poDataset.FlushCache(); poDataset.Dispose(); }