Diederik Krols

The XAML Brewer

SQL Spatial Tools: Map Projections

SQL Server Spatial Tools on CodePlex contains useful extra functions for the SqlGeometry and SqlGeography data types, as well as a new data type for affine transformations (to scale, translate, and rotate) and a handful of Map Projections. This article describes how to use these projections and visualize the result in Windows Presentation Foundation.

All projections are instantiated from static method calls against the SqlProjection class, with one to five parameters. SQL Spatial Tools contains sample T-Sql scripts, but here's how it looks like in C# (for the inverse projection, don't forget to first assign a Spatial Reference System Identifier to the geometry):

Projection

SqlGeography shape3D = new SqlGeography();

shape3D = SqlGeography.Parse("some valid WKT"); // Or read from DB

SqlProjection proj = SqlProjection.LambertConformalConic(0, 90, 12, 36);

SqlGeometry shape2D = proj.Project(shape3D);

Inverse Projection

SqlGeometry shape2D = SqlGeometry.Parse("some valid WKT");

shape2D.STSrid = 4326; // WGS 84

SqlProjection proj = SqlProjection.AlbersEqualArea(0, 0, 15, 30);

SqlGeography shape3D = proj.Unproject(shape2D);

 

That's all there is to!

 

I added this code to an improved version of my SqlGeometry Extension Methods for WPF Visualization. Here's how the resulting application looks like, displaying a reduced shape of Belgium, and the exact location of my office @ U2U Consult: 

 

OK, I admit: projecting Belgium is not that spectacular. It will have the same shape in virtually every projection: it's a small surface on an average latitude.

So let's apply some transformations on a more representative victim such as the Tropic of Cancer. This will generally be projected as a straight line, but if you stand on the North Pole -e.g. via a Gnomonic Projection- it looks like a circle:

 And with a Transverse Mercator projection it should look like an Ellipse:

 

Unfortunately the SQL Spatial Tools code only implements the Spherical version of the Transverse Mercator projection, and not (yet ?) the Ellipsoidal version. Otherwise SQL Spatial Tools would have all the ingredients for a latitude/longitude WGS84 to UTM conversion. After all, you only need to project, then scale (to convert to meters), and finally translate (for false easting). This is a horrible calculation, but don't worry: Proj.NET on CodePlex should be able to handle this (this feels like a topic for a later articleLaughing).

Anyway, here's the full solution: U2UConsult.DockOfTheBay.SpatialProjectionsSample.zip (144,07 kb).

Comments are closed