public IRaster Surface(IRaster raster) { int nr = raster.NumRows; int nc = raster.NumColumns; for (int row = 0; row < nr; row++) { for (int col = 0; col < nc; col++) { raster.Value[row, col] = data[col, row]; } } raster.Save(); raster.Close(); return raster; }
public override void SaveAs(string fileName, string driverCode, string[] options) { // Create a new raster file IRaster newRaster = DataManager.DefaultDataManager.CreateRaster(fileName, driverCode, NumColumns, NumRows, NumBands, DataType, options); // Copy the file based values // newRaster.Copy(Filename, true); newRaster.Projection = Projection; newRaster.Extent = Extent; // Copy the in memory value newRaster.SetData(this); newRaster.ProgressHandler = ProgressHandler; newRaster.NoDataValue = NoDataValue; newRaster.GetStatistics(); newRaster.Bounds = Bounds; // Save the in-memory values. if (newRaster is GdalRaster <T> ) { int count = 1024; int xSize, ySize; for (int i = 0; i < Bands.Count; i++) { var newBand = newRaster.Bands[i]; for (int row = 0; row < NumRows; row += count) { ySize = Math.Min(count, NumRows - row); for (int col = 0; col < NumColumns; col += count) { xSize = Math.Min(count, NumColumns - col); IRaster raster = Bands[i].ReadBlock(col, row, xSize, ySize); newBand.WriteBlock(raster, col, row, xSize, ySize); } } } newRaster.Save(); } else { newRaster.Save(); } newRaster.Close(); }
/// <summary> /// Saves the current raster to the specified file. /// </summary> /// <param name="fileName">The string fileName to save the current raster to.</param> /// <param name="driverCode">The driver code to use.</param> /// <param name="options">the string array of options that depend on the format.</param> public virtual void SaveAs(string fileName, string driverCode, string[] options) { // Create a new raster file IRaster newRaster = DataManager.DefaultDataManager.CreateRaster(fileName, driverCode, NumColumns, NumRows, NumBands, DataType, options); // Copy the file based values // newRaster.Copy(Filename, true); newRaster.Projection = Projection; newRaster.Extent = Extent; // Copy the in memory value newRaster.SetData(this); newRaster.ProgressHandler = ProgressHandler; newRaster.NoDataValue = NoDataValue; newRaster.GetStatistics(); newRaster.Bounds = Bounds; // Save the in-memory values. newRaster.Save(); newRaster.Close(); }
/// <summary> /// Finds the average slope in the given polygons. /// </summary> /// <param name="gridIn">The Polygon Raster(Grid file).</param> /// <param name="polyOut">The Polygon shapefile path.</param> /// <param name="progress">The progress handler.</param> public bool Execute(IRaster gridIn, IFeatureSet polyOut, ICancelProgressHandler cancelProgressHandler) { //Validates the input and output data if (gridIn == null || polyOut == null) { return false; } int maxX, maxY; int current = 0; int previous = 0; double noData, currVal, currTrack; string strTrackPath; IRaster gridTrack = new Raster(); maxX = gridIn.NumRows - 1; maxY = gridIn.NumColumns - 1; noData = gridIn.NoDataValue; //strTrackPath = System.IO.Path.GetDirectoryName(strInRast) + "\\" + System.IO.Path.GetFileNameWithoutExtension(strInRast) + "_track.bgd"; gridTrack = Raster.Create("gridTrack.bgd", "", gridIn.NumColumns, gridIn.NumRows, 1, gridIn.DataType, new string[] { "" }); //gridTrack.CreateNew("gridTrack", "", gridIn.NumColumns, gridIn.NumRows, 1, gridIn.DataType, new string[] { "" }); gridTrack.Bounds = gridIn.Bounds; gridTrack.NoDataValue = gridIn.NoDataValue; polyOut.DataTable.Columns.Add("Value", typeof(int)); polyOut.DataTable.Columns.Add("Zone", typeof(string)); polyOut.DataTable.Columns.Add("Area", typeof(double)); polyOut.DataTable.Columns.Add("COMID", typeof(string)); polyOut.DataTable.Columns.Add("AveSlope", typeof(double)); for (int i = 0; i <= maxX; i++) { current = Convert.ToInt32(i * 100 / maxX); //only update when increment in percentage if (current > previous+5) { cancelProgressHandler.Progress("", current, current.ToString() + "% progress completed"); previous = current; } for (int j = 0; j <= maxY; j++) { if (i > 0 && j > 0) { currVal = Convert.ToInt16(gridIn.Value[i, j]); currTrack = Convert.ToInt16(gridTrack.Value[i, j]); if (currVal == gridIn.NoDataValue) { gridTrack.Value[i, j] = 1; if (cancelProgressHandler.Cancel == true) return false; } else { if (currTrack == 1) { } else { formPolyFromCell(gridIn, gridTrack, i, j, polyOut, cancelProgressHandler); if (cancelProgressHandler.Cancel == true) return false; } } } else { gridTrack.Value[i, j] = gridIn.NoDataValue; } } } gridIn.Close(); gridTrack.Close(); polyOut.SaveAs(polyOut.Filename, true); polyOut.Close(); return true; }
/// <summary> /// Finds the average slope in the given polygons with more user preferences. /// </summary> /// <param name="ras">The dem Raster(Grid file).</param> /// <param name="inZFactor">The scaler factor</param> /// <param name="slopeInPercent">The slope in percentage.</param> /// <param name="poly">The flow poly shapefile path.</param> /// <param name="fldInPolyToStoreSlope">The field name to store average slope in the attribute.</param> /// <param name="outerShpFile">The Featureset where we have the area of interest</param> /// <param name="outerShpIndex">The index of featureset which give paticular area of interest.</param> /// <param name="output">The path to save created slope Feature set.</param> /// <param name="cancelProgressHandler">The progress handler.</param> /// <returns></returns> public bool Execute(IRaster ras, double inZFactor, bool slopeInPercent, IFeatureSet poly, string fldInPolyToStoreSlope, IFeatureSet outerShpFile, int outerShpIndex, IFeatureSet output, ICancelProgressHandler cancelProgressHandler) { // Validates the input and output data if (ras == null || poly == null || outerShpFile == null || output == null) { return(false); } if (poly.FeatureType != FeatureType.Polygon || outerShpFile.FeatureType != FeatureType.Polygon) { return(false); } int previous = 0; IRaster slopegrid = new Raster(); int[] areaCount = new int[poly.Features.Count]; double[] areaTotal = new double[poly.Features.Count]; double[] areaAve = new double[poly.Features.Count]; Slope(ras, inZFactor, slopeInPercent, slopegrid, cancelProgressHandler); if (slopegrid == null) { throw new SystemException(TextStrings.Slopegridfileisnull); } foreach (IFeature f in poly.Features) { output.Features.Add(f); } for (int i = 0; i < slopegrid.NumRows; i++) { int current = Convert.ToInt32(Math.Round(i * 100D / slopegrid.NumRows)); // only update when increment in percentage if (current > previous + 5) { cancelProgressHandler.Progress(string.Empty, current, current + TextStrings.progresscompleted); previous = current; } for (int j = 0; j < slopegrid.NumColumns; j++) { Coordinate coordin = slopegrid.CellToProj(i, j); IPoint pt = new Point(coordin); IFeature point = new Feature(pt); if (!outerShpFile.Features[outerShpIndex].Geometry.Covers(point.Geometry)) { continue; // not found the point inside. } for (int c = 0; c < poly.Features.Count; c++) { if (output.Features[c].Geometry.Covers(point.Geometry)) { areaCount[c]++; areaTotal[c] += slopegrid.Value[i, j] / 100; } if (cancelProgressHandler.Cancel) { return(false); } } } } // Add the column output.DataTable.Columns.Add("FID", typeof(int)); output.DataTable.Columns.Add(fldInPolyToStoreSlope, typeof(Double)); for (int c = 0; c < output.Features.Count; c++) { if (areaCount[c] == 0) { areaAve[c] = 0.0; } else { areaAve[c] = areaTotal[c] / areaCount[c]; } // Add the field values output.Features[c].DataRow["FID"] = c; output.Features[c].DataRow[fldInPolyToStoreSlope] = areaAve[c]; } output.SaveAs(output.Filename, true); slopegrid.Close(); ras.Close(); return(true); }
/// <summary> /// Finds the average slope in the given polygons with more user preferences. /// </summary> /// <param name="ras">The dem Raster(Grid file).</param> /// <param name="inZFactor">The scaler factor</param> /// <param name="slopeInPercent">The slope in percentage.</param> /// <param name="poly">The flow poly shapefile path.</param> /// <param name="fldInPolyToStoreSlope">The field name to store average slope in the attribute.</param> /// <param name="outerShpFile">The Featureset where we have the area of interest</param> /// <param name="outerShpIndex">The index of featureset which give paticular area of interest.</param> /// <param name="output">The path to save created slope Feature set.</param> /// <param name="cancelProgressHandler">The progress handler.</param> /// <returns></returns> public bool Execute( IRaster ras, double inZFactor, bool slopeInPercent, IFeatureSet poly, string fldInPolyToStoreSlope, IFeatureSet outerShpFile, int outerShpIndex, IFeatureSet output, ICancelProgressHandler cancelProgressHandler) { // Validates the input and output data if (ras == null || poly == null || outerShpFile == null || output == null) { return false; } if (poly.FeatureType != FeatureType.Polygon || outerShpFile.FeatureType != FeatureType.Polygon) { return false; } int previous = 0; IRaster slopegrid = new Raster(); int[] areaCount = new int[poly.Features.Count]; double[] areaTotal = new double[poly.Features.Count]; double[] areaAve = new double[poly.Features.Count]; Slope(ras, inZFactor, slopeInPercent, slopegrid, cancelProgressHandler); if (slopegrid == null) { throw new SystemException(TextStrings.Slopegridfileisnull); } foreach (IFeature f in poly.Features) { output.Features.Add(f); } for (int i = 0; i < slopegrid.NumRows; i++) { int current = Convert.ToInt32(Math.Round(i * 100D / slopegrid.NumRows)); // only update when increment in percentage if (current > previous + 5) { cancelProgressHandler.Progress(string.Empty, current, current + TextStrings.progresscompleted); previous = current; } for (int j = 0; j < slopegrid.NumColumns; j++) { Coordinate coordin = slopegrid.CellToProj(i, j); IPoint pt = new Point(coordin); IFeature point = new Feature(pt); if (!outerShpFile.Features[outerShpIndex].Covers(point)) { continue; // not found the point inside. } for (int c = 0; c < poly.Features.Count; c++) { if (output.Features[c].Covers(point)) { areaCount[c]++; areaTotal[c] += slopegrid.Value[i, j] / 100; } if (cancelProgressHandler.Cancel) { return false; } } } } // Add the column output.DataTable.Columns.Add("FID", typeof(int)); output.DataTable.Columns.Add(fldInPolyToStoreSlope, typeof(Double)); for (int c = 0; c < output.Features.Count; c++) { if (areaCount[c] == 0) { areaAve[c] = 0.0; } else { areaAve[c] = areaTotal[c] / areaCount[c]; } // Add the field values output.Features[c].DataRow["FID"] = c; output.Features[c].DataRow[fldInPolyToStoreSlope] = areaAve[c]; } output.SaveAs(output.Filename, true); slopegrid.Close(); ras.Close(); return true; }
public override bool Execute(DotSpatial.Data.ICancelProgressHandler cancelProgressHandler) { int progress = 0; int count = 1; int ndays = 174; if (senseor == 5) { ndays = 174; } else { ndays = 56; } //string lake_centroid = @"E:\Project\HRB\Badan Jarian\Data\Geospatial\mask_centroid.shp"; string lake_centroid = @"E:\Project\HRB\Badan Jarian\Data\Geospatial\mask_centroid_selected.shp"; var lake_centroid_fs = FeatureSet.Open(lake_centroid); DataTable centroid_dt = lake_centroid_fs.DataTable; //var lake_list = (from DataRow dr in centroid_dt.Rows where dr["Flag"].ToString() == "1" select int.Parse(dr["Id"].ToString())).ToList(); // var lake_list = new int[] { 1,18,19,28,30,35,41,49,50,55,57,58,59,60,63,89,91,97,99,100,112,113,118,133,135,136,160,169,181,183}; var lake_list = new int[] { 59 }; int nlakes = lake_list.Length; double [,] water_area = new double[ndays, nlakes]; double[,] water_bond_area = new double[ndays, nlakes]; for (int K = 0; K < nlakes; K++) { int lake_id = lake_list[K]; var dr_lake = (from DataRow dr in centroid_dt.Rows where dr["Id"].ToString() == lake_id.ToString() select dr).First(); string img_dir = Path.Combine(FileDirectory, lake_id.ToString()); StreamReader sr_date_list = new StreamReader(Path.Combine(img_dir, "date_list.txt")); int nfile = 0; while (!sr_date_list.EndOfStream) { sr_date_list.ReadLine(); nfile++; } sr_date_list.Close(); sr_date_list = new StreamReader(Path.Combine(img_dir, "date_list.txt")); string[] dirs = new string[nfile]; for (int i = 0; i < nfile; i++) { var str = sr_date_list.ReadLine(); dirs[i] = Path.Combine(img_dir, str + "_cmb.tif"); } string class_file_list = Path.Combine(img_dir, @"class\class_list.txt"); StreamWriter sw_area = new StreamWriter(Path.Combine(img_dir, "class\\area.csv")); FileStream fs_class = new FileStream(class_file_list, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); StreamReader sr_class_file = new StreamReader(fs_class); var center_pt = new Coordinate(double.Parse(dr_lake["X"].ToString()), double.Parse(dr_lake["Y"].ToString())); int cell_area = 5 * 5; int date_index = 0; try { foreach (var file in dirs) { int water_cell_count = 0; int water_bound_cell_count = 0; var temp = Path.GetFileNameWithoutExtension(file); var daystr = temp.Remove(10); var water_file = file.Replace("_cmb", "_water"); IRaster raster2 = Raster.OpenFile(file); raster2.SaveAs(water_file); IRaster raster_water = Raster.OpenFile(water_file); var class_file = sr_class_file.ReadLine(); var class_mat = ReadClassFile(class_file, raster2.NumRows, raster2.NumColumns); for (int i = 0; i < raster2.NumRows; i++) { for (int j = 0; j < raster2.NumColumns; j++) { if (raster2.Value[i, j] != raster2.NoDataValue) { raster_water.Value[i, j] = class_mat[i][j]; } else { raster_water.Value[i, j] = raster2.NoDataValue; } } } var cell = raster2.ProjToCell(center_pt); var center_class = raster_water.Value[cell.Row, cell.Column]; var center_bound_class = center_class; for (int i = cell.Row + 1; i < raster2.NumRows; i++) { if (raster_water.Value[i, cell.Column] != center_class) { center_bound_class = raster_water.Value[i, cell.Column]; break; } } if (center_bound_class == center_class) { for (int i = cell.Column + 1; i < raster2.NumColumns; i++) { if (raster_water.Value[cell.Row, i] != center_class) { center_bound_class = raster_water.Value[cell.Row, i]; break; } } } for (int i = 0; i < raster2.NumRows; i++) { for (int j = 0; j < raster2.NumColumns; j++) { if (raster2.Value[i, j] != raster2.NoDataValue) { if (raster_water.Value[i, j] == center_class) { water_cell_count++; } if (raster_water.Value[i, j] == center_bound_class) { water_bound_cell_count++; } } } } water_area[date_index, K] = water_cell_count * cell_area; water_bond_area[date_index, K] = water_bound_cell_count * cell_area; sw_area.WriteLine(string.Format("{0},{1},{2},{3}", daystr, water_area[date_index, K], water_bond_area[date_index, K], water_area[date_index, K] + water_bond_area[date_index, K])); raster_water.Save(); raster_water.Close(); date_index++; } } catch (Exception ex) { cancelProgressHandler.Progress("Package_Tool", 100, "Error: " + ex.Message); } finally { sw_area.Close(); sr_class_file.Close(); sr_date_list.Close(); } progress = K * 100 / nlakes; //if (progress > count) //{ cancelProgressHandler.Progress("Package_Tool", progress, "Processing lake " + lake_id); count++; // } } string lake_area_file = Path.Combine(FileDirectory, "water_area.csv"); string lake_area_bond_file = Path.Combine(FileDirectory, "waterbond_area.csv"); StreamWriter csv_water = new StreamWriter(lake_area_file); StreamWriter csv_bond = new StreamWriter(lake_area_bond_file); var line = string.Join(",", lake_list.ToArray()); csv_water.WriteLine(line); csv_bond.WriteLine(line); for (int t = 0; t < ndays; t++) { line = ""; for (int j = 0; j < nlakes; j++) { line += water_area[t, j] + ","; } line = line.TrimEnd(new char[] { ',' }); csv_water.WriteLine(line); line = ""; for (int j = 0; j < nlakes; j++) { line += water_bond_area[t, j] + ","; } line = line.TrimEnd(new char[] { ',' }); csv_bond.WriteLine(line); } csv_water.Close(); csv_bond.Close(); lake_centroid_fs.Close(); return(true); }
public bool Execute1(DotSpatial.Data.ICancelProgressHandler cancelProgressHandler) { StreamReader sr = new StreamReader(Path.Combine(FileDirectory, "list.txt")); int nfile = 0; while (!sr.EndOfStream) { sr.ReadLine(); nfile++; } sr.Close(); sr = new StreamReader(Path.Combine(FileDirectory, "list.txt")); string[] dirs = new string[nfile]; for (int i = 0; i < nfile; i++) { var str = sr.ReadLine(); dirs[i] = Path.Combine(FileDirectory, str + "_2.tif"); } int[] bandlist = new int[] { 4, 3, 2 }; //string[] dirs = Directory.GetFiles(FileDirectory, "*_2.tif"); string center_shp = @"E:\Project\HRB\Badan Jarian\Data\Geospatial\Center35.shp"; StreamWriter sw = new StreamWriter(Path.Combine(FileDirectory, "area.csv")); StreamWriter sw_alpha = new StreamWriter(Path.Combine(FileDirectory, "alpha.txt")); IFeatureSet center_fs = FeatureSet.Open(center_shp); List <double> vec = new List <double>(); try { double cell_area = 5 * 5; var center_pt = center_fs.Features[0].Geometry.Coordinate; var center_vec = new double[3]; var pt_vec = new double[3]; int progress = 0; int count = 1; int t = 0; vec.Clear(); foreach (var file in dirs) { long wcount = 0; var temp = Path.GetFileNameWithoutExtension(file); var daystr = temp.Remove(10); var water_file = file.Replace("_2", "_water"); IRaster raster2 = Raster.OpenFile(file); raster2.SaveAs(water_file); IRaster raster3 = Raster.OpenFile(file.Replace("_2", "_3")); IRaster raster4 = Raster.OpenFile(file.Replace("_2", "_4")); IRaster raster_water = Raster.OpenFile(water_file); double[,] img = new double[raster2.NumRows, raster2.NumColumns]; var cell = raster2.ProjToCell(center_pt); center_vec[0] = raster2.Value[cell.Row, cell.Column]; center_vec[1] = raster3.Value[cell.Row, cell.Column]; center_vec[2] = raster4.Value[cell.Row, cell.Column]; for (int i = 0; i < raster2.NumRows; i++) { for (int j = 0; j < raster2.NumColumns; j++) { if (raster2.Value[i, j] == raster2.NoDataValue) { img[i, j] = 0; raster_water.Value[i, j] = 0; sw_alpha.WriteLine(0); vec.Add(0); } else { pt_vec[0] = raster2.Value[i, j]; pt_vec[1] = raster3.Value[i, j]; pt_vec[2] = raster4.Value[i, j]; var alpha = sam(pt_vec, center_vec); if (alpha <= AlphaThreashhold) { raster_water.Value[i, j] = 1; wcount++; } else { raster_water.Value[i, j] = 0; } try { raster_water.Value[i, j] = alpha; } catch (Exception ex) { cancelProgressHandler.Progress("Package_Tool", progress, "Warning: " + ex.Message + " " + alpha); alpha = 0; raster_water.Value[i, j] = 0; } finally { img[i, j] = alpha; vec.Add(alpha); sw_alpha.WriteLine(alpha); } } } } var max = vec.Max(); var min = vec.Min(); var delta = max - min; int k = 0; for (int i = 0; i < raster2.NumRows; i++) { for (int j = 0; j < raster2.NumColumns; j++) { img[i, j] = (vec[k] - min) / delta * 255; raster_water.Value[i, j] = img[i, j]; k++; } } var sobled_img = sobel(img, raster2.NumRows, raster2.NumColumns); for (int i = 0; i < raster2.NumRows; i++) { for (int j = 0; j < raster2.NumColumns; j++) { raster_water.Value[i, j] = sobled_img[i, j]; } } var water_area = wcount * cell_area; sw.WriteLine(daystr + "," + water_area); raster2.Close(); raster3.Close(); raster4.Close(); raster_water.Save(); raster_water.Close(); progress = t * 100 / dirs.Length; if (progress > count) { cancelProgressHandler.Progress("Package_Tool", progress, "Processing: " + file); count++; } t++; } } catch (Exception ex) { cancelProgressHandler.Progress("Package_Tool", 100, "Error: " + ex.Message); } finally { sw_alpha.Close(); sr.Close(); sw.Close(); center_fs.Close(); } return(true); }