public static void ConvertFeatureToRaster(Layer layer, out Dataset outputDataset, double rasterCellSize, string fieldName)
{
DriverUtils.RegisterGdalOgrDrivers();
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);
//Console.WriteLine("Extent: " + envelope.MaxX + " " + envelope.MinX + " " + envelope.MaxY + " " + envelope.MinY);
//Console.WriteLine("X resolution: " + x_res);
//Console.WriteLine("X resolution: " + y_res);
//Create new tiff in disk
string tempRaster = "tempZoneRaster.tif";
if (File.Exists(tempRaster))
{
File.Delete(tempRaster);
}
OSGeo.GDAL.Driver outputDriver = Gdal.GetDriverByName("GTiff");
outputDataset = outputDriver.Create(tempRaster, x_res, y_res, 1, DataType.GDT_Int32, null);
//Extrac srs from input feature
string inputShapeSrs;
SpatialReference spatialRefrence = layer.GetSpatialRef();
spatialRefrence.ExportToWkt(out inputShapeSrs);
//Assign input feature srs to outpur raster
outputDataset.SetProjection(inputShapeSrs);
//Geotransform
double[] argin = new double[] { envelope.MinX, rasterCellSize, 0, envelope.MaxY, 0, -rasterCellSize };
outputDataset.SetGeoTransform(argin);
//Set no data
Band band = outputDataset.GetRasterBand(1);
//band.SetNoDataValue(GdalUtilConstants.NoDataValue);
band.Fill(GdalUtilConstants.NoDataValue, 0.0);
//Feature to raster rasterize layer options
//No of bands (1)
int[] bandlist = new int[] { 1 };
//Values to be burn on raster (10.0)
double[] burnValues = new double[] { 10.0 };
//Dataset myDataset = Gdal.Open(outputRasterFile, Access.GA_Update);
//additional options
string[] rasterizeOptions;
//rasterizeOptions = new string[] { "ALL_TOUCHED=TRUE", "ATTRIBUTE=" + fieldName }; //To set all touched pixels into raster pixel
rasterizeOptions = new string[] { "ATTRIBUTE=" + fieldName };
//Rasterize layer
//Gdal.RasterizeLayer(myDataset, 1, bandlist, layer, IntPtr.Zero, IntPtr.Zero, 1, burnValues, null, null, null); // To burn the given burn values instead of feature attributes
//Gdal.RasterizeLayer(outputDataset, 1, bandlist, layer, IntPtr.Zero, IntPtr.Zero, 1, burnValues, rasterizeOptions, new Gdal.GDALProgressFuncDelegate(ProgressFunc), "Raster conversion");
Gdal.RasterizeLayer(outputDataset, 1, bandlist, layer, IntPtr.Zero, IntPtr.Zero, 1, burnValues, rasterizeOptions, null, null);
}
C# GDAL基础使用之矢量栅格化处理5 gdal.RasterizeLayer()
最新推荐文章于 2023-06-14 18:42:41 发布
该代码段展示了一个将GIS图层的几何特征转换为栅格数据的过程。首先,它获取图层的边界并计算分辨率。接着,创建一个临时的TIFF文件作为输出栅格,并设置其坐标系、地理变换和无数据值。然后,使用`Gdal.RasterizeLayer`方法将图层特征烧录到栅格中,指定属性字段作为像素值。
摘要由CSDN通过智能技术生成