ArangoDB(六)——Geo(collection)

collection

座標包括經度和緯度,可以將兩者存儲爲一個屬性,也可以分開爲兩個屬性存儲。ArangoDB允許座標查詢。所以今天的主要內容時創建座標和查詢座標。

Create Collection

  • 創建數據

    數據來自各城市的座標數據。

    • 創建集合Collection(比如命名爲Locations) 在這裏插入圖片描述
    • 插入數據
      let places = [
          {"name":"Dragostone","coordinate":[55.167801,-6.815096]},
          {"name":"King's Landing","coordinate":[42.639752,18.110189]},
          {"name":"The Red Keep","coordinate":[35.896447,14.446442]},
          {"name":"Winterfell","coordinate":[54.368321,-5.581312]},
          {"name":"Vaes Dothrak","coordinate":[54.16776,-6.096125]},
          {"name":"Beyond the wall","coordinate":[64.265473,-21.094093]},
          {"name":"Yukai","coordinate":[31.046642,-7.129532]},
          {"name":"Astapor","coordinate":[31.50974,-9.774249]}
          ]
      for item in places
      insert item into Locations
      return NEW
      
  • 創建座標索引
    創建座標索引的步驟如下:

    • Go to COLLECTIONS
    • Click on the Locations collection
    • Switch to the Indexes tab at top
    • Click the geen button with a plus on the right -hand side
      在這裏插入圖片描述
    • Change the type to Geo Index
      在這裏插入圖片描述
    • Enter coordinate into the Fields field
      在這裏插入圖片描述
    • Click Create to confirm
      在這裏插入圖片描述

Query Collection

  • NEAR()

    • 語法 :NEAR(collection, latitude, longitude, limit, distanceName)
    • 🌰
      • collection, latitude, longitudelimit參數
        FOR item IN NEAR(Locations, 55.167801, -6.815096,4)  
        RETURN item
        
        在這裏插入圖片描述
      • collection, latitude, longitude, limitdistanceName參數
        FOR item IN NEAR(Locations, 55.167801, -6.815096,4,"distName")  
        RETURN item
        
        在這裏插入圖片描述
  • WITHIN()

    • 語法:WITHIN(coll, latitude, longitude, radius, distanceName)

    • 🌰

      FOR item IN WITHIN(Locations, 55.167801, -6.815096, 120000,"dist")
      RETURN item
      

      在這裏插入圖片描述

    注意 :3.4.0+ 版本中很少使用NEAR()WITHIN(),而是用DISTANCE()來代替,接下來看下DISTANCE()

  • DISTANCE()

    • 語法:DISTANCE(latitude1, longitude1, latitude2, longitude2)

    • 🌰

      FOR item IN Locations
        SORT DISTANCE(doc.latitude, doc.longitude, 55.167801, -6.815096)
        limit 3
        RETURN item
      

      在這裏插入圖片描述

綜合案例——A geo demonstration using Foxx

  • Preparations
    • Installation Foxx

      Arangodb Web UI—— Services——add Service——Upload——導入下載的.zip格式文件

    • 創建集合restaurantsneighborhoods
    • 創建索引
      • restaurants:(type: “geo”, fields: [ “location” ], geoJson:true )
      • neighborhoods(type: “geo”, fields: [ “geometry” ], geoJson:true )
    • 導入數據

      分別向2個集合中導入數據(下載文件的data文件中)

  • Usage
    在這裏插入圖片描述

學習資料

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章