bounding box mysql with a php after taste

I finally had to do it – Optimize lat/long searches.

The method I used is not original, but I did survey MANY suggestions, including mysql spatial data types.

end result:

function getBoundingBox($lat_degrees,$lon_degrees,$distance_in_miles) {
$radius = 3963.1; // of earth in miles
// bearings – FIX
$due_north = deg2rad(0);
$due_south = deg2rad(180);
$due_east = deg2rad(90);
$due_west = deg2rad(270);
// convert latitude and longitude into radians
$lat_r = deg2rad($lat_degrees);
$lon_r = deg2rad($lon_degrees);
// find the northmost, southmost, eastmost and westmost corners $distance_in_miles away
// original formula from
// http://www.movable-type.co.uk/scripts/latlong.html
$northmost  = asin(sin($lat_r) * cos($distance_in_miles/$radius) + cos($lat_r) * sin ($distance_in_miles/$radius) * cos($due_north));
$southmost  = asin(sin($lat_r) * cos($distance_in_miles/$radius) + cos($lat_r) * sin ($distance_in_miles/$radius) * cos($due_south));
$eastmost = $lon_r + atan2(sin($due_east)*sin($distance_in_miles/$radius)*cos($lat_r),cos($distance_in_miles/$radius)-sin($lat_r)*sin($lat_r));
$westmost = $lon_r + atan2(sin($due_west)*sin($distance_in_miles/$radius)*cos($lat_r),cos($distance_in_miles/$radius)-sin($lat_r)*sin($lat_r));

$northmost = rad2deg($northmost);
$southmost = rad2deg($southmost);
$eastmost = rad2deg($eastmost);
$westmost = rad2deg($westmost);

// sort the lat and long so that we can use them for a between query
if ($northmost > $southmost) {
$lat1 = $southmost;
$lat2 = $northmost;

} else {
$lat1 = $northmost;
$lat2 = $southmost;
}

if ($eastmost > $westmost) {
$lon1 = $westmost;
$lon2 = $eastmost;

} else {
$lon1 = $eastmost;
$lon2 = $westmost;
}
return array($lat1,$lat2,$lon1,$lon2);
}

function calcDistance($p1,$p2){
$R = 6371000; // m
$dLat = deg2rad($p2['lat']-$p1['lat']);
$dLon = deg2rad($p2['lng']-$p1['lng']);
$a = sin($dLat/2) * sin($dLat/2) +
cos(deg2rad($p1['lat'])) * cos(deg2rad($p2['lat'])) *
sin($dLon/2) * sin($dLon/2);
$c = 2 * atan2(sqrt($a), sqrt(1-$a));
$d = $R * $c;
return $d;
}

Posted in Math, MySQL, PHP | Leave a comment

timezones with php

I started a website about various places around the world. I wanted to display local date and time for locations related to those pages. I thought I may be able to download a database of timezones – even though I wasn’t sure how I was going to handle daylight savings time where applicable.

My first search resulted in a link to http://www.iana.org/time-zones. Way too complicated to parse. According to some slashdot articles this is easier to use than any built in php functionality. I believe I’m proving them wrong:

Turns out php’s DateTimeZone is capable of pretty much handling the whole thing. There are a few intermediate steps, and alternative approaches to matching locations to timezones.

It is possible to loops through all of php’s available timezones and build a data-object with things like latitude, longitude, and iso 2 country codes. I looped through this information and put it into a database – probably because I am more comfortable thinking about those attributes as table attributes, and it’s probably faster for searching, but slightly trivial since there are only something like 500 entries.

Another requirement for me was a generalized location search ability. All of the pages I am dealing with are at least associated with a country. So I made my search parameters latitude, longitude, and iso 2 country code. Of course that also means I had to through in a spherical circumstance calculation of some sort in there to get the distance between 2 latLng points / find the most appropriate timezone.

The daylight savings adjustments are taking care of themselves.
I checked 2 places with Unusual timezones – other than hour increments from GMT: Amsterdam and Katmandu. One of them was correct, one of them somehow faulted to the nearest hour offset. But, those things change. I am not sure if the “strange timezone” list I found was outdated or the php Date stuff is. Actually the first reference, the one I said was too difficult to parse, includes historical changes to timezone boundaries. They change often.

Posted in Other, PHP | Leave a comment

my favorite website. reserved for a while. they made everything better

http://grooveshark.com/s/The+Boss/3WIOyQ?src=5

Posted in .Net, Apache, css, git, Java, javascript, jQuery, Linux, Math, MySQL, Networking, Other, PHP, Postfix, Source Code Management, sqlite, subversion, wordpress | Leave a comment

very interesting

I like this idea. I was trying to figure out how cron handles locking if some of the jobs are really long. Synchronously? Asynchronously? I never found a very clear cut answer. However I did find this program for keeping the SAME cron task from starting if it’s already been start.

http://www.unixwiz.net/tools/lockrun.html

Posted in Linux | Leave a comment

apache log size limit

Sounds nasty. Pretty much is.

Found this cool tip from another blog:

Apache won’t do this for you – all it does is write lines to a filehandle. However, the filhandle doesn’t need to lead to a file, it can be a pipe to a program you write that can do whatever you like see: http://httpd.apache.org/docs/2.2/mod/mod_log_config.html

Rgds,
Owen Boyle
Disclaimer: Any disclaimer attached to this message may be ignored.


Posted in Apache, Linux | Leave a comment

mysql spatial lat long stuff

step 1: create point attribute, and create spatial index.
done.

research geo-spatial queries. The one mentioned in the post about setting up the point and spatial index is ok, but not exactly what I’m looking for.
will update soon

update 1: ~”MySQL only has implemented Minimal Bounding Rectangles (MBRs)”[1,2]
step 43: make a reasonable minimal bounding rectangle[?].
…will update soon:
After reading more about it I decided not to use the mysql geo-spatial stuff. According to what I read mysql spatial stuff is more for things in planes, not spheres. Dammit.

related post

Posted in MySQL | Leave a comment

youtube embed chrome problem

I’ve started using some of the css rules html5please probably gives warnings about: in this case column-count seemed to be the culprit.

Also this isn’t the first time I have notice chrome handling css columns poorly.

I was getting nothing but white boxes and/or frozen videos when I tried to embed objects in a css column layout.

Tried lots of element inspecting and messing around before I found the solution: remove the css column layout from that section of the page. Inline-block it is. Your move, chrome.

Posted in css | Leave a comment

where are wordpress xml feeds?

WordPress Post-Feed Formats for Permalinks

If you have permalinks enabled on your site, your main-content (posts) feed is accessible via the following URLs, depending on which feed format you would like to use:

  • http://domain.tld/feed/ (RSS 2.0 format)
  • http://domain.tld/feed/rss2/ (RSS 2.0 format)
  • http://domain.tld/feed/rss/ (RSS 0.92 format)
  • http://domain.tld/feed/rdf/ (RDF/RSS 1.0 format)
  • http://domain.tld/feed/atom/ (Atom format)

 

Posted in wordpress | Leave a comment

rename all files and folders lowercase

Interesting:

find my_root_dir -depth -exec rename 's/(.*)\/([^\/]*)/$1\/\L$2/' {} \;
Posted in Linux | Leave a comment

css3 fonts, font-face

go here http://www.fontsquirrel.com/fontface/generator

Posted in css | Leave a comment