Code Monkey home page Code Monkey logo

utm2latitude's Introduction

UTM2LATSqlServerLibrary

A .NET class library for Converting WGS84/UTM coordinates to Latitude and Longitude using CoordinateSharp nuget library This library contains utility methods that can be called from Microsoft SQL Server as SQL functions using transact sql code (t-sql)

This project is based in SqlLibrary (https://github.com/rsingh85/sql-dotnet-library) coded by Ravinder Singh (https://github.com/rsingh85) he wrote a blog explaining how to call a c sharp method from SQL Server: http://seesharpdeveloper.blogspot.com/2016/06/calling-c-method-from-sql-server.html

Extract from the file UTM2LATSqlServerLibrary.cs:

       /// <summary>
       /// Converts WGS84/UTM to Latitude and Longitude using CoordinateSharp nuget library
       /// </summary>
       /// <param name="XUTM">pos UTM X</param>
       /// <param name="YUTM">pos UTM Y</param>
       /// <param name="LatBand">Latitude band grid zone designation letter (see http://www.dmap.co.uk/utmworld.htm) </param>
       /// <param name="LongBand">Longitude band grid zone designation number (see http://www.dmap.co.uk/utmworld.htm) </param>
       /// <returns>Latitude</returns>
       [SqlProcedure]
       public static double UTM2LATITUDE(double XUTM, double YUTM, string LatBand, int LongBand)
       {

           UniversalTransverseMercator utm = new UniversalTransverseMercator(LatBand, LongBand, XUTM, YUTM);
           Coordinate c = UniversalTransverseMercator.ConvertUTMtoLatLong(utm);

           return c.Latitude.DecimalDegree;
       }

       /// <summary>
       /// Converts UTM Zone 30 Polar Region South to Latitude and Longitude using CoordinateSharp nuget library
       /// </summary>
       /// <param name="XUTM">pos UTM X</param>
       /// <param name="YUTM">pos UTM Y</param>
       /// <param name="LatBand">Latitude band grid zone designation letter (see http://www.dmap.co.uk/utmworld.htm) </param>
       /// <param name="LongBand">Longitude band grid zone designation number (see http://www.dmap.co.uk/utmworld.htm) </param>
       /// <returns>Longitude</returns>
       [SqlProcedure]
       public static double UTM2LONGITUDE(double XUTM, double YUTM, string LatBand, int LongBand)
       {

           UniversalTransverseMercator utm = new UniversalTransverseMercator(LatBand, LongBand, XUTM, YUTM);
           Coordinate c = UniversalTransverseMercator.ConvertUTMtoLatLong(utm);

           return c.Longitude.DecimalDegree;
       }
   

Usage:

  1. Compile the project with release configuration Any CPU

  2. Copy the compilation folder to your SQL Server

  3. Run this t-sql commands. Note that to be able to run this library you have to configure your database as trustworthy because how is the CoordinateSharp implemented. This setting enables your database to run code marked as not safe. This has relation with the permissions of the assembly to access memory locations. I wouldn't recommend to develop extensive assemblies with unsafe code because some error could end up messing your sql server memory with unknown results. Nevertheless as we are using this for a simple task and it makes sense to me using this aproach.

USE [INSERT_DB_NAME_HERE]
GO

ALTER DATABASE [INSERT_DB_NAME_HERE] SET trustworthy ON
GO

CREATE ASSEMBLY UTM2LATSqlServerLibrary from '[INSERT_ASSEMBLY_PATH_HERE]\UTM2LATSqlServerLibrary.dll' WITH PERMISSION_SET = UNSAFE
GO

CREATE FUNCTION UTM2LATITUDE(@XUTM float, @YUTM float, @HEMISPHERE nvarchar(1), @ZONE int)
RETURNS [float] WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [UTM2LATSqlServerLibrary].[UTM2LATSqlServerLibrary.UTM2LATSqlServerLibrary].[UTM2LATITUDE]
GO

CREATE FUNCTION UTM2LONGITUDE(@XUTM float, @YUTM float, @HEMISPHERE nvarchar(1), @ZONE int)
RETURNS [float] WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [UTM2LATSqlServerLibrary].[UTM2LATSqlServerLibrary.UTM2LATSqlServerLibrary].[UTM2LONGITUDE]
GO

CREATE FUNCTION UTM2LAT(@XUTM float, @YUTM float)
RETURNS [float] WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [UTM2LATSqlServerLibrary].[UTM2LATSqlServerLibrary.UTM2LATSqlServerLibrary].[UTM2LAT]
GO

CREATE FUNCTION UTM2LONG(@XUTM float, @YUTM float)
RETURNS [float] WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [UTM2LATSqlServerLibrary].[UTM2LATSqlServerLibrary.UTM2LATSqlServerLibrary].[UTM2LONG]
GO

sp_configure 'clr enabled', 1
GO

RECONFIGURE
GO
  1. You should have 4 functions in your database under Programmability/Functions/Scalar valued functions

  2. You can use the funcions like this:

SELECT dbo.UTM2LATITUDE(723399.51,4373328.5,'S',30) AS Latitude, dbo.UTM2LONGITUDE(723399.51,4373328.5,'S',30) AS Longitude
SELECT dbo.UTM2LAT(723399.51,4373328.5) AS Latitude, dbo.UTM2LONG(723399.51,4373328.5) AS Longitude

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.