Beispiel #1
0
        private Data.Polygon GetPolygon(double bottom, double left, double right, double top)
        {
            Data.Polygon polygon = new Data.Polygon();
            polygon.LinearRings = new List <ILinearRing>();
            Data.LinearRing linearRing = new LinearRing();
            BoundingBox     swedenExtentBoundingBox = SwedenExtentManager.GetSwedenExtentBoundingBox(MySettings.Presentation.Map.DisplayCoordinateSystem);

            double limitBottom = bottom;
            double limitLeft   = left;
            double limitRight  = right;
            double limitTop    = top;

            limitBottom = Math.Max(bottom, swedenExtentBoundingBox.Min.Y);
            limitTop    = Math.Min(top, swedenExtentBoundingBox.Max.Y);
            limitLeft   = Math.Max(left, swedenExtentBoundingBox.Min.X);
            limitRight  = Math.Min(right, swedenExtentBoundingBox.Max.X);

            linearRing.Points = new List <IPoint>();
            linearRing.Points.Add(new Data.Point(limitLeft, limitBottom));
            linearRing.Points.Add(new Data.Point(limitLeft, limitTop));
            linearRing.Points.Add(new Data.Point(limitRight, limitTop));
            linearRing.Points.Add(new Data.Point(limitRight, limitBottom));
            linearRing.Points.Add(new Data.Point(limitLeft, limitBottom));
            polygon.LinearRings.Add(linearRing);
            return(polygon);
        }
        /// <summary>
        /// Calculates the number of grid cells that will be generated.
        /// If no bounding box is specified in gridSpecification,
        /// the sweden extent will be used.
        /// </summary>
        /// <param name="gridSpecification">The grid specification.</param>
        /// <returns>The number of grid cells that will be generated.</returns>
        public long CalculateNumberOfGridCells(GridSpecification gridSpecification)
        {
            if (gridSpecification.GridCellSize <= 0)
            {
                throw new ArgumentException("GridCellSize must be greater than 0.");
            }

            IBoundingBox boundingBox = gridSpecification.BoundingBox;

            if (boundingBox == null)
            {
                boundingBox = SwedenExtentManager.GetSwedenExtentBoundingBox(gridSpecification.GridCoordinateSystem.ToCoordinateSystem());
            }

            double xMin, yMin, xMax, yMax;
            int    cellSize = gridSpecification.GridCellSize;

            xMin = Math.Floor(boundingBox.Min.X / cellSize) * cellSize;
            xMax = Math.Ceiling(boundingBox.Max.X / cellSize) * cellSize;
            yMin = Math.Floor(boundingBox.Min.Y / cellSize) * cellSize;
            yMax = Math.Ceiling(boundingBox.Max.Y / cellSize) * cellSize;

            long count = (long)(((xMax - xMin) / cellSize) * ((yMax - yMin) / cellSize));

            return(count);
        }
Beispiel #3
0
        private IList <IGridCellSpeciesObservationCount> AddEmptyGridCells(IList <IGridCellSpeciesObservationCount> gridCellObservations, GridSpecification gridSpecification, ICoordinateSystem displayCoordinateSystem)
        {
            long numberOfGridCellsThatWillBeGenerated = GisTools.GridCellManager.CalculateNumberOfGridCells(gridSpecification);

            if (numberOfGridCellsThatWillBeGenerated > MAX_NUMBER_OF_GRID_CELLS)
            {
                throw new ArgumentException(string.Format("The cell size is too small, which would have resulted in the {0} grid cells. The limit lies at {1}.", numberOfGridCellsThatWillBeGenerated, MAX_NUMBER_OF_GRID_CELLS));
            }

            if (gridSpecification.BoundingBox == null)
            {
                gridSpecification.BoundingBox = SwedenExtentManager.GetSwedenExtentBoundingBox(gridSpecification.GridCoordinateSystem.ToCoordinateSystem());
            }

            List <GridCellBase>  allGridCells     = GisTools.GridCellManager.GenerateGrid(gridSpecification, displayCoordinateSystem);
            List <IGridCellBase> dataGridCells    = gridCellObservations.Cast <IGridCellBase>().ToList();
            List <IGridCellBase> missingGridCells = GisTools.GridCellManager.GetMissingGridCells(allGridCells.Cast <IGridCellBase>().ToList(), dataGridCells);

            foreach (IGridCellBase missingGridCell in missingGridCells)
            {
                GridCellSpeciesObservationCount gridCellSpeciesObservationCount = new GridCellSpeciesObservationCount();
                gridCellSpeciesObservationCount.ObservationCount = 0;
                missingGridCell.CopyPropertiesTo(gridCellSpeciesObservationCount);
                gridCellObservations.Add(gridCellSpeciesObservationCount);
            }

            return(gridCellObservations);
        }
        public void GetMissingGridCells_SwedenExtentMinusSwedenExtent_ReturnZeroGridCells()
        {
            List <GridCellBase>  swedenExtentGridCells = CreateGridCells(10000, SwedenExtentManager.GetSwedenExtentBoundingBox(new CoordinateSystem(CoordinateSystemId.SWEREF99_TM)));
            GridCellManager      gridCellManager       = new GridCellManager();
            List <IGridCellBase> missingGridCells      = gridCellManager.GetMissingGridCells(swedenExtentGridCells.Cast <IGridCellBase>().ToList(), swedenExtentGridCells.Cast <IGridCellBase>().ToList());

            Assert.AreEqual(0, missingGridCells.Count);
        }
        /// <summary>
        /// Generates grid cells according to the gridSpecification.
        /// </summary>
        /// <param name="gridSpecification">The grid specification.</param>
        /// <param name="displayCoordinateSystem">The display coordinate system.</param>
        /// <returns>A list of grid cells according to the grid specification.</returns>
        public List <GridCellBase> GenerateGrid(GridSpecification gridSpecification, ICoordinateSystem displayCoordinateSystem)
        {
            if (gridSpecification.GridCellSize <= 0)
            {
                throw new ArgumentException("GridCellSize must be greater than 0.");
            }

            List <GridCellBase> gridCells = new List <GridCellBase>();

            IBoundingBox boundingBox = gridSpecification.BoundingBox;

            if (boundingBox == null)
            {
                boundingBox = SwedenExtentManager.GetSwedenExtentBoundingBox(gridSpecification.GridCoordinateSystem.ToCoordinateSystem());
            }

            double xMin, yMin, xMax, yMax;
            int    cellSize;

            cellSize = gridSpecification.GridCellSize;

            xMin = Math.Floor(boundingBox.Min.X / cellSize) * cellSize;
            xMax = Math.Ceiling(boundingBox.Max.X / cellSize) * cellSize;
            yMin = Math.Floor(boundingBox.Min.Y / cellSize) * cellSize;
            yMax = Math.Ceiling(boundingBox.Max.Y / cellSize) * cellSize;

            ICoordinateSystem fromCoordinateSystem = gridSpecification.GridCoordinateSystem.ToCoordinateSystem();
            ICoordinateSystem toCoordinateSystem   = displayCoordinateSystem;

            // The last grid cell may exceed the boundary box.
            while (xMin < xMax)
            {
                // The last grid cell may exceed the boundary box.
                while (yMin < yMax)
                {
                    GridCellBase gridCell = new GridCellBase();
                    gridCell.GridCoordinateSystem            = gridSpecification.GridCoordinateSystem;
                    gridCell.GridCellSize                    = gridSpecification.GridCellSize;
                    gridCell.OrginalGridCellBoundingBox      = new BoundingBox();
                    gridCell.OrginalGridCellBoundingBox.Min  = new Point(xMin, yMin);
                    gridCell.OrginalGridCellBoundingBox.Max  = new Point(xMin + cellSize, yMin + cellSize);
                    gridCell.OrginalGridCellCentreCoordinate = new Point(Math.Floor(xMin / cellSize) * cellSize + cellSize * 0.5, Math.Floor(yMin / cellSize) * cellSize + cellSize * 0.5);

                    gridCell.GridCellBoundingBox      = GisTools.CoordinateConversionManager.GetConvertedBoundingBox(gridCell.OrginalGridCellBoundingBox, fromCoordinateSystem, toCoordinateSystem);
                    gridCell.CoordinateSystem         = displayCoordinateSystem;
                    gridCell.GridCellCentreCoordinate = GisTools.CoordinateConversionManager.GetConvertedPoint(gridCell.OrginalGridCellCentreCoordinate, fromCoordinateSystem, toCoordinateSystem);

                    gridCells.Add(gridCell);
                    yMin = yMin + cellSize;
                }

                xMin = xMin + cellSize;
                yMin = Math.Floor(boundingBox.Min.Y / cellSize) * cellSize;
            }

            return(gridCells);
        }
        public void GetMissingGridCells_SwedenExtentAndEightGridCells_ReturnSwedenExtentGridCellsExceptEightGridCells()
        {
            List <GridCellBase> swedenExtentGridCells = CreateGridCells(10000, SwedenExtentManager.GetSwedenExtentBoundingBox(new CoordinateSystem(CoordinateSystemId.SWEREF99_TM)));
            BoundingBox         boundingBox           = new BoundingBox
            {
                Min = new Point(400000, 7000000),
                Max = new Point(400000 + 20000, 7000000 + 40000)
            };
            List <GridCellBase>  eightGridCellsInsideSwedenExtent = CreateGridCells(10000, boundingBox);
            GridCellManager      gridCellManager  = new GridCellManager();
            List <IGridCellBase> missingGridCells = gridCellManager.GetMissingGridCells(swedenExtentGridCells.Cast <IGridCellBase>().ToList(), eightGridCellsInsideSwedenExtent.Cast <IGridCellBase>().ToList());

            Assert.AreEqual(swedenExtentGridCells.Count, missingGridCells.Count + eightGridCellsInsideSwedenExtent.Count);
        }
        public void CalculateNumberOfGridCells_BoundingBoxIsNotSpecified_SwedenExtentIsUsedAsBoundingBox()
        {
            GridSpecification gridSpecification = new GridSpecification();

            gridSpecification.GridCellSize         = 10000;
            gridSpecification.GridCoordinateSystem = GridCoordinateSystem.SWEREF99_TM;
            GridCellManager gridCellManager             = new GridCellManager();
            long            calculatedNumberOfGridCells = gridCellManager.CalculateNumberOfGridCells(gridSpecification);

            gridSpecification.BoundingBox = SwedenExtentManager.GetSwedenExtentBoundingBox(new CoordinateSystem(CoordinateSystemId.SWEREF99_TM));
            long calculatedNumberOfGridCellsWithBbox = gridCellManager.CalculateNumberOfGridCells(gridSpecification);

            Assert.AreEqual(calculatedNumberOfGridCellsWithBbox, calculatedNumberOfGridCells);
        }
        public void GenerateGridCellsAndCalculateNumberOfGridCells_SwedenExtent_GridCellsCountAndCalculatedNumberOfGridCellsShouldBeEqual()
        {
            GridSpecification gridSpecification = new GridSpecification();

            gridSpecification.GridCellSize         = 10000;
            gridSpecification.GridCoordinateSystem = GridCoordinateSystem.SWEREF99_TM;
            gridSpecification.BoundingBox          = SwedenExtentManager.GetSwedenExtentBoundingBox(new CoordinateSystem(CoordinateSystemId.SWEREF99_TM));
            CoordinateSystem displayCoordinateSystem = new CoordinateSystem(CoordinateSystemId.GoogleMercator);
            GridCellManager  gridCellManager         = new GridCellManager();

            List <GridCellBase> gridCells    = gridCellManager.GenerateGrid(gridSpecification, displayCoordinateSystem);
            long calculatedNumberOfGridCells = gridCellManager.CalculateNumberOfGridCells(gridSpecification);

            Assert.AreEqual(gridCells.Count, calculatedNumberOfGridCells);
        }