[dba-VB] C# / SQL Server: select all zips within 5 miles of a ZIP list

Stuart McLachlan stuart at lexacorp.com.pg
Tue Mar 16 06:59:01 CDT 2010


Of the top of my head:

Length of 1 degree of Longitude = cosine (latitude) * length of degree at equator
I degree at equator = 69.172
5 / 69.172 =  .07228

So  LonZ is within 5 miles of longitude of LonX, LatY
if LonZ  lies in the range  LonX  +/-  cosine(LatY) * .07228

-- 
Stuart


On 15 Mar 2010 at 18:49, jwcolby wrote:

> I wrote an Access application to get a list of all the zips within X miles of another zip, based on 
> Lat/Long.  Today my client asked me to perform population counts of all zips within 5 miles of a 
> list of zips.
> 
> I do not particularly want to do an exhaustive calculation (cartesian product) of each zip in the 
> list against every other zip, there are 33K zips in my specific zip table.  The list itself (in this 
> instance) contains 400 zips and another that contains 250 zips.  The distance calc has a ton of math 
> and doing that math on 12 million lines is probably not going to be particularly speedy.
> 
> In thinking about how to narrow down the results, it occurred to me that if i did a preliminary 
> calculation on the Lat so that I only pulled other zips within 5 miles of the zip(s) in the list, 
> this would narrow things down pretty well.
> 
> So I did, and the result is 7K zips within 5 miles of the latitude of the 400 zips in the list. 
> Does that make sense.
> 
> OK so I have a list of 400 zips, and another list of 7K zips "in the 5 mile latitude band" as those 
> 400 zips.  That is certainly better.  I could have done the same thing with the longitude.  The 
> problem is that the longitude is not a fixed distance apart, but rather starts at zero at the poles 
> and becomes greatest at the equator.  Although the max distance between two adjacent longitudes (for 
> the US) is going to be found at a specific latitude point in the Hawaiian islands. If I just knew 
> what that the distance was at that latitude in Hawaii I could use that factor as well.  But I don't 
> know where to find that, though I will Google more.




More information about the dba-VB mailing list