Example #1
0
        public void ExtentIntersectsNullPointValuesTest()
        {
            Extent container = new Extent(0, 0, 10, 20);

            Assert.IsFalse(container.Intersects(double.NaN, 0));
            Assert.IsFalse(container.Intersects(0, double.NaN));
        }
Example #2
0
        public void ExtentIntersectsNullCoordinateTest()
        {
            Extent     container = new Extent(0, 0, 10, 20);
            Coordinate c         = null;

            // ReSharper disable once ExpressionIsAlwaysNull
            Assert.Throws <ArgumentNullException>(delegate { container.Intersects(c); });
        }
Example #3
0
        public void ExtentIntersectsNullEnvelopeTest()
        {
            Extent   container = new Extent(0, 0, 10, 20);
            Envelope contained = null;

            // ReSharper disable once ExpressionIsAlwaysNull
            Assert.Throws <ArgumentNullException>(() => { container.Intersects(contained); });
        }
Example #4
0
        public void ExtentIntersectsPointValuesTest(ExtentTestData data)
        {
            Extent container = new Extent(data.ContainerValues);
            var    vals      = data.ContainedValues;

            Assert.AreEqual(data.ExpectedReturnValue, container.Intersects(vals[0], vals[1]), "ExtentIntersectsPointValues");

            // this is to make sure that the extent methods work the same way the IGeometry methods do
            var containerEnv = container.ToEnvelope().ToPolygon();
            var containedPnt = new Point(vals[0], vals[1]);

            Assert.AreEqual(data.ExpectedReturnValue, containerEnv.Intersects(containedPnt), "ExtentGeometryIntersectsPointValuesGeometry");
        }
Example #5
0
        private (int index, Vector2 hitPoint) NewLineSegmentPointIndex()
        {
            var p = RawPoint1;

            if (_targetPoints.Count > 2)
            {
                (float X, float Y)p1 = (0, 0);  // used for testing line segments
                (float X, float Y)p2 = (0, 0);  // used for testing line segments

                var v = p;
                var E = new Extent((p.X, p.Y), 15);

                v  = _targetPoints[1];
                p1 = (v.X, v.Y);

                for (int i = 2; i < _targetPoints.Count; i++)
                {
                    v  = _targetPoints[i];
                    p2 = (v.X, v.Y);

                    var e = new Extent(p1, p2);
                    if (e.Intersects(E))
                    {
                        if (e.IsHorizontal)
                        {
                            return(i - 1, new Vector2(p.X, p2.Y));
                        }
                        else if (e.IsVertical)
                        {
                            return(i - 1, new Vector2(p1.X, p.Y));
                        }
                        else
                        {
                            var dx = (double)(p2.X - p1.X);
                            var dy = (double)(p2.Y - p1.Y);

                            int xi = (int)(p1.X + (dx * (p.Y - p1.Y)) / dy);
                            if (E.ContainsX(xi))
                            {
                                return(i - 1, new Vector2(xi, p.Y));
                            }

                            xi = (int)(p2.X + (dx * (p.Y - p2.Y)) / dy);
                            if (E.ContainsX(xi))
                            {
                                return(i - 1, new Vector2(xi, p.Y));
                            }

                            int yi = (int)(p1.Y + (dy * (p.X - p1.X)) / dx);
                            if (E.ContainsY(yi))
                            {
                                return(i - 1, new Vector2(p.X, yi));
                            }

                            yi = (int)(p2.Y + (dy * (p.X - p2.X)) / dx);
                            if (E.ContainsY(yi))
                            {
                                return(i - 1, new Vector2(p.X, yi));
                            }
                        }
                    }
                    p1 = p2;
                }
            }
            return(-1, Vector2.Zero);
        }
Example #6
0
 private static bool TilesWithinEnvelope(IEnumerable <TileInfo> tileInfos, Extent evenlope)
 {
     return(tileInfos.All(tileInfo => evenlope.Intersects(tileInfo.Extent)));
 }
Example #7
0
        private List <SeriesDataCart> GetSeriesListForExtent(Extent extent, string[] keywords, double tileWidth, double tileHeight,
                                                             DateTime startDate, DateTime endDate, WebServiceNode[] serviceIDs,
                                                             IProgressHandler bgWorker, Func <SeriesDataCart, bool> seriesFilter)
        {
            var servicesToSearch = new List <Tuple <WebServiceNode[], Extent> >();

            if (serviceIDs.Length > 0)
            {
                foreach (var webService in serviceIDs)
                {
                    if (webService.ServiceBoundingBox == null)
                    {
                        servicesToSearch.Add(new Tuple <WebServiceNode[], Extent>(new[] { webService }, extent));
                        continue;
                    }
                    const double eps      = 0.05; //tolerance (0.05 deg) used for services whose bounding box is one point
                    var          wsBox    = webService.ServiceBoundingBox;
                    var          wsExtent = new Extent(wsBox.XMin - eps, wsBox.YMin - eps, wsBox.XMax + eps, wsBox.YMax + eps);
                    if (wsExtent.Intersects(extent))
                    {
                        servicesToSearch.Add(new Tuple <WebServiceNode[], Extent>(new[] { webService }, wsExtent.Intersection(extent)));
                    }
                }
            }
            else
            {
                servicesToSearch.Add(new Tuple <WebServiceNode[], Extent>(new WebServiceNode[] { }, extent));
            }

            var servicesWithExtents = new List <Tuple <WebServiceNode[], List <Extent> > >(servicesToSearch.Count);
            int totalTilesCount     = 0;

            foreach (var wsInfo in servicesToSearch)
            {
                var tiles = SearchHelper.CreateTiles(wsInfo.Item2, tileWidth, tileHeight);
                servicesWithExtents.Add(new Tuple <WebServiceNode[], List <Extent> >(wsInfo.Item1, tiles));
                totalTilesCount += tiles.Count;
            }

            var fullSeriesList   = new List <SeriesDataCart>();
            int currentTileIndex = 0;

            foreach (var wsInfo in servicesWithExtents)
            {
                var webServices = wsInfo.Item1;
                var tiles       = wsInfo.Item2;

                for (int i = 0; i < tiles.Count; i++, currentTileIndex++)
                {
                    var tile = tiles[i];

                    bgWorker.CheckForCancel();

                    // Do the web service call
                    var tileSeriesList = new List <SeriesDataCart>();
                    foreach (var keyword in keywords)
                    {
                        bgWorker.ReportMessage(
                            string.Format("Retrieving series from server. Keyword: {0}. Tile: {1} of {2}", keyword,
                                          currentTileIndex + 1, totalTilesCount));
                        bgWorker.CheckForCancel();
                        tileSeriesList.AddRange(GetSeriesCatalogForBox(tile.MinX, tile.MaxX, tile.MinY, tile.MaxY,
                                                                       keyword, startDate, endDate,
                                                                       webServices.Select(item => item.ServiceID).ToArray()));
                    }

                    fullSeriesList.AddRange(tileSeriesList.Where(seriesFilter));

                    // Report progress
                    var message         = string.Format("{0} Series found", fullSeriesList.Count);
                    var percentProgress = (currentTileIndex * 100) / totalTilesCount + 1;
                    bgWorker.ReportProgress(percentProgress, message);
                }
            }

            return(fullSeriesList);
        }
Example #8
0
        private List <BusinessObjects.Models.SeriesDataCartModel.SeriesDataCart> GetSeriesListForExtent(Extent extent, IEnumerable <string> keywords, double tileWidth, double tileHeight,
                                                                                                        DateTime startDate, DateTime endDate, ICollection <WebServiceNode> serviceIDs,
                                                                                                        BusinessObjects.Models.IProgressHandler bgWorker, Func <BusinessObjects.Models.SeriesDataCartModel.SeriesDataCart, bool> seriesFilter)
        {
            var servicesToSearch = new List <Tuple <WebServiceNode[], Extent> >();

            if (serviceIDs.Count > 0)
            {
                foreach (var webService in serviceIDs)
                {
                    if (webService.ServiceBoundingBox == null)
                    {
                        servicesToSearch.Add(new Tuple <WebServiceNode[], Extent>(new[] { webService }, extent));
                        continue;
                    }
                    const double eps      = 0.05; //tolerance (0.05 deg) used for services whose bounding box is one point
                    var          wsBox    = webService.ServiceBoundingBox;
                    var          wsExtent = new Extent(wsBox.XMin - eps, wsBox.YMin - eps, wsBox.XMax + eps, wsBox.YMax + eps);
                    if (wsExtent.Intersects(extent))
                    {
                        servicesToSearch.Add(new Tuple <WebServiceNode[], Extent>(new[] { webService }, wsExtent.Intersection(extent)));
                    }
                }
            }
            else
            {
                servicesToSearch.Add(new Tuple <WebServiceNode[], Extent>(new WebServiceNode[] { }, extent));
            }

            var servicesWithExtents = new List <Tuple <WebServiceNode[], List <Extent> > >(servicesToSearch.Count);
            int totalTilesCount     = 0;

            foreach (var wsInfo in servicesToSearch)
            {
                var tiles = SearchHelper.CreateTiles(wsInfo.Item2, tileWidth, tileHeight);
                servicesWithExtents.Add(new Tuple <WebServiceNode[], List <Extent> >(wsInfo.Item1, tiles));
                totalTilesCount += tiles.Count;
            }

            var  fullSeriesList   = new List <List <BusinessObjects.Models.SeriesDataCartModel.SeriesDataCart> >();
            long currentTileIndex = 0;
            int  tilesFinished    = 0;

            totalSeriesCount = 0;

            bgWorker.ReportProgress(0, "0 Series found");

            var serviceLoopOptions = new ParallelOptions
            {
                CancellationToken      = bgWorker.CancellationToken,
                MaxDegreeOfParallelism = 2,
            };
            var tileLoopOptions = new ParallelOptions
            {
                CancellationToken = bgWorker.CancellationToken,
                // Note: currently HIS Central returns timeout if many requests are sent in the same time.
                // To test set  MaxDegreeOfParallelism = -1
                MaxDegreeOfParallelism = 4,
            };

            Parallel.ForEach(servicesWithExtents, serviceLoopOptions, wsInfo =>
            {
                bgWorker.CheckForCancel();
                var ids   = wsInfo.Item1.Select(item => item.ServiceID).ToArray();
                var tiles = wsInfo.Item2;

                Parallel.ForEach(tiles, tileLoopOptions, tile =>
                {
                    var current = Interlocked.Add(ref currentTileIndex, 1);
                    bgWorker.CheckForCancel();

                    // Do the web service call
                    var tileSeriesList = new List <BusinessObjects.Models.SeriesDataCartModel.SeriesDataCart>();

                    if (SearchSettings.AndSearch == true)
                    {
                        //CHANGES FOR "AND" SEARCH
                        var totalTileSeriesList = new List <BusinessObjects.Models.SeriesDataCartModel.SeriesDataCart>();
                        var tileSeriesList2     = new List <BusinessObjects.Models.SeriesDataCartModel.SeriesDataCart>();
                        var tileSeriesList3     = new List <BusinessObjects.Models.SeriesDataCartModel.SeriesDataCart>();
                        var tileSeriesList4     = new List <BusinessObjects.Models.SeriesDataCartModel.SeriesDataCart>();

                        SeriesComparer sc = new SeriesComparer();

                        for (int i = 0; i < keywords.Count(); i++)
                        {
                            String keyword = keywords.ElementAt(i);

                            bgWorker.CheckForCancel();
                            var series = GetSeriesCatalogForBox(tile.MinX, tile.MaxX, tile.MinY, tile.MaxY, keyword, startDate, endDate, ids, bgWorker, current, totalTilesCount);
                            totalTileSeriesList.AddRange(series);
                            if (tileSeriesList.Count() == 0)
                            {
                                if (i == 0)
                                {
                                    tileSeriesList.AddRange(series);
                                }
                                else
                                {
                                    break;
                                }
                            }
                            else
                            {
                                tileSeriesList2.AddRange(tileSeriesList.Intersect(series, sc));
                                tileSeriesList.Clear();
                                tileSeriesList.AddRange(tileSeriesList2);
                                tileSeriesList2.Clear();
                            }
                        }


                        for (int i = 0; i < tileSeriesList.Count(); i++)
                        {
                            tileSeriesList4 = totalTileSeriesList.Where(item => (item.SiteName.Equals(tileSeriesList.ElementAt(i).SiteName))).ToList();
                            tileSeriesList3.AddRange(tileSeriesList4);
                        }

                        tileSeriesList = tileSeriesList3;
                    }
                    else
                    {
                        tileSeriesList = new List <BusinessObjects.Models.SeriesDataCartModel.SeriesDataCart>();
                        foreach (var keyword in keywords)
                        {
                            bgWorker.CheckForCancel();
                            var series = GetSeriesCatalogForBox(tile.MinX, tile.MaxX, tile.MinY, tile.MaxY, keyword, startDate, endDate, ids, bgWorker, current, totalTilesCount);
                            tileSeriesList.AddRange(series);
                        }
                    }
                    //END CHANGES FOR "AND" SEARCH

                    bgWorker.CheckForCancel();
                    if (tileSeriesList.Count > 0)
                    {
                        var filtered = tileSeriesList.Where(seriesFilter).ToList();
                        if (filtered.Count > 0)
                        {
                            lock (_lockGetSeries)
                            {
                                totalSeriesCount += filtered.Count;
                                fullSeriesList.Add(filtered);
                            }
                        }
                    }

                    // Report progress
                    var currentFinished = Interlocked.Add(ref tilesFinished, 1);
                    var message         = string.Format("{0} Series found", totalSeriesCount);
                    var percentProgress = (currentFinished * 100) / totalTilesCount;
                    bgWorker.ReportProgress(percentProgress, message);
                });
            });

            // Collect all series into result list
            var result = new List <BusinessObjects.Models.SeriesDataCartModel.SeriesDataCart>(totalSeriesCount);

            fullSeriesList.ForEach(result.AddRange);
            return(result);
        }
Example #9
0
        private List <BusinessObjects.Models.SeriesDataCartModel.SeriesDataCart> GetSeriesListForExtent(Extent extent, IEnumerable <string> keywords, double tileWidth, double tileHeight,
                                                                                                        DateTime startDate, DateTime endDate, ICollection <WebServiceNode> serviceIDs,
                                                                                                        //BusinessObjects.Models.IProgressHandler bgWorker,
                                                                                                        Func <BusinessObjects.Models.SeriesDataCartModel.SeriesDataCart, bool> seriesFilter)
        {
            var servicesToSearch = new List <Tuple <WebServiceNode[], Extent> >();

            if (serviceIDs.Count > 0)
            {
                foreach (var webService in serviceIDs)
                {
                    if (webService.ServiceBoundingBox == null)
                    {
                        servicesToSearch.Add(new Tuple <WebServiceNode[], Extent>(new[] { webService }, extent));
                        continue;
                    }
                    const double eps      = 0.05;                //tolerance (0.05 deg) used for services whose bounding box is one point
                    var          wsBox    = webService.ServiceBoundingBox;
                    var          wsExtent = new Extent(wsBox.XMin - eps, wsBox.YMin - eps, wsBox.XMax + eps, wsBox.YMax + eps);
                    if (wsExtent.Intersects(extent))
                    {
                        servicesToSearch.Add(new Tuple <WebServiceNode[], Extent>(new[] { webService }, wsExtent.Intersection(extent)));
                    }
                }
            }
            else
            {
                servicesToSearch.Add(new Tuple <WebServiceNode[], Extent>(new WebServiceNode[] { }, extent));
            }

            var servicesWithExtents = new List <Tuple <WebServiceNode[], List <Extent> > >(servicesToSearch.Count);
            int totalTilesCount     = 0;

            foreach (var wsInfo in servicesToSearch)
            {
                var tiles = SearchHelper.CreateTiles(wsInfo.Item2, tileWidth, tileHeight);
                servicesWithExtents.Add(new Tuple <WebServiceNode[], List <Extent> >(wsInfo.Item1, tiles));
                totalTilesCount += tiles.Count;
            }

            var  fullSeriesList   = new List <List <BusinessObjects.Models.SeriesDataCartModel.SeriesDataCart> >();
            long currentTileIndex = 0;
            int  tilesFinished    = 0;

            totalSeriesCount = 0;

            //bgWorker.ReportProgress(0, "0 Series found");
            CancellationTokenSource cts = new CancellationTokenSource();

            var serviceLoopOptions = new ParallelOptions
            {
                //CancellationToken = bgWorker.CancellationToken,
#if (DEBUG)
                MaxDegreeOfParallelism = EnvironmentContext.LocalEnvironment() ? 1 : 2
#else
                MaxDegreeOfParallelism = 2
#endif
            };
            var tileLoopOptions = new ParallelOptions
            {
                //CancellationToken = bgWorker.CancellationToken,
                CancellationToken = cts.Token,
                // Note: currently HIS Central returns timeout if many requests are sent in the same time.
                // To test set  MaxDegreeOfParallelism = -1
#if (DEBUG)
                MaxDegreeOfParallelism = EnvironmentContext.LocalEnvironment() ? 1 : 4
#else
                MaxDegreeOfParallelism = 4
#endif
            };

            try
            {
                Parallel.ForEach(servicesWithExtents, serviceLoopOptions, wsInfo =>
                {
                    //bgWorker.CheckForCancel();
                    var ids   = wsInfo.Item1.Select(item => item.ServiceID).ToArray();
                    var tiles = wsInfo.Item2;
                    try
                    {
                        Parallel.ForEach(tiles, tileLoopOptions, tile =>
                        {
                            var current = Interlocked.Add(ref currentTileIndex, 1);
                            //bgWorker.CheckForCancel();

                            // Do the web service call
                            var tileSeriesList = new List <BusinessObjects.Models.SeriesDataCartModel.SeriesDataCart>();

                            if (SearchSettings.AndSearch == true)
                            {
                                //CHANGES FOR "AND" SEARCH
                                var totalTileSeriesList = new List <BusinessObjects.Models.SeriesDataCartModel.SeriesDataCart>();
                                var tileSeriesList2     = new List <BusinessObjects.Models.SeriesDataCartModel.SeriesDataCart>();
                                var tileSeriesList3     = new List <BusinessObjects.Models.SeriesDataCartModel.SeriesDataCart>();
                                var tileSeriesList4     = new List <BusinessObjects.Models.SeriesDataCartModel.SeriesDataCart>();

                                SeriesComparer sc = new SeriesComparer();

                                for (int i = 0; i < keywords.Count(); i++)
                                {
                                    String keyword = keywords.ElementAt(i);

                                    string sampleMedium = String.Empty;
                                    string dataType     = String.Empty;
                                    string valueType    = string.Empty;

                                    var series = GetSeriesCatalogForBox(tile.MinX, tile.MaxX, tile.MinY, tile.MaxY, sampleMedium, dataType, valueType, keyword, startDate, endDate, ids, current, totalTilesCount);

                                    totalTileSeriesList.AddRange(series);
                                    if (tileSeriesList.Count() == 0)
                                    {
                                        if (i == 0)
                                        {
                                            tileSeriesList.AddRange(series);
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }
                                    else
                                    {
                                        tileSeriesList2.AddRange(tileSeriesList.Intersect(series, sc));
                                        tileSeriesList.Clear();
                                        tileSeriesList.AddRange(tileSeriesList2);
                                        tileSeriesList2.Clear();
                                    }
                                }


                                for (int i = 0; i < tileSeriesList.Count(); i++)
                                {
                                    tileSeriesList4 = totalTileSeriesList.Where(item => (item.SiteName.Equals(tileSeriesList.ElementAt(i).SiteName))).ToList();
                                    tileSeriesList3.AddRange(tileSeriesList4);
                                }

                                tileSeriesList = tileSeriesList3;
                            }
                            else
                            {
                                tileSeriesList = new List <BusinessObjects.Models.SeriesDataCartModel.SeriesDataCart>();
                                foreach (var keyword in keywords)
                                {
                                    string sampleMedium = String.Empty;
                                    string dataType     = String.Empty;
                                    string valueType    = string.Empty;

                                    var series = GetSeriesCatalogForBox(tile.MinX, tile.MaxX, tile.MinY, tile.MaxY, sampleMedium, dataType, valueType, keyword, startDate, endDate, ids, current, totalTilesCount);

                                    tileSeriesList.AddRange(series);
                                }
                            }
                            //END CHANGES FOR "AND" SEARCH

                            //bgWorker.CheckForCancel();
                            if (tileSeriesList.Count > 0)
                            {
                                var filtered = tileSeriesList.Where(seriesFilter).ToList();
                                if (filtered.Count > 0)
                                {
                                    lock (_lockGetSeries)
                                    {
                                        totalSeriesCount += filtered.Count;
                                        fullSeriesList.Add(filtered);
                                    }
                                }
                            }

                            // Report progress
                            var currentFinished = Interlocked.Add(ref tilesFinished, 1);
                            if (totalSeriesCount > maxAllowedTimeseriesReturn)
                            {
                                //Maximum time series exceeded - register a delegate to add an exception to the aggregate exception returned by the task cancellation processing...
                                cts.Token.Register(() => {
                                    string errorMessage           = String.Format("Search returned more than {0:#,###0} timeseries and was canceled. Please limit search area and/or Keywords.", maxAllowedTimeseriesReturn);
                                    InvalidOperationException exp = new InvalidOperationException(errorMessage);
                                    throw exp;
                                });

                                cts.Cancel();
                            }
                            var message         = string.Format("{0} Series found", totalSeriesCount);
                            var percentProgress = (currentFinished * 100) / totalTilesCount;
                            //bgWorker.ReportProgress(percentProgress, message);
                        });
                    }
                    catch (OperationCanceledException oex)
                    {
                        throw oex;
                    }
                });
                // Collect all series into result list
                var result = new List <BusinessObjects.Models.SeriesDataCartModel.SeriesDataCart>(totalSeriesCount);
                fullSeriesList.ForEach(result.AddRange);
                return(result);
            }
            catch (OperationCanceledException oex)
            {
                throw oex;
            }
        }
Example #10
0
 private static bool TilesWithinEnvelope(IEnumerable<TileInfo> tileInfos, Extent evenlope)
 {
     return tileInfos.All(tileInfo => evenlope.Intersects(tileInfo.Extent));
 }