Ejemplo n.º 1
0
        private Bitmap GetBenchImageGeneric(BoundingBoxQuery query, Metrics metrics, string tableName, bool useCache)
        {
            Bitmap bmp = new Bitmap(query.Width, query.Height);

            try
            {
                string strQuery = null;
                if (useCache)
                {
                    strQuery = SqlServerModel.GenerateGetGeomIdInBBoxScript(tableName, query.BBox);
                }
                else
                {
                    strQuery = SqlServerModel.GenerateGetGeomInBBoxScript(tableName, query.BBox);
                }

                SqlGeometry bboxGeom = SqlServerModel.GeometryFromBoundingBox(query.BBox);


                //start = DateTime.Now;

                //IList<int> resultsWithIndex = _spatialIndex.Query(new Envelope(query.BBox.minX, query.BBox.maxX, query.BBox.minY, query.BBox.maxY));
                //foreach (int id in resultsWithIndex)
                //{
                //  SqlGeometry geom = GetGeomFromCache(tableName, id);
                //}

                //metrics.TaskTimesMilliseconds.Add(string.Format("quadTree {0} items", resultsWithIndex.Count), (int)(DateTime.Now - start).TotalMilliseconds);


                metrics.Start("STR");

                IList <int> resultsWithIndexSTR = GeometryCache.Query(tableName, new Envelope(query.BBox.minX, query.BBox.maxX, query.BBox.minY, query.BBox.maxY));
                foreach (int id in resultsWithIndexSTR)
                {
                    SqlGeometry geom = GeometryCache.GetGeometry(tableName, id);
                }

                metrics.Stop("STR");
                Trace.WriteLine(string.Format("STR {0} items", resultsWithIndexSTR.Count));

                //List<SqlGeometry> vlist =  ImageryDataService._geomCacheByTableThenId[tableName].Values.Where(g => g.STIntersects(bboxGeom).Value).ToList();

                metrics.Start("DB");

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

                        using (var rdr = cmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess))
                        {
                            while (rdr.Read())
                            {
                                int         id   = rdr.GetSqlInt32(0).Value;
                                SqlGeometry geom = GeometryCache.GetGeometry(tableName, id);
                                nbRecords++;
                            }
                            rdr.Close();
                        }
                    }
                }

                metrics.Stop("DB");
                Trace.WriteLine(string.Format("DB {0} items", nbRecords));
            }
            catch (Exception)
            {
                throw;
            }
            return(bmp);
        }
Ejemplo n.º 2
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);
        }