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.
Thursday, September 10, 2009
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") {This has the effect that was desired. Each individual page of the site will be redirected from "site.com" to "www.site.com".
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", Request.Url.ToString().Replace("site.com", "www.site.com"));
Response.End();
}
Subscribe to:
Posts (Atom)