c# MongoDB 经纬度应用示例

class Program 


    { 
        static string mongodb = "mongodb://127.0.0.1:27017"; 
        static string database = "dbCardInfo"; 
        static string tblName = "fcd1"; 
        static MongoCollection<BsonDocument> table; 
        static void Main(string[] args) 
        { 
            MongoClient client; 
            MongoServer server; 
            MongoDatabase db;
            MongoClientSettings setting = new MongoClientSettings(); 
            setting.MaxConnectionPoolSize = 1000; 
            setting.MinConnectionPoolSize = 500;
            client = new MongoClient(mongodb); 
           
            server = client.GetServer(); 
            db = server.GetDatabase(database); 
            table = db.GetCollection(tblName);
  #region 索引
            //IndexKeysDocument doc = new IndexKeysDocument();//新建索引
            //2d 平面坐标索引,适用于基于平面的坐标计算。也支持球面距离计算,不过官方推荐使用2dsphere索引
            //BsonValue value = BsonValue.Create("2d");//创建2d索引
            //2dsphere 几何球体索引,适用于球面几何运算
            //不过,只要坐标跨度不太大(比如几百几千公里),这两个索引计算出的距离相差几乎可以忽略不计
            //BsonValue value = BsonValue.Create("2dsphere");//创建2d索引
            //doc.Add("loc", value);//loc为数据库中2d索引的对象名称
            //table.CreateIndex(doc);//创建索引
            #endregion
            double y = 26.0623344427; 
            double x = 119.2916107177; 
            double maxDistance = 0.2;//单位公里(千米) 
            //6378137:地球半径,单位:米 
            IMongoQuery query = Query.WithinCircle("loc", x, y, maxDistance / (6378137 / 1000.0), true); 
            IMongoQuery query1 = Query.Near("loc", x, y, maxDistance / (6378137 / 1000.0), true);
            //var finds = table.Find(query); 
                var finds = table.Find(query1); 
                int count = 0;
                Parallel.ForEach(finds, (item) => 
                { 
                    Interlocked.Increment(ref count); 
                    Debug.WriteLine(item["loc"]["lat"]+","+ item["loc"]["lng"]); 
                }); 
        
            Console.WriteLine("over"); 
            Console.ReadLine(); 
        }
        static void Search(IMongoQuery query) 
        { 
            List<LbsItem> cards = new List<LbsItem>();
            try 
            { 
                MongoCursor<BsonDocument> res = table.Find(query).SetLimit(100000).SetSkip(1500000); 
                int count = 0; 
                var start = DateTime.Now; 
                Parallel.ForEach(res, (item) => 
                { 
                    Interlocked.Increment(ref count); 
                }); 
                double etime = (DateTime.Now - start).TotalSeconds; 
                Console.WriteLine(count + ":" + etime + ";" + count / etime); 
            } 
            catch (Exception ex) 
            { 
                LogHelper.Error(ex); 
            }
        } 
    }
 
public class LbsItem 
    { 
        /// <summary> 
        /// 终端标识 
        /// </summary> 
        public string tid 
        { 
            get; set; 
        } 
        /// <summary> 
        /// 
        /// </summary> 
public GeoJsonPoint<GeoJson2DGeographicCoordinates> loc { get; set; }
        //或:public Location loc 
        { 
            get;set; 
        }         
        /// <summary> 
        /// 定位时间 
        /// </summary> 
        public DateTime time 
        { 
            get; set; 
        }
}
    public class Location 
    { 
        /// <summary> 
        /// 经度 
        /// </summary> 
        public double lng 
        { 
            get; set; 
        } 
        /// <summary> 
        /// 纬度 
        /// </summary> 
        public double lat 
        { 
            get; set; 
        } 
    }
posted @ 2016-12-22 09:16  94cool  阅读(1068)  评论(0编辑  收藏  举报