DocumentDB - 地理空間資料



Microsoft 添加了地理空間支援,允許您在文件中儲存位置資料,並對點和多邊形之間的距離和交集執行空間計算。

  • 空間資料描述了物件在空間中的位置和形狀。

  • 通常,它可以用來表示一個人的位置、一個感興趣的地方,或者一個城市或湖泊的邊界。

  • 常見的用例通常涉及鄰近查詢。例如,“查詢我當前位置附近的全部大學”。

一個表示空間中的單個位置,表示確切位置,例如特定大學的街道地址。點在 DocumentDB 中使用其座標對(經度和緯度)表示。以下是一個 JSON 點的示例。

{ 
   "type":"Point", 
   "coordinates":[ 28.3, -10.7 ] 
} 

讓我們來看一個包含大學位置的簡單示例。

{ 
   "id":"case-university", 
   "name":"CASE: Center For Advanced Studies In Engineering", 
   "city":"Islamabad", 
	
   "location": { 
      "type":"Point", 
      "coordinates":[ 33.7194136, -73.0964862 ] 
   } 
}

要根據位置檢索大學名稱,您可以使用以下查詢。

SELECT c.name FROM c 

WHERE c.id = "case-university" AND ST_ISVALID({ 
      "type":"Point", 
      "coordinates":[ 33.7194136, -73.0964862 ] })

執行上述查詢後,您將收到以下輸出。

[ 
   { 
      "name": "CASE: Center For Advanced Studies In Engineering" 
   } 
] 

在 .NET 中建立包含地理空間資料的文件

您可以建立一個包含地理空間資料的文件,讓我們來看一個建立大學文件的簡單示例。

private async static Task CreateDocuments(DocumentClient client) {
   Console.WriteLine(); 
   Console.WriteLine("**** Create Documents ****"); 
   Console.WriteLine();
	
   var uniDocument = new UniversityProfile {
      Id = "nust", 
      Name = "National University of Sciences and Technology", 
      City = "Islamabad", 
      Loc = new Point(33.6455715, 72.9903447) 
   };
	
   Document document = await CreateDocument(client, uniDocument); 
   Console.WriteLine("Created document {0} from typed object", document.Id); 
   Console.WriteLine(); 
}

以下是 UniversityProfile 類的實現。

public class UniversityProfile { 
   [JsonProperty(PropertyName = "id")] 
   public string Id { get; set; }  
	
   [JsonProperty("name")] 
   public string Name { get; set; }
	
   [JsonProperty("city")] 
   public string City { get; set; }  
	
   [JsonProperty("location")] 
   public Point Loc { get; set; } 
} 

編譯並執行上述程式碼後,您將收到以下輸出。

**** Create Documents ****  
Created new document: nust 
{ 
   "id": "nust", 
   "name": "National University of Sciences and Technology", 
   "city": "Islamabad", 
   "location": { 
      "type": "Point", 
      "coordinates": [ 
         33.6455715, 
         72.9903447 
      ] 
   }, 
   "_rid": "Ic8LAMEUVgANAAAAAAAAAA==", 
   "_ts": 1450200910, 
   "_self": "dbs/Ic8LAA==/colls/Ic8LAMEUVgA=/docs/Ic8LAMEUVgANAAAAAAAAAA==/", 
   "_etag": "\"00004100-0000-0000-0000-56704f4e0000\"", 
   "_attachments": "attachments/" 
} 
Created document nust from typed object 
廣告
© . All rights reserved.