public override void Execute()
        {
            System.Diagnostics.Debug.WriteLine("CalculateDefaultTaxonGridTask started");
            IUserContext userContext = CoreData.UserManager.GetCurrentUser();

            MySettings.MySettings mySettings       = new MySettings.MySettings();
            TaxonGridCalculator   resultCalculator = new TaxonGridCalculator(userContext, mySettings);
            TaxonGridResult       result           = resultCalculator.CalculateTaxonGrid(mySettings);

            DefaultResultsManager.AddGridCellTaxa(result);
            System.Diagnostics.Debug.WriteLine("CalculateDefaultTaxonGridTask finished");
        }
Exemple #2
0
        /// <summary>
        /// Generates an GeoJson (.geojson) file with grid statistics on species richness.
        /// </summary>
        /// <returns>A .geojson file.</returns>
        public FileResult GridStatisticsOnSpeciesRichnessAsGeoJson()
        {
            TaxonGridCalculator resultCalculator = null;
            string geojson = null;

            resultCalculator = new TaxonGridCalculator(GetCurrentUser(), SessionHandler.MySettings);
            geojson          = resultCalculator.GetTaxonGridAsGeoJson();
            SetServerDone();
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(geojson);
            return(File(
                       bytes,
                       "application/json",
                       FilenameGenerator.CreateFilename("SpeciesRichnessGrid", FileType.GeoJSON)));
        }
Exemple #3
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 TaxonGridCalculator(currentUser, SessionHandler.MySettings);
                    var data             = resultCalculator.GetTaxonGridResultFromCacheIfAvailableOrElseCalculate();

                    // 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);
                    AddContentData(worksheet, data);
                    FormatHeader(worksheet, 1, 8);

                    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;
            }
        }
Exemple #4
0
        /// <summary>
        /// Constructor of an excel xml file with grid statistics on species counts.
        /// </summary>
        /// <param name="currentUser">Current user context</param>
        /// <param name="coordinateSystem">Coordinate system of the transformated coordinates.</param>
        /// <param name="addSettings">True if settings sheet should be included</param>
        /// <param name="addProvenance">True if provenance sheet should be included.</param>
        public GridStatisticsOnSpeciesCountExcelXml(IUserContext currentUser, CoordinateSystemId coordinateSystem, bool addSettings, bool addProvenance)
            : base()
        {
            var resultCalculator = new TaxonGridCalculator(currentUser, SessionHandler.MySettings);
            var data             = resultCalculator.GetTaxonGridResultFromCacheIfAvailableOrElseCalculate();

            _xmlBuilder = new StringBuilder();

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

            //Specify column and row counts
            _xmlBuilder.AppendLine(base.GetColumnInitialSection(8, data.Cells.Count));
            ////_xmlBuilder.AppendLine(base.getColumnInitialSection(10, data.Cells.Count));

            //Specify column widths
            _xmlBuilder.AppendLine(base.GetColumnWidthLine(200));
            _xmlBuilder.AppendLine(base.GetColumnWidthLine(100));
            _xmlBuilder.AppendLine(base.GetColumnWidthLine(60));
            _xmlBuilder.AppendLine(base.GetColumnWidthLine(150));
            _xmlBuilder.AppendLine(base.GetColumnWidthLine(150));
            _xmlBuilder.AppendLine(base.GetColumnWidthLine(170));
            _xmlBuilder.AppendLine(base.GetColumnWidthLine(170));
            _xmlBuilder.AppendLine(base.GetColumnWidthLine(170));
            ////_xmlBuilder.AppendLine(base.getColumnWidthLine(170));
            ////_xmlBuilder.AppendLine(base.getColumnWidthLine(170));

            //Add row with column headers
            _xmlBuilder.AppendLine(base.GetRowStart());
            _xmlBuilder.AppendLine(base.GetColumnNameRowLine("Id"));
            _xmlBuilder.AppendLine(base.GetColumnNameRowLine(Resource.GridStatisticsTaxaCount));
            _xmlBuilder.AppendLine(base.GetColumnNameRowLine(Resource.GridStatisticsObservationCount));
            _xmlBuilder.AppendLine(base.GetColumnNameRowLine(String.Format("Centre coordinate X ({0})", data.GridCellCoordinateSystem)));
            _xmlBuilder.AppendLine(base.GetColumnNameRowLine(String.Format("Centre coordinate Y ({0})", data.GridCellCoordinateSystem)));
            _xmlBuilder.AppendLine(base.GetColumnNameRowLine(String.Format("Centre coordinate X ({0})", coordinateSystem.ToString())));
            _xmlBuilder.AppendLine(base.GetColumnNameRowLine(String.Format("Centre coordinate Y ({0})", coordinateSystem.ToString())));
            _xmlBuilder.AppendLine(base.GetColumnNameRowLine("Grid cell width (metres)"));
            ////_xmlBuilder.AppendLine(base.getColumnNameRowLine("long"));
            ////_xmlBuilder.AppendLine(base.getColumnNameRowLine("lat"));
            _xmlBuilder.AppendLine(base.GetRowEnd());

            //Data values
            foreach (TaxonGridCellResult row in data.Cells)
            {
                _xmlBuilder.AppendLine(base.GetRowStart());
                _xmlBuilder.AppendLine(base.GetDataRowLine("String", row.Identifier));
                _xmlBuilder.AppendLine(base.GetDataRowLine("Number", row.SpeciesCount.ToString()));
                _xmlBuilder.AppendLine(base.GetDataRowLine("Number", row.ObservationCount.ToString()));
                _xmlBuilder.AppendLine(base.GetDataRowLine("Number", row.OriginalCentreCoordinateX.ToString()));
                _xmlBuilder.AppendLine(base.GetDataRowLine("Number", row.OriginalCentreCoordinateY.ToString()));
                _xmlBuilder.AppendLine(base.GetDataRowLine("Number", row.CentreCoordinateX.ToString(base.GetApprotiateGlobalization())));
                _xmlBuilder.AppendLine(base.GetDataRowLine("Number", row.CentreCoordinateY.ToString(base.GetApprotiateGlobalization())));
                _xmlBuilder.AppendLine(base.GetDataRowLine("Number", data.GridCellSize.ToString()));
                ////Point convertedPoint = GisTools.CoordinateConversionManager.GetConvertedPoint(
                ////    new Point(row.CentreCoordinateX, row.CentreCoordinateY),
                ////    new CoordinateSystem(CoordinateSystemId.GoogleMercator),
                ////    new CoordinateSystem(CoordinateSystemId.WGS84));
                ////_xmlBuilder.AppendLine(base.getDataRowLine("Number", convertedPoint.X.ToString(CultureInfo.InvariantCulture.NumberFormat)));
                ////_xmlBuilder.AppendLine(base.getDataRowLine("Number", convertedPoint.Y.ToString(CultureInfo.InvariantCulture.NumberFormat)));
                _xmlBuilder.AppendLine(base.GetRowEnd());
            }

            // Add final section of the xml document.
            _xmlBuilder.AppendLine(GetFinalSection(GetAditionalSheets(currentUser, addSettings, addProvenance)));
        }
        public static string GetLayerGeojson(
            IUserContext currentUser,
            int layerId,
            CoordinateSystemId coordinateSystemId,
            IDictionary <string, object> parameters,
            out string layerName,
            MapExportModel.Extent mapExtent)
        {
            string geoJson = null;

            layerName = null;

            if (layerId == null)
            {
                return(null);
            }

            if (layerId >= CustomLayersStartLayerId)
            {
                var viewManager = new WfsLayersViewManager(currentUser, SessionHandler.MySettings);

                var layer = viewManager.GetWfsLayers().FirstOrDefault(l => l.Id == layerId);
                layerName = layer.Name;

                if (layer.IsFile)
                {
                    geoJson = JsonConvert.SerializeObject(MySettingsManager.GetMapDataFeatureCollection(currentUser, layer.GeometryName, coordinateSystemId));
                }
                else
                {
                    var url = WFSFilterUtils.GetResultingUrl(layer.ServerUrl, layer.TypeName, "1.1.0", layer.Filter, "application%2Fjson", null, string.Format("EPSG%3A{0}", coordinateSystemId.Srid()));

                    var request = WebRequest.Create(url);
                    request.Credentials = CredentialCache.DefaultCredentials;
                    // Get the response.

                    using (var response = request.GetResponse())
                    {
                        using (var dataStream = response.GetResponseStream())
                        {
                            using (var reader = new StreamReader(dataStream))
                            {
                                geoJson = reader.ReadToEnd();
                                reader.Close();
                            }
                        }
                        response.Close();
                    }
                }
            }
            else
            {
                switch (layerId)
                {
                case SpeciesRichnessGridLayerId:
                    var taxonGridCalculator = new TaxonGridCalculator(currentUser, SessionHandler.MySettings);
                    geoJson = taxonGridCalculator.GetTaxonGridAsGeoJson();

                    var attribute = (string)(parameters.ContainsKey("attribute") ? parameters["attribute"] : null);

                    layerName = Resource.ResultViewSpeciesRichnessGridMapLayerName;
                    if (attribute != null)
                    {
                        switch (attribute.ToLower().Trim())
                        {
                        case "speciescount":
                            layerName = Resource.ResultDownloadSpeciesRichnessGridMap;
                            break;

                        case "observationcount":
                            layerName = Resource.ResultDownloadObservationsGridMap;
                            break;
                        }
                    }

                    break;

                case SpeciesObservationGridMapLayerId:
                case EooConvexHullLayerId:
                case EooConcaveHullLayerId:
                    var speciesObservationGridCalculator = new SpeciesObservationGridCalculator(currentUser, SessionHandler.MySettings);
                    if (layerId == SpeciesObservationGridMapLayerId)
                    {
                        geoJson   = speciesObservationGridCalculator.GetSpeciesObservationGridAsGeoJson();
                        layerName = Resource.ResultViewSpeciesObservationGridMap;
                    }
                    else
                    {
                        var alphaValue     = (int?)(parameters.ContainsKey("alphaValue") ? parameters["alphaValue"] : null);
                        var useCenterPoint = (bool?)(parameters.ContainsKey("useCenterPoint") ? parameters["useCenterPoint"] : null);
                        geoJson = speciesObservationGridCalculator.GetSpeciesObservationAOOEOOAsGeoJson(
                            layerId == EooConcaveHullLayerId ? alphaValue : 0,
                            useCenterPoint ?? true);
                        layerName = Resource.MapEOOLayer;
                    }

                    break;

                case ObservationsLayerId:     //Observations
                case SpeciesObservationClusterPointMapLayerId:
                    SpeciesObservationResultCalculator resultCalculator = null;
                    try
                    {
                        var displayCoordinateSystemId = SessionHandler.MySettings.Presentation.Map.PresentationCoordinateSystemId;
                        resultCalculator = new SpeciesObservationResultCalculator(currentUser, SessionHandler.MySettings);
                        geoJson          = resultCalculator.GetSpeciesObservationsAsGeoJson(displayCoordinateSystemId);
                        layerName        = Resource.MapLayerObservations;
                    }
                    catch (Exception)
                    {
                    }
                    break;
                }
            }

            return(geoJson);
        }