Wednesday 23 July 2014

Get Single Row Value from datatable using Linq C#

if (dtExcelData != null && dtExcelData.Rows.Count > 0)
                {
                    var results = (from m in dtExcelData.AsEnumerable()
                                   where m.Field<double>("WLATITUDE") == strLat
                                        && m.Field<double>("WLONGITUDE") == strLong
                                   select m);

                    dtDataFromExcel = results.CopyToDataTable();
                }

Calculate Distance by Latitude and Longitude using C# 2

public static double distance (double lat1, double lon1, double lat2, double lon2) { 
    double lat1 = Convert.ToDouble(latitude);
    double lon1 = Convert.ToDouble(longitude);
    double lat2 = Convert.ToDouble(destlat);
    double lon2 = Convert.ToDouble(destlon);

    double theta = toRadians(lon1-lon2); 
    lat1 = toRadians(lat1); 
    lon1 = toRadians(lon1); 
    lat2 = toRadians(lat2); 
    lon2 = toRadians(lon2); 

    double dist = sin(lat1)*sin(lat2) + cos(lat1)*cos(lat2)*cos(theta); 
    dist = toDegrees(acos(dist)) * 60 * 1.1515 * 1.609344 * 1000; 

    return dist; 
} 

Calculate Distance by Latitude and Longitude in SQl

CREATE FUNCTION [dbo].[DistanceBetween] (@Lat1 as real, 
                @Long1 as real, @Lat2 as real, @Long2 as real)
RETURNS real
AS
BEGIN

DECLARE @dLat1InRad as float(53);
SET @dLat1InRad = @Lat1 * (PI()/180.0);
DECLARE @dLong1InRad as float(53);
SET @dLong1InRad = @Long1 * (PI()/180.0);
DECLARE @dLat2InRad as float(53);
SET @dLat2InRad = @Lat2 * (PI()/180.0);
DECLARE @dLong2InRad as float(53);
SET @dLong2InRad = @Long2 * (PI()/180.0);

DECLARE @dLongitude as float(53);
SET @dLongitude = @dLong2InRad - @dLong1InRad;
DECLARE @dLatitude as float(53);
SET @dLatitude = @dLat2InRad - @dLat1InRad;
/* Intermediate result a. */
DECLARE @a as float(53);
SET @a = SQUARE (SIN (@dLatitude / 2.0)) + COS (@dLat1InRad) 
                 * COS (@dLat2InRad) 
                 * SQUARE(SIN (@dLongitude / 2.0));
/* Intermediate result c (great circle distance in Radians). */
DECLARE @c as real;
SET @c = 2.0 * ATN2 (SQRT (@a), SQRT (1.0 - @a));
DECLARE @kEarthRadius as real;
/* SET kEarthRadius = 3956.0 miles */
SET @kEarthRadius = 6376.5;        /* kms */

DECLARE @dDistance as real;
SET @dDistance = @kEarthRadius * @c;
return (@dDistance);
END

Calculate Distance by Latitude and Longitude using C#

Sample Code (C#)

01using System;
02//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
03//:::                                                                         :::
04//:::  This routine calculates the distance between two points (given the     :::
05//:::  latitude/longitude of those points). It is being used to calculate     :::
06//:::  the distance between two locations using GeoDataSource(TM) products    :::
07//:::                                                                         :::
08//:::  Definitions:                                                           :::
09//:::    South latitudes are negative, east longitudes are positive           :::
10//:::                                                                         :::
11//:::  Passed to function:                                                    :::
12//:::    lat1, lon1 = Latitude and Longitude of point 1 (in decimal degrees)  :::
13//:::    lat2, lon2 = Latitude and Longitude of point 2 (in decimal degrees)  :::
14//:::    unit = the unit you desire for results                               :::
15//:::           where: 'M' is statute miles                                   :::
16//:::                  'K' is kilometers (default)                            :::
17//:::                  'N' is nautical miles                                  :::
18//:::                                                                         :::
19//:::  Worldwide cities and other features databases with latitude longitude  :::
21//:::                                                                         :::
22//:::  For enquiries, please contact sales@geodatasource.com                  :::
23//:::                                                                         :::
25//:::                                                                         :::
26//:::           GeoDataSource.com (C) All Rights Reserved 2014                :::
27//:::                                                                         :::
28//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
29 
30private double distance(double lat1, double lon1, double lat2, double lon2, char unit) {
31  double theta = lon1 - lon2;
32  double dist = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta));
33  dist = Math.Acos(dist);
34  dist = rad2deg(dist);
35  dist = dist * 60 * 1.1515;
36  if (unit == 'K') {
37    dist = dist * 1.609344;
38  } else if (unit == 'N') {
39    dist = dist * 0.8684;
40    }
41  return (dist);
42}
43 
44//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
45//::  This function converts decimal degrees to radians             :::
46//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
47private double deg2rad(double deg) {
48  return (deg * Math.PI / 180.0);
49}
50 
51//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
52//::  This function converts radians to decimal degrees             :::
53//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
54private double rad2deg(double rad) {
55  return (rad / Math.PI * 180.0);
56}
57 
58Console.WriteLine(distance(32.9697, -96.80322, 29.46786, -98.53506, "M"));
59Console.WriteLine(distance(32.9697, -96.80322, 29.46786, -98.53506, "K"));
60Console.WriteLine(distance(32.9697, -96.80322, 29.46786, -98.53506, "N"));