<?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>God Object &#187; geo</title>
	<atom:link href="http://www.god-object.com/tag/geo/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.god-object.com</link>
	<description>Because I know too much</description>
	<lastBuildDate>Wed, 28 Sep 2011 09:34:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Convert Rijksdriehoeksco&#246;rdinaten to latitude/longitude</title>
		<link>http://www.god-object.com/2009/10/23/convert-rijksdriehoekscordinaten-to-latitudelongitude/</link>
		<comments>http://www.god-object.com/2009/10/23/convert-rijksdriehoekscordinaten-to-latitudelongitude/#comments</comments>
		<pubDate>Fri, 23 Oct 2009 15:20:26 +0000</pubDate>
		<dc:creator>webpatser</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[geo]]></category>

		<guid isPermaLink="false">http://www.god-object.com/?p=180</guid>
		<description><![CDATA[A Rijksdriehoekscoördinaat is used by the Dutch cadastre. Since all cadastre geo-data is not according the WGS84-standard, you need to convert a Rijksdriehoekscoördinaat (RD) to latitude / longitude values, for use in Google maps for example. You can convert them with this function PHP-function Input are the RD $x and $y coördinates. Output is an [...]]]></description>
			<content:encoded><![CDATA[<p>A Rijksdriehoekscoördinaat is used by the Dutch cadastre. Since all cadastre geo-data is not according the WGS84-standard, you need to convert a Rijksdriehoekscoördinaat (RD) to latitude / longitude values, for use in Google maps for example.</p>
<p>You can convert them with this function PHP-function</p>
<pre class="brush: php; title: ; notranslate">
function rd2wgs ($x, $y)
{
    // Calculate WGS84 coördinates
    $dX = ($x - 155000) * pow(10, - 5);
    $dY = ($y - 463000) * pow(10, - 5);
    $SomN = (3235.65389 * $dY) + (- 32.58297 * pow($dX, 2)) + (- 0.2475 *
         pow($dY, 2)) + (- 0.84978 * pow($dX, 2) *
         $dY) + (- 0.0655 * pow($dY, 3)) + (- 0.01709 *
         pow($dX, 2) * pow($dY, 2)) + (- 0.00738 *
         $dX) + (0.0053 * pow($dX, 4)) + (- 0.00039 *
         pow($dX, 2) * pow($dY, 3)) + (0.00033 * pow(
            $dX, 4) * $dY) + (- 0.00012 *
         $dX * $dY);
    $SomE = (5260.52916 * $dX) + (105.94684 * $dX * $dY) + (2.45656 *
         $dX * pow($dY, 2)) + (- 0.81885 * pow(
            $dX, 3)) + (0.05594 *
         $dX * pow($dY, 3)) + (- 0.05607 * pow(
            $dX, 3) * $dY) + (0.01199 *
         $dY) + (- 0.00256 * pow($dX, 3) * pow(
            $dY, 2)) + (0.00128 *
         $dX * pow($dY, 4)) + (0.00022 * pow($dY,
            2)) + (- 0.00022 * pow(
            $dX, 2)) + (0.00026 *
         pow($dX, 5));

    $Latitude = 52.15517 + ($SomN / 3600);
    $Longitude = 5.387206 + ($SomE / 3600);

    return array(
        'latitude' =&gt; $Latitude ,
        'longitude' =&gt; $Longitude);
}
</pre>
<p>Input are the RD <code>$x</code> and <code>$y</code> coördinates. Output is an <code>array</code> with latitude &amp; longitude</p>
]]></content:encoded>
			<wfw:commentRss>http://www.god-object.com/2009/10/23/convert-rijksdriehoekscordinaten-to-latitudelongitude/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Geo/Spatial search using Sphinx search and php</title>
		<link>http://www.god-object.com/2009/10/20/geospatial-search-using-sphinx-search-and-php/</link>
		<comments>http://www.god-object.com/2009/10/20/geospatial-search-using-sphinx-search-and-php/#comments</comments>
		<pubDate>Tue, 20 Oct 2009 21:30:30 +0000</pubDate>
		<dc:creator>webpatser</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[geo]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[sphinx]]></category>

		<guid isPermaLink="false">http://www.god-object.com/?p=132</guid>
		<description><![CDATA[I have some databases with geo-location data in it, and wanted it displayed on my iPhone using Layar. Doing some research on this, I found a great presentation on Geo/Spatial search in MySQL. Reading this presentation shows that doing a search of geo-locations in a radius around a geo-location, using a mysql version of the Haversine [...]]]></description>
			<content:encoded><![CDATA[<p>I have some databases with geo-location data in it, and wanted it displayed on my iPhone using <a href="http://layar.eu/" target="_blank">Layar</a>. Doing some research on this, I found a great presentation on <a title="Geo/Spatial search in MySQL" href="http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL" target="_blank">Geo/Spatial search in MySQL</a>. Reading this presentation shows that doing a search of geo-locations in a radius around a geo-location, using a mysql version of the <a title="Haversine formula" href="http://en.wikipedia.org/wiki/Haversine_formula" target="_blank">Haversine formula</a> is doable, but is <del datetime="2009-10-20T21:22:21+00:00">not the fastest solution</del> very slow.</p>
<p>In the presentation, Alexander Rubin suggests using <a title="free open-source SQL full-text search engine" href="http://www.sphinxsearch.com/" target="_blank">Sphinx search</a> for doing the geo/spatial search. I found this the way to go, since I already use sphinx for full-text searching large text tables. Sphinx uses a magic function <code>@geodist</code> for calculation of points of interest (POI) within range. But setup of the source/index and use within PHP is not documented that well.</p>
<p>SO what do you need:</p>
<ul>
<li>Some database supported by Sphinx.</li>
<li>Database filled with longitude / latitude data.</li>
</ul>
<p>I assume you have a working copy of Sphinx installed and that the searchd listens to port 3312.</p>
<p>To let the Sphinx indexer do it&#8217;s magic create a new Sphinx source and index. Add the following code to your <code>sphinx.conf</code></p>
<pre class="brush: plain; title: ; notranslate">
source geo
{
type                 = mysql
sql_host             = 127.0.0.1
sql_user             = sphinx_user
sql_pass             = sphinx_password
sql_db               = geo_database
sql_port             = 3306
sql_query_pre        = set names utf8
sql_query_pre        = set session query_cache_type=OFF

sql_query            = select id, radians(longitude) as longitude, radians(latitude) as latitude from locations

sql_attr_float       = longitude
sql_attr_float       = latitude

sql_ranged_throttle  = 0

sql_query_info       = select * from locations where id = $id
}
</pre>
<p>The above fragment  creates the Sphinx source. It&#8217;s pretty self-explanatory. Adjust the settings to match your MySQL setup.<br />
Note that the <code>sql_query</code> uses the <code>radians</code> function to convert degrees to radians. <code> sql_attr_float</code> tells Sphinx that the longitude/latitude are floats. THis is needed for the magic @geodist function. The <code>sql_query_info</code> is handy if you debugging on the command line.</p>
<p>Now for the Sphinx index.</p>
<pre class="brush: plain; title: ; notranslate">
index geo
{
source                  = geo
path                    = /var/lib/data/geo
docinfo                 = extern
mlock                   = 0
morphology              = none
min_word_len            = 1
charset_type            = utf-8
charset_table           = 0..9, A..Z-&gt;a..z, _, a..z, U+410..U+42F-&gt;U+430..U+44F, U+430..U+44F
ignore_chars            = U+00AD
html_strip              = 0
enable_star             = 0
}
</pre>
<p>Copy-paste this fragment to your <code>sphinx.conf</code>. Note that the <code>source</code> should be the set to your just-created source.</p>
<p>When the configuration is in place, build the index files:</p>
<pre class="brush: bash; title: ; notranslate">
indexer --rotate geo
</pre>
<p>If you create a new index the <code>--rotate</code> is optional, but is harmless. If you want to rebuild the index, the rotate option will send a SIGHUP to searchd, thus restarting the daemon.</p>
<p>With the geo-location data, in radians, in your sphinx index, we need to create a PHP script to get query the dataset.</p>
<p>This is not a full featured php file but only the code for connecting to searchd.</p>
<p>You need a copy of <code>sphinxapi.php</code> in your include_path. This is the PHP API class for Sphinx and is in the Sphinx distribution source file. Get a recent version, because there were some bugs using <code>@geodist</code> in older versions.</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
require_once 'sphinxapi.php';

$_longitude = $_GET['longitude'];
$_latitude = $_GET['latitude'];
$_radius = $_GET['radius'];

$search = new SphinxClient();
$search-&gt;SetServer(&quot;localhost&quot;, 3312);
$search-&gt;SetMatchMode(SPH_MATCH_ALL);
$search-&gt;SetArrayResult(true);
$search-&gt;SetLimits(0, 100);
$search-&gt;SetGeoAnchor('latitude', 'longitude', (float) deg2rad($_latitude), (float) deg2rad($_longitude));

$circle = (float) $_radius * 1.61;
$search-&gt;SetFilterFloatRange('@geodist', 0.0, $circle);

$result = $search-&gt;Query('', 'geo');
?&gt;
</pre>
<p>Longitude and latitude is passed via GET.<br />
<code>$cl-&gt;SetLimits</code> limits the result-set to 100.<br />
<code>$cl-&gt;SetGeoAnchor</code> set&#8217;s the centre location.<br />
Notice that the <code>$_longitude &amp; $_latitude</code> is converted to radians.<br />
<code>$_radius</code> is in meters.</p>
<p>Now apply a filter <code>$cl-&gt;SetFilterFloatRange('@geodist', 0.0, $circle)</code>; call the @geodist method and only return POI&#8217;s within the range of 0 meters to $_range meters.</p>
<p><code>$result = $cl-&gt;Query('', 'geo');</code> Do the actual query in the geo source, and put the result in $result.</p>
<p>The result is an array, that easy&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.god-object.com/2009/10/20/geospatial-search-using-sphinx-search-and-php/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

