/// <summary>
        /// Converts a bounding box to a GeoJSON FeatureCollection.
        /// </summary>
        /// <param name="boundingBox">The bounding box.</param>
        /// <returns>A feature collection containing one feature.</returns>
        public static FeatureCollection ConvertToGeoJSONFeatureCollection(BoundingBox boundingBox)
        {
            FeatureCollection featureCollection = new FeatureCollection(new List <Feature>());

            if (boundingBox == null)
            {
                return(featureCollection);
            }

            Polygon polygon = new Polygon(new List <LineString>());
            List <GeographicPosition> coordinates = new List <GeographicPosition>();

            coordinates.Add(new GeographicPosition(boundingBox.Min.X, boundingBox.Min.Y));
            coordinates.Add(new GeographicPosition(boundingBox.Min.X, boundingBox.Max.Y));
            coordinates.Add(new GeographicPosition(boundingBox.Max.X, boundingBox.Max.Y));
            coordinates.Add(new GeographicPosition(boundingBox.Max.X, boundingBox.Min.Y));
            coordinates.Add(new GeographicPosition(boundingBox.Min.X, boundingBox.Min.Y));
            var lineString = new LineString(coordinates);

            polygon.Coordinates.Add(lineString);
            Feature feature = new Feature(polygon);

            featureCollection.Features.Add(feature);

            return(featureCollection);
        }
        /// <summary>
        /// Converts a list of DataPolygons to a GeoJSON FeatureCollection.
        /// </summary>
        /// <param name="dataPolygons">The DataPolygons.</param>
        /// <returns></returns>
        public static FeatureCollection ConvertToGeoJSONFeatureCollection(List <DataPolygon> dataPolygons)
        {
            var featureCollection = new FeatureCollection(new List <Feature>());

            foreach (DataPolygon dataPolygon in dataPolygons)
            {
                Polygon polygon = ConvertToGeoJSONPolygon(dataPolygon);
                var     feature = new Feature(polygon);
                featureCollection.Features.Add(feature);
            }

            return(featureCollection);
        }
        public void Add_and_remove_spatial_filter()
        {
            var     filterController = new FilterController();
            Polygon polygon1         = CreateTestPolygon1();
            Polygon polygon2         = CreateTestPolygon2();
            var     feature1         = new Feature(polygon1);
            var     feature2         = new Feature(polygon2);
            var     features         = new List <Feature> {
                feature1, feature2
            };
            var    featureCollection = new FeatureCollection(features);
            string geojson           = JsonConvert.SerializeObject(featureCollection, JsonHelper.GetDefaultJsonSerializerSettings());

            // Update spatial filter
            JsonNetResult result     = filterController.UpdateSpatialFilter(geojson);
            JsonModel     jsonResult = (JsonModel)result.Data;

            Assert.IsTrue(jsonResult.Success);
            Assert.IsTrue(SessionHandler.MySettings.Filter.Spatial.IsActive == true);
            Assert.IsTrue(SessionHandler.MySettings.Filter.Spatial.Polygons.Count == 2);

            DataPolygon dataPolygon1 = SessionHandler.MySettings.Filter.Spatial.Polygons[0];

            Assert.IsTrue(Math.Abs(dataPolygon1.LinearRings[0].Points[0].X - ((GeographicPosition)polygon1.Coordinates[0].Coordinates[0]).Longitude) < 0.001);
            Assert.IsTrue(Math.Abs(dataPolygon1.LinearRings[0].Points[1].X - ((GeographicPosition)polygon1.Coordinates[0].Coordinates[1]).Longitude) < 0.001);
            Assert.IsTrue(Math.Abs(dataPolygon1.LinearRings[0].Points[2].X - ((GeographicPosition)polygon1.Coordinates[0].Coordinates[2]).Longitude) < 0.001);
            Assert.IsTrue(Math.Abs(dataPolygon1.LinearRings[0].Points[3].X - ((GeographicPosition)polygon1.Coordinates[0].Coordinates[3]).Longitude) < 0.001);
            Assert.IsTrue(Math.Abs(dataPolygon1.LinearRings[0].Points[0].Y - ((GeographicPosition)polygon1.Coordinates[0].Coordinates[0]).Latitude) < 0.001);
            Assert.IsTrue(Math.Abs(dataPolygon1.LinearRings[0].Points[1].Y - ((GeographicPosition)polygon1.Coordinates[0].Coordinates[1]).Latitude) < 0.001);
            Assert.IsTrue(Math.Abs(dataPolygon1.LinearRings[0].Points[2].Y - ((GeographicPosition)polygon1.Coordinates[0].Coordinates[2]).Latitude) < 0.001);
            Assert.IsTrue(Math.Abs(dataPolygon1.LinearRings[0].Points[3].Y - ((GeographicPosition)polygon1.Coordinates[0].Coordinates[3]).Latitude) < 0.001);


            // Get spatial filter
            result     = filterController.GetSpatialFilterAsGeoJSON();
            jsonResult = (JsonModel)result.Data;
            Assert.IsTrue(jsonResult.Success);
            FeatureCollection featureCollectionFromServer = (FeatureCollection)jsonResult.Data;

            Assert.IsTrue(featureCollectionFromServer.Features.Count == 2);


            // Clear spatial filter
            result     = filterController.ClearSpatialFilter();
            jsonResult = (JsonModel)result.Data;
            Assert.IsTrue(jsonResult.Success);
            Assert.IsTrue(SessionHandler.MySettings.Filter.Spatial.Polygons.Count == 0);
        }
        private Polygon CreateTestPolygon2()
        {
            var lineString = new LineString(new List <GeographicPosition>()
            {
                new GeographicPosition(10, 11),
                new GeographicPosition(12, 13),
                new GeographicPosition(14, 15),
                new GeographicPosition(10, 11)
            });
            var lineStrings = new List <LineString>();

            lineStrings.Add(lineString);
            var polygon = new Polygon(lineStrings);

            return(polygon);
        }
        private Polygon CreateTestPolygon1()
        {
            var lineString = new LineString(new List <GeographicPosition>()
            {
                new GeographicPosition(11.0878902207, 45.1602390564),
                new GeographicPosition(15.01953125, 48.1298828125),
                new GeographicPosition(18.01953125, 49.1298828125),
                new GeographicPosition(11.0878902207, 45.1602390564)
            });
            var lineStrings = new List <LineString>();

            lineStrings.Add(lineString);
            var polygon = new Polygon(lineStrings);

            return(polygon);
        }
        private void DrawMap(Graphics canvas)
        {
            using (var pen = new Pen(ColorLine, 1))
            {
                // Draw background map from initialized geojson file
                for (int recordIndex = 0; recordIndex < _geoJsonFile.Features.Count; recordIndex++)
                {
                    Feature        feature     = _geoJsonFile.Features[recordIndex];
                    List <Polygon> polygonList = new List <Polygon>();
                    if (feature.Geometry.Type == GeoJSONObjectType.MultiPolygon)
                    {
                        MultiPolygon multiPolygon = (MultiPolygon)feature.Geometry;
                        foreach (Polygon polygon in multiPolygon.Coordinates)
                        {
                            polygonList.Add(polygon);
                        }
                    }
                    else if (feature.Geometry.Type == GeoJSONObjectType.Polygon)
                    {
                        Polygon polygon = (Polygon)feature.Geometry;
                        polygonList.Add(polygon);
                    }

                    foreach (Polygon polygon in polygonList)
                    {
                        for (int i = 0; i < polygon.Coordinates.Count; i++)
                        {
                            var points = new List <System.Drawing.Point>();

                            for (int j = 0; j < polygon.Coordinates[i].Coordinates.Count; j++)
                            {
                                var coordinate = polygon.Coordinates[i].Coordinates[j];
                                var point      = new System.Drawing.Point(LEGEND_BOX_WIDTH - _legendBoxOffsetX + (int)((coordinate.Longitude - _xMin) * _scale) + MAP_OFFSET_LEFT, _bitmapHeight - (int)((coordinate.Latitude - _yMin) * _scale) + MAP_OFFSET_TOP);
                                points.Add(point);
                            }

                            using (var brush = new SolidBrush(ColorLandBackground))
                            {
                                canvas.FillPolygon(brush, points.ToArray());
                                canvas.DrawPolygon(pen, points.ToArray());
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// A rectangle over the southernparts of Sweden
        /// </summary>
        /// <returns></returns>
        private Polygon CreateTestPolygon1()
        {
            var lineString = new LineString(new List <GeographicPosition>()
            {
                new GeographicPosition(1334896.2617864, 7413599.5228778),
                new GeographicPosition(1334896.2617864, 9067085.3185126),
                new GeographicPosition(2249699.6161761, 9067085.3185126),
                new GeographicPosition(2249699.6161761, 7413599.5228778),
                new GeographicPosition(1334896.2617864, 7413599.5228778)
            });
            var lineStrings = new List <LineString>();

            lineStrings.Add(lineString);
            var polygon = new Polygon(lineStrings);

            return(polygon);
        }
Esempio n. 8
0
        /// <summary>
        /// Calculates a species observation grid and creates a Feature Collection.
        /// </summary>
        /// <returns>A feature collection containing all grid cells.</returns>
        public FeatureCollection GetSpeciesObservationGridAsFeatureCollection()
        {
            SpeciesObservationGridResult speciesObservationGridResult = GetSpeciesObservationGridResultFromCacheIfAvailableOrElseCalculate();

            List <Feature> features = new List <Feature>();

            foreach (SpeciesObservationGridCellResult gridCell in speciesObservationGridResult.Cells)
            {
                Dictionary <string, object> properties = new Dictionary <string, object>();
                properties.Add("Id", gridCell.Identifier);
                properties.Add("ObservationCount", gridCell.ObservationCount);
                properties.Add("CentreCoordinateX", gridCell.CentreCoordinateX);
                properties.Add("CentreCoordinateY", gridCell.CentreCoordinateY);
                properties.Add("OriginalCentreCoordinateX", gridCell.OriginalCentreCoordinateX);
                properties.Add("OriginalCentreCoordinateY", gridCell.OriginalCentreCoordinateY);

                List <GeographicPosition> coordinates = new List <GeographicPosition>();
                coordinates.Add(new GeographicPosition(gridCell.BoundingBox[0][0], gridCell.BoundingBox[0][1]));
                coordinates.Add(new GeographicPosition(gridCell.BoundingBox[1][0], gridCell.BoundingBox[1][1]));
                coordinates.Add(new GeographicPosition(gridCell.BoundingBox[2][0], gridCell.BoundingBox[2][1]));
                coordinates.Add(new GeographicPosition(gridCell.BoundingBox[3][0], gridCell.BoundingBox[3][1]));
                coordinates.Add(new GeographicPosition(gridCell.BoundingBox[0][0], gridCell.BoundingBox[0][1]));

                LineString lineString = new LineString(coordinates);

                List <LineString> linearRings = new List <LineString>();
                linearRings.Add(lineString);
                ArtDatabanken.GIS.GeoJSON.Net.Geometry.Polygon polygon = new ArtDatabanken.GIS.GeoJSON.Net.Geometry.Polygon(linearRings);

                Feature feature = new Feature(polygon, properties);
                features.Add(feature);
            }

            FeatureCollection featureCollection = new FeatureCollection(features);

            featureCollection.CRS = new NamedCRS(MySettings.Presentation.Map.PresentationCoordinateSystemId.EpsgCode());

            return(featureCollection);
        }
        ///// <summary>
        ///// Converts a list of DataPolygons to a GeoJSON FeatureCollection.
        ///// </summary>
        ///// <param name="dataPolygons">The DataPolygons.</param>
        ///// <returns></returns>
        //public static FeatureCollection ConvertToGeoJSONFeatureCollection(List<DataPolygon> dataPolygons, IUserContext userContext, CoordinateSystem fromCoordinateSystem, CoordinateSystem toCoordinateSystem)
        //{
        //    var featureCollection = new FeatureCollection(new List<Feature>());
        //    DataContext dataContext = new DataContext(userContext);
        //    foreach (DataPolygon dataPolygon in dataPolygons)
        //    {
        //        var pol = dataPolygon.ToPolygon(dataContext);
        //        pol = GisTools.CoordinateConversionManager.GetConvertedPolygon(pol, fromCoordinateSystem, toCoordinateSystem);

        //        Polygon polygon = ConvertToGeoJSONPolygon(dataPolygon);

        //        var feature = new Feature(polygon);
        //        featureCollection.Features.Add(feature);
        //    }

        //    return featureCollection;
        //}

        /// <summary>
        /// Converts a DataPolygon to a GeoJSON polygon.
        /// </summary>
        /// <param name="dataPolygon">The DataPolygon to convert.</param>
        /// <returns></returns>
        public static Polygon ConvertToGeoJSONPolygon(DataPolygon dataPolygon)
        {
            var polygon = new Polygon(new List <LineString>());

            if (dataPolygon.IsNull() || dataPolygon.LinearRings.IsEmpty())
            {
                return(polygon);
            }

            foreach (DataLinearRing dataLinearRing in dataPolygon.LinearRings)
            {
                var coordinates = new List <GeographicPosition>();
                foreach (DataPoint dataPoint in dataLinearRing.Points)
                {
                    var position = new GeographicPosition(dataPoint.X, dataPoint.Y, dataPoint.Z);
                    coordinates.Add(position);
                }
                var lineString = new LineString(coordinates);
                polygon.Coordinates.Add(lineString);
            }
            return(polygon);
        }
Esempio n. 10
0
        /// <summary>
        /// Generates the map.
        /// </summary>
        /// <returns>Map as bitmap file.</returns>
        private Bitmap GetBitmap()
        {
            Bitmap bitmap;
            float  xScale = 1.0F * _bitmapWidth / _width;
            float  yScale = 1.0F * _bitmapHeight / _height;

            if (xScale < yScale)
            {
                _scale = xScale;
            }
            else
            {
                _scale = yScale;
            }
            _bitmapHeight += _marginTop + _marginBottom;
            _bitmapWidth  += _marginLeft + _marginRight;

            bitmap = new Bitmap(_bitmapWidth + _legendBoxWidth - _legendBoxOffsetX, _bitmapHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            using (Graphics canvas = Graphics.FromImage(bitmap))
            {
                using (Pen pen = new Pen(_colorLine, 1))
                {
                    using (Brush backgroundBrush = new SolidBrush(_colorBackground))
                    {
                        canvas.FillRectangle(backgroundBrush, 0, 0, _bitmapWidth + _legendBoxWidth - _legendBoxOffsetX, _bitmapHeight);

                        //Draw background map from initialized geojson file
                        for (int recordIndex = 0; recordIndex < _geoJsonFile.Features.Count; recordIndex++)
                        {
                            Feature        feature     = _geoJsonFile.Features[recordIndex];
                            List <Polygon> polygonList = new List <Polygon>();
                            if (feature.Geometry.Type == GeoJSONObjectType.MultiPolygon)
                            {
                                MultiPolygon multiPolygon = (MultiPolygon)feature.Geometry;
                                foreach (Polygon polygon in multiPolygon.Coordinates)
                                {
                                    polygonList.Add(polygon);
                                }
                            }
                            else if (feature.Geometry.Type == GeoJSONObjectType.Polygon)
                            {
                                Polygon polygon = (Polygon)feature.Geometry;
                                polygonList.Add(polygon);
                            }

                            foreach (Polygon polygon in polygonList)
                            {
                                for (int i = 0; i < polygon.Coordinates.Count; i++)
                                {
                                    List <System.Drawing.Point> points = new List <System.Drawing.Point>();

                                    for (int j = 0; j < polygon.Coordinates[i].Coordinates.Count; j++)
                                    {
                                        GeographicPosition   coordinate = polygon.Coordinates[i].Coordinates[j];
                                        System.Drawing.Point point      = new System.Drawing.Point(_marginLeft + _legendBoxWidth - _legendBoxOffsetX + (int)((coordinate.Longitude - _xMin) * _scale), _bitmapHeight - _marginBottom - (int)((coordinate.Latitude - _yMin) * _scale));
                                        points.Add(point);
                                    }

                                    using (Brush countyBrush = new SolidBrush(_colorLandBackground))
                                    {
                                        canvas.FillPolygon(countyBrush, points.ToArray());
                                    }
                                }
                            }
                        }

                        //Heat rects
                        if (_observationCounts != null && _observationCounts.Count > 0)
                        {
                            int radius = (Int32)Math.Round(_observationCounts[0].GridCellSize * _scale * 0.5) + 1;

                            foreach (IGridCellSpeciesObservationCount cell in _observationCounts)
                            {
                                Double cellX = cell.OrginalGridCellCentreCoordinate.X;
                                Double cellY = cell.OrginalGridCellCentreCoordinate.Y;
                                System.Drawing.Point point = new System.Drawing.Point(_marginLeft + _legendBoxWidth - _legendBoxOffsetX + (int)((cellX - _xMin) * _scale), _bitmapHeight - _marginBottom - (int)((cellY - _yMin) * _scale));
                                using (Brush heatBrush = GetHeatBrush())
                                {
                                    canvas.FillEllipse(heatBrush, point.X - radius, point.Y - radius, 2 * radius, 2 * radius);
                                }
                            }
                        }

                        //Draw polygon border lines from initialized geojson file
                        for (int recordIndex = 0; recordIndex < _geoJsonFile.Features.Count; recordIndex++)
                        {
                            Feature        feature     = _geoJsonFile.Features[recordIndex];
                            List <Polygon> polygonList = new List <Polygon>();
                            if (feature.Geometry.Type == GeoJSONObjectType.MultiPolygon)
                            {
                                MultiPolygon multiPolygon = (MultiPolygon)feature.Geometry;
                                foreach (Polygon polygon in multiPolygon.Coordinates)
                                {
                                    polygonList.Add(polygon);
                                }
                            }
                            else if (feature.Geometry.Type == GeoJSONObjectType.Polygon)
                            {
                                Polygon polygon = (Polygon)feature.Geometry;
                                polygonList.Add(polygon);
                            }

                            foreach (Polygon polygon in polygonList)
                            {
                                for (int i = 0; i < polygon.Coordinates.Count; i++)
                                {
                                    List <System.Drawing.Point> points = new List <System.Drawing.Point>();

                                    for (int j = 0; j < polygon.Coordinates[i].Coordinates.Count; j++)
                                    {
                                        GeographicPosition   coordinate = polygon.Coordinates[i].Coordinates[j];
                                        System.Drawing.Point point      = new System.Drawing.Point(_marginLeft + _legendBoxWidth - _legendBoxOffsetX + (int)((coordinate.Longitude - _xMin) * _scale), _bitmapHeight - _marginBottom - (int)((coordinate.Latitude - _yMin) * _scale));
                                        points.Add(point);
                                    }

                                    canvas.DrawPolygon(pen, points.ToArray());
                                }
                            }
                        }
                    }
                }
            }

            return(bitmap);
        }
        public void ViewObservationsForLoggedInUser()
        {
            using (ShimsContext.Create())
            {
                //Login user
                LoginTestUserAnalyser();
                ShimFilePath();

                // Add another role to user context
                base.AddSightingRoleToUserContext();

                UserRoleModel roleModel       = new UserRoleModel();
                IRole         newAnalyzerRole = SessionHandler.UserContext.CurrentRoles[0];
                AccountController.ChangeUserRole(Convert.ToString(newAnalyzerRole.Id), roleModel);

                // Verify role
                Assert.IsTrue(SessionHandler.UserContext.CurrentRole.Id == newAnalyzerRole.Id);

                // Set language to swedish since reference data is on that language
                SetSwedishLanguage();

                // Create controllers
                FilterController filterController = new FilterController();
                ResultController resultController = new ResultController();
                StubResultController(resultController);
                MySettingsController settingsController = new MySettingsController();

                // TODO Add tests for presentation controller data
                FormatController presentationController = new FormatController();

                //Check that no data is set to mysettings, except for map and table that should be checked by default
                Assert.IsTrue(SessionHandler.MySettings.Filter.Taxa.TaxonIds.Count == 0);
                Assert.IsTrue(SessionHandler.MySettings.Presentation.Map.IsActive);
                Assert.IsTrue(SessionHandler.MySettings.Presentation.Table.IsActive);
                Assert.IsFalse(SessionHandler.MySettings.Filter.Taxa.IsActive);

                //*********************************   Set and test filter data ***************************
                //Select to filter on taxa and select taxa ids
                //SessionHandler.MySettings.Filter.TaxaSetting.IsSelected = true;

                filterController.RemoveAllFilteredTaxon("Home/Index");
                Assert.IsTrue(SessionHandler.MySettings.Filter.Taxa.TaxonIds.Count == 0);

                //Use taxon 100573-Griffelblomfluga
                int[]  taxaIds = new int[] { 100573 };
                string strJson = JsonConvert.SerializeObject(taxaIds);
                filterController.AddTaxaToFilter(strJson, "Home/Index");
                Assert.IsTrue(SessionHandler.MySettings.Filter.Taxa.TaxonIds.Contains(100573));
                Assert.IsTrue(SessionHandler.MySettings.Filter.Taxa.TaxonIds.Count == 1);

                //****************************  Set spatial data ***************************************************
                Polygon polygon1 = CreateTestPolygon1();
                var     feature1 = new Feature(polygon1);
                var     features = new List <Feature> {
                    feature1
                };
                var    featureCollection = new FeatureCollection(features);
                string geojson           = JsonConvert.SerializeObject(featureCollection, JsonHelper.GetDefaultJsonSerializerSettings());

                // Update spatial filter
                JsonNetResult filterResult     = filterController.UpdateSpatialFilter(geojson);
                JsonModel     jsonFilterResult = (JsonModel)filterResult.Data;
                Assert.IsTrue(jsonFilterResult.Success);
                Assert.IsTrue(SessionHandler.MySettings.Filter.Spatial.IsActive == true);
                Assert.IsTrue(SessionHandler.MySettings.Filter.Spatial.Polygons.Count == 1);

                //**************************** Check map data *******************************************************
                // Sow map view for Griffelblomfluga
                int        numberOfObservations = 0;
                ViewResult result = resultController.SpeciesObservationMap();
                Assert.IsNotNull(result);

                //Get the map data to be shown
                JsonNetResult obsMap = resultController.GetObservationsAsGeoJSON();
                Assert.IsNotNull(obsMap.Data);
                var jsonMapResult = (JsonModel)obsMap.Data;
                var noObs         = (SpeciesObservationsGeoJsonModel)jsonMapResult.Data;
                numberOfObservations = noObs.Points.Features.Count;
                Assert.IsTrue(SessionHandler.MySettings.Presentation.Map.IsActive);
                Assert.IsTrue(SessionHandler.MySettings.Presentation.Table.IsActive);
                Assert.IsTrue(SessionHandler.MySettings.Filter.Taxa.IsActive);

                //************************ Check table data *********************************************
                //Select to show table data from observations
                result = resultController.SpeciesObservationTable() as ViewResult;
                Assert.IsNotNull(result);
                var viewModel = result.ViewData.Model as ViewTableViewModel;
                Assert.IsNotNull(viewModel);
                //Get observations
                var obsResult = resultController.GetPagedObservationListAsJSON(1, 0, 25);
                Assert.IsNotNull(obsResult);
                JsonModel jsonResult = (JsonModel)obsResult.Data;
                List <Dictionary <string, string> > observationListResult = (List <Dictionary <string, string> >)jsonResult.Data;

                //Check observations
                Assert.IsNotNull(jsonResult);
                Assert.IsTrue(jsonResult.Success);
                Assert.IsTrue(jsonResult.Total > 5);
                Assert.IsTrue(observationListResult.Count > 5);
                Assert.IsTrue(numberOfObservations == observationListResult.Count, "Number of observations differs between Map and ObsTable");
                //Verfy observations
                BaseDataTest.VerifyObservationDataForGriffelblomfluga1000573(observationListResult);

                //*********************** Check detailsPanel ****************************************************
                string            observationGUID   = observationListResult[5]["ObservationId"];
                DetailsController detailsController = new DetailsController();

                //Act
                // Returning "hard coded" value för first observation for griffelblomfluga
                var detailsResult = detailsController.ObservationDetail(observationGUID) as ViewResult;

                //Assert
                Assert.IsNotNull(detailsResult);

                //*********************** Verfy user settings, save and load *************************************

                // Save settings
                settingsController.SaveMySettings("Home/Index");

                Assert.IsTrue(SessionHandler.MySettings.Filter.Taxa.TaxonIds.Count > 0);

                //Logout TestUser
                LogoutTestUser();

                Assert.IsTrue(SessionHandler.MySettings.Filter.Taxa.TaxonIds.Count == 0);
                Assert.IsTrue(SessionHandler.MySettings.Presentation.Map.IsActive);
                Assert.IsTrue(SessionHandler.MySettings.Presentation.Table.IsActive);
                Assert.IsFalse(SessionHandler.MySettings.Filter.Taxa.IsActive);

                LoginTestUserAnalyser();
                //Use taxon 100573-Griffelblomfluga
                taxaIds = new int[] { 100573 };
                strJson = JsonConvert.SerializeObject(taxaIds);
                filterController.AddTaxaToFilter(strJson, "Home/Index");

                result = resultController.SpeciesObservationTable() as ViewResult;
                Assert.IsNotNull(result);

                // Check observations in table
                obsResult = resultController.GetPagedObservationListAsJSON(1, 0, 25);
                Assert.IsNotNull(obsResult);
                jsonResult            = (JsonModel)obsResult.Data;
                observationListResult = (List <Dictionary <string, string> >)jsonResult.Data;
                Assert.IsNotNull(jsonResult);
                Assert.IsTrue(jsonResult.Success);

                Assert.IsTrue(jsonResult.Total > 5);
                Assert.IsTrue(observationListResult.Count > 5);
                Assert.IsTrue(numberOfObservations == observationListResult.Count, "Number of observations differs between saved settings and not saved settings for ObsTable");

                BaseDataTest.VerifyObservationDataForGriffelblomfluga1000573(observationListResult);


                // Reset to english language
                SetEnglishLanguage();
            }
        }
        public void ViewObservations()
        {
            //Arrange
            // Set language to swedish since reference data is on that language
            SetSwedishLanguage();
            FilterController filterController = new FilterController();
            ResultController resultController = new ResultController();

            // TODO Add tests for presentation controller data
            FormatController presentationController = new FormatController();

            //Act and Assert
            Assert.IsTrue(SessionHandler.MySettings.Filter.Taxa.TaxonIds.Count == 0);

            filterController.RemoveAllFilteredTaxon("Home/Index");
            Assert.IsTrue(SessionHandler.MySettings.Filter.Taxa.TaxonIds.Count == 0);

            int[]  taxaIds = new int[] { 100573 };
            string strJson = JsonConvert.SerializeObject(taxaIds);

            filterController.AddTaxaToFilter(strJson, "Home/Index");
            Assert.IsTrue(SessionHandler.MySettings.Filter.Taxa.TaxonIds.Contains(100573));
            Assert.IsTrue(SessionHandler.MySettings.Filter.Taxa.TaxonIds.Count == 1);

            //****************************  Set spatial data ***************************************************

            Polygon polygon1 = CreateTestPolygon1();
            var     feature1 = new Feature(polygon1);
            var     features = new List <Feature> {
                feature1
            };
            var    featureCollection = new FeatureCollection(features);
            string geojson           = JsonConvert.SerializeObject(featureCollection, JsonHelper.GetDefaultJsonSerializerSettings());
            // Update spatial filter
            JsonNetResult filterResult     = filterController.UpdateSpatialFilter(geojson);
            JsonModel     jsonFilterResult = (JsonModel)filterResult.Data;

            Assert.IsTrue(jsonFilterResult.Success);
            Assert.IsTrue(SessionHandler.MySettings.Filter.Spatial.IsActive == true);
            Assert.IsTrue(SessionHandler.MySettings.Filter.Spatial.Polygons.Count == 1);

            //**************************** Check map data *******************************************************
            // Sow map view for Griffelblomfluga
            ViewResult pvResult = resultController.SpeciesObservationMap();

            Assert.IsNotNull(pvResult);
            // Test that correct view is returned, Todo: No view is returned, delete?
            // Assert.AreEqual("SpeciesObservationMap", pvResult.ViewName);
            //Get the map data to be shown
            JsonNetResult obsMap = resultController.GetObservationsAsGeoJSON();

            Assert.IsNotNull(obsMap.Data);
            var jsonMapResult        = (JsonModel)obsMap.Data;
            var noObs                = (SpeciesObservationsGeoJsonModel)jsonMapResult.Data;
            int numberOfObservations = noObs.Points.Features.Count;

            //Check settings
            Assert.IsTrue(SessionHandler.MySettings.Presentation.Map.IsActive);
            Assert.IsFalse(SessionHandler.MySettings.Presentation.Table.IsActive);
            Assert.IsTrue(SessionHandler.MySettings.Filter.Taxa.IsActive);

            //************************ Check table data *********************************************

            var result = resultController.SpeciesObservationTable() as ViewResult;

            Assert.IsNotNull(result);
            // Test that correct view is returned
            Assert.AreEqual("Table", result.ViewName);

            var viewModel = result.ViewData.Model as ViewTableViewModel;

            Assert.IsNotNull(viewModel);


            var obsResult = resultController.GetPagedObservationListAsJSON(1, 0, 25);

            Assert.IsNotNull(obsResult);

            JsonModel jsonResult = (JsonModel)obsResult.Data;
            List <Dictionary <string, string> > observationListResult = (List <Dictionary <string, string> >)jsonResult.Data;

            Assert.IsNotNull(jsonResult);
            Assert.IsTrue(jsonResult.Success);

            Assert.IsTrue(jsonResult.Total > 20);
            Assert.IsTrue(observationListResult.Count > 20);
            Assert.IsTrue(numberOfObservations == observationListResult.Count, "Number of observations differs between Map and ObsTable");


            BaseDataTest.VerifyObservationDataForGriffelblomfluga1000573(observationListResult);

            //*********************** Check detailsPanel ****************************************************
            string            observationGUID   = observationListResult[20]["ObservationId"];
            DetailsController detailsController = new DetailsController();

            //Act
            // Returning "hard coded" value för first observation for griffelblomfluga
            var detailsResult = detailsController.ObservationDetail(observationGUID) as ViewResult;

            //Assert
            Assert.IsNotNull(detailsResult);
            var detailsViewModel = detailsResult.ViewData.Model as ObservationDetailViewModel;

            Assert.IsNotNull(detailsViewModel);
            // Test that correct view is returned
            Assert.AreEqual("Detail", detailsResult.ViewName);

            //Check number of properties
            Assert.IsTrue(detailsViewModel.Fields.Count > 10);


            // Reset to english language
            SetEnglishLanguage();
        }