Exemplo n.º 1
0
    private ContourTileData GetContourTileData(ContourQuery contourQuery)
    {
        string          key             = new ConnectionStringParser().ComposeConnectionString(contourQuery);
        ContourTileData contourTileData = new ContourTileData();
        CacheItemPolicy cacheItemPolicy = new CacheItemPolicy()
        {
            SlidingExpiration = TimeSpan.FromMinutes(1)
        };

        contourTileData = (ContourTileData)s_contourDataCache.AddOrGetExisting(key, contourTileData, cacheItemPolicy) ?? contourTileData;

        if ((object)contourTileData.IDWFunction != null && (object)contourTileData.ColorFunction != null)
        {
            return(contourTileData);
        }

        using (ManualResetEvent waitHandle = new ManualResetEvent(false))
        {
            ManualResetEvent cachedWaitHandle = Interlocked.CompareExchange(ref contourTileData.WaitHandle, waitHandle, null);

            try
            {
                try
                {
                    if ((object)cachedWaitHandle != null)
                    {
                        cachedWaitHandle.WaitOne();
                        return(contourTileData);
                    }
                }
                catch (ObjectDisposedException)
                {
                    return(contourTileData);
                }

                List <TrendingDataLocation> locations     = GetFrameFromDailySummary(contourQuery);
                Func <double, double>       colorFunction = GetColorScale(contourQuery);
                IDWFunc idwFunction = GetIDWFunction(contourQuery, locations);

                if (locations.Any())
                {
                    double latDif = locations.Max(location => location.Latitude) - locations.Min(location => location.Latitude);
                    double lonDif = locations.Max(location => location.Longitude) - locations.Min(location => location.Longitude);
                    contourTileData.MinLatitude  = locations.Min(location => location.Latitude) - (latDif * 0.1D);
                    contourTileData.MaxLatitude  = locations.Max(location => location.Latitude) + (latDif * 0.1D);
                    contourTileData.MinLongitude = locations.Min(location => location.Longitude) - (lonDif * 0.1D);
                    contourTileData.MaxLongitude = locations.Max(location => location.Longitude) + (lonDif * 0.1D);
                }

                contourTileData.IDWFunction   = idwFunction;
                contourTileData.ColorFunction = colorFunction;

                return(contourTileData);
            }
            finally
            {
                waitHandle.Set();
            }
        }
    }
    private ContourTileData GetContourTileData(ContourQuery contourQuery)
    {
        string key = new ConnectionStringParser().ComposeConnectionString(contourQuery);
        ContourTileData contourTileData = new ContourTileData();
        CacheItemPolicy cacheItemPolicy = new CacheItemPolicy() { SlidingExpiration = TimeSpan.FromMinutes(1) };

        contourTileData = (ContourTileData)s_contourDataCache.AddOrGetExisting(key, contourTileData, cacheItemPolicy) ?? contourTileData;

        if ((object)contourTileData.IDWFunction != null && (object)contourTileData.ColorFunction != null)
            return contourTileData;

        using (ManualResetEvent waitHandle = new ManualResetEvent(false))
        {
            ManualResetEvent cachedWaitHandle = Interlocked.CompareExchange(ref contourTileData.WaitHandle, waitHandle, null);

            try
            {
                try
                {
                    if ((object)cachedWaitHandle != null)
                    {
                        cachedWaitHandle.WaitOne();
                        return contourTileData;
                    }
                }
                catch (ObjectDisposedException)
                {
                    return contourTileData;
                }

                List<TrendingDataLocation> locations = GetFrameFromDailySummary(contourQuery);
                Func<double, double> colorFunction = GetColorScale(contourQuery);
                IDWFunc idwFunction = GetIDWFunction(contourQuery, locations);

                contourTileData.MinLatitude = locations.Min(location => location.Latitude) - GetLatFromMiles(50.0D);
                contourTileData.MaxLatitude = locations.Max(location => location.Latitude) + GetLatFromMiles(50.0D);
                contourTileData.MinLongitude = locations.Min(location => location.Longitude) - GetLngFromMiles(50.0D, 0.0D);
                contourTileData.MaxLongitude = locations.Max(location => location.Longitude) + GetLngFromMiles(50.0D, 0.0D);

                contourTileData.IDWFunction = idwFunction;
                contourTileData.ColorFunction = colorFunction;

                return contourTileData;
            }
            finally
            {
                waitHandle.Set();
            }
        }
    }
Exemplo n.º 3
0
    public void getContourTile()
    {
        ContourQuery contourQuery = new ContourQuery()
        {
            StartDate      = HttpContext.Current.Request.QueryString["StartDate"],
            EndDate        = HttpContext.Current.Request.QueryString["EndDate"],
            ColorScaleName = HttpContext.Current.Request.QueryString["ColorScaleName"],
            DataType       = HttpContext.Current.Request.QueryString["DataType"],
            UserName       = HttpContext.Current.Request.QueryString["Username"],
            Meters         = HttpContext.Current.Request.QueryString["Meters"],
            MeterIds       = HttpContext.Current.Request.QueryString["MeterIds"]
        };

        ContourTileData contourTileData = GetContourTileData(contourQuery);

        double minLat = contourTileData.MinLatitude;
        double maxLat = contourTileData.MaxLatitude;
        double minLng = contourTileData.MinLongitude;
        double maxLng = contourTileData.MaxLongitude;

        CoordinateReferenceSystem crs       = s_crs;
        IDWFunc idwFunction                 = contourTileData.IDWFunction;
        Func <double, double> colorFunction = contourTileData.ColorFunction;

        int tileX = Convert.ToInt32(HttpContext.Current.Request.QueryString["x"]);
        int tileY = Convert.ToInt32(HttpContext.Current.Request.QueryString["y"]);
        int zoom  = Convert.ToInt32(HttpContext.Current.Request.QueryString["zoom"]);

        int tileSize = 256;
        int offsetX  = tileSize * tileX;
        int offsetY  = tileSize * tileY;

        uint[] pixelData = new uint[tileSize * tileSize];

        for (int x = 0; x < tileSize; x++)
        {
            GSF.Drawing.Point validationPixel      = new GSF.Drawing.Point(offsetX + x, 0.0D);
            GeoCoordinate     validationCoordinate = crs.Translate(validationPixel, zoom);

            if (validationCoordinate.Longitude < minLng || validationCoordinate.Longitude > maxLng)
            {
                continue;
            }

            for (int y = 0; y < tileSize; y++)
            {
                GSF.Drawing.Point offsetPixel     = new GSF.Drawing.Point(offsetX + x, offsetY + y);
                GeoCoordinate     pixelCoordinate = crs.Translate(offsetPixel, zoom);

                if (pixelCoordinate.Latitude < minLat || pixelCoordinate.Latitude > maxLat)
                {
                    continue;
                }

                double interpolatedValue = idwFunction(pixelCoordinate.Longitude, pixelCoordinate.Latitude);
                uint   color             = (uint)colorFunction(interpolatedValue);
                pixelData[y * tileSize + x] = color;
            }
        }

        using (Bitmap bitmap = BitmapExtensions.FromPixelData(256, pixelData))
        {
            HttpContext.Current.Response.ContentType = "image/png";
            HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment;filename=tile{0}x{1}.png", tileX, tileY));
            bitmap.Save(HttpContext.Current.Response.OutputStream, ImageFormat.Png);
        }
    }