Пример #1
0
        /// <summary>
        /// Ctor
        /// </summary>
        public RangeGrid(string originName, int rangeMins, int unitDistance)
        {
            var connectionStringPath = Path.Combine(Default.RangerFolder, "connectionString.txt");
            var connectionString     = File.ReadAllText(connectionStringPath);

            Home = Origin.Load(connectionString, originName);

            if (Home == null)
            {
                throw new Exception("Could not load Origin from DB.");
            }

            queriedPoints = new Dictionary <bool, HashSet <LatticePoint> >()
            {
                //inside points
                [true] = new HashSet <LatticePoint>(),

                // outside points
                [false] = new HashSet <LatticePoint>()
            };

            this.rangeMins    = rangeMins;
            this.unitDistance = unitDistance;

            // make sure an exception is thrown if we accidentaly use deltas before setting them
            deltaLat = double.NaN;
            deltaLon = double.NaN;

            var apiKeyPath = Path.Combine(Default.RangerFolder, "apiKey.txt");

            distanceApi = new DistanceMatrixApi(apiKeyPath);

            dbContext = new RangerDataContext(connectionString);
        }
Пример #2
0
        public double CalculateArea(ComputeAreaInputs rangeOptions)
        {
            var range = new RangeGrid(rangeOptions.OriginName, rangeOptions.RangeMins, rangeOptions.UnitDistance);

            range.Process();
            var border = range.GetBorder(rangeOptions.SmoothPct);

            var area = GeometryApi.ComputeArea(border, apiKey);

            using (var dbContext = new RangerDataContext())
            {
                var existingRegion = dbContext
                                     .Regions
                                     .SingleOrDefault(x => x.OriginId == range.Home.Id && x.RangeMins == rangeOptions.RangeMins && x.UnitDistance == rangeOptions.UnitDistance);

                if (existingRegion == null)
                {
                    var newRegion = new Region()
                    {
                        OriginId     = range.Home.Id,
                        RangeMins    = rangeOptions.RangeMins,
                        UnitDistance = rangeOptions.UnitDistance,
                        Area         = area,
                        BorderNodes  = border.Length
                    };

                    dbContext.Regions.InsertOnSubmit(newRegion);
                }
                else
                {
                    existingRegion.Area        = area;
                    existingRegion.BorderNodes = border.Length;
                }

                dbContext.SubmitChanges();
            }

            return(area);
        }
Пример #3
0
        /// <summary>
        /// Ctor
        /// Loads the origin with the given name. Returns null if the origin is not present in the DB.
        /// </summary>
        public static Origin Load(string connectionString, string originName)
        {
            var dbContext = new RangerDataContext(connectionString);

            return(dbContext.Origins.SingleOrDefault(o => o.Name == originName));
        }