SQL Spatial Tools : SQL Data Type « Database ADO.net « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Database ADO.net » SQL Data TypeScreenshots 
SQL Spatial Tools
        


// Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using Microsoft.SqlServer.Types;

namespace SQLSpatialTools
{
    /**
     * This class contains functions that are meant to be used internally in this library.
     */
    class Util
    {
        // Convert a SqlGeography to an X,Y,Z vector.
        public static Vector3 GeographicToCartesian(SqlGeography point)
        {
            return SphericalDegToCartesian(point.Lat.Value, point.Long.Value);
        }

        // Convert an X,Y,Z vector to a SqlGeography
        public static SqlGeography CartesianToGeographic(Vector3 point, int srid)
        {
            return SqlGeography.Point(LatitudeDeg(point), LongitudeDeg(point), srid);
        }

        // Convert a Lat/Long in degrees to an X,Y,Z vector.
    public static Vector3 SphericalDegToCartesian(double latitudeDeg, double longitudeDeg)
    {
      if (Math.Abs(latitudeDeg90)
        throw new ArgumentOutOfRangeException("|latitudeDeg| > 90");

      double latitudeRad = ToRadians(latitudeDeg);
      double longitudeRad = ToRadians(longitudeDeg);
      double r = Math.Cos(latitudeRad);
      return new Vector3(r * Math.Cos(longitudeRad), r * Math.Sin(longitudeRad), Math.Sin(latitudeRad));
    }

    // Convert a Lat/Long in radians to an X,Y,Z vector.
    public static Vector3 SphericalRadToCartesian(double latitudeRad, double longitudeRad)
    {
      if (Math.Abs(latitudeRad> Math.PI / 2)
        throw new ArgumentOutOfRangeException("|latitudeRad| > PI / 2");

      double r = Math.Cos(latitudeRad);
      return new Vector3(r * Math.Cos(longitudeRad), r * Math.Sin(longitudeRad), Math.Sin(latitudeRad));
    }

    // Returns longitude in radians given the vector.
    public static double Longitude(Vector3 p)
    
      return Math.Atan2(p.y, p.x)
    }

    // Returns longitude in degrees given the vector.
    public static double LongitudeDeg(Vector3 p)
    
      return ToDegrees(Longitude(p))
    }

    // Returns latitude in radians given the vector.
    public static double Latitude(Vector3 p)
    {
      return Math.Atan2(p.z, Math.Sqrt(p.x * p.x + p.y * p.y));
    }

    // Returns latitude in degrees given the vector.
    public static double LatitudeDeg(Vector3 p)
    {
      return ToDegrees(Latitude(p));
    }

    // Converts degrees to radians.
    public static double ToRadians(double a)
    {
      return a / 180 * Math.PI;
    }

    // Converts radians to degrees.
    public static double ToDegrees(double a)
    {
      return a * 180 / Math.PI;
    }
    }
}

   
    
    
    
    
    
    
    
  
Related examples in the same category
1.Shows the SQL server types, the corresponding Sql types, and the GetSql*** methods used to read a column as the Sql type
2.SQL SERVER TYPES, COMPATIBLE Sql* TYPES, AND GetSql* METHODS
3.Sql Database Type Utilities
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.