Ejemplo n.º 1
0
        public Toutput GetObjectGeneric_FromCacheBBox <Toutput, Tpoint>(BoundingBoxQuery query, Metrics metrics, string tableName, GeometryWriterBase <Toutput, Tpoint> writer)
        {
            Toutput objOut = default(Toutput);

            metrics.Start("Init");

            try
            {
                double reduceTolerance       = Math.Min(query.BBox.Width / query.Width, query.BBox.Height / query.Height);           // degrees per pixel / 2
                double reduceToleranceMeters = reduceTolerance * 6378137;
                //double pixelArea = Math.Pow(BingMapsTileSystem.GroundResolution(query.BBox.maxY, query.ZoomLevel), 2); // mapResolution * mapResolution
                double pixelRadiansAreaXY = ((query.BBox.maxX - query.BBox.minX) / query.Width) * ((query.BBox.maxY - query.BBox.minY) / query.Height);
                int    numPixels          = 0;

                #region Get data from cache or open DB

                if (UseInMemoryCache)
                {
                    #region From Cache
                    #region Preload cache

                    metrics.Start("Cache");
                    if (!GeometryCache.IsCacheLoaded(query.Tables()[0]))
                    {
                        while (!GeometryCache.IsCacheLoaded(query.Tables()[0]))
                        {
                            GeometryCache.LoadCache(query.Tables()[0], this.GetConnectionString());
                            Trace.WriteLine(string.Format("Thread {0} waiting for cache...", System.Threading.Thread.CurrentThread.ManagedThreadId));
                        }
                    }
                    metrics.Stop("Cache");

                    #endregion

                    SqlGeometry bboxGeom            = SqlServerModel.GeometryFromBoundingBox(query.BBox);
                    IList <int> resultsWithIndexSTR = GeometryCache.Query(tableName, new Envelope(query.BBox.minX, query.BBox.maxX, query.BBox.minY, query.BBox.maxY));

                    if (resultsWithIndexSTR.Count > 0)
                    {
                        foreach (int id in resultsWithIndexSTR)
                        {
                            SqlGeometry geom     = GeometryCache.GetGeometry(tableName, id);
                            double      geomArea = GeometryCache.GetGeometryArea(tableName, id);

                            bool geomIsPixel = geomArea > 0 && geomArea <= pixelRadiansAreaXY;


                            metrics.Start("Process");
                            ProcessGeometry <Toutput, Tpoint>(writer, reduceTolerance, pixelRadiansAreaXY, ref numPixels, ref geom, geomArea);
                            metrics.Stop("Process");
                        }
                        metrics.Start("GetOutput");
                        objOut = writer.GetOutput();
                        metrics.Stop("GetOutput");
                    }

                    #endregion
                }
                else
                {
                    #region Get data from SQL DB

                    using (SqlConnection conn = GetOpenConnection())
                    {
                        string strQuery = SqlServerModel.GenerateGetGeomInBBoxScript(tableName, query.BBox);

                        using (var cmd = new SqlCommand(strQuery, conn))
                        {
                            cmd.CommandType = System.Data.CommandType.Text;

                            using (var rdr = cmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess))
                            {
                                if (rdr.HasRows)
                                {
                                    while (rdr.Read())
                                    {
                                        metrics.Start("Area");
                                        SqlGeometry geom     = SqlGeometry.STGeomFromWKB(rdr.GetSqlBytes(0), 4326);
                                        double      geomArea = geom.STArea().Value;                                    // optimize this by having area field in DB
                                        metrics.Stop("Area");

                                        metrics.Start("Process");
                                        ProcessGeometry <Toutput, Tpoint>(writer, reduceTolerance, pixelRadiansAreaXY, ref numPixels, ref geom, geomArea);
                                        metrics.Stop("Process");
                                    }
                                    metrics.Start("GetOutput");
                                    objOut = writer.GetOutput();
                                    metrics.Stop("GetOutput");
                                }


                                rdr.Close();
                            }
                        }
                    }

                    #endregion
                }
                #endregion
            }
            catch (Exception)
            {
                throw;
            }


            return(objOut);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// For each table in the query, return a FeatureCollection.
        /// </summary>
        /// <param name="query">query with a bounding box</param>
        /// <returns>set of GeoJson Features</returns>
        public Bitmap GetImage(BoundingBoxQuery query)
        {
            Bitmap  bmp     = new Bitmap(query.Width, query.Height);
            Metrics metrics = new Metrics(this._metricsType);


            try
            {
                metrics.Start("Global");

                using (Graphics g = Graphics.FromImage(bmp))
                {
                    foreach (string table in query.Tables())
                    {
                        Bitmap bmpTable = null;

                        #region Preload cache if necessary

                        if (UseInMemoryCache)
                        {
                            metrics.Start("Cache");
                            if (!GeometryCache.IsCacheLoaded(table))
                            {
                                while (!GeometryCache.IsCacheLoaded(table))
                                {
                                    GeometryCache.LoadCache(table, this.GetConnectionString());
                                    Trace.WriteLine(string.Format("Thread {0} waiting for cache...", Thread.CurrentThread.ManagedThreadId));
                                }
                            }
                            metrics.Stop("Cache");
                        }
                        #endregion

                        if (query.IsBench)
                        {
                            bmpTable = GetBenchImageGeneric(query, metrics, table, UseInMemoryCache);
                        }
                        else if (UseInMemoryCache)
                        {
                            bmpTable = GetImage_BBox(query, metrics, table);
                        }
                        //else
                        //  bmpTable = GetImageGeneric_FromDB(query, metrics, table, false);

                        g.DrawImageUnscaled(bmpTable, 0, 0);
                        bmp.Tag = bmpTable.Tag;
                        bmpTable.Dispose();
                    }
                }

                metrics.Stop("Global");

                #region Calculate metrics to return.
                // Calculate metrics to return.
                switch (_metricsType)
                {
                case enMetricsType.OnlyTime:

                    string msg = null;
                    foreach (var kv in metrics.GetTaskTimes())
                    {
                        msg += string.Format("{0}: {1,6:###,###} ms{2}", kv.Key, kv.Value.TotalMilliseconds, Environment.NewLine);
                    }
                    this.DrawMsgInImage(ref bmp, msg);



                    break;

                default:

                    break;
                }

                #endregion
            }
            catch (Exception ex)
            {
                #region Write exception in image
                // Write exception in image
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    g.Clear(Color.FromArgb(128, Color.Red));

                    using (Font font = new Font(FontFamily.GenericMonospace, 11, FontStyle.Regular, GraphicsUnit.Pixel))
                        g.DrawString(ex.ToString(), font, Brushes.Black, new RectangleF(0, 0, 256, 256));
                }
                #endregion
            }

            return(bmp);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// For each table in the query, return a FeatureCollection.
        /// </summary>
        /// <param name="query">query with a bounding box</param>
        /// <returns>set of GeoJson Features</returns>
        public string GetGeoJson(BoundingBoxQuery query)
        {
            string  geojson = null;
            Metrics metrics = new Metrics(this._metricsType);


            try
            {
                metrics.Start("Global");

                foreach (string table in query.Tables())
                {
                    #region Preload cache if necessary

                    if (UseInMemoryCache)
                    {
                        metrics.Start("Cache");
                        if (!GeometryCache.IsCacheLoaded(table))
                        {
                            while (!GeometryCache.IsCacheLoaded(table))
                            {
                                GeometryCache.LoadCache(table, this.GetConnectionString());
                                Trace.WriteLine(string.Format("Thread {0} waiting for cache...", Thread.CurrentThread.ManagedThreadId));
                            }
                        }
                        metrics.Stop("Cache");
                    }
                    #endregion

                    geojson += GetGeoJson_BBox(query, metrics, table);
                }


                metrics.Stop("Global");

                #region Calculate metrics to return. (TODO)
                // Calculate metrics to return.
                switch (_metricsType)
                {
                case enMetricsType.OnlyTime:

                    string msg = null;
                    foreach (var kv in metrics.GetTaskTimes())
                    {
                        msg += string.Format("{0}: {1,6:###,###} ms{2}", kv.Key, kv.Value.TotalMilliseconds, Environment.NewLine);
                    }
                    //this.DrawMsgInImage(ref bmp, msg);



                    break;

                default:

                    break;
                }

                #endregion
            }
            catch (Exception ex)
            {
                throw;
            }

            return(geojson);
        }