Skip to content
This repository has been archived by the owner on Oct 5, 2023. It is now read-only.

Commit

Permalink
Add sample showing how to calculate distances using SQL Server CLR types
Browse files Browse the repository at this point in the history
  • Loading branch information
HowardvanRooijen committed Oct 21, 2012
1 parent ba44bf6 commit b16e225
Show file tree
Hide file tree
Showing 12 changed files with 202 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Release
Data
Drops
_Resharper.*
*.DotCover
*.opensdf
*.sdf
*.xap
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>Microsoft.SqlServer.Types</id>
<version>10.50.1600.1</version>
<title>Microsoft.SqlServer.Types</title>
<authors>Microsoft</authors>
<owners>Microsoft</owners>
<projectUrl>http://www.microsoft.com/sqlserver/en/us/default.aspx</projectUrl>
<iconUrl>https://nugetgallery.blob.core.windows.net/icons/Microsoft.SqlServer.Types.10.50.1600.1.png</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Sql Server Spatial Types Assembly.

This is the spatial library used in Sql Server 2008. It can be found in C:\Program Files (x86)\Microsoft SQL Server\100\SDK\Assemblies\Microsoft.SqlServer.Types.dll if you have Sql Server (or Sql Server Management Studio) installed on your machine. If you don't or you can never find this assembly, then this package is for you :)

Please Microsoft - don't kill me. I mean well. I just want to share your love!
PS. Anyone can download Sql Server Express (for free!) and copy this assembly from the path, listed above .. but having a simple package just saves so much pain.</description>
<summary>Sql Server Spatial Types Assembly. This is the spatial library used in Sql Server 2008. It can be found in C:\Program Files (x86)\Microsoft SQL Server\100\SDK\Assemblies\Microsoft.SqlServer.Types.dll if you have Sql Server (or Sql Server Management Studio) installed on your machine. If you don't or you can never find this assembly, then this package is for you :) Please Microsoft - don't kill me. I mean well. I just want to share your love! PS. Anyone can download Sql Server Express (for free!) and copy this assembly from the path, listed above .. but having a simple package just saves so much pain.</summary>
<copyright>Microsoft Corp. All Rights Reserved.</copyright>
<tags>sql sql-server sql-server-2008 spatial geospatial latitude longitude geometry geography</tags>
</metadata>
</package>
Binary file not shown.
4 changes: 4 additions & 0 deletions CalculateDistanceWithSqlClrTypes/Packages/repositories.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<repositories>
<repository path="..\Solutions\Endjin.Sql.Spatial.Example\packages.config" />
</repositories>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Endjin.Sql.Spatial.Example", "Endjin.Sql.Spatial.Example\Endjin.Sql.Spatial.Example.csproj", "{5FBF3E6F-9145-4259-BAE0-F1EA6C3361F5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5FBF3E6F-9145-4259-BAE0-F1EA6C3361F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5FBF3E6F-9145-4259-BAE0-F1EA6C3361F5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5FBF3E6F-9145-4259-BAE0-F1EA6C3361F5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5FBF3E6F-9145-4259-BAE0-F1EA6C3361F5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/FilterSettingsManager/CoverageFilterXml/@EntryValue">&lt;data&gt;&lt;IncludeFilters /&gt;&lt;ExcludeFilters /&gt;&lt;/data&gt;</s:String>
<s:String x:Key="/Default/FilterSettingsManager/AttributeFilterXml/@EntryValue">&lt;data /&gt;</s:String></wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{5FBF3E6F-9145-4259-BAE0-F1EA6C3361F5}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Endjin.Sql.Spatial.Example</RootNamespace>
<AssemblyName>Endjin.Sql.Spatial.Example</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.SqlServer.Types, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL">
<Private>True</Private>
<HintPath>..\..\Packages\Microsoft.SqlServer.Types.10.50.1600.1\lib\Net20\Microsoft.SqlServer.Types.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
namespace Endjin.Sql.Spatial.Example
{
#region Using Directives

using System;
using System.Data.SqlTypes;

using Microsoft.SqlServer.Types;

#endregion

/// <summary>
/// Sample app that uses SQL Spatial Types to calculate the distance between two locations.
/// </summary>
/// <remarks>
/// If you want to find our a specific lon / lat use http://www.doogal.co.uk/LatLong.php
/// </remarks>
public class Program
{
public static void Main(string[] args)
{
const int WorldGeodeticSystemId = 4326;

var firstlocationLonLat = new Tuple<string, string>("-0.081389", "51.502195");
var secondlocationLonLat = new Tuple<string, string>("-0.185348", "51.410933");

var firstLocationAsPoint = string.Format("POINT({0} {1})", firstlocationLonLat.Item1, firstlocationLonLat.Item2);
var secondLocationAsPoint = string.Format("POINT({0} {1})", secondlocationLonLat.Item1, secondlocationLonLat.Item2);

var firstLocation = SqlGeography.STGeomFromText(new SqlChars(firstLocationAsPoint), WorldGeodeticSystemId);
var secondLocation = SqlGeography.STGeomFromText(new SqlChars(secondLocationAsPoint), WorldGeodeticSystemId);

var distance = firstLocation.STDistance(secondLocation);

Console.WriteLine("First Location is " + MetersToMiles((double)distance).ToString("0") + " miles from Second Location");
Console.ReadKey();
}

public static double MetersToMiles(double? meters)
{
if (meters == null)
{
return 0F;
}

return meters.Value * 0.000621371192;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Endjin.Sql.Spatial.Example")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Endjin.Sql.Spatial.Example")]
[assembly: AssemblyCopyright("Copyright © 2012")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("125ae64e-6b31-4eb8-a54d-b6e57eff2dd4")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.SqlServer.Types" version="10.50.1600.1" targetFramework="net40" />
</packages>
3 changes: 3 additions & 0 deletions CalculateDistanceWithSqlClrTypes/Solutions/NuGet.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<settings>
<repositoryPath>..\Packages\</repositoryPath>
</settings>

0 comments on commit b16e225

Please sign in to comment.