<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: PHP and MySQL: Calculating Distance</title>
	<atom:link href="http://marketingtechblog.com/2007/09/15/calculate-distance/feed/" rel="self" type="application/rss+xml" />
	<link>http://marketingtechblog.com/2007/09/15/calculate-distance/</link>
	<description>new media strategies and other marketing gems</description>
	<lastBuildDate>Sun, 05 Jul 2009 05:48:19 -0400</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: matthew</title>
		<link>http://marketingtechblog.com/2007/09/15/calculate-distance/comment-page-1/#comment-284946</link>
		<dc:creator>matthew</dc:creator>
		<pubDate>Thu, 04 Jun 2009 01:07:00 +0000</pubDate>
		<guid isPermaLink="false">http://marketingtechblog.com/2007/09/15/calculate-distance/#comment-284946</guid>
		<description>This is great, however it is just as the birds fly. It would be great to try and incorporate the google maps API to this somehow (maybe using roads etc.) Just to give an idea using a different form of transportation. I still have yet to make a simulated annealing function in PHP that would be able to offer an efficient solution to the traveling salesman problem. But I think that I may be able to reuse some of your code to do so.</description>
		<content:encoded><![CDATA[<p>This is great, however it is just as the birds fly. It would be great to try and incorporate the google maps API to this somehow (maybe using roads etc.) Just to give an idea using a different form of transportation. I still have yet to make a simulated annealing function in PHP that would be able to offer an efficient solution to the traveling salesman problem. But I think that I may be able to reuse some of your code to do so.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: PHP and MySQL Calculating Distance The Marketing Technology &#124; Paid Surveys</title>
		<link>http://marketingtechblog.com/2007/09/15/calculate-distance/comment-page-1/#comment-284882</link>
		<dc:creator>PHP and MySQL Calculating Distance The Marketing Technology &#124; Paid Surveys</dc:creator>
		<pubDate>Wed, 03 Jun 2009 20:20:48 +0000</pubDate>
		<guid isPermaLink="false">http://marketingtechblog.com/2007/09/15/calculate-distance/#comment-284882</guid>
		<description>[...] PHP and MySQL Calculating Distance The Marketing Technology   Posted by root 1 day 14 hours ago (http://marketingtechblog.com)        Gret job reply to this comment pingback by calculating distance between geocoded the marketing technology blog is proudly powered by wordpress 2 7 1        Discuss&#160;  &#124;&#160; Bury &#124;&#160;    News &#124; PHP and MySQL Calculating Distance The Marketing Technology [...]</description>
		<content:encoded><![CDATA[<p>[...] PHP and MySQL Calculating Distance The Marketing Technology   Posted by root 1 day 14 hours ago (<a href="http://marketingtechblog.com" rel="nofollow">http://marketingtechblog.com</a>)        Gret job reply to this comment pingback by calculating distance between geocoded the marketing technology blog is proudly powered by wordpress 2 7 1        Discuss&nbsp;  |&nbsp; Bury |&nbsp;    News | PHP and MySQL Calculating Distance The Marketing Technology [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John</title>
		<link>http://marketingtechblog.com/2007/09/15/calculate-distance/comment-page-1/#comment-283442</link>
		<dc:creator>John</dc:creator>
		<pubDate>Fri, 29 May 2009 03:02:47 +0000</pubDate>
		<guid isPermaLink="false">http://marketingtechblog.com/2007/09/15/calculate-distance/#comment-283442</guid>
		<description>Doug,

I have been trying to use mysql and php to evaluate whether a lat long point is within a polygon.   Do you know if your developer friend published any examples on how to accomplish this task.  Or do you know any good examples.  Thanks in advance.</description>
		<content:encoded><![CDATA[<p>Doug,</p>
<p>I have been trying to use mysql and php to evaluate whether a lat long point is within a polygon.   Do you know if your developer friend published any examples on how to accomplish this task.  Or do you know any good examples.  Thanks in advance.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matis</title>
		<link>http://marketingtechblog.com/2007/09/15/calculate-distance/comment-page-1/#comment-283346</link>
		<dc:creator>Matis</dc:creator>
		<pubDate>Thu, 28 May 2009 18:32:02 +0000</pubDate>
		<guid isPermaLink="false">http://marketingtechblog.com/2007/09/15/calculate-distance/#comment-283346</guid>
		<description>Hi everyone this is my test SQL statement:
&lt;code&gt;
 SELECT DISTINCT area_id, (
(
(
acos( sin( ( 13.65 * pi( ) /180 ) ) * sin( (
`lat_dec` * pi( ) /180 ) ) + cos( ( 13.65 * pi( ) /180 ) ) * cos( (
`lat_dec` * pi( ) /180 )
) * cos( (
( 51.02 - `lon_dec` ) * pi( ) /180 )
)
)
) *180 / pi( )
) *60 * 1.1515 * 1.609344
) AS distance
FROM `post_codes` WHERE distance &lt;= 50
&lt;/code&gt;

and Mysql is telling me that distance, doesn&#039;t exist as a column, i can use order by, i can do it without WHERE, and it works, but not with it...</description>
		<content:encoded><![CDATA[<p>Hi everyone this is my test SQL statement:<br />
<code><br />
 SELECT DISTINCT area_id, (<br />
(<br />
(<br />
acos( sin( ( 13.65 * pi( ) /180 ) ) * sin( (<br />
`lat_dec` * pi( ) /180 ) ) + cos( ( 13.65 * pi( ) /180 ) ) * cos( (<br />
`lat_dec` * pi( ) /180 )<br />
) * cos( (<br />
( 51.02 - `lon_dec` ) * pi( ) /180 )<br />
)<br />
)<br />
) *180 / pi( )<br />
) *60 * 1.1515 * 1.609344<br />
) AS distance<br />
FROM `post_codes` WHERE distance &lt;= 50<br />
</code></p>
<p>and Mysql is telling me that distance, doesn&#8217;t exist as a column, i can use order by, i can do it without WHERE, and it works, but not with it&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Douglas Karr</title>
		<link>http://marketingtechblog.com/2007/09/15/calculate-distance/comment-page-1/#comment-276551</link>
		<dc:creator>Douglas Karr</dc:creator>
		<pubDate>Fri, 17 Apr 2009 00:28:46 +0000</pubDate>
		<guid isPermaLink="false">http://marketingtechblog.com/2007/09/15/calculate-distance/#comment-276551</guid>
		<description>Fantastic advice!  I actually worked with a developer who wrote a function that pulled the inside square and then a recursive function that made &#039;squares&#039; around the perimeter to include and exclude the remaining points.  The result was an incredibly fast result - he could evaluate millions of points in microseconds.

My approach above is definitely &#039;crude&#039; but capable.  Thanks again!</description>
		<content:encoded><![CDATA[<p>Fantastic advice!  I actually worked with a developer who wrote a function that pulled the inside square and then a recursive function that made &#8217;squares&#8217; around the perimeter to include and exclude the remaining points.  The result was an incredibly fast result &#8211; he could evaluate millions of points in microseconds.</p>
<p>My approach above is definitely &#8216;crude&#8217; but capable.  Thanks again!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Somebody</title>
		<link>http://marketingtechblog.com/2007/09/15/calculate-distance/comment-page-1/#comment-276531</link>
		<dc:creator>Somebody</dc:creator>
		<pubDate>Thu, 16 Apr 2009 19:52:51 +0000</pubDate>
		<guid isPermaLink="false">http://marketingtechblog.com/2007/09/15/calculate-distance/#comment-276531</guid>
		<description>Minor mistake in the example... that would be for within 50 km (not 100) since we are looking at the &quot;radius&quot; of our... square.</description>
		<content:encoded><![CDATA[<p>Minor mistake in the example&#8230; that would be for within 50 km (not 100) since we are looking at the &#8220;radius&#8221; of our&#8230; square.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Somebody</title>
		<link>http://marketingtechblog.com/2007/09/15/calculate-distance/comment-page-1/#comment-276481</link>
		<dc:creator>Somebody</dc:creator>
		<pubDate>Thu, 16 Apr 2009 14:56:05 +0000</pubDate>
		<guid isPermaLink="false">http://marketingtechblog.com/2007/09/15/calculate-distance/#comment-276481</guid>
		<description>Keep in mind that a select statement like that will be very computationally intense and therefore slow. If you have a lot of those queries, it can bog things down quite quickly.

A much less intense approach is to run a first (crude) select using a SQUARE area defined by a calculated distance i.e. &quot;select * from tablename where latitude between lat1 and lat2 and longitude between lon1 and lon2&quot;. lat1 = targetlatitude - latdiff, lat2 = targetlatitude + latdiff, similar with lon. latdiff ~= distance / 111 (for km), or distance/69 for miles since 1 degree of latitude is ~ 111 km (slight variation since earth is slightly oval, but sufficient for this purpose). londiff = distance / (abs(cos(deg2rad(latitude))*111))  -- or 69 for miles (you can actually take a slightly larger square in order to account for variations). Then take the result of that and feed it into the radial select. Just don&#039;t forget to account for out-of-bounds coordinates - i.e. the range of acceptable longitude is -180 to +180 and the range of acceptable latitude is -90 to +90 -- in case your latdiff or londiff runs outside this range. Note that in most cases this may not be applicable since it only affects calculations over a line through the pacific ocean from pole to pole, though it does intersect part of chukotka and part of alaska.

What we accomplish by this is a significant reduction in the number of points against which you make this calculation. If you have a million global points in the database distributed roughly evenly and you want to search within 100 km, then your first (fast) search is of an area 10000 sq km and will probably yield about 20 results (based on even distribution over a surface area of about 500M sq km), which means that you run the complex distance calculation 20 times for this query instead of a million times.</description>
		<content:encoded><![CDATA[<p>Keep in mind that a select statement like that will be very computationally intense and therefore slow. If you have a lot of those queries, it can bog things down quite quickly.</p>
<p>A much less intense approach is to run a first (crude) select using a SQUARE area defined by a calculated distance i.e. &#8220;select * from tablename where latitude between lat1 and lat2 and longitude between lon1 and lon2&#8243;. lat1 = targetlatitude &#8211; latdiff, lat2 = targetlatitude + latdiff, similar with lon. latdiff ~= distance / 111 (for km), or distance/69 for miles since 1 degree of latitude is ~ 111 km (slight variation since earth is slightly oval, but sufficient for this purpose). londiff = distance / (abs(cos(deg2rad(latitude))*111))  &#8212; or 69 for miles (you can actually take a slightly larger square in order to account for variations). Then take the result of that and feed it into the radial select. Just don&#8217;t forget to account for out-of-bounds coordinates &#8211; i.e. the range of acceptable longitude is -180 to +180 and the range of acceptable latitude is -90 to +90 &#8212; in case your latdiff or londiff runs outside this range. Note that in most cases this may not be applicable since it only affects calculations over a line through the pacific ocean from pole to pole, though it does intersect part of chukotka and part of alaska.</p>
<p>What we accomplish by this is a significant reduction in the number of points against which you make this calculation. If you have a million global points in the database distributed roughly evenly and you want to search within 100 km, then your first (fast) search is of an area 10000 sq km and will probably yield about 20 results (based on even distribution over a surface area of about 500M sq km), which means that you run the complex distance calculation 20 times for this query instead of a million times.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tamik</title>
		<link>http://marketingtechblog.com/2007/09/15/calculate-distance/comment-page-1/#comment-266928</link>
		<dc:creator>tamik</dc:creator>
		<pubDate>Thu, 19 Feb 2009 11:17:02 +0000</pubDate>
		<guid isPermaLink="false">http://marketingtechblog.com/2007/09/15/calculate-distance/#comment-266928</guid>
		<description>thanks a lot works great</description>
		<content:encoded><![CDATA[<p>thanks a lot works great</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robbie</title>
		<link>http://marketingtechblog.com/2007/09/15/calculate-distance/comment-page-1/#comment-255994</link>
		<dc:creator>Robbie</dc:creator>
		<pubDate>Tue, 23 Dec 2008 10:48:11 +0000</pubDate>
		<guid isPermaLink="false">http://marketingtechblog.com/2007/09/15/calculate-distance/#comment-255994</guid>
		<description>Incredibly helpful, thank you very much! I was having some problems with the new &quot;HAVING&quot;, rather than &quot;WHERE&quot;, but once I read the comments here (after about half an hour of grinding my teeth in frustration =P), I got it working nicely. Thank you ^_^</description>
		<content:encoded><![CDATA[<p>Incredibly helpful, thank you very much! I was having some problems with the new &#8220;HAVING&#8221;, rather than &#8220;WHERE&#8221;, but once I read the comments here (after about half an hour of grinding my teeth in frustration =P), I got it working nicely. Thank you ^_^</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Douglas Karr</title>
		<link>http://marketingtechblog.com/2007/09/15/calculate-distance/comment-page-1/#comment-199473</link>
		<dc:creator>Douglas Karr</dc:creator>
		<pubDate>Thu, 28 Aug 2008 20:57:01 +0000</pubDate>
		<guid isPermaLink="false">http://marketingtechblog.com/2007/09/15/calculate-distance/#comment-199473</guid>
		<description>I found the &lt;a href=&quot;http://alienryderflex.com/polygon/&quot; rel=&quot;nofollow&quot;&gt;equation to figure out if a point in a polygon&lt;/a&gt;!</description>
		<content:encoded><![CDATA[<p>I found the <a href="http://alienryderflex.com/polygon/" rel="nofollow">equation to figure out if a point in a polygon</a>!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
