//
        // 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"));
            }
        }
Beispiel #2
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"));
            }
        }
Beispiel #3
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"));
            }
        }