/// <summary> /// Write results to a rester. /// </summary> /// <param name="raster">DEM raster</param> /// <param name="outputDataStore">Output datastore</param> /// <param name="result">Elevation map with resuluts</param> private void WriteToRaster(Raster raster, FileSystemDatastore outputDataStore, GeoMap result) { raster.SetNoDataValue(0); raster.SetPixelType(RasterPixelType.DOUBLE); RasterDataset resultRasterDataset = raster.SaveAs(tmpRasterName, outputDataStore, rasterFormat); GarbageHelper.Instance.AddGarbage(Path.Combine(outputFolder, tmpRasterName)); Raster resultRaster = resultRasterDataset.CreateRaster(new int[1] { 0 }); resultRaster.Refresh(); if (!resultRaster.CanEdit()) { MessageBox.Show("Cannot write to raster", "Error", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); return; } PixelBlock pixelBlock = resultRaster.CreatePixelBlock(resultRaster.GetWidth(), resultRaster.GetHeight()); resultRaster.Read(0, 0, pixelBlock); pixelBlock.Clear(0); Array pixel = new double[resultRaster.GetWidth(), resultRaster.GetHeight()]; pixelBlock.SetPixelData(0, result.Transpose()); resultRaster.Write(0, 0, pixelBlock); resultRaster.Refresh(); resultRaster.SaveAs(SettingsManager.Instance.CurrentSettings.OutputFilename, outputDataStore, rasterFormat); }
public static async Task <TableStatisticsResult> GetRasterStats(Uri rasterUri, string field) { // parse the uri for the folder and file string strFileName = null; string strFolderPath = null; if (rasterUri.IsFile) { strFileName = System.IO.Path.GetFileName(rasterUri.LocalPath); strFolderPath = System.IO.Path.GetDirectoryName(rasterUri.LocalPath); } RasterDataset rDataset = null; await QueuedTask.Run(() => { // Opens a file geodatabase. This will open the geodatabase if the folder exists and contains a valid geodatabase. using (Geodatabase geodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(strFolderPath)))) { // Use the geodatabase. try { rDataset = geodatabase.OpenDataset <RasterDataset>(strFileName); } catch (GeodatabaseTableException e) { Module1.Current.ModuleLogManager.LogError(nameof(GetRasterStats), "Unable to open raster " + strFileName); Module1.Current.ModuleLogManager.LogError(nameof(GetRasterStats), "Exception: " + e.Message); return; } } }); TableStatisticsResult tableStatisticsResult = null; if (rDataset != null) { await QueuedTask.Run(() => { Raster raster = rDataset.CreateRaster(new int[] { 0 }); if (raster != null) { var table = raster.GetAttributeTable(); if (table != null) { Field statField = table.GetDefinition().GetFields().First(x => x.Name.Equals(field)); StatisticsDescription statisticsDescription = new StatisticsDescription(statField, new List <StatisticsFunction>() { StatisticsFunction.Min, StatisticsFunction.Max }); TableStatisticsDescription tableStatisticsDescription = new TableStatisticsDescription(new List <StatisticsDescription>() { statisticsDescription }); IReadOnlyList <TableStatisticsResult> statResult = table.CalculateStatistics(tableStatisticsDescription); tableStatisticsResult = statResult[0]; } } }); } return(tableStatisticsResult); }