Showing posts with label latitude. Show all posts
Showing posts with label latitude. Show all posts

Sunday, February 12, 2012

Calculating distince

I have a table with only one column each for longitude and latitude. They will be passing the sa_property_id which has a long and lat listed. I need to show what else is withing 1 mile or 5 miles of that properties long/lat

--Table
CREATE TABLE [dbo].[PropertyDetails] (
[SA_PROPERTY_ID] [int] NOT NULL ,
[SA_X_COORD] [float] NULL ,
[SA_Y_COORD] [float] NULL ,
[Longitude] [float] NULL ,
[Latitude] [float] NULL ,
) ON [PRIMARY]
GO

Data output from above table

SASA_PROPERTY_ID Longitude Latitude
-- -- --
23790208 -120.619821 39.568587999999998

(1 row(s) affected)

Passed parameter = SA_PROPERTY_ID

Need list of matching records within 1 mile of above record

Would this be the best query for it? how do I define the radius?

I also have a function, but am not sure it can do what I need it to.


/*
* History: 14-Nov-05 CNH Simplified to resolve Divide By Zero problems
* 29-Mar-02 DKS Created by Deepak K Srinivasan
*
* Test Data:
*
* City Latt Long
* - - -
* NYC 40.77 74
* SF 37.75 122.68
* Oakland 37.73 122.22
* Burbank 34.2 118.37
*/
CREATE FUNCTION dbo.CalculateDistance(@.LatDeg1 FLOAT, @.LonDeg1 FLOAT, @.LatDeg2 FLOAT, @.LonDeg2 FLOAT)
RETURNS FLOAT AS BEGIN
DECLARE @.EarthRadiusMiles AS FLOAT,
@.LatRad1 AS FLOAT,
@.LonRad1 AS FLOAT,
@.LatRad2 AS FLOAT,
@.LonRad2 AS FLOAT,
@.DotProd AS FLOAT

SET @.EarthRadiusMiles = 3958.755 -- Volumic Radius of the Earth in Miles

SET @.LatRad1 = RADIANS(ISNULL(@.LatDeg1, 0.0))
SET @.LonRad1 = RADIANS(ISNULL(@.LonDeg1, 0.0))
SET @.LatRad2 = RADIANS(ISNULL(@.LatDeg2, 0.0))
SET @.LonRad2 = RADIANS(ISNULL(@.LonDeg2, 0.0))

SET @.DotProd = SIN(@.LatRad1) * SIN(@.LatRad2) + COS(@.LatRad1) * COS(@.LatRad2) * COS(@.LonRad1 - @.LonRad2)

-- T-SQL provides ACOS. So, there is no need to implement it via ATAN:

RETURN @.EarthRadiusMiles * ACOS(@.DotProd)
END


I'm not quite sure what your question is, but using calculating the dot product to get the radius is the right idea. You're assuming that the Earth is a sphere, which isn't quite correct, but it's probably fine for the distances you're interested in.

Cheers,

Calculating distance based on latitude and longitude

I need to be able to take the latitude and logitude of two locations and compare then to determine the number of miles between each point. It doesn't need to account for elevation, but assumes a flat plane with lat and long.

Does anyone have any algorithms in T-SQL to do this?

if this were easy to do, esri/mapinfo wouldn't charge big bucks for their GIS libraries. People get PhD's in geodesia!

Now, there is a solution.

Search your registry for MapX. MapInfo's MapX ocx is distributed in a few different software packages. I thought it came in Microsoft Office. But I see it also comes in Crystal XI.

Discussion about the usage of this library is not appropriate for this forum and I would direct you to Mapinfo.com.

I would be more than happy to develop a wrapper for you if you are willing to pay. I charge US$150 an hour for GIS development. probably looking at 3 hours.

|||
The SQL Server 2005 Samples download contains the source
for the Spatial Data Library which I believe has
a C# function for calculating the distance between two
points from their lat/lons.
|||

it appears there is one in the SQL class. result is in Arc Minutes.

should be ok for small distances, and not near the equator. it doesn't take into account 'equatorial bulge'

in most applications it should be okay, just be aware of its propensity for error.

the algorithm doesnt look that hard to translate to straight T-SQL for increased speed:


CREATE FUNCTION CARTESIAN_PI() RETURNS DECIMAL(36, 35)
AS
BEGIN
RETURN 3.14159265358979323846264338327950288
END
GO
CREATE FUNCTION DBO.CARTESIAN_EPSILON() RETURNS DECIMAL(15,15)
AS
BEGIN
RETURN 0.000000000000001
END
GO
CREATE TYPE LATLONGUNIT FROM DECIMAL(26, 20);
GO
CREATE FUNCTION CARTESIAN_COS(@.arg LATLONGUNIT) RETURNS REAL
AS
BEGIN
DECLARE @.result REAL;
IF ABS(@.ARG-DBO.CARTESIAN_PI()) < DBO.CARTESIAN_EPSILON()
SET @.RESULT = 0
ELSE
SET @.RESULT = COS(@.arg)
RETURN @.result;
END
GO
CREATE FUNCTION DistanceLatLon(@.lat1 LATLONGUNIT, @.lon1 LATLONGUNIT, @.lat2 LATLONGUNIT, @.lon2 LATLONGUNIT) RETURNS REAL
AS
BEGIN
DECLARE @.d2r REAL
DECLARE @.nx1 REAL
DECLARE @.ny1 REAL
DECLARE @.nz1 REAL
DECLARE @.nx2 REAL
DECLARE @.ny2 REAL
DECLARE @.nz2 REAL

SET @.d2r = DBO.CARTESIAN_PI() / 180;
SET @.nx1 = DBO.CARTESIAN_COS(@.lat1 * @.d2r) * DBO.CARTESIAN_COS(@.lon1 * @.d2r);
SET @.ny1 = DBO.CARTESIAN_COS(@.lat1 * @.d2r) * SIN(@.lon1 * @.d2r);
SET @.nz1 = SIN(@.lat1 * @.d2r);
SET @.nx2 = DBO.CARTESIAN_COS(@.lat2 * @.d2r) * DBO.CARTESIAN_COS(@.lon2 * @.d2r);
SET @.ny2 = DBO.CARTESIAN_COS(@.lat2 * @.d2r) * SIN(@.lon2 * @.d2r);
SET @.nz2 = SIN(@.lat2 * @.d2r);
RETURN (60 * 2 * (1 / @.d2r) * ASIN(SQRT((@.nx1 - @.nx2) * (@.nx1 - @.nx2)
+ (@.ny1 - @.ny2) * (@.ny1 - @.ny2)
+ (@.nz1 - @.nz2) * (@.nz1 - @.nz2)
) / 2));
END

|||

Cool!

Might want to double check the precisions. Real number precision confuses the hell out of me. I never feel 100% comfortable that I have allocated enough for the application.

Also. . . if you are doing alot of this, you might want to investigate MapInfo or ESRI for some really slick GIS db extensions.