<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sébastien Barbieri's blog &#187; longitude</title>
	<atom:link href="http://blog.sbw.be/tag/longitude/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sbw.be</link>
	<description>My life, my work, my projects</description>
	<lastBuildDate>Sat, 24 Dec 2011 00:12:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Distance between cities</title>
		<link>http://blog.sbw.be/2009/06/12/distance-between-cities/</link>
		<comments>http://blog.sbw.be/2009/06/12/distance-between-cities/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 07:06:45 +0000</pubDate>
		<dc:creator>Sébastien Barbieri</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[truc de g33k]]></category>
		<category><![CDATA[belgium]]></category>
		<category><![CDATA[cities]]></category>
		<category><![CDATA[latitude]]></category>
		<category><![CDATA[longitude]]></category>
		<category><![CDATA[zip]]></category>

		<guid isPermaLink="false">http://blog.sbw.be/?p=256</guid>
		<description><![CDATA[First of all I need to cite my sources: Calculate distance, bearing and more between two Latitude/Longitude points Geocoding Belgium Postal Codes The goal of my project was to find out which cities are nearby (within a user specified distance). Of course users don&#8217;t know their city coordinate, so you will have to find out [...]]]></description>
			<content:encoded><![CDATA[<p>First of all I need to cite my sources: </p>
<ul>
<li>
		<a href="http://www.movable-type.co.uk/scripts/latlong.html">Calculate distance, bearing and more between two Latitude/Longitude points</a>
	</li>
<li>
		<a href="http://www.jedi.be/blog/2009/04/29/geocoding-belgium-postal-codes/">Geocoding Belgium Postal Codes</a>
	</li>
</ul>
<p>The goal of my project was to find out which cities are nearby (within a user specified distance).<br />
Of course users don&#8217;t know their city coordinate, so you will have to find out by yourself. To do so, I used the JEDI trick (<a href="http://www.jedi.be/blog/2009/04/29/geocoding-belgium-postal-codes/">Geocoding Belgium Postal Codes</a>) which is:</p>
<ul>
<ol>Downloading the csv of cities and zip code (available on the <a href="http://www.post.be/site/fr/residential/customerservice/search/postal_codes.html">national mail </a>site for Belgium, I don&#8217;t know for other countries)</ol>
<ol>Writing a small piece of code to parse the csv and retrieve the right latitude and longitude via google geo service</ol>
</ul>
<p>I could have downloaded <a href="http://www.jedi.be/blog/wp-content/uploads/2009/04/geocodes.csv">the file from the JEDI site</a> but I really needed to do it by myself.</p>
<p>So the next thing to do is: finding the cities within an arbitrary distance.</p>
<p>This was based on the documentation I found on the movable-type math site.</p>
<p>All I did is translating the Javascript function to PHP functions:</p>
<pre class="brush: php; title: ;">
    private function sphericalDistance($lat1,$lon1,$lat2,$lon2){
        $R = 6371.0; // km
        $d = acos(
                sin(deg2rad($lat1))*sin(deg2rad($lat2))
                + cos(deg2rad($lat1))*cos(deg2rad($lat2))
                * cos(deg2rad($lon2-$lon1))
             )
             * $R;
        return $d;
    }

    private function haversineDistance($lat1,$lon1,$lat2,$lon2){
        $R = 6371.0; // km
        $dLat = deg2rad($lat2-$lat1);
        $dLon = deg2rad($lon2-$lon1);
        $a = sin($dLat/2) * sin($dLat/2)
            + cos(deg2rad($lat1)) * cos(deg2rad($lat2))
            * sin($dLon/2) * sin($dLon/2);
        $c = 2.0 * atan2(sqrt($a), sqrt(1-$a));
        $d = $R * $c;
        return $d;
    }

    private function vincentyDistance($lat1,$lon1,$lat2,$lon2){
        $a = 6378137.0; $b = 6356752.3142;  $f = 1/298.257223563;  // WGS-84 ellipsiod
        $L = deg2rad($lon2-$lon1);
        $U1 = atan((1-$f) * tan(deg2rad($lat1)));
        $U2 = atan((1-$f) * tan(deg2rad($lat2)));
        $sinU1 = sin($U1);
        $cosU1 = cos($U1);
        $sinU2 = sin($U2);
        $cosU2 = cos($U2);
        $lambda = $L;
        $lambdaP = $L;
        $iterLimit = 100;
        do{
            $sinLambda = sin($lambda);
            $cosLambda = cos($lambda);
            $sinSigma = sqrt(($cosU2*$sinLambda) * ($cosU2*$sinLambda) +
                        ($cosU1*$sinU2-$sinU1*$cosU2*$cosLambda) * ($cosU1*$sinU2-$sinU1*$cosU2*$cosLambda));
            if($sinSigma==0) return 0;
            $cosSigma = $sinU1 * $sinU2 + $cosU1*$cosU2*$cosLambda;
            $sigma = atan2($sinSigma, $cosSigma);
            $sinAlpha = $cosU1 * $cosU2 * $sinLambda / $sinSigma;
            $cosSqAlpha = 1 - $sinAlpha*$sinAlpha;
            if($cosSqAlpha!=0){
                $cos2SigmaM = $cosSigma - 2*$sinU1*$sinU2/$cosSqAlpha;
            }else{
                $cos2SigmaM =0;
            }
            $C = $f/16*$cosSqAlpha*(4+$f*(4-3*$cosSqAlpha));
            $lambdaP = $lambda;
            $lambda = $L + (1-$C) * $f * $sinAlpha *
                    ($sigma + $C*$sinSigma*($cos2SigmaM+$C*$cosSigma*(-1+2*$cos2SigmaM*$cos2SigmaM)));
        }while(abs($lambda-$lambdaP) &gt; 1e-12 &amp;&amp; --$iterLimit&gt;0);
        if($iterLimit == 0) return false; // formula failed to converge
        $uSq = $cosSqAlpha * ($a*$a - $b*$b) / ($b*$b);
        $A = 1 + $uSq/16384.0*(4096+$uSq*(-768+$uSq*(320-175*$uSq)));
        $B = $uSq/1024.0 * (256+$uSq*(-128+$uSq*(74-47*$uSq)));
        $deltaSigma = $B*$sinSigma*($cos2SigmaM+$B/4*($cosSigma*(-1+2*$cos2SigmaM*$cos2SigmaM)
                    - $B/6*$cos2SigmaM*(-3+4*$sinSigma*$sinSigma)*(-3+4*$cos2SigmaM*$cos2SigmaM)));
        $s = $b*$A*($sigma-$deltaSigma);
        return $s/1000.0;
    }
</pre>
<p>And that&#8217;s it&#8230;</p>
<p>So if you want the<a href="http://code.sbw.be/php/belgianCitiesCoord.data"> php serialized cities of Belgium with Zip, cityname, uppercase cityname, longitude,latitude</a>.</p>
<p>I didn&#8217;t do a lot at all, just put every source together.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sbw.be/2009/06/12/distance-between-cities/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

