/// <summary> /// Calculates species observation count grid for each taxon in <paramref name="taxonIds"/>. /// </summary> /// <param name="taxonIds">The taxon ids.</param> /// <param name="gridCoordinateSystem">The grid coordinate system.</param> /// <returns>Species observation count for each taxon grouped by grid cells.</returns> public TaxonSpecificGridSpeciesObservationCountResult CalculateMultipleSpeciesObservationGrid( List <int> taxonIds, GridCoordinateSystem?gridCoordinateSystem) { Dictionary <IGridCellBase, Dictionary <int, IGridCellSpeciesObservationCount> > totalResult; IList <IGridCellSpeciesObservationCount> taxonResult; List <int> taxonIdList = new List <int>(taxonIds); if (taxonIdList.IsEmpty()) { taxonIdList.Add(0); } totalResult = new Dictionary <IGridCellBase, Dictionary <int, IGridCellSpeciesObservationCount> >(new GridCellBaseCenterPointComparer()); foreach (int taxonId in taxonIdList) { taxonResult = CalculateSpeciesObservationGrid(taxonId, gridCoordinateSystem); AddTaxonResult(taxonId, taxonResult, totalResult); } TaxonList taxonList = CoreData.TaxonManager.GetTaxa(UserContext, taxonIdList); List <TaxonViewModel> taxaList = taxonList.GetGenericList().ToTaxonViewModelList(); TaxonSpecificGridSpeciesObservationCountResult result = new TaxonSpecificGridSpeciesObservationCountResult() { Taxa = taxaList, GridCells = totalResult }; return(result); }
/// <summary> /// Generates a CSV file with grid based counts of number of species observations for each selected taxon. /// </summary> /// <param name="filename"> /// The filename (without file extension). /// </param> /// <param name="convertCountToOccurrenceBoolean"> /// If set to <c>true</c> the observation count will be written as 1 if the count > 0 and 0 if count = 0; /// If set to <c>false</c> the observation count will be written. /// </param> /// <returns> /// A CSV file. /// </returns> private FileResult TaxonSpecificGridStatisticsOnSpeciesObservationCountsAsCsv(string filename, bool convertCountToOccurrenceBoolean) { SpeciesObservationGridCalculator resultCalculator = new SpeciesObservationGridCalculator(GetCurrentUser(), SessionHandler.MySettings); TaxonSpecificGridSpeciesObservationCountResult result = resultCalculator.CalculateMultipleSpeciesObservationGrid(); TaxonSpecificSpeciesObservationCountGridCsv file = FileExportManager.GetTaxonSpecificGridStatisticsOnSpeciesObservationCountsAsCsv(result, convertCountToOccurrenceBoolean); MemoryStream returnStream = file.ToStream(); SetServerDone(); return(File( returnStream.ToArray(), "text/csv", FilenameGenerator.CreateFilename(filename, FileType.Csv))); }
private void AddContentFormat(ExcelWorksheet worksheet, TaxonSpecificGridSpeciesObservationCountResult data) { // Formatting straight columns if (IsColumnHeaderBackgroundUsed) { // Format style by columns var maxColumnIndex = 6 + data.Taxa.Count; using (var range = worksheet.Cells[1, 1, 1, maxColumnIndex]) { range.Style.Font.Bold = false; range.Style.Font.Color.SetColor(ExcelHelper.ColorTable[0]); range.Style.Fill.PatternType = ExcelFillStyle.Solid; range.Style.Fill.BackgroundColor.SetColor(ExcelHelper.ColorTable[57]); } } }
private void AddContentData(ExcelWorksheet worksheet, TaxonSpecificGridSpeciesObservationCountResult data) { var rowIndex = 2; // Data values. List <IGridCellBase> orderedGridCells = data.GridCells.Keys.OrderBy(x => x.Identifier).ToList(); foreach (IGridCellBase gridCell in orderedGridCells) { worksheet.Cells[rowIndex, 1].Value = gridCell.Identifier; worksheet.Cells[rowIndex, 2].Value = gridCell.OrginalGridCellCentreCoordinate.X; worksheet.Cells[rowIndex, 3].Value = gridCell.OrginalGridCellCentreCoordinate.Y; worksheet.Cells[rowIndex, 4].Value = gridCell.GridCellCentreCoordinate.X; worksheet.Cells[rowIndex, 5].Value = gridCell.GridCellCentreCoordinate.Y; worksheet.Cells[rowIndex, 6].Value = gridCell.GridCellSize; var columnIndex = 7; foreach (var taxon in data.Taxa) { long nrObservations = 0; if (data.GridCells[gridCell].ContainsKey(taxon.TaxonId)) { nrObservations = data.GridCells[gridCell][taxon.TaxonId].ObservationCount; } if (FormatCountAsOccurrence) { worksheet.Cells[rowIndex, columnIndex].Value = nrObservations > 0 ? 1 : 0; } else { worksheet.Cells[rowIndex, columnIndex].Value = nrObservations; } columnIndex++; } rowIndex++; } }
/// <summary> /// Adds the headers. /// </summary> /// <param name="worksheet">The worksheet.</param> private void AddHeaders(ExcelWorksheet worksheet, TaxonSpecificGridSpeciesObservationCountResult data) { var gridCoordinateSystemDescription = ""; if (data.GridCells.Count > 0) { gridCoordinateSystemDescription = data.GridCells.First().Key.GridCoordinateSystem.ToString(); } worksheet.Cells[1, 1].Value = "Id"; worksheet.Cells[1, 2].Value = String.Format("Centre coordinate X ({0})", gridCoordinateSystemDescription); worksheet.Cells[1, 3].Value = String.Format("Centre coordinate Y ({0})", gridCoordinateSystemDescription); worksheet.Cells[1, 4].Value = String.Format("Centre coordinate X ({0})", CoordinateSystem); worksheet.Cells[1, 5].Value = String.Format("Centre coordinate Y ({0})", CoordinateSystem); worksheet.Cells[1, 6].Value = "Grid cell width (metres)"; var columnCount = 7; foreach (TaxonViewModel taxon in data.Taxa) { worksheet.Cells[1, columnCount].Value = String.Format("{0} (TaxonId {1})", taxon.ScientificName, taxon.TaxonId); columnCount++; } }
/// <summary> /// Writes a comma separated file (CSV) with grid /// based counts of number of species observations for each selected taxon. /// </summary> /// <param name="stream"> /// The stream to be written to. /// </param> /// <param name="multipleSpeciesObservationCountGrid"> /// The data to write. /// </param> public void WriteDataToStream(Stream stream, TaxonSpecificGridSpeciesObservationCountResult data, bool formatCountAsOccurrence) { CsvConfiguration csvConfiguration = new CsvConfiguration(); csvConfiguration.QuoteAllFields = FileFormatSetting.CsvFileSettings.QuoteAllColumns; csvConfiguration.Delimiter = FileFormatSetting.CsvFileSettings.GetSeparator(); csvConfiguration.Encoding = Encoding.UTF8; using (var streamWriter = new StreamWriter(stream, Encoding.UTF8)) using (CsvWriter writer = new CsvWriter(streamWriter, csvConfiguration)) { // Write header row string gridCoordinateSystemDescription = ""; string displayCoordinateSystemDescription = ""; if (data.GridCells.Count > 0) { gridCoordinateSystemDescription = data.GridCells.First().Key.GridCoordinateSystem.ToString(); displayCoordinateSystemDescription = data.GridCells.First().Key.CoordinateSystem.Id.ToString(); } writer.WriteField("Id"); writer.WriteField(String.Format("Centre coordinate X ({0})", gridCoordinateSystemDescription)); writer.WriteField(String.Format("Centre coordinate Y ({0})", gridCoordinateSystemDescription)); writer.WriteField(String.Format("Centre coordinate X ({0})", displayCoordinateSystemDescription)); writer.WriteField(String.Format("Centre coordinate Y ({0})", displayCoordinateSystemDescription)); writer.WriteField("Grid cell width (metres)"); foreach (TaxonViewModel taxon in data.Taxa) { writer.WriteField(string.Format("{0} (TaxonId {1})", taxon.ScientificName, taxon.TaxonId)); } writer.NextRecord(); // Write data // Data values. List <IGridCellBase> orderedGridCells = data.GridCells.Keys.OrderBy(x => x.Identifier).ToList(); foreach (IGridCellBase gridCell in orderedGridCells) { writer.WriteField(gridCell.Identifier); writer.WriteField(gridCell.OrginalGridCellCentreCoordinate.X.ToString(CultureInfo.InvariantCulture)); writer.WriteField(gridCell.OrginalGridCellCentreCoordinate.Y.ToString(CultureInfo.InvariantCulture)); writer.WriteField(gridCell.GridCellCentreCoordinate.X.ToString(CultureInfo.InvariantCulture)); writer.WriteField(gridCell.GridCellCentreCoordinate.Y.ToString(CultureInfo.InvariantCulture)); writer.WriteField(gridCell.GridCellSize.ToString(CultureInfo.InvariantCulture)); foreach (TaxonViewModel taxon in data.Taxa) { long nrObservations = 0; if (data.GridCells[gridCell].ContainsKey(taxon.TaxonId)) { nrObservations = data.GridCells[gridCell][taxon.TaxonId].ObservationCount; } if (formatCountAsOccurrence) { int binaryVal = nrObservations > 0 ? 1 : 0; writer.WriteField(binaryVal.ToString(CultureInfo.InvariantCulture)); } else { writer.WriteField(nrObservations.ToString(CultureInfo.InvariantCulture)); } } writer.NextRecord(); } } }
/// <summary> /// Writes a comma separated file (CSV) with grid /// based counts of number of species observations for each selected taxon. /// </summary> /// <param name="filePath">The file path.</param> /// <param name="taxonSpecificSpeciesObservationCountGrid">The data to write.</param> public void WriteDataToFile(string filePath, TaxonSpecificGridSpeciesObservationCountResult taxonSpecificSpeciesObservationCountGrid, bool formatCountAsOccurrence) { WriteDataToStream(new FileStream(filePath, FileMode.Create), taxonSpecificSpeciesObservationCountGrid, formatCountAsOccurrence); }
/// <summary> /// Initializes a new instance of the <see cref="TaxonSpecificSpeciesObservationCountGridCsv"/> class. /// </summary> /// <param name="taxonSpecificSpeciesObservationCountGrid">The multiple species observation count grid data.</param> public TaxonSpecificSpeciesObservationCountGridCsv(TaxonSpecificGridSpeciesObservationCountResult taxonSpecificSpeciesObservationCountGrid, bool formatCountAsOccurrence) { _taxonSpecificSpeciesObservationCountGrid = taxonSpecificSpeciesObservationCountGrid; _formatCountAsOccurrence = formatCountAsOccurrence; }
/// <summary> /// Generates a CSV file with grid based counts of number of species observations for each selected taxon. /// </summary> /// <param name="data"> /// The data. /// </param> /// <param name="formatCountAsOccurrence"> /// If set to <c>true</c> the observation count will be written as 1 if the count > 0 and 0 if count = 0; /// If set to <c>false</c> the observation count will be written. /// </param> /// <returns> /// A CSV file. /// </returns> public static TaxonSpecificSpeciesObservationCountGridCsv GetTaxonSpecificGridStatisticsOnSpeciesObservationCountsAsCsv(TaxonSpecificGridSpeciesObservationCountResult data, bool formatCountAsOccurrence) { TaxonSpecificSpeciesObservationCountGridCsv file = new TaxonSpecificSpeciesObservationCountGridCsv(data, formatCountAsOccurrence); return(file); }