public static void Rasterize(string inputFeature, string referRaster, string outRaster, string fieldName = "") { //Register the raster drivers Gdal.AllRegister(); //Register the vector drivers Ogr.RegisterAll(); //Open referRaster Dataset _referRaster = Gdal.Open(referRaster, Access.GA_ReadOnly); //Get geoTransform Args double[] _geoTransform = new double[6]; _referRaster.GetGeoTransform(_geoTransform); // Define pixel_size and NoData value of new raster //int rasterCellSize = cellSize; double _xCellSize = _geoTransform[1]; double _yCellSize = -_geoTransform[5]; const double noDataValue = -10000; string outputRasterFile = outRaster; //Reading the vector data DataSource dataSource = Ogr.Open(inputFeature, 0); Layer layer = dataSource.GetLayerByIndex(0); Envelope envelope = new Envelope(); layer.GetExtent(envelope, 0); //Compute the out raster cell resolutions int x_res = _referRaster.RasterXSize; int y_res = _referRaster.RasterYSize; //Console.WriteLine("Extent: " + envelope.MaxX + " " + envelope.MinX + " " + envelope.MaxY + " " + envelope.MinY); //Console.WriteLine("X resolution: " + x_res); //Console.WriteLine("X resolution: " + y_res); //Check if output raster exists & delete (optional) if (File.Exists(outputRasterFile)) { File.Delete(outputRasterFile); } //Create new tiff DataType dType = _referRaster.GetRasterBand(1).DataType; //OSGeo.GDAL.Driver outputDriver = Gdal.GetDriverByName("GTiff"); OSGeo.GDAL.Driver outputDriver = Gdal.GetDriverByName("HFA"); Dataset outputDataset = outputDriver.Create(outputRasterFile, x_res, y_res, 1, dType, null); //Extrac srs from input feature string inputShapeSrs; OSGeo.OSR.SpatialReference spatialRefrence = layer.GetSpatialRef(); if (spatialRefrence != null) { spatialRefrence.ExportToWkt(out inputShapeSrs); //Assign input feature srs to outpur raster outputDataset.SetProjection(inputShapeSrs); } //Geotransform outputDataset.SetGeoTransform(_geoTransform); //Set no data Band band = outputDataset.GetRasterBand(1); band.SetNoDataValue(noDataValue); //close tiff outputDataset.FlushCache(); outputDataset.Dispose(); //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 }; rasterizeOptions = new string[] { }; //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(myDataset, 1, bandlist, layer, IntPtr.Zero, IntPtr.Zero, 1, burnValues, rasterizeOptions, new Gdal.GDALProgressFuncDelegate(ProgressFunc), "Raster conversion"); Gdal.RasterizeLayer( myDataset, //NEW inDS 1, //BAND bandlist, //int[] bandlist = new int[] { 1 };Band数量 layer, // 待转 IntPtr.Zero, IntPtr.Zero, 1, burnValues, //Values to be burn on raster (10.0) rasterizeOptions, new Gdal.GDALProgressFuncDelegate(ProgressFunc), "Raster conversion" ); myDataset.FlushCache(); myDataset.Dispose(); }
public bool TransToRaster(string strAtrrName, string dstRasterFile, double[] dEnvelopes, double dPixCellSize = 0.0005, double dEnvelope_offsetX = 0, double dEnvelope_offsetY = 0, string driverName = "GTiff", DataType typeData = DataType.GDT_Float32) { //载入矢量数据 if (this._GdalRead == null || this._GdalRead.Layer == null) { Console.WriteLine("vectorFile open failed."); return(false); } if (dEnvelopes == null || dEnvelopes.Length == 0) { //计算图像大小-按数据边界 Envelope envelope = new Envelope(); _GdalRead.Layer.GetExtent(envelope, 1); dEnvelopes = new double[] { envelope.MinX, envelope.MaxX, envelope.MinY, envelope.MaxY }; envelope.Dispose(); } //计算图像大小-按数据边界(dEnvelopes:MinX,MaxX,MinY,MaxY) int nXSize = (int)((dEnvelopes[1] - dEnvelopes[0] + 2 * dEnvelope_offsetX) / dPixCellSize); int nYSize = (int)((dEnvelopes[3] - dEnvelopes[2] + 2 * dEnvelope_offsetY) / dPixCellSize); //计算六参数 double[] adfGeoTransform = new double[] { dEnvelopes[0] - dEnvelope_offsetX, dPixCellSize, 0, dEnvelopes[3] + dEnvelope_offsetY, 0, -dPixCellSize }; //创建目标栅格数据 Dataset poDstDataset = Gdal.GetDriverByName(driverName).Create(dstRasterFile, nXSize, nYSize, 1, typeData, null); if (poDstDataset == null) { Console.WriteLine(dstRasterFile + " create failed."); return(false); } poDstDataset.SetGeoTransform(adfGeoTransform); poDstDataset.SetSpatialRef(this._GdalRead.Layer.GetSpatialRef()); Band poBand = poDstDataset.GetRasterBand(1); poBand.SetNoDataValue(_NoDataValue); //RasterizeLayer矢量创建栅格 int[] nBands = new int[] { 1 }; double[] urnValues = new double[] { 1 }; List <string> lstOptions = new List <string>() { }; if (strAtrrName != "") { lstOptions.Add("ATTRIBUTE:" + strAtrrName); } //lstOptions.Add("ALL_TOUCHED=TRUE"); int nRes = Gdal.RasterizeLayer(poDstDataset, 1, nBands, this._GdalRead.Layer, IntPtr.Zero, IntPtr.Zero, 1, urnValues, lstOptions.ToArray(), null, null); // 写入影像 poBand.FlushCache(); poDstDataset.FlushCache(); // 释放资源 关闭图像 poBand.Dispose(); poDstDataset.Dispose(); return(true); }
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); }
//http://www.gisremotesensing.com/2015/09/vector-to-raster-conversion-using-gdal-c.html public static void Rasterize(string inputFeature, string outRaster, string fieldName, int cellSize) { // Define pixel_size and NoData value of new raster int rasterCellSize = cellSize; const double noDataValue = -9999; string outputRasterFile = outRaster; //Register the vector drivers Ogr.RegisterAll(); //Reading the vector data DataSource dataSource = Ogr.Open(inputFeature, 0); var count = dataSource.GetLayerCount(); Layer layer = dataSource.GetLayerByIndex(0); var litems = layer.GetFeatureCount(0); var lname = layer.GetName(); 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); //Register the raster drivers Gdal.AllRegister(); //Check if output raster exists & delete (optional) if (File.Exists(outputRasterFile)) { File.Delete(outputRasterFile); } //Create new tiff OSGeo.GDAL.Driver outputDriver = Gdal.GetDriverByName("GTiff"); Dataset outputDataset = outputDriver.Create(outputRasterFile, x_res, y_res, 1, DataType.GDT_Float64, 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(noDataValue); //close tiff outputDataset.FlushCache(); outputDataset.Dispose(); //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(myDataset, 1, bandlist, layer, IntPtr.Zero, IntPtr.Zero, 1, burnValues, rasterizeOptions, new Gdal.GDALProgressFuncDelegate(ProgressFunc), "Raster conversion"); }
public static void CLipRasterWithVector(string InputPolygonFile, string InputRasterFile, string OutputRasterFile) { var rasterCellSize = 1000; var noDataValue = -9999; var fieldName = "fid"; //Reading the vector data var dataSource = Ogr.Open(InputPolygonFile, 0); var layer = dataSource.GetLayerByIndex(0); var 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); //Check if output raster exists & delete (optional) if (File.Exists(OutputRasterFile)) { File.Delete(OutputRasterFile); } //Create new tiff var outputDriver = Gdal.GetDriverByName("GTiff"); var outputDataset = outputDriver.Create(OutputRasterFile, x_res, y_res, 1, DataType.GDT_Float64, null); // Extract srs from input feature and Assign to outpur raster string inputShapeSrs; var spatialRefrence = layer.GetSpatialRef(); spatialRefrence.ExportToWkt(out inputShapeSrs, null); outputDataset.SetProjection(inputShapeSrs); //Set Geotransform var argin = new double[] { envelope.MinX, rasterCellSize, 0, envelope.MaxY, 0, -rasterCellSize }; outputDataset.SetGeoTransform(argin); //Set no data Band band = outputDataset.GetRasterBand(1); band.SetNoDataValue(noDataValue); //close tiff outputDataset.FlushCache(); outputDataset.Dispose(); //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(myDataset, 1, bandlist, layer, IntPtr.Zero, IntPtr.Zero, 1, burnValues, rasterizeOptions, GdalUtils.GDalProgress, "Raster conversion"); }