zipcodes within a specific radius - sql

I'm looking for an SQL query to let me retrieve Zipcodes within a specific radius.
I have a table called tblZip with columns: Zipcode, Lat(latitude), Long(Longitude).
tblZip
Zipcode | Lat | Long
Short Text | Number | Number
I have checked few other answers that suggest use of Great-circle Distance but I don't seem to understand how it works. I get a headache just seeing all the conversions from / to radians and degrees and I really tried to understand it, but I'm so bad with mathematics.
I appreciate your help and guidance.
PS: I'm using a Microsoft Access Database.

Consider using the haversine distance formula in a VBA function (borrowed here). Then have it called in your SQL query. And even have your query filtered by distance with WHERE clause per your needs. Do note: this solution will only work inside the MSAccess.exe program. You will not be able to call it externally via ODBC/OLEDB.
And since you need comparison geocodes, SQL query below uses a cross join where each record is compared with every other record in table. Additionally, query avoids reverse duplicates and same-matched pairings reducing the size. But be careful if table is large as cross joins on self returns N2 records, that is before the filtering of duplicates.
SQL (Zipcode pairings with distance of 5 km or less)
SELECT z1.Zipcode, z2.Zipcode,
GetDistance(z1.Lat, z1.Lon, z2.Lat, z2.Lon) As km_distance
FROM tblZip z1, tblZip z2
WHERE z1.Zipcode > z2.Zipcode
AND GetDistance(s1.lat, s1.lon, s1.lat, s2.lon) <= 5;
VBA (save in a standalone module)
Function GetDistance(lat1Degrees As Double, lon1Degrees As Double, lat2Degrees As Double, lon2Degrees As Double) As Double
Dim earthSphereRadiusKilometers As Double
Dim kilometerConversionToMilesFactor As Double
Dim lat1Radians As Double
Dim lon1Radians As Double
Dim lat2Radians As Double
Dim lon2Radians As Double
Dim AsinBase As Double
Dim DerivedAsin As Double
'Mean radius of the earth (replace with 3443.89849 to get nautical miles)
earthSphereRadiusKilometers = 6371
'Convert kilometers into miles (replace 0.621371 with 1 to keep in kilometers)
kilometerConversionToMilesFactor = 0.621371
'Convert each decimal degree to radians
lat1Radians = (lat1Degrees / 180) * (4 * ATN(1))
lon1Radians = (lon1Degrees / 180) * (4 * ATN(1))
lat2Radians = (lat2Degrees / 180) * (4 * ATN(1))
lon2Radians = (lon2Degrees / 180) * (4 * ATN(1))
AsinBase = Sin(Sqr(Sin((lat1Radians - lat2Radians) / 2) ^ 2 + Cos(lat1Radians) * Cos(lat2Radians) * Sin((lon1Radians - lon2Radians) / 2) ^ 2))
DerivedAsin = (AsinBase / Sqr(-AsinBase * AsinBase + 1))
'Get distance from [lat1,lon1] to [lat2,lon2]
GetMiles = Round(2 * DerivedAsin * (earthSphereRadiusKilometers * kilometerConversionToMilesFactor), 2)
End Function

Related

Given a long/lat, convert meters into longitude/latitude degrees

I have a longitude and latitude stored as a geometry with SRID 4326.
I want to make a line that is exactly 1000 meters long that is 90 degrees (to the right).
I know that the conversion from a longitudal/latitudal degree to a meter varies about where you are on the globe. That is why I will pass a reference long/lat that can be taken into consideration.
I am looking for something "good enough" that assumes that the distance you want will be no greater than 100 miles.
Given a long/lat and a meter distance of 1000 meters, return to me the size of the line long/lat degrees.
This question is specific to one example but I am looking for a general solution, because I have many functions that work upon "SRID Units" and the SRID I work with is long/lat (4326) but I always want to deal with meters and not degrees.
The hope is that I can call scaler function to convert the meters I want to the 4326 units.
Some hacks I have considered for finding X meters is converting the geometry into a geography and using the STBuffer(X) to make a circle with a radius equal to that of X, then create a long line that intersects the buffer and to find the point of the intersection, which will be the long/lat of exactly X meters away. This seems very hacky and inefficient but might be the most accurate
Edit:
To find the deltaX and deltaY the function is doing the the following:
DeltaX = Cos(#AngleRads) * #DistanceInDegrees;
DeltaY = Sin(#AngleRads) * #DistanceInDegrees;
I can supply #AngleRads and #DistanceInDegrees.
The problem is that #DistanceInDegrees has to match 4326 units (degrees). Is it even possible to find a #DistanceInDegrees that will correspond to 1000 meters no matter what angle is given?
When I use the formula
#DistanceInDegrees = (#Meters / 6371008.7714) * (180 / pi()) / cos(#Lat1 * pi()/180) and a angle of 90 degrees, then the length of the line is 1002 meters, close but not identically 1000.. If I use a degree of 45 the length of the line is 1191.67 meters.
If I understand your question, this can be done with a little math.
This also assumes the mean radius of the earth to be 6,371,008.7714 meters.
Example
Declare #Lat1 float = -37.786570
Declare #Lng1 float = 145.178179
Declare #Meters float = -1000
Select Lat1 = #Lat1
,Lng1 = #Lng1
,Lat2 = #Lat1
,Lng2 = #Lng1 + (#Meters / 6371008.7714) * (180 / pi()) / cos(#Lat1 * pi()/180)
Returns
The results can be validated here
After your EDIT, It seems you are looking for DISTANCE and BEARING
Here is a Table-Valued Function which may help
Example
Select * from [dbo].[tvf-Geo-Distance-Bearing](-37.786570,145.178179,1000,-45)
Returns
RetLat RetLng
-37.7802105711301 145.170133170589
Now, when I calculate the distance, I get 999.99999448737 meters
The Table-Valued Function If Interested
CREATE Function [dbo].[tvf-Geo-Distance-Bearing] (#Lat1 float,#Lng1 float,#Dist Float,#Degr float)
Returns #LatLng Table (RetLat float,RetLng Float)
As
Begin
Declare #Lat2 float,#Lng2 float,#eRad float = 6371008.7714
Select #Lat1 = RADIANS(#Lat1)
,#Lng1 = RADIANS(#Lng1)
,#Degr = RADIANS(#Degr)
Set #Lat2 = Asin(Sin(#Lat1) * Cos(#Dist / #eRad ) + Cos(#Lat1) * Sin(#Dist / #eRad ) * Cos(#Degr ))
Set #Lng2 = #Lng1 + Atn2(Sin(#Degr) * Sin(#Dist / #eRad ) * Cos(#Lat1), Cos(#Dist / #eRad ) - Sin(#Lat1) * Sin(#Lat2))
Insert Into #LatLng
Select Degrees(#Lat2)
,Degrees(#Lng2)
Return;
End

Converting an equation to code

I have an equation that can be used to find the gun elevation for artillery, using the range, muzzle velocity and change in altitude in a game called Arma 3. The equation looks like this:
With g being the acceleration due to gravity (9.80665), V being the muzzle velocity, X being the range and Y being the change in altitude (called DAlt in my code).
I'm trying to convert it to a line of code so that I can make a program to calculate the elevation based on given coordinates. However I'm having trouble with it. I currently have this:
If rdoLow.Checked = True Then
Elevation = Math.Atan(((Velocity ^ 2) - (Math.Sqrt((Velocity ^ 4) - (G) * (G * (Range ^ 2) + (2 * DAlt * (Velocity ^ 2)))))) / G * Range)
Else
Elevation = Math.Atan(((Velocity ^ 2) + (Math.Sqrt((Velocity ^ 4) - (G) * (G * (Range ^ 2) + 2 * DAlt * (Velocity ^ 2))))) / G * Range)
End If
Which isn't particularly nice looking but as far as I can tell, it should work. However when I put in the values that the video I got the equation from used, I got a different answer. So there must be something wrong with my equation.
I've tried breaking it in to various parts as separate variables and calculating them, then using those variables in the overall equation, and that still didn't work and gave me an answer that was wrong in another way.
So I'm currently at a loss on how to fix it, starting to wonder if the way that vb handles long equations is different or something.
Any help is much appreciated.
You haven't given any sample data, so I can't verify that this gives the correct answer, but the last part of your equation is missing some parentheses.
Elevation = Math.Atan(((Velocity ^ 2) + Math.Sqrt((Velocity ^ 4) - (G * ((G * (Range ^ 2)) + (2 * DAlt * (Velocity ^ 2)))))) / (G * Range))
Note the parenthesis around the last G * Range.
Multiplication and division have equal precedence, so they are evaluated from left-to-right. See Operator Precedence in Visual Basic.
You were dividing everything by G, then multiplying the result by Range, whereas you needed to multiply G by Range, then divide everything by the result of that.
You can see the difference in this simple example:
Console.WriteLine(3 / 4 * 5) ' Prints 3.75
Console.WriteLine(3 / (4 * 5)) ' Prints 0.15
Out of curiosity I tried the problem. In order to have test data I found this web site, Range Tables For Mortars. I tested with the '82mm Mortar - Far' that has an initial velocity of 200 m/s. One problem I had, and don't know if I fixed it correctly, was that the first part of the equation was returning negative numbers... I also solved for the ±. To test I created a form with a button to perform the calculation, a textbox to enter the distance, and two labels to show the angles. This is what I came up with.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'A - launch angle
'Target
' r - range
' y - altitude
'g - gravity 9.80665 m/s^2
'v - launch speed e.g. 50 m/s
'
'
'Formula
'from - https://en.wikipedia.org/wiki/Trajectory_of_a_projectile#Angle_required_to_hit_coordinate_.28x.2Cy.29
'in parts
'parts - px
' p1 = sqrt( v^4 - g * (g * r^2 + 2 * y * v^2) )
' p2 = v^2 ± p1 note plus / minus
' p3 = p2 / g * r
'
' A = arctan(p3)
Dim Ap, Am, r, y As Double
Dim g As Double = 9.80665
Dim v As Double
Dim p1, p2p, p2m, p3p, p3m As Double
If Not Double.TryParse(TextBox1.Text, r) Then Exit Sub
y = 0
v = 200 '82mm Mortar - Far velocity
p1 = v ^ 4 - g * (g * r ^ 2 + 2 * y * v ^ 2)
If p1 < 0 Then
Debug.WriteLine(p1)
p1 = Math.Abs(p1) 'square root does not like negative numbers
End If
p1 = Math.Sqrt(p1)
'plus / minus
p2p = v ^ 2 + p1
p2m = v ^ 2 - p1
p3p = p2p / (g * r)
p3m = p2m / (g * r)
Const radiansToDegrees As Double = 180 / Math.PI
Ap = Math.Atan(p3p) * radiansToDegrees
Am = Math.Atan(p3m) * radiansToDegrees
Label1.Text = Ap.ToString("n3")
Label2.Text = Am.ToString("n3")
End Sub
Using the web site to verify the calculations the code seem correct.
Writing long formulas in a bunch of nested parentheses serves no purpose, unless you are going for confusion.

Gps distance between two points

I am developing a Meter app (in android). I am calculating distance between two points by formula (in back ground)
double calculateDistancs(double lat1, double long1, double lat2,
double long2) {
double earthRadius = 6371000; // meters
double dLat = Math.toRadians(lat2 - lat1);
double dLng = Math.toRadians(long2 - long1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
+ Math.cos(Math.toRadians(lat1))
* Math.cos(Math.toRadians(lat2)) * Math.sin(dLng / 2)
* Math.sin(dLng / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
float dist = (float) (earthRadius * c);
return dist;
}
I am not getting same and accurate distance between my home to metro station .
Provide other approach if available in android.
So second option is use google api for getting rout distance bn two latlong but in this case it will hit api many times.I want t know which approach is best.
I am getting different latlong at same place, may some one please make me understand for this.
The problem most probably is not caused by the function you posted.
Probably you are summing up jumping gps locations.
Just summing up the distance from one location to the other is not sufficient, especially at low speeds, like walking speed.
However there are weak points in the formula:
float dist = (float) (earthRadius * c);
You are always using double, and then downgrade to the lower precision float, but return the double. This is makes no sense.
Fix that with:
double dist = earthRadius * c;

Math.round: round number by conditions

I am trying to round a number by the next things:
number with unit digits between 5-10 will be rounded to the nearest 10*x:
(for example: 5->10, 6->10, 27->30, 40->40, 56->60, etc).
number with unit digits between 1-4 will be rounded to 0:
(for example: 4->0, 11->10, 12->10, 20->20, etc).
I want to write it bu Math.Round function.
Meantime, I did it without it:
Dim rest As Integer = r Mod 10
' round up
If rest >= 5 Then
r = r + (10 - rest)
Else ' round down
r = r - rest
End If
Any help appreciated!
Very simple to do with Math.Round
Dim roundedDecade as Double, originalNumber as Double
:
roundedDecade = Math.Round(originalNumber / 10, MidpointRounding.AwayFromZero) * 10
If you want to force the use of integers, just use CDbl and CInt to force some conversions.
Dim roundedDecade as Integer, originalNumber as Integer
:
roundedDecade = CInt(Math.Round(CDbl(originalNumber) / 10, MidpointRounding.AwayFromZero) * 10)

Distance between Postcodes

I am trying to find out the best way of working out distance between locations (we have the postcode and locations).
Basically with each record we have a Location and Postcode, the user on the front-end can enter a postcode and select records ..
- Within 10 Miles
- Within 20 Miles
- Within 40 Miles
- Within 50 Miles
The only way I can see doing this is by getting EACH record from the database and working out the distance between each, entering it into a temporary table and then ordering this table by location and filtering it. This seems like a long-winded and time-consuming way of doing it - does anyone have any suggestions of how this could be done better? Or is this the best way of doing it?
An example would be a customer enters SO40 9AA (Southampton Postcode) and then selects "within 50 miles", this should display and records within 50 miles of that postcode or location.
Do you have the geo-code in your dataset?
If not you can add it for the zipcodes you have with a service like http://geocoder.us/help/city_state_zip.shtml
Or you can get a pre-coded database, available at http://www.zipinfo.com/products/z5ll/z5ll.htm
Once you add the code to the records, then you can use a standard mathematical distance calculation using the lat / long coordinates to calculate distance.
If you want driving distance, use the Google Maps API, otherwise this distance will be "as the crow flies."
Once you the geo coords for all your postcodes, here's a VB function to get the strait distance between any two points. This produces results in miles, but easy enough to convert it.
Function distance(lat1 As Single, lon1 As Single, lat2 As Single, lon2 As Single)
Dim theta As Single, dist As Single
theta = deg2rad(lon1 - lon2)
lat1 = deg2rad(lat1)
lat2 = deg2rad(lat2)
dist = Sin(lat1) * Sin(lat2) + Cos(lat1) * Cos(lat2) * Cos(theta)
dist = acos(dist)
dist = rad2deg(dist)
distance = dist * 60 * 1.1515
End Function
'::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::: This function get the arccos function from arctan function :::
'::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Function acos(Rad)
If Abs(Rad) <> 1 Then
acos = pi / 2 - Atn(Rad / Sqr(1 - Rad * Rad))
ElseIf Rad = -1 Then
acos = pi
End If
End Function
'::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::: This function converts decimal degrees to radians :::
'::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Function deg2rad(Deg As Single) As Single
deg2rad = CSng(Deg * pi / 180)
End Function
'::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::: This function converts radians to decimal degrees :::
'::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Function rad2deg(Rad As Single) As Single
rad2deg = CSng(Rad * 180 / pi)
End Function