/// <summary> /// 通过Feature设置栅格无效值 /// </summary> /// <param name="polyFeat"></param> /// <param name="inDs"></param> public static void setNoData(Feature polyFeat, Dataset inDs) { //注册库 Gdal.AllRegister(); Ogr.RegisterAll(); //Get Geometry & Envelope Geometry Geom = polyFeat.GetGeometryRef(); Envelope enve = new Envelope(); Geom.GetEnvelope(enve); //Get all Transform double[] GeoTransform = new double[6]; inDs.GetGeoTransform(GeoTransform); // Envelope Conver to ImgSpace -- off to end int xoff, yoff, xend, yend; StaticTools.geoToImageSpace(GeoTransform, enve.MinX, enve.MaxY, out xoff, out yoff); StaticTools.geoToImageSpace(GeoTransform, enve.MaxX, enve.MinY, out xend, out yend); int xsize = xend - xoff; int ysize = yend - yoff; //Get The Values double[] rasterValues = new double[xsize * ysize]; inDs.GetRasterBand(1).ReadRaster(xoff, yoff, xsize, ysize, rasterValues, xsize, ysize, 0, 0); //Get new Transform double[] NewTransform = GeoTransform; NewTransform[0] = enve.MinX; NewTransform[3] = enve.MaxY; //无效值,特定值 double noData = 9999; for (int iline = 0; iline < ysize; iline++) { for (int iPixel = 0; iPixel < xsize; iPixel++) { Geometry poiGeom = new Geometry(wkbGeometryType.wkbPoint); double x, y; StaticTools.imageToGeoSpace(NewTransform, iPixel, iline, out x, out y); poiGeom.AddPoint_2D(x, y); if (poiGeom.Within(Geom)) { rasterValues[iline * xsize + iPixel] = noData; } poiGeom.Dispose(); } } inDs.GetRasterBand(1).WriteRaster(xoff, yoff, xsize, ysize, rasterValues, xsize, ysize, 0, 0); inDs.Dispose(); polyFeat.Dispose(); }