DocumentDB教程
DocumentDB SQL

DocumentDB 地理空间数据

Microsoft 添加了 地理空间支持,让您可以将位置数据存储在文档中,并对点和多边形之间的距离和交点执行空间计算。
空间数据描述了物体在空间中的位置和形状。 通常,它可用于表示一个人的位置、感兴趣的地点、城市边界或湖泊。 常见用例通常涉及邻近查询。例如,"查找我当前位置附近的所有大学"。
A Point 表示空间中的单个位置,表示确切位置,例如特定大学的街道地址。一个点在 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 
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4