空间类型 - geographySpatial Types - geography

适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions) 是Azure SQL 数据库Azure SQL DatabaseYesAzure SQL 数据库Azure SQL Database适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions) 是Azure SQL 数据库Azure SQL DatabaseYesAzure SQL 数据库Azure SQL Database

地理空间数据类型 geography 是作为 SQL ServerSQL Server 中的 .NET 公共语言运行时 (CLR) 数据类型实现的。The geography spatial data type, geography, is implemented as a .NET common language runtime (CLR) data type in SQL ServerSQL Server. 此类型表示圆形地球坐标系中的数据。This type represents data in a round-earth coordinate system. geography 数据类型存储椭球体(圆形地球)数据,如 GPS 纬度和经度坐标SQL ServerSQL ServerThe SQL ServerSQL Server geography data type stores ellipsoidal (round-earth) data, such as GPS latitude and longitude coordinates.

SQL ServerSQL Server 支持 geography 空间数据类型的一组方法。supports a set of methods for the geography spatial data type. 这些方法包括开放地理空间信息联盟 (OGC) 标准和对该标准的一组 MicrosoftMicrosoft 扩展所定义的 geography 方法。This includes methods on geography that are defined by the Open Geospatial Consortium (OGC) standard and a set of MicrosoftMicrosoft extensions to that standard.

geography 方法的容错可高达 1.0e-7 * extents。The error tolerance for the geography methods can be as large as 1.0e-7 * extents. extents 表示 geography 对象的各点之间的近似最大距离。The extents refer to the approximate maximal distance between points of the geography object.

注册 geography 类型Registering the geography Type

geography 类型已进行预定义,可在每个数据库中使用。The geography type is predefined and available in each database. 你可以创建 geography 类型的表列并对 geography 数据进行操作,就像使用其他系统提供的数据类型一样。You can create table columns of type geography and operate on geography data in the same manner as you would use other system-supplied types. 可以用在持久化和非持久化计算列中。Can be used in persisted and non-persisted computed columns.

示例Examples

A.A. 显示如何添加和查询地理数据Showing how to add and query geography data

以下示例说明如何添加和查询地理数据。The following examples show how to add and query geography data. 第一个示例创建包含一个标识列和一个 geographyGeogCol1 的表。The first example creates a table with an identity column and a geography column, GeogCol1. 第三列将 geography 列呈现为其开放地理空间信息联盟 (OGC) 熟知文本 (WKT) 表示形式,并使用 STAsText() 方法。A third column renders the geography column into its Open Geospatial Consortium (OGC) Well-Known Text (WKT) representation, and uses the STAsText() method. 接下来将插入两行:一行包含 LineString 类型的 geography实例,一行包含 Polygon 实例。Two rows are then inserted: one row contains a LineString instance of geography, and one row contains a Polygon instance.

IF OBJECT_ID ( 'dbo.SpatialTable', 'U' ) IS NOT NULL   
    DROP TABLE dbo.SpatialTable;  
GO  
  
CREATE TABLE SpatialTable   
    ( id int IDENTITY (1,1),  
    GeogCol1 geography,   
    GeogCol2 AS GeogCol1.STAsText() );  
GO  
  
INSERT INTO SpatialTable (GeogCol1)  
VALUES (geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656 )', 4326));  
  
INSERT INTO SpatialTable (GeogCol1)  
VALUES (geography::STGeomFromText('POLYGON((-122.358 47.653 , -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))', 4326));  
GO  

B.B. 返回两个 geography 实例的交集Returning the intersection of two geography instances

下面的示例使用 STIntersection() 方法返回以前插入的两个 geography 实例相交的点。The following example uses the STIntersection() method to return the points where the two previously inserted geography instances intersect.

DECLARE @geog1 geography;  
DECLARE @geog2 geography;  
DECLARE @result geography;  
  
SELECT @geog1 = GeogCol1 FROM SpatialTable WHERE id = 1;  
SELECT @geog2 = GeogCol1 FROM SpatialTable WHERE id = 2;  
SELECT @result = @geog1.STIntersection(@geog2);  
SELECT @result.STAsText();  

C.C. 在计算列中使用地理数据Using geography in a computed column

下面的示例使用 geography 类型创建具有持久化计算列的表。The following example creates a table with a persisted computed column using a geography type.

IF OBJECT_ID ( 'dbo.SpatialTable', 'U' ) IS NOT NULL   
    DROP TABLE dbo.SpatialTable;  
GO  
  
CREATE TABLE SpatialTable  
(  
    locationId int IDENTITY(1,1),  
    location geography,  
    deliveryArea as location.STBuffer(10) persisted  
);  

另请参阅See Also

空间数据 (SQL Server)Spatial Data (SQL Server)