mongodb-odm-docs-dash/Doctrine ODM.docset/Contents/Resources/Documents/reference/geospatial-queries.html

276 lines
16 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Geospatial Queries &mdash; Doctrine MongoDB ODM 1.1.5 documentation</title>
<link rel="stylesheet" href="../_static/bootstrap/css/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="../_static/default.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../_static/layout.css" type="text/css" />
<link rel="stylesheet" href="../_static/configurationblock.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '1.1.5',
COLLAPSE_MODINDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/configurationblock.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/configurationblock.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="../_static/configurationblock.js"></script>
<script src="../_static/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript">
<!--
$(document).ready(function() {
$("#versions").change(function() {
var docsUrl = $(this).val();
window.location.href = docsUrl;
});
});
-->
</script>
<link rel="shortcut icon" href="../_static/doctrine.ico"/>
<link rel="search" title="Search" href="../search.html" />
<link rel="top" title="Doctrine MongoDB ODM 1.1.5 documentation" href="../index.html" />
</head>
<body>
<div id="wrapper">
<div id="header">
<h1 id="h1title"></h1>
<div id="logo">
<a href="http://www.doctrine-project.org/">Doctrine - PHP Database Libraries</a>
</div>
</div>
<div id="nav" class="cls">
<div class="tl cls">
<ul>
<li><a target="_top" href="http://www.doctrine-project.org/">Home</a></li>
<li><a target="_top" href="http://www.doctrine-project.org/about.html">About</a></li>
<li><a target="_top" href="http://www.doctrine-project.org/projects.html">Projects</a></li>
<li><a target="_top" href="http://www.doctrine-project.org/contribute.html">Contribute</a></li>
<li><a target="_top" href="http://www.doctrine-project.org/community.html">Community</a></li>
<li><a target="_top" href="http://www.doctrine-project.org/archive.html">Blog</a></li>
<li><a target="_top" href="http://www.doctrine-project.org/jira">Development</a></li>
</ul>
</div>
</div>
<div id="content" class="cls">
<div class="related">
<h3>Navigation</h3>
<ul>
<li><a href="/">Doctrine Homepage</a> &raquo;</li>
<li><a href="../index.html">Doctrine MongoDB ODM 1.1.5 documentation</a> &raquo;</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" >
<div class="section" id="geospatial-queries">
<h1>Geospatial Queries<a class="headerlink" href="#geospatial-queries" title="Permalink to this headline"></a></h1>
<p>You can execute some special queries when using geospatial indexes
like checking for documents within a rectangle or circle.</p>
<div class="section" id="mapping">
<h2>Mapping<a class="headerlink" href="#mapping" title="Permalink to this headline"></a></h2>
<p>First, setup some documents like the following:</p>
<div class="configuration-block">
<ul class="simple">
<li><em>PHP</em><div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="sd">/**</span>
<span class="sd"> * @Document</span>
<span class="sd"> * @Index(keys={&quot;coordinates&quot;=&quot;2d&quot;})</span>
<span class="sd"> */</span>
<span class="k">class</span> <span class="nc">City</span>
<span class="p">{</span>
<span class="sd">/** @Id */</span>
<span class="k">public</span> <span class="nv">$id</span><span class="p">;</span>
<span class="sd">/** @Field(type=&quot;string&quot;) */</span>
<span class="k">public</span> <span class="nv">$name</span><span class="p">;</span>
<span class="sd">/** @EmbedOne(targetDocument=&quot;Coordinates&quot;) */</span>
<span class="k">public</span> <span class="nv">$coordinates</span><span class="p">;</span>
<span class="sd">/** @Distance */</span>
<span class="k">public</span> <span class="nv">$distance</span><span class="p">;</span>
<span class="p">}</span>
<span class="sd">/** @EmbeddedDocument */</span>
<span class="k">class</span> <span class="nc">Coordinates</span>
<span class="p">{</span>
<span class="sd">/** @Field(type=&quot;float&quot;) */</span>
<span class="k">public</span> <span class="nv">$x</span><span class="p">;</span>
<span class="sd">/** @Field(type=&quot;float&quot;) */</span>
<span class="k">public</span> <span class="nv">$y</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
</li>
<li><em>XML</em><div class="highlight-xml"><div class="highlight"><pre><span class="nt">&lt;indexes&gt;</span>
<span class="nt">&lt;index&gt;</span>
<span class="nt">&lt;key</span> <span class="na">name=</span><span class="s">&quot;coordinates&quot;</span> <span class="na">order=</span><span class="s">&quot;2d&quot;</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/index&gt;</span>
<span class="nt">&lt;/indexes&gt;</span>
</pre></div>
</div>
</li>
<li><em>YAML</em><div class="highlight-yaml"><div class="highlight"><pre><span class="l l-Scalar l-Scalar-Plain">indexes</span><span class="p p-Indicator">:</span>
<span class="l l-Scalar l-Scalar-Plain">coordinates</span><span class="p p-Indicator">:</span>
<span class="l l-Scalar l-Scalar-Plain">keys</span><span class="p p-Indicator">:</span>
<span class="l l-Scalar l-Scalar-Plain">coordinates</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">2d</span>
</pre></div>
</div>
</li>
</ul>
</div>
</div>
<div class="section" id="near-query">
<h2>Near Query<a class="headerlink" href="#near-query" title="Permalink to this headline"></a></h2>
<p>Now you can execute queries against these documents like the
following. Check for the 10 nearest cities to a given longitude
and latitude with the <code class="docutils literal"><span class="pre">near($longitude,</span> <span class="pre">$latitude)</span></code> method:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="nv">$cities</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">dm</span><span class="o">-&gt;</span><span class="na">createQuery</span><span class="p">(</span><span class="s1">&#39;City&#39;</span><span class="p">)</span>
<span class="o">-&gt;</span><span class="na">field</span><span class="p">(</span><span class="s1">&#39;coordinates&#39;</span><span class="p">)</span><span class="o">-&gt;</span><span class="na">near</span><span class="p">(</span><span class="o">-</span><span class="mi">120</span><span class="p">,</span> <span class="mi">40</span><span class="p">)</span>
<span class="o">-&gt;</span><span class="na">execute</span><span class="p">();</span>
</pre></div>
</div>
</div>
<div class="section" id="geonear-command">
<span id="geonear"></span><h2>GeoNear Command<a class="headerlink" href="#geonear-command" title="Permalink to this headline"></a></h2>
<p>You can also execute the <a class="reference external" href="https://docs.mongodb.com/manual/reference/command/geoNear/">geoNear command</a> using the query builder's
<code class="docutils literal"><span class="pre">geoNear()</span></code> method. Additional builder methods can be used to set options for
this command (e.g. <code class="docutils literal"><span class="pre">distanceMultipler()</span></code>, <code class="docutils literal"><span class="pre">maxDistance()</span></code>, <code class="docutils literal"><span class="pre">spherical()</span></code>).
Unlike <code class="docutils literal"><span class="pre">near()</span></code>, which uses a query operator, <code class="docutils literal"><span class="pre">geoNear()</span></code> does not require
the location field to be specified in the builder, as MongoDB will use the
single geospatial index for the collection. Documents will be returned in order
of nearest to farthest.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="nv">$cities</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">dm</span><span class="o">-&gt;</span><span class="na">createQuery</span><span class="p">(</span><span class="s1">&#39;City&#39;</span><span class="p">)</span>
<span class="o">-&gt;</span><span class="na">geoNear</span><span class="p">(</span><span class="o">-</span><span class="mi">120</span><span class="p">,</span> <span class="mi">40</span><span class="p">)</span>
<span class="o">-&gt;</span><span class="na">spherical</span><span class="p">(</span><span class="k">true</span><span class="p">)</span>
<span class="c1">// Convert radians to kilometers (use 3963.192 for miles)</span>
<span class="o">-&gt;</span><span class="na">distanceMultiplier</span><span class="p">(</span><span class="mf">6378.137</span><span class="p">)</span>
<span class="o">-&gt;</span><span class="na">execute</span><span class="p">();</span>
</pre></div>
</div>
<p>If the model has a property mapped with <a class="reference internal" href="annotations-reference.html#annotation-distance"><span class="std std-ref">&#64;Distance</span></a>,
that field will be set with the calculated distance between the document and the
query coordinates.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="k">foreach</span> <span class="p">(</span><span class="nv">$cities</span> <span class="k">as</span> <span class="nv">$city</span><span class="p">)</span> <span class="p">{</span>
<span class="nb">printf</span><span class="p">(</span><span class="s2">&quot;%s is %f kilometers away.</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">,</span> <span class="nv">$city</span><span class="o">-&gt;</span><span class="na">name</span><span class="p">,</span> <span class="nv">$city</span><span class="o">-&gt;</span><span class="na">distance</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="within-box">
<h2>Within Box<a class="headerlink" href="#within-box" title="Permalink to this headline"></a></h2>
<p>You can also query for cities within a given rectangle using the
<code class="docutils literal"><span class="pre">withinBox($x1,</span> <span class="pre">$y1,</span> <span class="pre">$x2,</span> <span class="pre">$y2)</span></code> method:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="nv">$cities</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">dm</span><span class="o">-&gt;</span><span class="na">createQuery</span><span class="p">(</span><span class="s1">&#39;City&#39;</span><span class="p">)</span>
<span class="o">-&gt;</span><span class="na">field</span><span class="p">(</span><span class="s1">&#39;coordinates&#39;</span><span class="p">)</span><span class="o">-&gt;</span><span class="na">withinBox</span><span class="p">(</span><span class="mi">41</span><span class="p">,</span> <span class="mi">41</span><span class="p">,</span> <span class="mi">72</span><span class="p">,</span> <span class="mi">72</span><span class="p">)</span>
<span class="o">-&gt;</span><span class="na">execute</span><span class="p">();</span>
</pre></div>
</div>
</div>
<div class="section" id="within-center">
<h2>Within Center<a class="headerlink" href="#within-center" title="Permalink to this headline"></a></h2>
<p>In addition to boxes you can check for cities within a circle using
the <code class="docutils literal"><span class="pre">withinCenter($x,</span> <span class="pre">$y,</span> <span class="pre">$radius)</span></code> method:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="nv">$cities</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">dm</span><span class="o">-&gt;</span><span class="na">createQuery</span><span class="p">(</span><span class="s1">&#39;City&#39;</span><span class="p">)</span>
<span class="o">-&gt;</span><span class="na">field</span><span class="p">(</span><span class="s1">&#39;coordinates&#39;</span><span class="p">)</span><span class="o">-&gt;</span><span class="na">withinCenter</span><span class="p">(</span><span class="mi">50</span><span class="p">,</span> <span class="mi">50</span><span class="p">,</span> <span class="mi">20</span><span class="p">)</span>
<span class="o">-&gt;</span><span class="na">execute</span><span class="p">();</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<div id="searchbox" style="">
<h3>Search</h3>
<form class="search" action="http://readthedocs.org/search/project/" method="get">
<input type="text" name="q" size="18">
<input type="submit" value="Go">
<input type="hidden" name="selected_facets" value="project:">
</form>
</div>
<h3><a href="../index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Geospatial Queries</a><ul>
<li><a class="reference internal" href="#mapping">Mapping</a></li>
<li><a class="reference internal" href="#near-query">Near Query</a></li>
<li><a class="reference internal" href="#geonear-command">GeoNear Command</a></li>
<li><a class="reference internal" href="#within-box">Within Box</a></li>
<li><a class="reference internal" href="#within-center">Within Center</a></li>
</ul>
</li>
</ul>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/reference/geospatial-queries.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
&copy; Copyright 2013, Doctrine Project Team.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.6.2.
<br/>
<a target="_BLANK" href="http://www.servergrove.com"><img src="http://www.doctrine-project.org/_static/servergrove.jpg" /></a> <br/><br/>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick" />
<input type="hidden" name="hosted_button_id" value="BAE2E3XANQ77Y" />
<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!" />
<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1" />
</form>
</div>
</div>
<div id="bot-rcnr">
<div class="tl"><!-- corner --></div>
</div>
</div>
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-288343-7";
urchinTracker();
</script>
<a class="githublink" href="http://github.com/doctrine"><img src="https://s3.amazonaws.com/github/ribbons/forkme_right_orange_ff7600.png" alt="Fork me on GitHub"></a>
</body>
</html>