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 ] })
执行上述查询后,您将收到以下o utput.
[ { "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/" } 从类型对象创建的文档