使用mongodb提供的dotnet core sdk进行地理位置运算

mongodb提供地理位置运算功能,比较常用的场景比如,先判断用户所在的街道,然后看看街道附近有啥餐厅,然后算算用户与餐厅的距离什么的,官网里提供了比较详细的demo介绍不同api的用法

此处记录下dotnet core中的用法

首先安装官网提供的nuget安装包:MongoDB.Driver

定义一个点,线或者多边形进而使用驱动时通常有两种方式,以直线为例:

A.轻松些的方式,可以先获取GeoJsonLineString<GeoJson2DCoordinates>的实例,比较方便,获取实例的方式有两种

1.使用SDK中提供的方法,比如:  GeoJson.Point(GeoJson.Position(100.0, 0.0))

2.通过拼接字符串进行反序列化,比如(此处注意“type”属性与里边的值都是指定的而且区分大小写,“coordinates”属性也必须是小写的):

            string lineString = "{ "type" : "LineString", "coordinates" :  [[101.0, 0.0], [102.0, 1.0]] }";
            GeoJsonLineString<GeoJson2DCoordinates> line = BsonSerializer
                .Deserialize<GeoJsonLineString<GeoJson2DCoordinates>>(lineString);

B.BsonDocument的方式通常在插入的时候需要得到一个BsonDocument实例,但感觉比较累,不是很喜欢,BsonDocument实例可以通过初始化获得,也可以通过BsonDocument.Parse("")一个json字符串获得,不过位置坐标复杂的情况下比较纠结。

下面以一个判断一个人的坐标在哪个区域为例

先定义一个区域类:

    public class AreaPolygon 
    {
        public int AreaId { set; get; }
        public string AreaName { set; get; }
        public GeoJsonPolygon<GeoJson2DCoordinates> Geometry { set; get; }
    }

然后先插入一条测试数据:

            AreaPolygon pp = new AreaPolygon();
            pp.AreaName = "Central Harlem North-Polo Grounds";
            pp.AreaId = 1; 
            pp.Geometry = BsonSerializer.Deserialize<GeoJsonPolygon<GeoJson2DCoordinates>>(GetPolgon());
            var client = new MongoClient("mongodb://localhost:27017");
            var database = client.GetDatabase("areadb");
            IMongoCollection<AreaPolygon> collection = database.GetCollection<AreaPolygon>("area");
            collection.InsertOne(pp);

GetPolgon()方法为一个拼接的多边形:

private string GetPolgon()
        {
            var json =
                "{ 'type' : 'Polygon', 'coordinates' :[[[-73.93383000695911,40.81949109558767],[-73.93411701695138,40.81955053491088],[-73.93431276819767,40.81962986684897],[-73.93440040009484,40.819667782434465],[-73.93439036753757,40.81958743539698],[-73.93438112852347,40.81932807562949],[-73.9342948780807,40.81890498677685],[-73.93423576072603,40.818649319251364],[-73.93408482013453,40.81793086150489],[-73.9340746397773,40.81782651394852],[-73.93412816761214,40.8177339625139],[-73.93423519549512,40.81755488121525],[-73.93445400138874,40.81718852906137],[-73.93463790272767,40.81690804797262],[-73.93487343834981,40.8165278475176],[-73.93575548210782,40.81532558429708],[-73.93613554330996,40.8147881266393],[-73.93620957748075,40.81467385042275],[-73.93668854882762,40.81404163072326],[-73.93708163694987,40.81350614105259],[-73.93714398424214,40.81342104144455],[-73.93761714951756,40.81274036483138],[-73.93813552407511,40.812065619232335],[-73.93820642688317,40.81196760479798],[-73.93830925043022,40.81182546401121],[-73.93838748529838,40.81171844357887],[-73.93863753326706,40.81137638806709],[-73.93874394437573,40.811230820566436],[-73.93901753314599,40.810856556874455],[-73.93740733660461,40.810175611401206],[-73.93580780201182,40.80949763825762],[-73.93586263420018,40.80943020292132],[-73.93625936035667,40.808855608725686],[-73.93671827467928,40.80822047872675],[-73.93718011799034,40.80759900035796],[-73.93763439424364,40.806974606921834],[-73.93808666916728,40.80635301191095],[-73.93854407929403,40.805729652932854],[-73.93869430486176,40.80579102921082],[-73.94015793743944,40.80640908964353],[-73.94177140170032,40.807089000349656],[-73.94491601785558,40.80841350378125],[-73.94499360671229,40.80844618384186],[-73.94506102986607,40.8084745003499],[-73.94773985453432,40.80959972161701],[-73.94783892229975,40.8096411278052],[-73.9479230680906,40.809676922465236],[-73.95067975515929,40.81084296435352],[-73.95210125149222,40.81144285350124],[-73.95150537264615,40.81200768463676],[-73.95098203327805,40.81252162978943],[-73.95092865672767,40.81257507153118],[-73.95035019142966,40.81315938878694],[-73.94986218985588,40.81379998043675],[-73.94962668076091,40.81410004983843],[-73.94962653685337,40.81410023874317],[-73.94962639580584,40.81410043619291],[-73.94940550546026,40.814407409502984],[-73.9491996001398,40.81472085408311],[-73.94900927796705,40.815039942538704],[-73.9488349125204,40.81536398055535],[-73.94867681741437,40.81569228551455],[-73.94835601519225,40.816374305618886],[-73.94800732867309,40.8171165088993],[-73.94766641252582,40.817855509489],[-73.94734953979686,40.81853499822355],[-73.94703623206819,40.81920837200807],[-73.94671832033764,40.81989426803384],[-73.9464028984772,40.82057204218477],[-73.94607828674226,40.82126321606192],[-73.94516004895775,40.82087904397888],[-73.94460364603806,40.82146198118819],[-73.9440498636813,40.822051592105],[-73.94350594463333,40.82262775327937],[-73.94292366912582,40.82326058725043],[-73.9424295395631,40.823909156935464],[-73.94195569728996,40.82455444012053],[-73.9415031265368,40.82517136689069],[-73.94104840809351,40.8257926296038],[-73.94059472654726,40.82641360402922],[-73.94013953822807,40.82703869055559],[-73.9396895279914,40.827656574227866],[-73.93924885616293,40.828296351211876],[-73.93896034889055,40.82898632996019],[-73.93864026693868,40.82973887442052],[-73.93899743437669,40.82988885286428],[-73.93943428772432,40.83007611669215],[-73.93962569930143,40.83015583738796],[-73.94017089788652,40.83038439112869],[-73.94007905795213,40.83046395774134],[-73.93998939894344,40.83073096688919],[-73.93996368656906,40.83082053430401],[-73.93996366268432,40.83082060632692],[-73.9399239511922,40.83090806016343],[-73.93992389241943,40.830908177064586],[-73.93992383233875,40.8309082768776],[-73.93987063346383,40.83099179606697],[-73.93987057339177,40.83099188616358],[-73.93987049016128,40.83099197658315],[-73.9398049898395,40.831069790159276],[-73.93980484742822,40.831069960289355],[-73.93980468099204,40.83107011331944],[-73.93972831443872,40.831140898571746],[-73.9396098257029,40.83125089467677],[-73.93960970782902,40.831251002332976],[-73.93960960075394,40.83125111920869],[-73.93950338652492,40.831369460472445],[-73.9394108340693,40.83149499458965],[-73.93941078701702,40.83149507581406],[-73.93933313154191,40.83162629993882],[-73.93927119324165,40.8317616573971],[-73.93927116936439,40.8317617200385],[-73.93927114613948,40.8317617928992],[-73.93922536460632,40.83189966097508],[-73.93913480796968,40.83209759542905],[-73.93902897268019,40.83229149797039],[-73.93894859789032,40.832417335825774],[-73.9389083447814,40.8324803571376],[-73.93877360195538,40.83266321215949],[-73.93868316230177,40.83280078941747],[-73.93862894892672,40.83285794170846],[-73.93857474693523,40.83291508577085],[-73.93838508900461,40.83311350252125],[-73.93795718243024,40.83345441842215],[-73.93729719818138,40.83397732122566],[-73.93729666512158,40.833977744101354],[-73.93705905884407,40.834165988650966],[-73.93700641578684,40.83419311017997],[-73.93649557128383,40.83456879562555],[-73.93572624332147,40.835156346599724],[-73.93530788003893,40.83547772443074],[-73.93506336342621,40.835677658809985],[-73.93496274110689,40.83575993474983],[-73.93485740276071,40.83598502557723],[-73.93479886277844,40.83611011575883],[-73.93474569422972,40.83609209412601],[-73.93467393068329,40.8360649032391],[-73.93460027698636,40.836096868110225],[-73.93436132705641,40.836200775593426],[-73.93445239397373,40.83598096014986],[-73.93456051826973,40.83586608172802],[-73.9347546645066,40.83558929983925],[-73.93475368707841,40.835569821114206],[-73.93480514039258,40.835427472645954],[-73.93481898168221,40.83539825039465],[-73.93483181137555,40.83538552549233],[-73.93484813571963,40.83535143220732],[-73.93486985067804,40.835332715131045],[-73.93489460041994,40.83525404312231],[-73.9349153357865,40.83523082193877],[-73.93493620828455,40.83508021765152],[-73.93494225855345,40.83502687541725],[-73.93495859566971,40.834882778542124],[-73.93497941208676,40.83478912095097],[-73.93498833140738,40.83474865739103],[-73.9349508717846,40.83470667336766],[-73.93495775984087,40.83468276036334],[-73.9349652301556,40.83465684795367],[-73.93496624164193,40.83462912314566],[-73.93497316061229,40.83462163453901],[-73.93497219073575,40.834608144294975],[-73.9349938973671,40.83459766568047],[-73.9350306032399,40.83439236534125],[-73.93503612427712,40.83429832034331],[-73.9350312411865,40.8342488623469],[-73.93504212490855,40.83421139923443],[-73.93507644502898,40.83392404328705],[-73.93506958098244,40.83388731750217],[-73.93508341363373,40.8338655969296],[-73.93509687471908,40.83371535679221],[-73.93509690469142,40.83368463190984],[-73.93510384255589,40.83365765691782],[-73.93511245100076,40.83342573907345],[-73.9351174304809,40.83337703428137],[-73.93512041704275,40.83335380306745],[-73.93513225848743,40.83334482290499],[-73.93512636041868,40.833326080356244],[-73.93512935500891,40.83329460947993],[-73.93512443552862,40.833281118185184],[-73.93513826819081,40.83326014375564],[-73.93513729127852,40.83324141318857],[-73.9351304030063,40.833228667273474],[-73.93512154973723,40.83320169227403],[-73.93512352949863,40.83318969900627],[-73.93511565892307,40.83317620538069],[-73.93513243602689,40.83316272511775],[-73.93513935847737,40.83315149100031],[-73.93512752379405,40.833142488809095],[-73.93512465984577,40.833051069029295],[-73.93512171853354,40.83302859034757],[-73.93511580946038,40.83302184217288],[-73.93511187882301,40.833003100729215],[-73.9351197936552,40.832983628174176],[-73.93511783020568,40.83296713902448],[-73.93512376770083,40.832945412996715],[-73.93512085547363,40.83290419576848],[-73.93513170518374,40.832902697673674],[-73.93514455114399,40.83288472203649],[-73.93516725294636,40.83287499227589],[-73.9351648084094,40.832852883264245],[-73.93515659173381,40.83268240623723],[-73.93512878131627,40.83236353447687],[-73.93511398341515,40.83236577683714],[-73.9350778924617,40.83194162361134],[-73.9350956518488,40.83194238126701],[-73.93508783728588,40.83185919835],[-73.93506824974105,40.83170406762572],[-73.93502275465515,40.83131999954984],[-73.93498505621454,40.83102735335791],[-73.93493515655048,40.83060244471323],[-73.93489353332376,40.83028656383774],[-73.9348308720848,40.82980843705177],[-73.93479881905735,40.82979230914966],[-73.93480080676932,40.82977283325497],[-73.93478996278796,40.82976832407672],[-73.93479293827903,40.829756340752134],[-73.93478111085824,40.82974059316723],[-73.93477028229691,40.829720361694214],[-73.93475944294427,40.82970012100015],[-73.93476343288322,40.82965740394856],[-73.93474372591281,40.82963716812013],[-73.93473930830996,40.82960906094397],[-73.93474919492779,40.82959708136339],[-73.93473737349919,40.82957458942519],[-73.93472260127821,40.82955060049557],[-73.93472557699927,40.82952736927084],[-73.93471476025762,40.82949514407967],[-73.93470100252415,40.82944043079997],[-73.93469412707415,40.829416455216],[-73.93471288560723,40.829400725442056],[-73.93470504902284,40.8293407746905],[-73.93469321669619,40.829328773601276],[-73.93467597101815,40.82931340221048],[-73.93466228987832,40.82918075994599],[-73.93468302702506,40.829165787333515],[-73.9347076940248,40.82915830979219],[-73.93470080780772,40.829132073631314],[-73.93468107924859,40.82913355777876],[-73.93467714880202,40.82911632854787],[-73.93467025222027,40.829112570426375],[-73.9346544761815,40.82909757696104],[-73.93464959243246,40.829048119407005],[-73.93466193198351,40.82903876029445],[-73.93466295225605,40.82901403331146],[-73.93466495971361,40.82897356682647],[-73.9346561102258,40.8289428363861],[-73.93465515536505,40.82891285858107],[-73.93463641852125,40.828906860149004],[-73.93461640343895,40.82869365579327],[-73.93461171264785,40.82868983525941],[-73.9346026032382,40.8286824012377],[-73.93460883576688,40.82866118868208],[-73.93461052506919,40.82865543568963],[-73.93460804993306,40.82865336301816],[-73.93460065845316,40.82864718221304],[-73.93459873640631,40.82860072400893],[-73.9346019211835,40.82859254929859],[-73.93461159553205,40.82856775518616],[-73.9346091744548,40.82852167496809],[-73.93461017716513,40.828502936290164],[-73.93461219001,40.828457229149016],[-73.93460433489886,40.82842800279464],[-73.9346073136309,40.828401025563686],[-73.93460043185587,40.828382282414324],[-73.93460834555331,40.82836355753896],[-73.93459750565475,40.82834482035352],[-73.93460048488119,40.828328333985056],[-73.93459360040842,40.82830135116008],[-73.93458674147796,40.82825938499242],[-73.93459562914447,40.82825114985731],[-73.93458676210544,40.828238402776456],[-73.9345917047489,40.82822716755982],[-73.9345976397154,40.82820843167847],[-73.93458877267295,40.828195693476445],[-73.93458684240801,40.828156719068794],[-73.93456513010248,40.828162704449355],[-73.93455810179866,40.82811740277772],[-73.93454586266017,40.82804816146416],[-73.93454230638145,40.82802687031302],[-73.93453258880567,40.82796855836172],[-73.93452175638055,40.82790780009134],[-73.93453898134064,40.827903472670705],[-73.93453183092765,40.82784940204341],[-73.93452076528119,40.827765689650995],[-73.93451590672417,40.82772893075845],[-73.9344239633905,40.82704289397212],[-73.93448291058759,40.82703709277874],[-73.93446426549492,40.826842205193444],[-73.93442844400516,40.82646785059142],[-73.93436717955377,40.82582759815849],[-73.93435643452149,40.82582093714432],[-73.93432574289122,40.82523259029442],[-73.93429530732776,40.824649231222764],[-73.93429371670614,40.82449638641463],[-73.93427285440853,40.82443895275131],[-73.93424642895086,40.82416688303718],[-73.93421834803296,40.8237990457901],[-73.93416594530659,40.82307858087094],[-73.93414657666992,40.82302903205041],[-73.93414604382139,40.82302061918035],[-73.93413373625339,40.82280065597678],[-73.93412126165074,40.822647252741696],[-73.93411752612663,40.82252433268843],[-73.9341023336635,40.82220121691671],[-73.9340998673827,40.8221399123411],[-73.93409843799961,40.822104227343814],[-73.9340911251742,40.82199603748752],[-73.93407944723481,40.82180557579623],[-73.93407434354855,40.82171709911024],[-73.93407734095156,40.82169456177001],[-73.93407005522697,40.82155932093417],[-73.93404447476466,40.82117670357437],[-73.93404599779085,40.82113387652021],[-73.93403560443227,40.82095254677987],[-73.93402662334586,40.820816809752856],[-73.93400389615744,40.820397839681526],[-73.93400069207455,40.82033819717466],[-73.93398295705481,40.8199999202017],[-73.93398664640615,40.819968034725136],[-73.93397241531086,40.819729574765596],[-73.93383009163725,40.81971974292704],[-73.9338307684026,40.81959665747723],[-73.93383000695911,40.81949109558767]]] }";
            var expected = json.Replace("'", """);
            return expected;
        }
View Code

然后测试下结果:

            var point = GeoJson.Point(GeoJson.Position(-73.93414657, 40.82302903));
            FilterDefinition<AreaPolygon> find = Builders<AreaPolygon>.Filter.GeoIntersects("Geometry", point);
            var pro = Builders<AreaPolygon>.Projection.Include("AreaName");//获取结果只包含的字段
            var client = new MongoClient("mongodb://localhost:27017");
            var database = client.GetDatabase("areadb");
            IMongoCollection<AreaPolygon> collection = database.GetCollection<AreaPolygon>("area");
            var result= collection.Find(find).Project(pro).FirstOrDefault();

用BsonDocument的方式与此类型,不过对于C#这种静态类型语言,还是直接使用类型泛型比较方便(主要还是辛辛苦苦整理了一份仓储类,然后发现还用用BsonDocument怎么能忍。。。)

            BsonDocument bd = new BsonDocument
            {
                {"AreaName", "Central Harlem North-Polo Grounds"},
                {"AreaId",2 },
                {"Geometry",new BsonDocument()
                {
                    {  "type", "Polygon"},
                    {"coordinates",new BsonArray(){ 73.9557413, 40.7720266 } }
                } }
            };
            var client = new MongoClient("mongodb://localhost:27017");
            var database = client.GetDatabase("areadb");
            var collection = database.GetCollection<BsonDocument>("area");
            collection.InsertOne(bd);

            var point = GeoJson.Point(GeoJson.Position(-73.93414657, 40.82302903));
            FilterDefinition<BsonDocument> find = Builders<BsonDocument>.Filter.GeoIntersects("Geometry", point);
            var res = collection.Find(find);

此处coordinates太复杂了是个fload[][][]类型。。。我吓怕了,测试的时候是序列化对象成Json然后在 BsonDocument.Parse(“”)的方式获取的BsonDocument对象,有兴趣可以试试。。;

原文地址:https://www.cnblogs.com/ylsforever/p/7359186.html