<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Calculate the Distance Between Two Latitude/Longitude Points using Plain SQL</title>
	<atom:link href="http://hoopercharles.wordpress.com/2011/06/13/calculate-the-distance-between-two-latitudelongitude-points-using-plain-sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://hoopercharles.wordpress.com/2011/06/13/calculate-the-distance-between-two-latitudelongitude-points-using-plain-sql/</link>
	<description>Miscellaneous Random Oracle Topics: Stop, Think, ... Understand</description>
	<lastBuildDate>Thu, 13 Jun 2013 22:46:43 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Charles Hooper</title>
		<link>http://hoopercharles.wordpress.com/2011/06/13/calculate-the-distance-between-two-latitudelongitude-points-using-plain-sql/#comment-3844</link>
		<dc:creator><![CDATA[Charles Hooper]]></dc:creator>
		<pubDate>Sun, 04 Sep 2011 22:49:03 +0000</pubDate>
		<guid isPermaLink="false">http://hoopercharles.wordpress.com/?p=4998#comment-3844</guid>
		<description><![CDATA[Tom,

I am not sure what might have happened with your formula in Excel.  In this article I converted the Excel formula to work in a SQL statement, and that formula appeared to be producing the expected results.  A couple of things to keep in mind:
* North and West positions should be entered as negative values
* Two negatives make a positive ( -p1 - -p2 ) = ( -p1 + p2)
* Accidentally reversing the longitude and latitude values will cause the calculations to be significantly off
* Excel, SQL, and various programming languages expect SIN and COS values in radians, so you need to convert the longitude and latitude values to their radian equivalents before plugging the values into the formula in Excel.  This article describes how to perform the conversion:
&lt;pre&gt;
=SIN(degree / 180 * 3.141592)
&lt;/pre&gt;

Once the conversion to radians is performed, you should be able to obtain the correct answer.  The following is from the SQL statement: 
&lt;pre&gt;
ABS(ACOS(SIN(LOC1.LAT_RAD)*SIN(LOC2.LAT_RAD)+COS(LOC1.LAT_RAD)*COS(LOC2.LAT_RAD)*COS(LOC2.LON_RAD-LOC1.LON_RAD)))* 3960
&lt;/pre&gt;]]></description>
		<content:encoded><![CDATA[<p>Tom,</p>
<p>I am not sure what might have happened with your formula in Excel.  In this article I converted the Excel formula to work in a SQL statement, and that formula appeared to be producing the expected results.  A couple of things to keep in mind:<br />
* North and West positions should be entered as negative values<br />
* Two negatives make a positive ( -p1 &#8211; -p2 ) = ( -p1 + p2)<br />
* Accidentally reversing the longitude and latitude values will cause the calculations to be significantly off<br />
* Excel, SQL, and various programming languages expect SIN and COS values in radians, so you need to convert the longitude and latitude values to their radian equivalents before plugging the values into the formula in Excel.  This article describes how to perform the conversion:</p>
<pre>
=SIN(degree / 180 * 3.141592)
</pre>
<p>Once the conversion to radians is performed, you should be able to obtain the correct answer.  The following is from the SQL statement: </p>
<pre>
ABS(ACOS(SIN(LOC1.LAT_RAD)*SIN(LOC2.LAT_RAD)+COS(LOC1.LAT_RAD)*COS(LOC2.LAT_RAD)*COS(LOC2.LON_RAD-LOC1.LON_RAD)))* 3960
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom Heller</title>
		<link>http://hoopercharles.wordpress.com/2011/06/13/calculate-the-distance-between-two-latitudelongitude-points-using-plain-sql/#comment-3843</link>
		<dc:creator><![CDATA[Tom Heller]]></dc:creator>
		<pubDate>Sun, 04 Sep 2011 22:09:41 +0000</pubDate>
		<guid isPermaLink="false">http://hoopercharles.wordpress.com/?p=4998#comment-3843</guid>
		<description><![CDATA[Hi, I just stumbled upon this, in an effort to figure distances between radio towers for which I have the Lat/Lon coordinates.

I typed your first &#039;flat Earth&#039; formula into Excel and it returned a figure very close to what I was expecting (tho&#039; a bit larger than the county&#039;s GIS mapping system&#039;s &#039;digitize distance&#039; function provides).  I&#039;m not worried at this point about absolute precision.

BUT....then I typed into Excel the kilometer formula that started with acos -- and I got blown out of the water.  The 2.9 miles of the first equation swelled to -I forget, some couple thousand km&#039;s.  So, is there a decimal point missing somewhere in there?

I probably won&#039;t return to learn your answer, but wanted to point out the &#039;mis-spelling&#039;.  (I checked the formula I had typed and it perfectly reflected yours.)

Any way, thanks for the interesting post!  I also came out with lots of respect for the challenges GPS software (and users) must cope!]]></description>
		<content:encoded><![CDATA[<p>Hi, I just stumbled upon this, in an effort to figure distances between radio towers for which I have the Lat/Lon coordinates.</p>
<p>I typed your first &#8216;flat Earth&#8217; formula into Excel and it returned a figure very close to what I was expecting (tho&#8217; a bit larger than the county&#8217;s GIS mapping system&#8217;s &#8216;digitize distance&#8217; function provides).  I&#8217;m not worried at this point about absolute precision.</p>
<p>BUT&#8230;.then I typed into Excel the kilometer formula that started with acos &#8212; and I got blown out of the water.  The 2.9 miles of the first equation swelled to -I forget, some couple thousand km&#8217;s.  So, is there a decimal point missing somewhere in there?</p>
<p>I probably won&#8217;t return to learn your answer, but wanted to point out the &#8216;mis-spelling&#8217;.  (I checked the formula I had typed and it perfectly reflected yours.)</p>
<p>Any way, thanks for the interesting post!  I also came out with lots of respect for the challenges GPS software (and users) must cope!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Charles Hooper</title>
		<link>http://hoopercharles.wordpress.com/2011/06/13/calculate-the-distance-between-two-latitudelongitude-points-using-plain-sql/#comment-3488</link>
		<dc:creator><![CDATA[Charles Hooper]]></dc:creator>
		<pubDate>Wed, 15 Jun 2011 13:48:36 +0000</pubDate>
		<guid isPermaLink="false">http://hoopercharles.wordpress.com/?p=4998#comment-3488</guid>
		<description><![CDATA[Mark,

Excellent points.  

It probably would be a good idea to point out that the calculations do not determine driving distances between locations, nor do the calculations exactly determine flying distances (if a plane is a couple miles up in the air, that effectively makes the earth&#039;s radius a couple of miles larger from the perspective of the plane.  I now have a little more respect for the GPS manufacturers whose products attempt to determine the final destination arrival time.]]></description>
		<content:encoded><![CDATA[<p>Mark,</p>
<p>Excellent points.  </p>
<p>It probably would be a good idea to point out that the calculations do not determine driving distances between locations, nor do the calculations exactly determine flying distances (if a plane is a couple miles up in the air, that effectively makes the earth&#8217;s radius a couple of miles larger from the perspective of the plane.  I now have a little more respect for the GPS manufacturers whose products attempt to determine the final destination arrival time.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark W. Farnham</title>
		<link>http://hoopercharles.wordpress.com/2011/06/13/calculate-the-distance-between-two-latitudelongitude-points-using-plain-sql/#comment-3486</link>
		<dc:creator><![CDATA[Mark W. Farnham]]></dc:creator>
		<pubDate>Wed, 15 Jun 2011 11:26:59 +0000</pubDate>
		<guid isPermaLink="false">http://hoopercharles.wordpress.com/?p=4998#comment-3486</guid>
		<description><![CDATA[Interesting analyses and optimizations, including the care folks stated about results being within requirements. If you need to be extremely precise though, remember that the earth is neither flat nor an ideal sphere.]]></description>
		<content:encoded><![CDATA[<p>Interesting analyses and optimizations, including the care folks stated about results being within requirements. If you need to be extremely precise though, remember that the earth is neither flat nor an ideal sphere.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Charles Hooper</title>
		<link>http://hoopercharles.wordpress.com/2011/06/13/calculate-the-distance-between-two-latitudelongitude-points-using-plain-sql/#comment-3480</link>
		<dc:creator><![CDATA[Charles Hooper]]></dc:creator>
		<pubDate>Tue, 14 Jun 2011 23:16:35 +0000</pubDate>
		<guid isPermaLink="false">http://hoopercharles.wordpress.com/?p=4998#comment-3480</guid>
		<description><![CDATA[Louis and Gary,

Thank you for the ideas.  In an OTN thread (http://forums.oracle.com/forums/thread.jspa?threadID=1020948 ) I found an example of using SDO_GEOM.SDO_DISTANCE.  The result?  An error, why?
http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14255/sdo_locator.htm
&quot;The installation of Locator depends on the successful and proper installation of Oracle interMedia. interMedia is installed and configured with Oracle Database 10g...&quot;
 
http://download.oracle.com/docs/cd/E18283_01/appdev.112/e11830/sdo_locator.htm
&quot;The installation of Locator depends on the successful and proper installation of Oracle Multimedia. Oracle Multimedia is installed and configured with Oracle Database 11g, although you can...&quot;

If someone (like me) did not see much point in installing Oracle Multimedia (formerly called Oracle interMedia), it is quite likely that the same error will be received.

With Oracle Multimedia installed:
&lt;pre&gt;
SELECT SDO_GEOM.SDO_DISTANCE(
SDO_GEOMETRY(2001,8307,SDO_POINT_TYPE((14 + 26/60),-(50 + 5/60),NULL),NULL,NULL),
SDO_GEOMETRY(2001,8307,SDO_POINT_TYPE(-(43 + 12/60),(22 + 57/60),NULL),NULL,NULL),
0.0001,&#039;unit=km&#039;) AS DISTANCE_BETWEEN_POINTS 
FROM DUAL ;
 
DISTANCE_BETWEEN_POINTS
-----------------------
             9876.67951
&lt;/pre&gt;

According to Google, 9,876.67951 kilometers = 6,137.08412 miles - very close to the 6,152 miles returned by the formula I posted above.

---
&lt;pre&gt;
SELECT SDO_GEOM.SDO_DISTANCE(
SDO_GEOMETRY(2001,8307,SDO_POINT_TYPE(-(36 + 44/60),(19 + 48/60),NULL),NULL,NULL),
SDO_GEOMETRY(2001,8307,SDO_POINT_TYPE((42),(88 + 30/60),NULL),NULL,NULL),
0.0001,&#039;unit=km&#039;) AS DISTANCE_BETWEEN_POINTS 
FROM DUAL ;
 
DISTANCE_BETWEEN_POINTS
-----------------------
             7779.77983
&lt;/pre&gt;

According to Google, 7,779.77983 kilometers = 4,834.13107 miles - very close to the 4,832.09 miles returned by the formula I posted above.

I have not compared the performance of the SDO_GEOM.SDO_DISTANCE method with the performance of the formula that I posted above.]]></description>
		<content:encoded><![CDATA[<p>Louis and Gary,</p>
<p>Thank you for the ideas.  In an OTN thread (<a href="http://forums.oracle.com/forums/thread.jspa?threadID=1020948" rel="nofollow">http://forums.oracle.com/forums/thread.jspa?threadID=1020948</a> ) I found an example of using SDO_GEOM.SDO_DISTANCE.  The result?  An error, why?<br />
<a href="http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14255/sdo_locator.htm" rel="nofollow">http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14255/sdo_locator.htm</a><br />
&#8220;The installation of Locator depends on the successful and proper installation of Oracle interMedia. interMedia is installed and configured with Oracle Database 10g&#8230;&#8221;</p>
<p><a href="http://download.oracle.com/docs/cd/E18283_01/appdev.112/e11830/sdo_locator.htm" rel="nofollow">http://download.oracle.com/docs/cd/E18283_01/appdev.112/e11830/sdo_locator.htm</a><br />
&#8220;The installation of Locator depends on the successful and proper installation of Oracle Multimedia. Oracle Multimedia is installed and configured with Oracle Database 11g, although you can&#8230;&#8221;</p>
<p>If someone (like me) did not see much point in installing Oracle Multimedia (formerly called Oracle interMedia), it is quite likely that the same error will be received.</p>
<p>With Oracle Multimedia installed:</p>
<pre>
SELECT SDO_GEOM.SDO_DISTANCE(
SDO_GEOMETRY(2001,8307,SDO_POINT_TYPE((14 + 26/60),-(50 + 5/60),NULL),NULL,NULL),
SDO_GEOMETRY(2001,8307,SDO_POINT_TYPE(-(43 + 12/60),(22 + 57/60),NULL),NULL,NULL),
0.0001,'unit=km') AS DISTANCE_BETWEEN_POINTS 
FROM DUAL ;
 
DISTANCE_BETWEEN_POINTS
-----------------------
             9876.67951
</pre>
<p>According to Google, 9,876.67951 kilometers = 6,137.08412 miles &#8211; very close to the 6,152 miles returned by the formula I posted above.</p>
<p>&#8212;</p>
<pre>
SELECT SDO_GEOM.SDO_DISTANCE(
SDO_GEOMETRY(2001,8307,SDO_POINT_TYPE(-(36 + 44/60),(19 + 48/60),NULL),NULL,NULL),
SDO_GEOMETRY(2001,8307,SDO_POINT_TYPE((42),(88 + 30/60),NULL),NULL,NULL),
0.0001,'unit=km') AS DISTANCE_BETWEEN_POINTS 
FROM DUAL ;
 
DISTANCE_BETWEEN_POINTS
-----------------------
             7779.77983
</pre>
<p>According to Google, 7,779.77983 kilometers = 4,834.13107 miles &#8211; very close to the 4,832.09 miles returned by the formula I posted above.</p>
<p>I have not compared the performance of the SDO_GEOM.SDO_DISTANCE method with the performance of the formula that I posted above.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gary</title>
		<link>http://hoopercharles.wordpress.com/2011/06/13/calculate-the-distance-between-two-latitudelongitude-points-using-plain-sql/#comment-3478</link>
		<dc:creator><![CDATA[Gary]]></dc:creator>
		<pubDate>Tue, 14 Jun 2011 09:51:15 +0000</pubDate>
		<guid isPermaLink="false">http://hoopercharles.wordpress.com/?p=4998#comment-3478</guid>
		<description><![CDATA[sdo_distance is one of the SDO_GEOM permitted under Locator (or as I prefer to call it sub-spatial). [Why they can&#039;t put the permitted procedures in a separate package from the non-permitted ones is a question only the Auditors can answer.]

http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28400/sdo_locator.htm#i632018

I don&#039;t recall comparing the long equation mechanism with Locator/Spatial, but I did switch to the &#039;flat earth&#039; calculation because it was a lot less cpu-intensive that SDO_GEOM and the error wasn&#039;t significant for my purposes.]]></description>
		<content:encoded><![CDATA[<p>sdo_distance is one of the SDO_GEOM permitted under Locator (or as I prefer to call it sub-spatial). [Why they can't put the permitted procedures in a separate package from the non-permitted ones is a question only the Auditors can answer.]</p>
<p><a href="http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28400/sdo_locator.htm#i632018" rel="nofollow">http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28400/sdo_locator.htm#i632018</a></p>
<p>I don&#8217;t recall comparing the long equation mechanism with Locator/Spatial, but I did switch to the &#8216;flat earth&#8217; calculation because it was a lot less cpu-intensive that SDO_GEOM and the error wasn&#8217;t significant for my purposes.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Louis Ywema</title>
		<link>http://hoopercharles.wordpress.com/2011/06/13/calculate-the-distance-between-two-latitudelongitude-points-using-plain-sql/#comment-3477</link>
		<dc:creator><![CDATA[Louis Ywema]]></dc:creator>
		<pubDate>Tue, 14 Jun 2011 06:01:21 +0000</pubDate>
		<guid isPermaLink="false">http://hoopercharles.wordpress.com/?p=4998#comment-3477</guid>
		<description><![CDATA[Hi,
using Oracle Locator might speed it up and : The Earth isn&#039;t flat anymore and Spatial knows it! ;-)

select sdo_geom.sdo_distance(sdo_geometry(2001
                                       , 
                                       , null    
                                       , mdsys.SDO_ELEM_INFO_ARRAY(1, 1, 1)
                                       , mdsys.SDO_ORDINATE_ARRAY(, )
                                       )
                            ,sdo_geometry(2001
                                       , 
                                       , null    
                                       , mdsys.SDO_ELEM_INFO_ARRAY(1, 1, 1)
                                       , mdsys.SDO_ORDINATE_ARRAY(, )
                                       )
                            ,0.1
                            ,&#039;unit=km&#039;) distance
from dual;]]></description>
		<content:encoded><![CDATA[<p>Hi,<br />
using Oracle Locator might speed it up and : The Earth isn&#8217;t flat anymore and Spatial knows it! <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>select sdo_geom.sdo_distance(sdo_geometry(2001<br />
                                       ,<br />
                                       , null<br />
                                       , mdsys.SDO_ELEM_INFO_ARRAY(1, 1, 1)<br />
                                       , mdsys.SDO_ORDINATE_ARRAY(, )<br />
                                       )<br />
                            ,sdo_geometry(2001<br />
                                       ,<br />
                                       , null<br />
                                       , mdsys.SDO_ELEM_INFO_ARRAY(1, 1, 1)<br />
                                       , mdsys.SDO_ORDINATE_ARRAY(, )<br />
                                       )<br />
                            ,0.1<br />
                            ,&#8217;unit=km&#8217;) distance<br />
from dual;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Charles Hooper</title>
		<link>http://hoopercharles.wordpress.com/2011/06/13/calculate-the-distance-between-two-latitudelongitude-points-using-plain-sql/#comment-3476</link>
		<dc:creator><![CDATA[Charles Hooper]]></dc:creator>
		<pubDate>Mon, 13 Jun 2011 22:19:26 +0000</pubDate>
		<guid isPermaLink="false">http://hoopercharles.wordpress.com/?p=4998#comment-3476</guid>
		<description><![CDATA[Niall,

Thank you for the information.

I just had another thought regarding how to further decrease the CPU requirements for this SQL statement, and thus to speed up the SQL statement... the mathematics logic is starting to come back to me.  The latitude values must be in the range of +- the radian value that is associated with the distance along the surface of the earth.  The same is true for the longitude values.  

With that logic in mind, it would be possible to reduce the number of rows leaving the inline view, and thus entering the WHERE clause of the main SQL statement.  If the radius of the earth is roughly 3960 miles, the circumference is 3960 * 2 * 3.14159275 = 24881 miles.  1 degree = 69.115 miles, 1 mile = 0.0144686 degrees, 1 mile = 2.525254004999719e-4 radians.  Thus, for each mile of the radius, the search range beyond the central latitude/longitude pair would be extended by +-2.525254004999719e-4 radians to pre-filter the majority of the rows that would exit the inline view and enter the WHERE clause of the main portion of the query.]]></description>
		<content:encoded><![CDATA[<p>Niall,</p>
<p>Thank you for the information.</p>
<p>I just had another thought regarding how to further decrease the CPU requirements for this SQL statement, and thus to speed up the SQL statement&#8230; the mathematics logic is starting to come back to me.  The latitude values must be in the range of +- the radian value that is associated with the distance along the surface of the earth.  The same is true for the longitude values.  </p>
<p>With that logic in mind, it would be possible to reduce the number of rows leaving the inline view, and thus entering the WHERE clause of the main SQL statement.  If the radius of the earth is roughly 3960 miles, the circumference is 3960 * 2 * 3.14159275 = 24881 miles.  1 degree = 69.115 miles, 1 mile = 0.0144686 degrees, 1 mile = 2.525254004999719e-4 radians.  Thus, for each mile of the radius, the search range beyond the central latitude/longitude pair would be extended by +-2.525254004999719e-4 radians to pre-filter the majority of the rows that would exit the inline view and enter the WHERE clause of the main portion of the query.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Niall Litchfield</title>
		<link>http://hoopercharles.wordpress.com/2011/06/13/calculate-the-distance-between-two-latitudelongitude-points-using-plain-sql/#comment-3475</link>
		<dc:creator><![CDATA[Niall Litchfield]]></dc:creator>
		<pubDate>Mon, 13 Jun 2011 21:33:40 +0000</pubDate>
		<guid isPermaLink="false">http://hoopercharles.wordpress.com/?p=4998#comment-3475</guid>
		<description><![CDATA[Charles

I *believe* but don&#039;t know for sure that oracle locator (which is included in SE) will deal with this. For certain you can just call the web service directly via xdb. :)]]></description>
		<content:encoded><![CDATA[<p>Charles</p>
<p>I *believe* but don&#8217;t know for sure that oracle locator (which is included in SE) will deal with this. For certain you can just call the web service directly via xdb. <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
</channel>
</rss>
