在 Azure Cosmos DB SQL API 帐户中使用地理空间和 GeoJSON 位置数据Use Geospatial and GeoJSON location data with Azure Cosmos DB SQL API account

本文介绍 Azure Cosmos DB 中的地理空间功能。This article is an introduction to the geospatial functionality in Azure Cosmos DB. 目前,仅 Cosmos DB SQL API 帐户支持存储和访问地理空间数据。Currently storing and accessing geospatial data is supported by Cosmos DB SQL API accounts only. 阅读本文后,能够回答以下问题:After reading this article, you will be able to answer the following questions:

  • 如何在 Azure Cosmos DB 中存储空间数据?How do I store spatial data in Azure Cosmos DB?
  • 如何使用 SQL 和 LINQ 查询 Azure Cosmos DB 中的地理空间数据?How can I query geospatial data in Azure Cosmos DB in SQL and LINQ?
  • 如何在Azure Cosmos DB 中启用或禁用空间索引?How do I enable or disable spatial indexing in Azure Cosmos DB?

本文介绍如何通过 SQL API 使用空间数据。This article shows how to work with spatial data with the SQL API. 请参阅此 GitHub 项目中的代码示例。See this GitHub project for code samples.

空间数据简介Introduction to spatial data

空间数据用于描述空间中对象的位置和形状。Spatial data describes the position and shape of objects in space. 在大部分应用程序中,这些会对应于地球上的对象,也就是地理空间数据。In most applications, these correspond to objects on the earth and geospatial data. 空间数据可以用来表示人、名胜古迹、城市边界或湖泊所处的位置。Spatial data 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 example, "find all coffee shops near my current location."

GeoJSONGeoJSON

Azure Cosmos DB 支持对使用 GeoJSON 规范表示的地理空间点数据进行索引编制和查询。Azure Cosmos DB supports indexing and querying of geospatial point data that's represented using the GeoJSON specification. GeoJSON 数据结构永远是有效的 JSON 对象,因此可以通过 Azure Cosmos DB 来存储和查询,无需任何专用的工具或库。GeoJSON data structures are always valid JSON objects, so they can be stored and queried using Azure Cosmos DB without any specialized tools or libraries. Azure Cosmos DB SDK 提供帮助程序类和方法,让空间数据更易使用。The Azure Cosmos DB SDKs provide helper classes and methods that make it easy to work with spatial data.

点、线串和多边形Points, LineStrings, and Polygons

代表空间中的单一位置。A Point denotes a single position in space. 在地理空间数据中,某个点所代表的确切位置可能是杂货店、电话亭、汽车或城市的街道地址。In geospatial data, a Point represents the exact location, which could be a street address of a grocery store, a kiosk, an automobile, or a city. 点使用其坐标对或经纬度,以 GeoJSON 格式(和 Azure Cosmos DB)表示。A point is represented in GeoJSON (and Azure Cosmos DB) using its coordinate pair or longitude and latitude. 以下是点的 JSON 示例。Here's an example JSON for a point.

Azure Cosmos DB 中的点 Points in Azure Cosmos DB

{
    "type":"Point",
    "coordinates":[ 31.9, -4.8 ]
}

备注

GeoJSON 规范先指定经度,再指定纬度。The GeoJSON specification specifies longitude first and latitude second. 与其他地图应用程序一样,经度和纬度为角度,并以度为单位表示。Like in other mapping applications, longitude and latitude are angles and represented in terms of degrees. 经度值从本初子午线测量,并介于 -180 度和 180.0 度之间;纬度值从赤道测量,并介于 -90.0 度和 90.0 度之间。Longitude values are measured from the Prime Meridian and are between -180 degrees and 180.0 degrees, and latitude values are measured from the equator and are between -90.0 degrees and 90.0 degrees.

Azure Cosmos DB 会将坐标解释为按照 WGS-84 参考系统表示。Azure Cosmos DB interprets coordinates as represented per the WGS-84 reference system. 有关坐标参考系统的更多详细信息,请参阅下文。See below for more details about coordinate reference systems.

如下面包含位置数据的用户配置文件示例所示,此数据可以嵌入 Azure Cosmos DB 文档中:This can be embedded in an Azure Cosmos DB document as shown in this example of a user profile containing location data:

存储在 Azure Cosmos DB 中包含位置的用户配置文件 Use Profile with Location stored in Azure Cosmos DB

{
    "id":"cosmosdb-profile",
    "screen_name":"@CosmosDB",
    "city":"Redmond",
    "topics":[ "global", "distributed" ],
    "location":{
        "type":"Point",
        "coordinates":[ 31.9, -4.8 ]
    }
}

除了点之外,GeoJSON 也支持 LineString 和多边形。In addition to points, GeoJSON also supports LineStrings and Polygons. LineString 表示空间中一连串的点(两个或更多个)以及连接这些点的线段。LineStrings represent a series of two or more points in space and the line segments that connect them. 在地理空间数据中,LineString 通常用来表示高速公路或河流。In geospatial data, LineStrings are commonly used to represent highways or rivers. 多边形 是形成闭合的 LineString 的相连接的点的边界。A Polygon is a boundary of connected points that forms a closed LineString. 多边形通常用来表示自然构成物(例如湖泊),或表示政治管辖权(例如省/市/自治区)。Polygons are commonly used to represent natural formations like lakes or political jurisdictions like cities and states. 以下是 Azure Cosmos DB 中多边形的示例。Here's an example of a Polygon in Azure Cosmos DB.

GeoJSON 中的多边形 Polygons in GeoJSON

{
    "type":"Polygon",
    "coordinates":[ [
        [ 31.8, -5 ],
        [ 31.8, -4.7 ],
        [ 32, -4.7 ],
        [ 32, -5 ],
        [ 31.8, -5 ]
    ] ]
}

备注

GeoJSON 规范需要此数据才能形成有效的多边形;若要创建一个闭合的形状,最后一个坐标对应该与第一个坐标对相同。The GeoJSON specification requires that for valid Polygons, the last coordinate pair provided should be the same as the first, to create a closed shape.

多边形内的点必须以逆时针顺序指定。Points within a Polygon must be specified in counter-clockwise order. 以顺时针顺序指定的多边形表示其中的区域倒转。A Polygon specified in clockwise order represents the inverse of the region within it.

除了点、LineString 和多边形之外,GeoJSON 也会针对如何对多个地理空间位置分组以及如何将任意属性与地理位置关联成为特征指定表示形式。In addition to Point, LineString, and Polygon, GeoJSON also specifies the representation for how to group multiple geospatial locations, as well as how to associate arbitrary properties with geolocation as a Feature. 由于这些对象都是有效的 JSON,因此均可在 Azure Cosmos DB 中存储及处理。Since these objects are valid JSON, they can all be stored and processed in Azure Cosmos DB. 不过,Azure Cosmos DB 仅支持自动编制点的索引。However Azure Cosmos DB only supports automatic indexing of points.

坐标参考系统Coordinate reference systems

由于地球的形状不规则,地理空间数据的坐标可以用许多坐标参考系统 (CRS) 来表示,而这些系统各有自己的参考框架和测量单位。Since the shape of the earth is irregular, coordinates of geospatial data are represented in many coordinate reference systems (CRS), each with their own frames of reference and units of measurement. 例如,“英国国家网格”对英国而言是非常精确的参考系统,但对其他地区则不是。For example, the "National Grid of Britain" is a reference system is accurate for the United Kingdom, but not outside it.

现今最常使用的 CRS 是世界测地系统 WGS-84The most popular CRS in use today is the World Geodetic System WGS-84. GPS 设备和许多地图服务(包括谷歌地图和必应地图 API)均使用 WGS-84。GPS devices, and many mapping services including Google Maps and Bing Maps APIs use WGS-84. Azure Cosmos DB 仅支持对使用 WGS-84 CRS 的地理空间数据进行索引编制和查询。Azure Cosmos DB supports indexing and querying of geospatial data using the WGS-84 CRS only.

创建包含空间数据的文档Creating documents with spatial data

创建包含 GeoJSON 值的文档时,值会根据容器的索引策略,自动以空间索引进行索引编制。When you create documents that contain GeoJSON values, they are automatically indexed with a spatial index in accordance to the indexing policy of the container. 如果以动态类型化语言(如 Python 或 Node.js)使用 Azure Cosmos DB SDK,则必须创建有效的 GeoJSON。If you're working with an Azure Cosmos DB SDK in a dynamically typed language like Python or Node.js, you must create valid GeoJSON.

在 Node.js 中创建包含地理空间数据的文档Create Document with Geospatial data in Node.js

var userProfileDocument = {
    "name":"cosmosdb",
    "location":{
        "type":"Point",
        "coordinates":[ -122.12, 47.66 ]
    }
};

client.createDocument(`dbs/${databaseName}/colls/${collectionName}`, userProfileDocument, (err, created) => {
    // additional code within the callback
});

如果使用 SQL API,则可以在 Microsoft.Azure.Documents.Spatial 命名空间中使用 PointPolygon 类,将位置信息嵌入应用程序对象中。If you're working with the SQL APIs, you can use the Point and Polygon classes within the Microsoft.Azure.Documents.Spatial namespace to embed location information within your application objects. 这些类有助于简化将空间数据序列化和反序列化为 GeoJSON 的过程。These classes help simplify the serialization and deserialization of spatial data into GeoJSON.

在 .NET 中创建包含地理空间数据的文档Create Document with Geospatial data in .NET

using Microsoft.Azure.Documents.Spatial;

public class UserProfile
{
    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("location")]
    public Point Location { get; set; }

    // More properties
}

await client.CreateDocumentAsync(
    UriFactory.CreateDocumentCollectionUri("db", "profiles"), 
    new UserProfile 
    { 
        Name = "cosmosdb", 
        Location = new Point (-122.12, 47.66) 
    });

如果没有经纬度信息,但有物理地址或位置名称,如城市或国家/地区,则可以使用必应地图 REST 服务等地理编码服务来查找实际的坐标。If you don't have the latitude and longitude information, but have the physical addresses or location name like city or country/region, you can look up the actual coordinates by using a geocoding service like Bing Maps REST Services. 此处详细了解必应地图地理编码。Learn more about Bing Maps geocoding here.

查询空间类型Querying spatial types

探讨过如何插入地理空间数据之后,现在来看看如何通过 SQL 和 LINQ 使用 Azure Cosmos DB 查询此数据。Now that we've taken a look at how to insert geospatial data, let's take a look at how to query this data using Azure Cosmos DB using SQL and LINQ.

空间 SQL 内置函数Spatial SQL built-in functions

Azure Cosmos DB 支持以下用于查询地理空间的开放地理空间信息联盟 (OGC) 内置函数。Azure Cosmos DB supports the following Open Geospatial Consortium (OGC) built-in functions for geospatial querying. 有关 SQL 语言中的整套内置函数的详细信息,请参阅查询 Azure Cosmos DBFor more information on the complete set of built-in functions in the SQL language, see Query Azure Cosmos DB.

使用情况Usage 说明Description
ST_DISTANCE (spatial_expr, spatial_expr)ST_DISTANCE (spatial_expr, spatial_expr) 返回两个 GeoJSON 点、多边形或 LineString 表达式之间的距离。Returns the distance between the two GeoJSON Point, Polygon, or LineString expressions.
ST_WITHIN (spatial_expr, spatial_expr)ST_WITHIN (spatial_expr, spatial_expr) 返回一个布尔表达式,该表达式指示第一个 GeoJSON 对象(点、多边形或 LineString)是否在第二个 GeoJSON 对象(点、多边形或 LineString)内。Returns a Boolean expression indicating whether the first GeoJSON object (Point, Polygon, or LineString) is within the second GeoJSON object (Point, Polygon, or LineString).
ST_INTERSECTS (spatial_expr, spatial_expr)ST_INTERSECTS (spatial_expr, spatial_expr) 返回一个布尔表达式,该表达式指示两个指定的 GeoJSON 对象(点、多边形或 LineString)是否相交。Returns a Boolean expression indicating whether the two specified GeoJSON objects (Point, Polygon, or LineString) intersect.
ST_ISVALIDST_ISVALID 返回一个布尔值,该值指示指定的 GeoJSON 点、多边形或 LineString 表达式是否有效。Returns a Boolean value indicating whether the specified GeoJSON Point, Polygon, or LineString expression is valid.
ST_ISVALIDDETAILEDST_ISVALIDDETAILED 如果指定的 GeoJSON 点、多边形或 LineString 表达式有效,则返回包含布尔值的 JSON 值;如果无效,则额外加上作为字符串值的原因。Returns a JSON value containing a Boolean value if the specified GeoJSON Point, Polygon, or LineString expression is valid, and if invalid, additionally the reason as a string value.

空间函数可用于对空间数据执行邻近查询。Spatial functions can be used to perform proximity queries against spatial data. 例如,以下查询使用 ST_DISTANCE 内置函数返回所有家族文档,且这些文档在指定位置的 30 公里内。For example, here's a query that returns all family documents that are within 30 km of the specified location using the ST_DISTANCE built-in function.

查询Query

SELECT f.id 
FROM Families f 
WHERE ST_DISTANCE(f.location, {'type': 'Point', 'coordinates':[31.9, -4.8]}) < 30000

结果Results

[{
  "id": "WakefieldFamily"
}]

如果索引策略中包含空间索引,则通过索引有效地进行“距离查询”。If you include spatial indexing in your indexing policy, then "distance queries" will be served efficiently through the index. 有关空间索引的详细信息,请参阅以下部分。For more information on spatial indexing, see the section below. 如果没有指定路径的空间索引,仍然可以通过指定 x-ms-documentdb-query-enable-scan 请求标头(其值设置为“true”)执行空间查询。If you don't have a spatial index for the specified paths, you can still perform spatial queries by specifying x-ms-documentdb-query-enable-scan request header with the value set to "true". 在 .NET 中,可以通过将可选的 FeedOptions 参数传递到 EnableScanInQuery 设置为 true 的查询来完成此操作。In .NET, this can be done by passing the optional FeedOptions argument to queries with EnableScanInQuery set to true.

ST_WITHIN 可用于检查点是否在多边形内。ST_WITHIN can be used to check if a point lies within a Polygon. 多边形通常用于表示边界,例如邮政编码、省/自治区边界或自然构成物。Commonly Polygons are used to represent boundaries like zip codes, state boundaries, or natural formations. 再次说明,如果在索引策略中包含空间索引,则将通过索引有效地进行“within”查询。Again if you include spatial indexing in your indexing policy, then "within" queries will be served efficiently through the index.

ST_WITHIN 中的多边形参数只能包含单个环形,也就是说,多边形本身不能包含洞。Polygon arguments in ST_WITHIN can contain only a single ring, that is, the Polygons must not contain holes in them.

查询Query

SELECT * 
FROM Families f 
WHERE ST_WITHIN(f.location, {
    'type':'Polygon', 
    'coordinates': [[[31.8, -5], [32, -5], [32, -4.7], [31.8, -4.7], [31.8, -5]]]
})

结果Results

[{
  "id": "WakefieldFamily",
}]

备注

与 Azure Cosmos DB 查询中不匹配类型的工作方式类似,如果任一参数中指定的位置值格式不正确或无效,则会评估为未定义,并且会在查询结果中跳过已评估的文档。Similar to how mismatched types work in Azure Cosmos DB query, if the location value specified in either argument is malformed or invalid, then it evaluates to undefined and the evaluated document to be skipped from the query results. 如果查询没有返回任何结果,请运行 ST_ISVALIDDETAILED 进行调试,以了解空间类型无效的原因。If your query returns no results, run ST_ISVALIDDETAILED To debug why the spatial type is invalid.

Azure Cosmos DB 还支持执行反向查询,即可在 Azure Cosmos DB 中索引多边形或线,并查询包含指定点的区域。Azure Cosmos DB also supports performing inverse queries, that is, you can index polygons or lines in Azure Cosmos DB, then query for the areas that contain a specified point. 这种模式通常在物流中用于识别如卡车进入或离开指定区域的时间等情况。This pattern is commonly used in logistics to identify, for example, when a truck enters or leaves a designated area.

查询Query

SELECT * 
FROM Areas a 
WHERE ST_WITHIN({'type': 'Point', 'coordinates':[31.9, -4.8]}, a.location)

结果Results

[{
  "id": "MyDesignatedLocation",
  "location": {
    "type":"Polygon", 
    "coordinates": [[[31.8, -5], [32, -5], [32, -4.7], [31.8, -4.7], [31.8, -5]]]
  }
}]

ST_ISVALID 和 ST_ISVALIDDETAILED 可用来检查空间对象是否有效。ST_ISVALID and ST_ISVALIDDETAILED can be used to check if a spatial object is valid. 例如,下列查询检查纬度值 (-132.8) 超出范围的点的有效性。For example, the following query checks the validity of a point with an out of range latitude value (-132.8). ST_ISVALID 仅返回一个布尔值,ST_ISVALIDDETAILED 则返回布尔值和字符串,字符串中包含被视为无效的原因。ST_ISVALID returns just a Boolean value, and ST_ISVALIDDETAILED returns the Boolean and a string containing the reason why it is considered invalid.

查询Query

SELECT ST_ISVALID({ "type": "Point", "coordinates": [31.9, -132.8] })

结果Results

[{
  "$1": false
}]

这些函数也可以用来验证多边形。These functions can also be used to validate Polygons. 例如,在这里我们使用 ST_ISVALIDDETAILED 来验证未闭合的多边形。For example, here we use ST_ISVALIDDETAILED to validate a Polygon that is not closed.

查询Query

SELECT ST_ISVALIDDETAILED({ "type": "Polygon", "coordinates": [[ 
    [ 31.8, -5 ], [ 31.8, -4.7 ], [ 32, -4.7 ], [ 32, -5 ] 
    ]]})

结果Results

[{
   "$1": { 
        "valid": false, 
        "reason": "The Polygon input is not valid because the start and end points of the ring number 1 are not the same. Each ring of a Polygon must have the same start and end points." 
      }
}]

.NET SDK 中的 LINQ 查询LINQ Querying in the .NET SDK

SQL .NET SDK 还提供存根方法 Distance()Within(),供用户在 LINQ 表达式中使用。The SQL .NET SDK also providers stub methods Distance() and Within() for use within LINQ expressions. SQL LINQ 提供程序会将这些方法调用转换为等效的 SQL 内置函数调用(分别为 ST_DISTANCE 和 ST_WITHIN)。The SQL LINQ provider translates this method calls to the equivalent SQL built-in function calls (ST_DISTANCE and ST_WITHIN respectively).

以下是 LINQ 查询的示例,该查询使用 LINQ 在 Azure Cosmos 容器中查找其“位置”值在指定点的 30 公里半径内的所有文档。Here's an example of a LINQ query that finds all documents in the Azure Cosmos container whose "location" value is within a radius of 30 km of the specified point using LINQ.

LINQ 距离查询LINQ query for Distance

foreach (UserProfile user in client.CreateDocumentQuery<UserProfile>(UriFactory.CreateDocumentCollectionUri("db", "profiles"))
    .Where(u => u.ProfileType == "Public" && u.Location.Distance(new Point(32.33, -4.66)) < 30000))
{
    Console.WriteLine("\t" + user);
}

同样地,以下查询查找所有文档,这些文档的“位置”均在指定的方框/多边形内。Similarly, here's a query for finding all the documents whose "location" is within the specified box/Polygon.

LINQ Within 查询LINQ query for Within

Polygon rectangularArea = new Polygon(
    new[]
    {
        new LinearRing(new [] {
            new Position(31.8, -5),
            new Position(32, -5),
            new Position(32, -4.7),
            new Position(31.8, -4.7),
            new Position(31.8, -5)
        })
    });

foreach (UserProfile user in client.CreateDocumentQuery<UserProfile>(UriFactory.CreateDocumentCollectionUri("db", "profiles"))
    .Where(a => a.Location.Within(rectangularArea)))
{
    Console.WriteLine("\t" + user);
}

探讨过如何使用 LINQ 和 SQL 查询文档之后,现在来看看如何针对空间索引配置 Azure Cosmos DB。Now that we've taken a look at how to query documents using LINQ and SQL, let's take a look at how to configure Azure Cosmos DB for spatial indexing.

索引Indexing

使用 Azure Cosmos DB 进行架构不可知的索引一文中所述,Azure Cosmos DB 数据库引擎具有真正不可知的架构,并提供一流的 JSON 支持。As we described in the Schema Agnostic Indexing with Azure Cosmos DB paper, we designed Azure Cosmos DB's database engine to be truly schema agnostic and provide first class support for JSON. Azure Cosmos DB 的写入优化数据库引擎可以通过本机方式了解用 GeoJSON 标准表示的空间数据(点、多边形和线)。The write optimized database engine of Azure Cosmos DB natively understands spatial data (points, Polygons, and lines) represented in the GeoJSON standard.

简单来说,测地坐标的几何图形会投影在 2D 平面上,并使用四叉树以渐进方式划分成单元格。In a nutshell, the geometry is projected from geodetic coordinates onto a 2D plane then divided progressively into cells using a quadtree. 这些单元格会根据 Hilbert 空间填充曲线内的单元格位置映射到 1D,并保留点的位置。These cells are mapped to 1D based on the location of the cell within a Hilbert space filling curve, which preserves locality of points. 此外,当位置数据进行索引编制后,会经历称为 分割的过程,也就是说,在某个位置上相交的所有单元格都会被识别为键并存储在 Azure Cosmos DB 索引中。Additionally when location data is indexed, it goes through a process known as tessellation, that is, all the cells that intersect a location are identified and stored as keys in the Azure Cosmos DB index. 在查询时,点和多边形等参数也会经过分割,以提取相关的格子 ID 范围,并用于从索引检索数据。At query time, arguments like points and Polygons are also tessellated to extract the relevant cell ID ranges, then used to retrieve data from the index.

如果指定的索引策略包含 /*(所有路径)的空间索引,则表示在集合中找到的所有点均已编制索引,能进行有效的空间查询(ST_WITHIN 和 ST_DISTANCE)。If you specify an indexing policy that includes spatial index for /* (all paths), then all points found within the collection are indexed for efficient spatial queries (ST_WITHIN and ST_DISTANCE). 空间索引没有精度值,并且始终使用默认的精度值。Spatial indexes do not have a precision value, and always use a default precision value.

备注

Azure Cosmos DB 支持点、多边形和 LineString 的自动索引Azure Cosmos DB supports automatic indexing of Points, Polygons, and LineStrings

以下 JSON 代码段演示已启用空间索引的索引策略,也就是为文档中找到的所有 GeoJSON 点编制索引,以用于空间查询。The following JSON snippet shows an indexing policy with spatial indexing enabled, that is, index any GeoJSON point found within documents for spatial querying. 如果要使用 Azure 门户修改索引策略,可以为索引策略指定以下 JSON,以便对集合启用空间索引。If you are modifying the indexing policy using the Azure portal, you can specify the following JSON for indexing policy to enable spatial indexing on your collection.

针对点和多边形启用了空间索引的集合索引策略 JSONCollection Indexing Policy JSON with Spatial enabled for points and Polygons

{
   "automatic":true,
   "indexingMode":"Consistent",
   "includedPaths":[
      {
         "path":"/*",
         "indexes":[
            {
               "kind":"Range",
               "dataType":"String",
               "precision":-1
            },
            {
               "kind":"Range",
               "dataType":"Number",
               "precision":-1
            },
            {
               "kind":"Spatial",
               "dataType":"Point"
            },
            {
               "kind":"Spatial",
               "dataType":"Polygon"
            }                
         ]
      }
   ],
   "excludedPaths":[
   ]
}

以下是 .NET 中的代码段,演示如何创建针对所有包含点的路径启用空间索引的集合。Here's a code snippet in .NET that shows how to create a collection with spatial indexing turned on for all paths containing points.

创建具有空间索引的集合Create a collection with spatial indexing

DocumentCollection spatialData = new DocumentCollection()
spatialData.IndexingPolicy = new IndexingPolicy(new SpatialIndex(DataType.Point)); //override to turn spatial on by default
collection = await client.CreateDocumentCollectionAsync(UriFactory.CreateDatabaseUri("db"), spatialData);

下面说明了如何修改现有的集合,以便对文档内存储的所有点使用空间索引。And here's how you can modify an existing collection to take advantage of spatial indexing over any points that are stored within documents.

修改具有空间索引的现有集合Modify an existing collection with spatial indexing

Console.WriteLine("Updating collection with spatial indexing enabled in indexing policy...");
collection.IndexingPolicy = new IndexingPolicy(new SpatialIndex(DataType.Point));
await client.ReplaceDocumentCollectionAsync(collection);

Console.WriteLine("Waiting for indexing to complete...");
long indexTransformationProgress = 0;
while (indexTransformationProgress < 100)
{
    ResourceResponse<DocumentCollection> response = await client.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri("db", "coll"));
    indexTransformationProgress = response.IndexTransformationProgress;

    await Task.Delay(TimeSpan.FromSeconds(1));
}

备注

如果文档中的 GeoJSON 位置值格式不正确或无效,则不会为其编制索引以用于空间查询。If the location GeoJSON value within the document is malformed or invalid, then it will not get indexed for spatial querying. 可以使用 ST_ISVALID 和 ST_ISVALIDDETAILED 验证位置值。You can validate location values using ST_ISVALID and ST_ISVALIDDETAILED.

如果集合定义包含分区键,则不会报告索引转换进度。If your collection definition includes a partition key, indexing transformation progress is not reported.

后续步骤Next steps

已经学会如何开始使用 Azure Cosmos DB 中的地理空间支持,下一步现在可以:Now that you have learned how to get started with geospatial support in Azure Cosmos DB, next you can: