programming4us
programming4us
DATABASE

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

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
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
  •  
    Top 10
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
    - Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
    - Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
    - Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
    - Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
    - Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
    REVIEW
    - First look: Apple Watch

    - 3 Tips for Maintaining Your Cell Phone Battery (part 1)

    - 3 Tips for Maintaining Your Cell Phone Battery (part 2)
    programming4us programming4us
    programming4us
     
     
    programming4us