Documentdb 简明教程
DocumentDB - Geospatial Data
Microsoft 添加了 geospatial support ,它允许您在文档中存储位置数据,并对点和多边形之间的距离和相交进行空间计算。
Microsoft added geospatial support, which lets you store location data in your documents and perform spatial calculations for distance and intersections between points and polygons.
-
Spatial data describes the position and shape of objects in space.
-
Typically, it can be used to represent the location of a person, a place of interest, or the boundary of a city, or a lake.
-
Common use cases often involve proximity queries. For e.g., "find all universities near my current location".
Point 表示空间中的单个位置,它表示确切位置,例如特定大学的街道地址。点在 DocumentDB 中使用其坐标对(经度和纬度)表示。以下是 JSON 点示例。
A Point denotes a single position in space which represents the exact location, e.g. street address of particular university. A point is represented in DocumentDB using its coordinate pair (longitude and latitude). Following is an example of JSON point.
{
"type":"Point",
"coordinates":[ 28.3, -10.7 ]
}
我们来看一个包含大学位置的简单示例。
Let’s take a look at a simple example which contains the location of a university.
{
"id":"case-university",
"name":"CASE: Center For Advanced Studies In Engineering",
"city":"Islamabad",
"location": {
"type":"Point",
"coordinates":[ 33.7194136, -73.0964862 ]
}
}
若要根据位置检索大学名称,您可以使用以下查询。
To retrieve the university name based on the location, you can use the following query.
SELECT c.name FROM c
WHERE c.id = "case-university" AND ST_ISVALID({
"type":"Point",
"coordinates":[ 33.7194136, -73.0964862 ] })
执行以上查询时,您将收到以下输出。
When the above query is executed you will receive the following output.
[
{
"name": "CASE: Center For Advanced Studies In Engineering"
}
]
Create Document with Geospatial Data in .NET
您可以创建包含地理空间数据的新文档,我们来看一个创建大学文档的简单示例。
You can create a document with geospatial data, let’s take a look at a simple example in which a university document is created.
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 类的实现。
Following is the implementation for the UniversityProfile class.
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; }
}
当上文代码被编译和执行时,您将收到如下输出。
When the above code is compiled and executed, you will receive the following output.
**** 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