//
        // GET: /Tiles/GeographyBoundary/

        public ActionResult Index(int x, int y, int zoom, long geographicLocationId, int width = 256, int height = 256)
        {
            using (var context = ContextFactory.SizeUpContext)
            {
                GeographyBoundary tile        = new GeographyBoundary(width, height, x, y, zoom);
                BoundingBox       boundingBox = tile.GetBoundingBox(TileBuffer);
                double            tolerance   = GetPolygonTolerance(zoom);
                var boundingGeo = boundingBox.GetDbGeography();

                var geos = context.GeographicLocations
                           .Where(i => i.Id == geographicLocationId)
                           .SelectMany(i => i.Geographies.Where(g => g.GeographyClass.Name == Core.Geo.GeographyClass.Display)
                                       .Select(g => SqlSpatialFunctions.Reduce(g.Polygon, tolerance).Intersection(boundingGeo)))
                           .Where(g => g != null)
                           .ToList()
                           .Select(g => new GeographyEntity()
                {
                    Geography     = SqlGeography.Parse(g.AsText()),
                    BorderColor   = "#6495ED",
                    BorderOpacity = 200,
                    BorderWidth   = 2
                })
                           .ToList();

                tile.Draw(geos);
                var stream = new System.IO.MemoryStream();
                tile.Bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                return(File(stream.GetBuffer(), "image/png"));
            }
        }
        //
        // GET: /Tiles/Revenue/

        public ActionResult Index(int x, int y, int zoom, long industryId, long boundingGeographicLocationId, string startColor, string endColor, int bands, Core.DataLayer.Granularity granularity, int width = 256, int height = 256)
        {
            using (var context = ContextFactory.SizeUpContext)
            {
                Heatmap     tile        = new Heatmap(width, height, x, y, zoom);
                BoundingBox boundingBox = tile.GetBoundingBox(TileBuffer);
                double      tolerance   = GetPolygonTolerance(zoom);
                var         boundingGeo = boundingBox.GetDbGeography();

                var gran = Enum.GetName(typeof(Core.DataLayer.Granularity), granularity);

                var geos = Core.DataLayer.GeographicLocation.Get(context)
                           .Where(i => i.Granularity.Name == gran)
                           .Where(i => i.GeographicLocations.Any(g => g.Id == boundingGeographicLocationId));

                var data = Core.DataLayer.IndustryData.Get(context).Where(i => i.IndustryId == industryId);

                var list = geos
                           .GroupJoin(data, i => i.Id, o => o.GeographicLocationId, (i, o) => new { IndustryData = o, GeographicLocation = i })
                           .Select(i => new KeyValue <DbGeography, Band <double> >
                {
                    Key = i.GeographicLocation.Geographies.Where(g => g.GeographyClass.Name == Core.Geo.GeographyClass.Display)
                          .Select(g => SqlSpatialFunctions.Reduce(g.Polygon, tolerance).Intersection(boundingGeo)).FirstOrDefault(),
                    Value = i.IndustryData.Select(d => d.Bands.Where(b => b.Attribute.Name == IndustryAttribute.CostEffectiveness).Select(b => new Band <double> {
                        Min = (double)b.Min.Value, Max = (double)b.Max.Value
                    }).FirstOrDefault()).FirstOrDefault()
                }).ToList();

                var quantiles = list
                                .Where(i => i.Value != null)
                                .NTileDescending(i => i.Value.Max, bands);

                ColorBands colorBands = new Core.Tiles.ColorBands(System.Drawing.ColorTranslator.FromHtml("#" + startColor), System.Drawing.ColorTranslator.FromHtml("#" + endColor), quantiles.Count());
                string[]   bandList   = colorBands.GetColorBands().ToArray();

                var validValues = quantiles
                                  .Select((i, index) => i.Where(g => g.Key != null).Select(g => new GeographyEntity()
                {
                    Geography = SqlGeography.Parse(g.Key.AsText()), Color = bandList[index]
                }))
                                  .SelectMany(i => i)
                                  .ToList();

                var invalidValues = list
                                    .Where(i => i.Value == null)
                                    .Where(i => i.Key != null)
                                    .Select(g => new GeographyEntity()
                {
                    Geography = SqlGeography.Parse(g.Key.AsText())
                })
                                    .ToList();

                var output = validValues.Union(invalidValues).ToList();

                tile.Draw(output);
                var stream = new System.IO.MemoryStream();
                tile.Bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                return(File(stream.GetBuffer(), "image/png"));
            }
        }
 public static IEnumerable <OgrEntity> Reduce(this IQueryable <OgrEntity> query, double threshold)
 {
     return(query.Select(s => new
     {
         g = SqlSpatialFunctions.Reduce(s.Geometry, threshold),
         s
     }).AsEnumerable().Select(t => { t.s.Geometry = t.g; return t.s; }));
 }
예제 #4
0
        public async Task <IList <PlaceInfo> > GetShortPlaceInAreaAsync(Geolocation geolocation, double radiusMetres, int limit)
        {
            var         radiusSm = radiusMetres * 1000;
            DbGeography area     = GeographyHelper.PointFromGeoPoint(geolocation).Buffer(radiusSm);

            var pointsQuery = (from place in _dbContext.Place
                               where SqlSpatialFunctions.Filter(place.GeoPoint, area) == true
                               select new { place.Id, place.GeoPoint, place.Name }).Take(limit);

            var points = await pointsQuery.ToListAsync();

            return(points.Select(t => new PlaceInfo(t.Id, t.Name, new Geolocation((double)t.GeoPoint.Latitude, (double)t.GeoPoint.Longitude))).ToList());
        }
예제 #5
0
        public ActionResult Show(double latitude, double longitude)
        {
            var point    = DbGeography.FromText(string.Format("POINT ({0} {1})", longitude, latitude), 4326);
            var district = _context.Features
                           .Where(x => x.Geography.Intersects(point))
                           .Select(x => new { Name = x.Name, Geography = SqlSpatialFunctions.Reduce(x.Geography, 100).AsText() })
                           .SingleOrDefault();

            if (district != null)
            {
                return(Json(new
                {
                    name = district.Name,
                    geography = district.Geography,
                }, JsonRequestBehavior.AllowGet));
            }

            Response.StatusCode             = 404;
            Response.TrySkipIisCustomErrors = true;
            return(Json(new { }, JsonRequestBehavior.AllowGet));
        }
예제 #6
0
        private async Task <IList <BL.Model.Offer> > GetOffersAsync(int?offerId, string ownerUserId, Area area, int?placeId, bool?isActive, int?minItemsAmount, int?limit)
        {
            #region query
            var query = from user in _dbContext.AspNetUsers
                        from place in user.Place
                        from offer in place.Offer
                        select new
            {
                User        = user,
                Place       = place,
                Offer       = offer,
                OfferAmount = offer.OfferTransactions.Select(t => t.Amount).DefaultIfEmpty(0).Sum()
            };

            if (area != null)
            {
                var         radiusSm = area.RadiusMeters * 100;
                DbGeography dbArea   = GeographyHelper.PointFromGeoPoint(area).Buffer(radiusSm);

                query = query.Where(t => SqlSpatialFunctions.Filter(t.Place.GeoPoint, dbArea) == true);
            }

            if (offerId != null)
            {
                query = query.Where(t => t.Offer.Id == offerId.Value);
            }

            if (!string.IsNullOrEmpty(ownerUserId))
            {
                query = query.Where(t => t.User.Id == ownerUserId);
            }

            if (placeId != null)
            {
                query = query.Where(t => t.Place.Id == placeId.Value);
            }

            if (isActive != null)
            {
                query = query.Where(t => t.Offer.IsActive == isActive.Value);
            }

            if (minItemsAmount != null)
            {
                query = query.Where(t => t.OfferAmount >= minItemsAmount.Value);
            }

            if (limit != null)
            {
                query = query.Take(limit.Value);
            }

            #endregion

            var dbOffers = await query.Select(t =>
                                              new
            {
                Id              = t.Offer.Id,
                Title           = t.Offer.Title,
                Description     = t.Offer.Description,
                Price           = t.Offer.Price,
                IsActive        = t.Offer.IsActive,
                PlaceId         = t.Offer.PlaceId,
                AvailableAmount = t.OfferAmount,
                LogoUrl         = t.Offer.LogoUrl
            }).ToListAsync().ConfigureAwait(false);

            var offers = dbOffers.Select(t => new BL.Model.Offer()
            {
                Id              = t.Id,
                Title           = t.Title,
                Description     = t.Description,
                Price           = t.Price,
                IsActive        = t.IsActive,
                PlaceId         = t.PlaceId,
                AvailableAmount = t.AvailableAmount,
                LogoUrl         = t.LogoUrl
            });

            return(offers.ToList());
        }
예제 #7
0
        //
        // GET: /Tiles/Revenue/
        public ActionResult Index(int x, int y, int zoom, long variableId, long boundingGeographicLocationId, string startColor, string endColor, int bands, Core.DataLayer.Granularity granularity = Core.DataLayer.Granularity.State, int width = 256, int height = 256)
        {
            using (var context = ContextFactory.SizeUpContext)
            {
                Heatmap     tile        = new Heatmap(width, height, x, y, zoom);
                BoundingBox boundingBox = tile.GetBoundingBox(TileBuffer);
                double      tolerance   = GetPolygonTolerance(zoom);
                var         boundingGeo = boundingBox.GetDbGeography();

                var variable = Core.DataLayer.ConsumerExpenditures.Variables(context).Where(i => i.Id == variableId).Select(i => i.Variable).FirstOrDefault();

                var gran = Enum.GetName(typeof(Core.DataLayer.Granularity), granularity);

                var geos = Core.DataLayer.GeographicLocation.Get(context)
                           .Where(i => i.Granularity.Name == gran)
                           .Where(i => i.GeographicLocations.Any(g => g.Id == boundingGeographicLocationId));

                var data = Core.DataLayer.ConsumerExpenditures.Get(context);
                //.Where(i => i.GeographicLocation.Granularity.Name == gran)
                //.Where(i => i.GeographicLocation.GeographicLocations.Any(g => g.Id == boundingGeographicLocationId));

                ConstantExpression constant = Expression.Constant(data); //empty set
                IQueryProvider     provider = data.Provider;
                Type dataType = typeof(ConsumerExpenditure);
                var  param    = Expression.Parameter(dataType, "c");

                var varSelector     = Expression.Convert(Expression.Property(param, variable), typeof(long?)) as Expression;
                var idSelector      = Expression.Property(param, "GeographicLocationId") as Expression;
                var transType       = typeof(KeyValue <long, long?>);
                var constructor     = transType.GetConstructor(new Type[] { typeof(long), typeof(long?) });
                var selector        = Expression.New(constructor, new Expression[] { idSelector, varSelector }.AsEnumerable(), new System.Reflection.MemberInfo[] { transType.GetProperty("Key"), transType.GetProperty("Value") });
                var pred            = Expression.Lambda(selector, param) as Expression;
                var expression      = Expression.Call(typeof(Queryable), "Select", new Type[] { dataType, transType }, constant, pred);
                var transformedData = data.Provider.CreateQuery <KeyValue <long, long?> >(expression);

                var list = geos.GroupJoin(transformedData, i => i.Id, o => o.Key, (i, o) => new { Data = o, GeographicLocation = i })
                           .Select(i => new KeyValue <DbGeography, long?>
                {
                    Key = i.GeographicLocation.Geographies.Where(g => g.GeographyClass.Name == Core.Geo.GeographyClass.Display)
                          .Select(g => SqlSpatialFunctions.Reduce(g.Polygon, tolerance).Intersection(boundingGeo)).FirstOrDefault(),
                    Value = i.Data.Select(d => d.Value).FirstOrDefault()
                }).ToList();

                var quantiles = list
                                .Where(i => i.Value != null)
                                .NTileDescending(i => i.Value, bands);

                ColorBands colorBands = new Core.Tiles.ColorBands(System.Drawing.ColorTranslator.FromHtml("#" + startColor), System.Drawing.ColorTranslator.FromHtml("#" + endColor), quantiles.Count());
                string[]   bandList   = colorBands.GetColorBands().ToArray();

                var validValues = quantiles
                                  .Select((i, index) => i.Where(g => g.Key != null).Select(g => new GeographyEntity()
                {
                    Geography = SqlGeography.Parse(g.Key.AsText()), Color = bandList[index]
                }))
                                  .SelectMany(i => i)
                                  .ToList();

                var invalidValues = list
                                    .Where(i => i.Value == null || i.Value <= 0)
                                    .Where(i => i.Key != null)
                                    .Select(g => new GeographyEntity()
                {
                    Geography = SqlGeography.Parse(g.Key.AsText())
                })
                                    .ToList();

                var output = validValues.Union(invalidValues).ToList();

                tile.Draw(output);
                var stream = new System.IO.MemoryStream();
                tile.Bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                return(File(stream.GetBuffer(), "image/png"));
            }
        }
예제 #8
0
        public ActionResult Index(TilesData tilesData)
        {
            Heatmap     tile        = new Heatmap(256, 256, tilesData.x, tilesData.y, tilesData.zoom);
            BoundingBox boundingBox = tile.GetBoundingBox(TileBuffer);
            double      tolerance   = GetPolygonTolerance(tilesData.zoom);
            var         boundingGeo = boundingBox.GetDbGeography();


            using (var context = ContextFactory.SizeUpContext)
            {
                var kvf = new List <KeyValue <DbGeography, Band <double> > >();
                if (tilesData != null && tilesData.Bands != null)
                {
                    var geoIdList   = (from b in tilesData.Bands from s in b.band select s.geoId).ToList();
                    var goegraphies = context.Geographies.Where(i => geoIdList.Contains(i.Id));

                    var kv = goegraphies.Select(i => new KeyValue <DbGeography, double>()
                    {
                        Key   = SqlSpatialFunctions.Reduce(i.Polygon, tolerance).Intersection(boundingGeo),
                        Value = i.Id
                    }).ToList();

                    kvf = (from tl in tilesData.Bands
                           from t in tl.band
                           from k in kv
                           where k.Value == t.geoId
                           select
                           new KeyValue <DbGeography, Band <double> >(k.Key, new Band <double>()
                    {
                        Min = t.min, Max = t.max
                    }))
                          .ToList();
                }

                var quantiles = kvf
                                .Where(i => i.Value != null)
                                .NTileDescending(i => i.Value.Max, 5);

                ColorBands colorBands = new ColorBands(ColorTranslator.FromHtml("#" + tilesData.startColor), ColorTranslator.FromHtml("#" + tilesData.endColor), quantiles.Count());
                string[]   bandList   = colorBands.GetColorBands().ToArray();

                var validValues = quantiles
                                  .Select((i, index) => i.Where(g => g.Key != null).Select(g => new GeographyEntity()
                {
                    Geography = SqlGeography.Parse(g.Key.AsText()), Color = bandList[index]
                }))
                                  .SelectMany(i => i)
                                  .ToList();

                var invalidValues = kvf
                                    .Where(i => i.Value == null)
                                    .Where(i => i.Key != null)
                                    .Select(g => new GeographyEntity()
                {
                    Geography = SqlGeography.Parse(g.Key.AsText())
                })
                                    .ToList();

                var output = validValues.Union(invalidValues).ToList();

                tile.Draw(output);
                var stream = new MemoryStream();
                tile.Bitmap.Save(stream, ImageFormat.Png);
                return(File(stream.GetBuffer(), "image/png"));
            }
        }