1. 程式人生 > >讀取bson文件——JavaScript讀取/解析bson文件並存至txt文件

讀取bson文件——JavaScript讀取/解析bson文件並存至txt文件

for 指定元素 http 讀取 con UNC pos tps pre

由於最近項目需要,不得不讀取 .bson 文件,網上都是讀取 json 的,關於 bson 的幾乎沒有!!折騰了兩天,最終整理出個路子,如下:

首先是通過 JavaScript 讀取的,為啥要用這個語言,不知道不清楚不了解!!!好吧,實際上是 libbson(C++)沒搞出來,一沒文檔,二沒帖子,看源碼也沒看出個所以然;Python 就更別說了(┭┮﹏┭┮被逼地簡直頭皮發麻***)

要說明的是,我是需要讀取 .bson 文件中的特定內容,也就是指定元素的值。 libbson 提供了掰著手指頭都能數得過來的幾個例子,有一個是可以讀取bson文件然後輸出到命令行的,是一股腦地輸出,不想要的也輸出,而且沒有層次,長下面這鬼樣紙:

技術分享圖片

所以最終還是用 JS 實現的。

JS無需編譯,直接運行 .js 文件即可。這裏是通過 Node.js (一個方便使用 JavaScript 的環境)實現的,使用方法如下:

node bson_gao.js gao.bson 789.txt

node 就是用 Node.js 啟動,bson_gao.js 是我編寫的讀取 bson 文件的腳本,gao.bson 是要讀取的文件,123.txt 是最終要存儲數據的文件。

運行這個指令後會出現一堆莫名其妙的文件,本人不是專業搞 JS 的,所以也不清楚運作機理,不過這些都不重要。重要的是,程序會讀取 gao.bson 文件,然後把需要讀取的數據存入 123.txt。

運行代碼前,當然要先裝上 Node.js,並且在裏面下載好 fs 和 bson 模塊,這個網上搜一下有很多資料的。

bson_gao.js 代碼如下:

 1 const fs = require("fs");
 2 const bson = require(‘bson‘);
 3 
 4 var fileName = process.argv.splice(2);    //讀取命令行輸入參數,從第3個開始
 5 console.log("讀取內容為 : ");
 6 fs.readFile(fileName[0].toString(),
 7     function(err,data){
8 if(err){ 9 console.error(err); 10 } 11 else{ 12 var mappoints = bson.deserialize(data); 13 let mapPointsCount = 0; 14 15 for(let pointIndex in mappoints){ 16 let pos = getPosition(mappoints[pointIndex][‘mWorldPos‘]); 17 let pos_x = pos.x; 18 let pos_y = pos.y; 19 let pos_z = pos.z; 20 console.log(pos_x + ‘\t‘ + pos_y + ‘\t‘ + pos_z); 21 mapPointsCount ++; 22 writeToTxt(‘pos_x:‘+pos_x + ‘\t‘ + ‘pos_y:‘+pos_y + ‘\t‘ + ‘pos_z:‘+pos_z + ‘\n‘); 23 } 24 console.log("Number of data : " + mapPointsCount); 25 } 26 } 27 ); 28 29 //定義的子函數 30 31 function getPosition (worldPos){ 32 var buf = worldPos[‘data‘][‘buffer‘]; 33 return { 34 ‘x‘: buf.readFloatLE(0), 35 ‘y‘: buf.readFloatLE(4), 36 ‘z‘: buf.readFloatLE(8) 37 } 38 } 39 40 function writeToTxt(_data){ 41 fs.writeFile(fileName[1].toString(),_data,{flag:‘a‘},function(err){ 42 if(err) 43 console.log("文件寫入失敗") 44 }); 45 }

我的這個bson文件保存的是關於點雲的數據,當然還有一些亂七八糟的,我要讀取的是 mWorldPos,保存了 x、y、z 三個坐標值,每個值占 4 個字節(32位)。所以我首先要確定每個 mWorldPos 的位置,然後從它的 buffer 裏分別讀取三個坐標數據。

我的 gao.bson 的部分數據(通過 libbson 讀取的):

{ "0" : { "mnId" : "30", "nNextId" : "156441", "mnFirstKFid" : { "$numberLong" : "2"}, "mnFirstFrame" : { "$numberLong" : "64"}, "nObs" : { "$numberInt" : "5" }, "mTrackProjX" : { "$numberDouble" : "511.873443603515625" }, "mTrackProjY" : { "$numberDouble" : "37.295444488525390625" }, "mTrackProjXR" : { "$numberDouble" : "511.873443603515625" }, "mbTrackInView" : false, "mnTrackScaleLevel" : { "$numberInt" : "0" }, "mTrackViewCos" : { "$numberDouble" : "0.99079859256744384766" }, "mnTrackReferenceForFrame" : "5215", "mnLastFrameSeen" : "106", "mnBALocalForKF" : "611", "mnFuseCandidateForKF" : "313", "mnLoopPointForKF" : "608", "mnCorrectedByKF" : "0", "mnCorrectedReference" : "0", "mPosGBA" : { "rows" : { "$numberLong" : "3"}, "cols" : { "$numberLong" : "1"}, "type" : { "$numberLong" : "5"}, "data" : { "$binary" : { "base64": "RZ4+Pzs14L9m31RA", "subType" : "00" } } }, "mnBAGlobalForKF" : "621", "mWorldPos" : { "rows" : { "$numberLong" : "3"}, "cols" : { "$numberLong" : "1"}, "type" : { "$numberLong" : "5"}, "data" : { "$binary" : { "base64": "RZ4+Pzs14L9m31RA", "subType" : "00" } } }, "mObservations" : [ [ "7", "22" ], [ "1", "105" ], [ "3", "385" ], [ "2", "121" ], [ "6", "274" ] ], "mNormalVector" : { "rows" : { "$numberLong" : "3"}, "cols" : { "$numberLong" : "1"}, "type" : { "$numberLong" : "5"}, "data" : { "$binary" : { "base64": "J7BnPnsp576v6Vw/", "subType" : "00" } } }, "mDescriptor" : { "rows" : { "$numberLong" : "1"}, "cols" : { "$numberLong" : "32"}, "type" : { "$numberLong" : "0"}, "data" : { "$binary" : { "base64": "ghQf++KkvVe8W1N2tP4saec7P4GznRNT1ar6iJhAobs=", "subType" : "00" } } }, "mpRefKF" : "2", "mnVisible" : { "$numberInt" : "151" }, "mnFound" : { "$numberInt" : "19" }, "mbBad" : false, "mfMinDistance" : { "$numberDouble" : "1.1126192808151245117" }, "mfMaxDistance" : { "$numberDouble" : "3.9867167472839355469" } }, "1" : { "mnId" : "31", "nNextId" : "156441", "mnFirstKFid" : { "$numberLong" : "2"}, "mnFirstFrame" : { "$numberLong" : "64"}, "nObs" : { "$numberInt" : "9" }, "mTrackProjX" : { "$numberDouble" : "513.37762451171875" }, "mTrackProjY" : { "$numberDouble" : "39.406429290771484375" }, "mTrackProjXR" : { "$numberDouble" : "513.37762451171875" }, "mbTrackInView" : false, "mnTrackScaleLevel" : { "$numberInt" : "0" }, "mTrackViewCos" : { "$numberDouble" : "0.99058109521865844727" }, "mnTrackReferenceForFrame" : "5215", "mnLastFrameSeen" : "112", "mnBALocalForKF" : "611", "mnFuseCandidateForKF" : "313", "mnLoopPointForKF" : "608", "mnCorrectedByKF" : "0", "mnCorrectedReference" : "0", "mPosGBA" : { "rows" : { "$numberLong" : "3"}, "cols" : { "$numberLong" : "1"}, "type" : { "$numberLong" : "5"}, "data" : { "$binary" : { "base64": "QndHP4Ia5r/8pl9A", "subType" : "00" } } }, "mnBAGlobalForKF" : "621", "mWorldPos" : { "rows" : { "$numberLong" : "3"}, "cols" : { "$numberLong" : "1"}, "type" : { "$numberLong" : "5"}, "data" : { "$binary" : { "base64": "QndHP4Ia5r/8pl9A", "subType" : "00" } } }, "mObservations" : [ [ "7", "18" ], [ "1", "109" ], [ "3", "25" ], [ "2", "122" ], [ "6", "275" ], [ "4", "30" ], [ "9", "18" ], [ "8", "20" ], [ "10", "20" ] ], "mNormalVector" : { "rows" : { "$numberLong" : "3"}, "cols" : { "$numberLong" : "1"}, "type" : { "$numberLong" : "5"}, "data" : { "$binary" : { "base64": "mC1fPi3U6r6sbFw/", "subType" : "00" } } }, "mDescriptor" : { "rows" : { "$numberLong" : "1"}, "cols" : { "$numberLong" : "32"}, "type" : { "$numberLong" : "0"}, "data" : { "$binary" : { "base64": "UpC/+OrkPderW9BztP9se+fzP5GTS7Lh2bJ+ypxQpXs=", "subType" : "00" } } }, "mpRefKF" : "2", "mnVisible" : { "$numberInt" : "176" }, "mnFound" : { "$numberInt" : "47" }, "mbBad" : false, "mfMinDistance" : { "$numberDouble" : "1.1623446941375732422" }, "mfMaxDistance" : { "$numberDouble" : "4.1648921966552734375" } }, "2" : { "mnId" : "3", "nNextId" : "156441", "mnFirstKFid" : { "$numberLong" : "2"}, "mnFirstFrame" : { "$numberLong" : "64"}, "nObs" : { "$numberInt" : "3" }, "mTrackProjX" : { "$numberDouble" : "-0.34699571132659912109" }, "mTrackProjY" : { "$numberDouble" : "-102.95076751708984375" }, "mTrackProjXR" : { "$numberDouble" : "-0.34699571132659912109" }, "mbTrackInView" : false, "mnTrackScaleLevel" : { "$numberInt" : "1" }, "mTrackViewCos" : { "$numberDouble" : "0.77780538797378540039" }, "mnTrackReferenceForFrame" : "5215", "mnLastFrameSeen" : "176", "mnBALocalForKF" : "611", "mnFuseCandidateForKF" : "89", "mnLoopPointForKF" : "608", "mnCorrectedByKF" : "0", "mnCorrectedReference" : "0",

最後存儲的 123.txt 部分數據(通過 bson_gao.js 存儲):

pos_x:11.609704971313477 pos_y:-9.638463020324707 pos_z:48.26372146606445
pos_x:16.311811447143555 pos_y:-0.7860449552536011 pos_z:42.86808776855469
pos_x:-12.45207405090332 pos_y:-7.568289279937744 pos_z:47.80936050415039
pos_x:15.905488967895508 pos_y:-7.598543643951416 pos_z:45.10790252685547
pos_x:16.373966217041016 pos_y:-1.6582328081130981 pos_z:42.468746185302734
pos_x:6.332942962646484 pos_y:-8.755221366882324 pos_z:39.46611022949219
pos_x:16.789087295532227 pos_y:-0.8732663989067078 pos_z:46.31952667236328
pos_x:5.774340629577637 pos_y:-9.464967727661133 pos_z:41.57020950317383
pos_x:0.9664075374603271 pos_y:-2.1578664779663086 pos_z:35.57773971557617
pos_x:3.38576602935791 pos_y:-10.23925495147705 pos_z:43.15412139892578
pos_x:16.545536041259766 pos_y:-1.392843246459961 pos_z:46.86875915527344
pos_x:17.851194381713867 pos_y:-8.643896102905273 pos_z:58.38813781738281
pos_x:18.102949142456055 pos_y:-5.744359493255615 pos_z:53.23180389404297
pos_x:18.095937728881836 pos_y:-0.978411078453064 pos_z:41.19490432739258
pos_x:-5.2878923416137695 pos_y:-3.3611886501312256 pos_z:57.568790435791016
pos_x:-4.955174922943115 pos_y:-18.943540573120117 pos_z:78.9381103515625
pos_x:14.425850868225098 pos_y:-9.960193634033203 pos_z:45.55425262451172
pos_x:3.803712844848633 pos_y:-1.2650833129882812 pos_z:42.81630325317383
pos_x:8.404397010803223 pos_y:-4.206243515014648 pos_z:38.56267547607422
pos_x:10.646106719970703 pos_y:-2.663133144378662 pos_z:33.2987174987793
pos_x:18.19788360595703 pos_y:-4.282032012939453 pos_z:37.866153717041016
pos_x:3.149993896484375 pos_y:-14.502290725708008 pos_z:66.28900146484375
pos_x:18.281272888183594 pos_y:-3.59720516204834 pos_z:37.58603286743164
pos_x:16.164188385009766 pos_y:-5.562918663024902 pos_z:43.636688232421875
pos_x:10.910879135131836 pos_y:-10.468049049377441 pos_z:49.67988204956055
pos_x:9.463968276977539 pos_y:-4.861118793487549 pos_z:37.85740661621094
pos_x:5.207038879394531 pos_y:-10.244396209716797 pos_z:40.54631805419922
pos_x:9.275063514709473 pos_y:-8.239974975585938 pos_z:37.77473068237305
pos_x:15.380149841308594 pos_y:-5.8706159591674805 pos_z:44.781829833984375
pos_x:5.903839588165283 pos_y:-9.801711082458496 pos_z:39.871360778808594

可以看到變得很工整,而且都是有用的數據。事實上,你懂得,存儲的時候可以按照自己想要存儲的格式保存到 txt,比如我這個可以去掉 pos_x、pos_y、pos_z 這些字母,每行只保留三個數字。

好吧,就這樣吧,放松一下 b( ̄▽ ̄)d ~~~ kebawoleihuaile!!!!

讀取bson文件——JavaScript讀取/解析bson文件並存至txt文件