<slide>
<title>Querying our database</title>

<div effect="fade-out">
<example><![CDATA[<?php
header('Content-type: text/plain');
$m = new MongoClient( 'mongodb://localhost:27017' );
$c = $m->demo->poi;
$c->ensureIndex( array( 'loc' => '2d' ) );
]]></example>
</div>
<div effect="fade-in-out">
<example><![CDATA[
$wantedD = isset($_GET['d']) ? $_GET['d']: 1;
$query = array( 'tags_indexed.k' => array( '$ne' => 'landuse' ) );

$s = $d->command(
    array(
        'geoNear' => 'poi',
        'spherical' => true,
        'near' => array(
            (float) $_GET['lon'],
            (float) $_GET['lat']
        ),
        'num' => 1000,
        'maxDistance' => $wantedD / 6371.01,
        'query' => $query,
    )
);

]]></example>
</div>
<div effect="fade-in-out">
<example><![CDATA[
foreach( $s['results'] as $res)
{
    $o = $res['obj'];
    if (!isset($o['tags']['name'])) {
        continue;
    }
    $ret = array(
        'type' => 'Feature',
        'properties' => array( 'name' => '?', 'popupContent' => '' ),
    );
    if (isset( $o['tags']['name'] ) )
    {
        $ret['properties'] = array(
            'name' => $o['tags']['name'],
            'popupContent' => "<b>{$o['tags']['name']}</b>",
        );
    }
    if ( isset( $o['tags'] ) )
    {
        foreach ( $o['tags'] as $tagName => $value )
        {
            $ret['properties']['popupContent'] .= "<br/>{$tagName}: {$value}\n";
        }
    }
    if ($o['type'] == 1)
    {
        $ret['geometry'] = array(
            'type' => "Point",
            'coordinates' => $o['loc']
        );
    }
    if ($o['type'] == 2)
    {
        if ($o['loc'][0] == $o['loc'][count($o['loc']) - 1])
        {
            $ret['geometry'] = array(
                'type' => "Polygon",
                'coordinates' => array($o['loc']),
            );
        }
        else
        {
            $ret['geometry'] = array(
                'type' => "LineString",
                'coordinates' => $o['loc'],
            );
        }
    }
    $rets[] = $ret;
}
echo json_encode( $rets, JSON_PRETTY_PRINT );
]]></example>
</div>
<div effect="fade-in">
<example><![CDATA[
{
  "type": "Feature",
  "properties": {
    "name": "La Ballerina",
    "popupContent": "<b>La Ballerina<\/b><br\/>addr:housenumber: 7-8\n<br\/>addr:street: Bow street\n<br\/>amenity: restaurant\n<br\/>cuisine: italian\n<br\/>phone: +442073796659\n"
  },
  "geometry": {
    "type": "Point",
    "coordinates": [
      -0.122713,
      51.513472
    ]
  }
},
]]></example>
</div>
</slide>
