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,
No comments:
Post a Comment