Top 5 Recent Articles
ARTICLES CATEGORIES
- Algorithms (22)
- All (399)
- Biography (1)
- Blog (44)
- Business Requirements (1)
- Commentary (1)
- Conversion (2)
- Customers (2)
- Data Models (1)
- Education (2)
- GeoRaptor (13)
- GPS (1)
- Image Processing (2)
- Import Export (8)
- Licensing (2)
- LiDAR (1)
- Linear Referencing (4)
- Manifold GIS (3)
- Mapping (1)
- MySQL Spatial (7)
- Networking and Routing (including Optimization) (5)
- Open Source (18)
- Oracle Spatial and Locator (194)
- Partitioning (1)
- PostGIS (36)
- Projections (1)
- Published Articles (1)
- qGIS (1)
- Recommendations (1)
- Services (1)
- Software Change Log (1)
- Source Code (37)
- Space Curves (9)
- Spatial Database Functions (109)
- Spatial DB comparison (1)
- Spatial XML Processing (11)
- SQL Server Spatial (92)
- Standards (3)
- Stored Procedure (17)
- Tessellation or Gridding (10)
- Tools (2)
- Topological Relationships (1)
- Training (2)
- Triangulation (2)
toGeography and toGeography Conversion Functions for SQL Server 2008 Spatial
I have been doing some SQL Server 2008 programming lately and have come across the fact that TSQL does not allowing a programmer to create overloaded functions.
This is an issue, when programming SQL Server 2008 Spatial, because of use of its implementation of two different data types for geodetic and projected data: geography and geometry.
Thus, if I want to create a function like “DumpPoints”: I have to implement it twice (as is done with the CLR functions in SQL Spatial Tools ) eg:
CREATE FUNCTION STDumpPointsGeom( @p_geom geometry ) etc -- CREATE FUNCTION STDumpPointsGeog( @p_geom geography ) etc
Or I can implement it once – STDumpPoints( @p_geom geometry) – and call it using the following conversion functions.
USE [GISDB] -- Change this to your database GO -- CREATE FUNCTION [dbo].[STToGeography](@p_geom geometry, @p_srid INT = NULL) RETURNS geography AS BEGIN RETURN geography::STGeomFromText(@p_geom.AsTextZM(), CASE WHEN @p_srid IS NULL THEN @p_geom.STSrid ELSE @p_srid END ); END GO -- CREATE FUNCTION [dbo].[STToGeometry](@p_geog geography, @p_srid INT = NULL) RETURNS geometry AS BEGIN RETURN geometry::STGeomFromText(@p_geog.AsTextZM(), CASE WHEN @p_srid IS NULL THEN @p_geog.STSrid ELSE @p_srid END ); END GO
Testing its we get:
SELECT [dbo].[STDensify] ( [dbo].[STToGeometry] ( geography::STGeomFromText('LINESTRING(-10 0,-5 5,10 10)',4326), NULL), 2.1,3,2).AsTextZM(); GO dGeom LINESTRING (-10 0, -8.75 1.25, -7.5 2.5, -6.25 3.75, -5 5, -3.125 5.625, -1.25 6.25, 0.625 6.875, 2.5 7.5, 4.375 8.125, 6.25 8.75, 8.125 9.375, 10 10) -- SELECT dbo.STToGeography( dbo.STToGeometry( geography::STGeomFromText('LINESTRING(147.234 -43.2345, 148.234 -43.2345)',4326), 0), 4326).STAsText() AS geog;
geog |
---|
LINESTRING (147.234 -43.2345, 148.234 -43.2345) |
Not all that clever, but I find them useful. I hope you might too.
Documentation
- MySQL Spatial General Functions
- Oracle LRS Objects
- Oracle Spatial Exporter (Java + pl/SQL)
- Oracle Spatial Object Functions
- Oracle Spatial Object Functions (Multi Page)
- PostGIS pl/pgSQL Functions
- SC4O Oracle Java Topology Suite (Java + pl/SQL)
- SQL Server Spatial General TSQL Functions
- SQL Server Spatial LRS TSQL Functions