Exemplo n.º 1
0
        public void GetSpeciesObservations_And_SpeciesCount_Per_Polygon_No_Count_Selected()
        {
            using (ShimsContext.Create())
            {
                // Arrange
                SummaryStatisticsPerPolygonResultCalculator resultCalculator;
                List <SpeciesObservationsCountPerPolygon>   result;

                LoginApplicationUser();
                SessionHandler.MySettings.Calculation.SummaryStatistics.CalculateNumberOfObservationsfromObsData = false;
                SessionHandler.MySettings.Calculation.SummaryStatistics.CalculateNumberOfSpeciesfromObsData      = false;
                SessionHandler.MySettings.Calculation.SummaryStatistics.WfsSummaryStatisticsLayerId = 0;
                resultCalculator = new SummaryStatisticsPerPolygonResultCalculator(SessionHandler.UserContext, SessionHandler.MySettings);

                // Act
                result = resultCalculator.GetSummaryStatisticsPerPolygonFromCacheIfAvailableOrElseCalculate();

                // Assert
                Assert.IsNotNull(result);
                Assert.IsTrue(result.Count > 0);
                Assert.IsTrue(result[0].Properties.Split(new[] { '\n' }).Length > 0);
                Assert.IsTrue(result[0].SpeciesObservationsCount == "-"); // species observation count not checked
                Assert.IsTrue(result[0].SpeciesCount == "-");             // species count not checked
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates an excel file.
        /// Writes the content of a list into a worksheet of an excelfile and save the file.
        /// </summary>
        /// <param name="autosizeColumnWidth">
        /// If true, the columns will be autosized.
        /// </param>
        /// <returns>
        /// The <see cref="MemoryStream"/>.
        /// </returns>
        private MemoryStream CreateExcelFile(bool autosizeColumnWidth = false)
        {
            var memoryStream = new MemoryStream();

            try
            {
                using (var package = new ExcelPackage(memoryStream))
                {
                    var resultCalculator = new SummaryStatisticsPerPolygonResultCalculator(currentUser, SessionHandler.MySettings);
                    var data             = resultCalculator.CalculateSpeciesObservationCountPerPolygonAndTaxa(SessionHandler.MySettings.Filter.Taxa.TaxonIds.ToList());

                    // Add a new worksheet to the empty workbook.
                    // The name of the sheet can not be longer than 31 characters.
                    var worksheet = package.Workbook.Worksheets.Add("SLW Data");
                    AddHeaders(worksheet, data);
                    AddSpeciesObservationsData(worksheet, data);
                    FormatHeader(worksheet, 1, data.Taxa.Count + 1);

                    if (autosizeColumnWidth)
                    {
                        worksheet.Cells.AutoFitColumns(0);
                    }
                    // Set first column's width
                    worksheet.Column(1).Width = 38;

                    //Add aditional sheets if user has request that
                    AddAditionalSheets(package);

                    package.Save();
                }

                memoryStream.Position = 0;
                return(memoryStream);
            }
            catch (Exception)
            {
                memoryStream.Dispose();

                throw;
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Constructor of an excel xml file with summary statistics.
        /// </summary>
        /// <param name="currentUser">Current user context</param>
        /// <param name="addSettings">True if settings sheet should be included</param>
        /// <param name="addProvenance">True if provenance sheet should be included.</param>
        public SummaryStatisticsPerPolygonExcelXml(IUserContext currentUser, bool addSettings, bool addProvenance)
        {
            var resultCalculator = new SummaryStatisticsPerPolygonResultCalculator(currentUser, SessionHandler.MySettings);
            var data             = resultCalculator.GetSummaryStatisticsPerPolygonFromCacheIfAvailableOrElseCalculate();

            _xmlBuilder = new StringBuilder();

            // Add file definitions and basic format settings
            _xmlBuilder.AppendLine(GetInitialSection());

            // Specify column and row counts
            _xmlBuilder.AppendLine(GetColumnInitialSection(3, data.Count));

            // Specify column widths
            _xmlBuilder.AppendLine(GetColumnWidthLine(100));
            _xmlBuilder.AppendLine(GetColumnWidthLine(90));
            _xmlBuilder.AppendLine(GetColumnWidthLine(200));

            // Add row with column headers
            _xmlBuilder.AppendLine(GetRowStart());
            _xmlBuilder.AppendLine(GetColumnNameRowLine(Resource.SummaryStatisticsObservationCount));
            _xmlBuilder.AppendLine(GetColumnNameRowLine(Resource.SummaryStatisticsTaxaCount));
            _xmlBuilder.AppendLine(GetColumnNameRowLine(Resource.SummaryStatisticsPolygon));
            _xmlBuilder.AppendLine(GetRowEnd());

            // Data values
            foreach (SpeciesObservationsCountPerPolygon row in data)
            {
                string[] multiLine = row.Properties.Split(new[] { "<br />" }, StringSplitOptions.RemoveEmptyEntries);

                _xmlBuilder.AppendLine(GetRowStart(true, multiLine.Length));
                _xmlBuilder.AppendLine(GetDataRowLine(row.SpeciesObservationsCount == "-" ? "String" : "Number", row.SpeciesObservationsCount));
                _xmlBuilder.AppendLine(GetDataRowLine(row.SpeciesCount == "-" ? "String" : "Number", row.SpeciesCount));
                _xmlBuilder.AppendLine(GetDataRowLine("String", row.Properties.Replace("<br />", "\n"), true));
                _xmlBuilder.AppendLine(GetRowEnd());
            }

            // Add final section of the xml document.
            _xmlBuilder.AppendLine(GetFinalSection(GetAditionalSheets(currentUser, addSettings, addProvenance)));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates an excel file.
        /// Writes the content of a list into a worksheet of an excelfile and save the file.
        /// </summary>
        /// <param name="autosizeColumnWidth">
        /// If true, the columns will be autosized.
        /// </param>
        /// <returns>
        /// The <see cref="MemoryStream"/>.
        /// </returns>
        private MemoryStream CreateExcelFile(bool autosizeColumnWidth = false)
        {
            var memoryStream = new MemoryStream();

            try
            {
                using (ExcelPackage package = new ExcelPackage(memoryStream))
                {
                    var resultCalculator = new SummaryStatisticsPerPolygonResultCalculator(currentUser, SessionHandler.MySettings);
                    var data             = resultCalculator.GetSummaryStatisticsPerPolygonFromCacheIfAvailableOrElseCalculate();

                    // Add a new worksheet to the empty workbook.
                    // The name of the sheet can not be longer than 31 characters.
                    var worksheet = package.Workbook.Worksheets.Add("SLW Data");
                    AddHeaders(worksheet);
                    AddSpeciesObservationsData(worksheet, data);
                    AddSpeciesObservationsFormat(worksheet);

                    if (autosizeColumnWidth)
                    {
                        worksheet.Cells.AutoFitColumns(0);
                    }

                    //Add aditional sheets if user has request that
                    AddAditionalSheets(package);

                    package.Save();
                }

                memoryStream.Position = 0;
                return(memoryStream);
            }
            catch (Exception)
            {
                memoryStream.Dispose();

                throw;
            }
        }
Exemplo n.º 5
0
        public void CalculateSpeciesObservationCountPerPolygonAndTaxa_TwoTaxonAndTwoPolygons_Success()
        {
            // Arrange
            SummaryStatisticsPerPolygonResultCalculator          resultCalculator;
            TaxonSpecificSpeciesObservationCountPerPolygonResult result;
            List <int> taxonIds;

            LoginApplicationUser();
            SessionHandler.MySettings.Calculation.SummaryStatistics.WfsSummaryStatisticsLayerId = 0;
            resultCalculator = new SummaryStatisticsPerPolygonResultCalculator(SessionHandler.UserContext, SessionHandler.MySettings);
            taxonIds         = new List <int> {
                1, 2
            };

            // Act
            result = resultCalculator.CalculateSpeciesObservationCountPerPolygonAndTaxa(taxonIds);

            // Assert
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Taxa.Count > 0);
            Assert.IsTrue(result.SpeciesObservationCountPerPolygon.Count > 0);
        }
        /// <summary>
        /// Constructor of an excel xml file with species observations count per polygon and for each selected taxon.
        /// </summary>
        /// <param name="currentUser">The user context.</param>
        /// <param name="formatCountAsOccurrence">
        /// If set to <c>true</c> the result cells will be set to 1 if there are any observations; otherwise 0.
        /// </param>
        /// <param name="addSettings">True if settings sheet should be included</param>
        /// <param name="addProvenance">True if provenance sheet should be included.</param>
        public TaxonSpecificSpeciesObservationCountPerPolygonExcelXml(IUserContext currentUser, bool formatCountAsOccurrence, bool addSettings, bool addProvenance)
            : base()
        {
            var resultCalculator = new SummaryStatisticsPerPolygonResultCalculator(currentUser, SessionHandler.MySettings);
            var data             = resultCalculator.CalculateSpeciesObservationCountPerPolygonAndTaxa(SessionHandler.MySettings.Filter.Taxa.TaxonIds.ToList());

            _xmlBuilder = new StringBuilder();

            int nrColumns = 1 + data.Taxa.Count;

            // Add file definitions and basic format settings.
            _xmlBuilder.AppendLine(base.GetInitialSection());

            // Specify column and row counts.
            _xmlBuilder.AppendLine(base.GetColumnInitialSection(nrColumns, data.SpeciesObservationCountPerPolygon.Count));

            // Specify column widths.
            _xmlBuilder.AppendLine(base.GetColumnWidthLine(200));

            for (int i = 0; i < data.Taxa.Count; i++)
            {
                _xmlBuilder.AppendLine(base.GetColumnWidthLine(140));
            }

            // Add row with column headers.
            _xmlBuilder.AppendLine(base.GetRowStart());
            _xmlBuilder.AppendLine(base.GetColumnNameRowLine("Polygon"));
            foreach (TaxonViewModel taxon in data.Taxa)
            {
                _xmlBuilder.AppendLine(base.GetColumnNameRowLine(string.Format("{0} (TaxonId {1})", taxon.ScientificName, taxon.TaxonId)));
            }

            _xmlBuilder.AppendLine(base.GetRowEnd());

            // Data values.
            foreach (KeyValuePair <string, Dictionary <int, long> > pair in data.SpeciesObservationCountPerPolygon)
            {
                string[] multiLine = pair.Key.Split(new[] { "<br />" }, StringSplitOptions.RemoveEmptyEntries);
                _xmlBuilder.AppendLine(GetRowStart(true, multiLine.Length));
                _xmlBuilder.AppendLine(GetDataRowLine("String", pair.Key.Replace("<br />", "\n"), true));
                foreach (TaxonViewModel taxon in data.Taxa)
                {
                    long speciesObservationCount = 0;
                    pair.Value.TryGetValue(taxon.TaxonId, out speciesObservationCount);

                    if (formatCountAsOccurrence)
                    {
                        int binaryVal = speciesObservationCount > 0 ? 1 : 0;
                        _xmlBuilder.AppendLine(base.GetDataRowLine("Number", binaryVal.ToString(CultureInfo.InvariantCulture)));
                    }
                    else
                    {
                        _xmlBuilder.AppendLine(base.GetDataRowLine("Number", speciesObservationCount.ToString(CultureInfo.InvariantCulture)));
                    }
                }

                _xmlBuilder.AppendLine(base.GetRowEnd());
            }

            // Add final section of the xml document.
            _xmlBuilder.AppendLine(GetFinalSection(GetAditionalSheets(currentUser, addSettings, addProvenance)));
        }