2017-07-04 34 views
0

我想處理具有下述格式的GeoJson數據;R中的GeoJson數據

{ "id": 1, 
     "geometry": 
    { "type": "Point", 
    "coordinates": [ 
     -3.706, 
     40.3], 
    "properties": {"appuserid": "5b46-7d3c-48a6-9c08-cc894", 
    "eventtype": "location", 
    "devicedate": "2016-06-08T07:25:21", 
    "date": "2016-06-08T07:25:06.507", 
    "location": { 
     "building": "2", 
     "floor": "0", 
     "elevation": "" 
     }}} 

的問題是我想用一個「去哪兒」條款爲「appuserid」,並選擇要處理選定的記錄。我不知道該怎麼做?我已經在數據框中保存了Mongodb的數據。
現在我正在嘗試做如下;

library(sqldf) 
    sqldf("SELECT * FROM d WHERE d$properties$appuserid = '0000-0000-0000-0000'") 

但它給出了一個錯誤。

Error: Only lists of raw vectors are currently supported 

代碼在下面;

library(jsonlite); 
    con <- mongo(collection = "geodata", db = "MongoDb", url = "mongodb://192.168.26.18:27017", verbose = FALSE, options = ssl_options()); 
    d <- con$find(); 

    library(jqr) 
    jq(d, '.features[] | select(d$properties$appuserid == "5b46-7d3c-48a6-9c08-cc894")') 

    Error : Error in jq.default(d, ".features[] | select(d$properties$appuserid == \"5b46-7d3c-48a6-9c08-cc894\")") : 
    jq method not implemented for data.frame. 
+0

上的數據幀sqldf工作,並要求您提供一個有效的SQL字符串作爲第一個參數。是數據框嗎? $不是一個SQL運算符。建議您查看[問]和[mcve]。 –

回答

0

jqr是一種選擇,的R客戶端JQ https://stedolan.github.io/jq/

x <- '{ 
     "type": "FeatureCollection", 
     "features": [ 
      { 
       "type": "Feature", 
       "properties": { 
        "population": 200 
       }, 
       "geometry": { 
        "type": "Point", 
        "coordinates": [ 
         10.724029, 
         59.926807 
        ], 
        "properties": { 
         "appuserid": "5b46-7d3c-48a6-9c08-cc894" 
        } 
       } 
      }, 
      { 
       "type": "Feature", 
       "properties": { 
        "population": 600 
       }, 
       "geometry": { 
        "type": "Point", 
        "coordinates": [ 
         10.715789, 
         59.904778 
        ], 
        "properties": { 
         "appuserid": "c7e866a7-e32d-4dc2-adfd-c2ca065b25ce" 
        } 
       } 
      } 
     ] 
    }' 

    library(jqr) 
    jq(x, '.features[] | select(.geometry.properties.appuserid == "5b46-7d3c-48a6-9c08-cc894")') 

回報

{ 
     "type": "Feature", 
     "properties": { 
      "population": 200 
     }, 
     "geometry": { 
      "type": "Point", 
      "coordinates": [ 
       10.724029, 
       59.926807 
      ], 
      "properties": { 
       "appuserid": "5b46-7d3c-48a6-9c08-cc894" 
      } 
     } 
    } 
+0

謝謝你的回覆。但它不適用於數據框架。 – sara

+0

我建議你嘗試'dplyr'解析data.frames – sckott