Thursday, September 10, 2009

Been doing some distance calculations using lattitude and longitude. One of the things that I needed to know was the number of miles in a degree of longitude at the equator. The answer is 69.1703234283616.
I was also needing to calculate if two points were within each others radius. For instance if point 1 has a radius of 10 miles, and point 2 has a radius of 25 miles, do the circles that those radii form intersect each other anywhere. The formula for this is below:

(r1 + r2) ^ 2 <= (clat1 - clat 2) ^ 2 + (clon1 - clon2) ^ 2

where
lat1 = the latitude of the first point
lat2 = the latitude of the second point
lon1 = the longitude of the first point
lon2 = the longitude of the second point
r1 = the radius from the first point
r2 = the radius from the second point
clat1 = (lat1 * 69.1703234283616)
clat2 = (lat2 * 69.1703234283616)
clon1 = ((lon1 * 69.1703234283616) * cos(lat1))
clon2 = ((lon2 * 69.1703234283616) * cos(lat2))

Now, since clat1, clat2, clon2, and clon2 all use constants, or lat/lon values of the same point, the calculation results will be the same everytime for a particluar lat/lon pair, so I store the results of these calculation in the database as well, to save calculation time later.

Redirecting Non-www Host to www Host in C#

Today I was tasked with redirecting all of the pages of a site (we'll call it site.com) without the "www" subdomain (site.com) to the one with the "www" (www.site.com). One of the things I wanted was that if the request was for "site.com/about.html" that it would be redirected to "www.site.com/about.html" not to "www.site.com/index.html". Below is the code I used in the Application_BeginRequest event handler in my global.asax file to accomplish this:
if (Request.Url.Host.ToLower() == "site.com") {

Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", Request.Url.ToString().Replace("site.com", "www.site.com"));
Response.End();

}
This has the effect that was desired. Each individual page of the site will be redirected from "site.com" to "www.site.com".

Wednesday, October 29, 2008

I have been searching the internet to find a free, good quality U.S. zip code database. I have found two options that I may use:

1. GeoNames.com - This database covers all countries and has over eight million place names. It is available for download or can be accesses via a web service.
2. The TIGER database from the U.S. Census Bureau - This database covers only the United States, but has information down to the address level, so lookups can be done via address. It is available for download, or there are sites that have created web services based on the data.

Either way, I think I want to have my own database for this data, then I can do however much of whatever I want with it. I will be posting an update when I decide what I am going to use, and as I implement my choice.