DATABASE

Transact-SQL in SQL Server 2008 : Spatial Data Types (part 2) - Working with Geography Data

8/1/2012 6:13:08 PM

Working with Geography Data

The GEOGRAPHY data type is also implemented as a .NET common language runtime data type in SQL Server. Unlike the GEOMETRY data type in which locations are defined in terms of X and Y coordinates that can conceivably extend to infinity, the GEOGRAPHY type represents data in a round-earth coordinate system. Whereas flat models do not “wrap around,” the round-earth coordinate system does wrap around such that if you start at a point on the globe and continue in one direction, you eventually return to the starting point.

Because defining points on a ball using X and Y is not very practical, the GEOGRAPHY data type instead defines points using angles. The SQL Server GEOGRAPHY data type stores ellipsoidal (round-earth) data as GPS latitude and longitude coordinates. Longitude represents the horizontal angle and ranges from -180 degrees to 180 degrees, and latitude represents the vertical angle and ranges from -90 degrees to 90 degrees.

The GEOGRAPHY data type provides similar built-in methods as the GEOMETRY data type that you can use to create, validate, and query geography instances.

To assign a value to a geography column or variable, you can use the STGeogFromText methods to parse the parse geometry data provided in a valid WKT syntax into a valid geography value:

Declare @geog GEOGRAPHY
Declare @geog2 GEOGRAPHY
SET @geog =
geography::STGeomFromText('LINESTRING(-122.360 47.656,
                                      -122.343 47.656)', 4326)
SET @geog2 =
geography::STGeomFromText('POLYGON((-122.358 47.653,
                                    -122.348 47.649,
                                    -122.348 47.658,
                                    -122.358 47.658,
                                    -122.358 47.653))', 4326)

As with the GEOMETRY data type, you can also use the STLineFromText static method that accepts only valid LINESTRINGS as input, or you can take advantage of the support for implicit conversion of WKT strings:

DECLARE @geog GEOGRAPHY
SET @geog = Geography::STLineFromText('LINESTRING (-122.360 47.656,
                                      -122.343 47.656)', 4326)
set @geog = Geography::Parse('LINESTRING (-122.360 47.656,
                                      -122.343 47.656)')

set @geog = 'LINESTRING (-122.360 47.656, -122.343 47.656)'

The following code uses the STLength() and STArea() methods to return the length of the LINESTRING:

DECLARE @geom GEOMETRY
SET @geom = geometry::STLineFromText('LINESTRING (100 100, 20 180, 180 180)', 0)

select @geom.STLength() as "Length"
go

Length
----------------------
273.137084989848


					  

The preceding examples use local variables in a batch. You also can declare columns in a table using the geography data type, and you can use the instance properties and methods against the columns as well:

CREATE TABLE #geog
    ( id int IDENTITY (1,1),
    GeogCol1 GEOGRAPHY,
    GeogCol2 AS GeogCol1.STAsText() );
GO

INSERT INTO #geog (GeogCol1)
VALUES (geography::STGeomFromText
         ('LINESTRING(-122.360 47.656, -122.343 47.656)', 4326));

INSERT INTO #geog (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
DECLARE @geog1 GEOGRAPHY;
DECLARE @geog2 GEOGRAPHY;
DECLARE @result GEOGRAPHY;

SELECT @geog1 = GeogCol1 FROM #geog WHERE id = 1;
SELECT @geog2 = GeogCol1 FROM #geog WHERE id = 2;
SELECT @result = @geog1.STIntersection(@geog2);
SELECT Intersection = @result.STAsText();
go

Intersection
-------------------------------------------------
-----------------------------------------
LINESTRING (-122.3479999999668 47.656000260658459
, -122.35799999998773 47.656000130309728)
Other  
  •  Transact-SQL in SQL Server 2008 : Sparse Columns
  •  SQL Server 2005 : Beyond OWC: Full-On OLAP Development (part 4) - Analysis Services CLR Support: Server-Side ADO MD.NET
  •  SQL Server 2005 : Beyond OWC: Full-On OLAP Development (part 3) - XMLA at Your Service
  •  SQL Server 2005 : Beyond OWC: Full-On OLAP Development (part 2) - OLAP Development with ADO MD.NET
  •  SQL Server 2005 : Beyond OWC: Full-On OLAP Development (part 1) - Management Studio as an MDX Client
  •  Microsoft SQL Server 2008 R2 : Using FILESTREAM Storage (part 2) - Using FILESTREAM Storage for Data Columns
  •  Microsoft SQL Server 2008 R2 : Using FILESTREAM Storage (part 1) - Enabling FILESTREAM Storage
  •  SQL Server 2005 : Using Excel (part 2) - Using PivotTables and Charts in Applications and Web Pages
  •  SQL Server 2005 : Using Excel (part 1) - Working Within Excel
  •  Microsoft SQL Server 2008 R2 : Hierarchyid Data Type (part 2) - Modifying the Hierarchy
  •  Microsoft SQL Server 2008 R2 : Hierarchyid Data Type (part 1) - Creating a Hierarchy, Populating the Hierarchy, Querying the Hierarchy
  •  Using SQL Server 2005 Integration Services : Extensibility (part 4) - Custom Connection Managers
  •  Using SQL Server 2005 Integration Services : Extensibility (part 3) - Script Components
  •  Using SQL Server 2005 Integration Services : Extensibility (part 2) - Custom Components
  •  Using SQL Server 2005 Integration Services : Extensibility (part 1) - Script Tasks
  •  .NET Compact Framework 3.5 : Working with Data Sets (part 3) - Reading and Writing a Data Set as XML
  •  .NET Compact Framework 3.5 : Working with Data Sets (part 2) - Data Binding
  •  .NET Compact Framework 3.5 : Working with Data Sets (part 1) - Creating and Accessing DataSet, DataTable, and DataView Objects
  •  .NET Compact Framework 3.5 : Examining ADO.NET
  •  Using SQL Server 2005 Integration Services : Programming Integration Services (part 4) - Connecting the Source and Destination Adapters with a Path
  •  
    Most View
    Razer Blade - A Sharp Looking, Super Slim Gaming Laptop
    All New Storage Options from Western Digital (Part 1)
    In Car Android : Easy access, The Car Home app
    The Great In-App Purchase Rip-Off (Part 2)
    HTC Droid DNA Review (Part 1)
    Downloading and Installing 3CX Phone System (part 1)
    Motorola RAZRi - Excellent UI Skin
    Iphone Application : Building Rotatable and Resizable User Interfaces - Swapping Views on Rotation
    External Drive: Istorage Disk Ashur DT 3TB
    How To Enable Big Picture Mode In Steam
    Top 10
    G-360 And G-550 Power Supply Devices Review (Part 4)
    G-360 And G-550 Power Supply Devices Review (Part 2)
    Canon IXUS 140 Camera - Great Color Reproduction
    Nikon Coolpix S5200 Camera - 10fps Continuous Shooting Mode
    Corsair Neutron GTX 240GB - A Fast Performing SSD
    G-360 And G-550 Power Supply Devices Review (Part 3)
    G-360 And G-550 Power Supply Devices Review (Part 1)
    OCZ Vector 256GB - One Of The Dominant Names In SSD
    Don’t Pay For Office 2013 (Part 2)
    Don’t Pay For Office 2013 (Part 1)