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
    在这里插入图片描述

学习资料

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