The GeoCouch extension uses the standard Couchbase indexing system to build a two-dimensional index from the point data within the bucket. The format of the index information is based on the GeoJSON specification.
To create a geospatial index, use the
emit() function to output a GeoJSON Point
value containing the coordinates of the point you are
describing. For example, the following function will create a
geospatial index on the earlier spatial record example.
function(doc, meta) { if (doc.loc) { emit( { type: "Point", coordinates: doc.loc, }, [meta.id, doc.loc]); } }
The key in the spatial view index can be any valid GeoJSON geometry value, including points, multipoints, linestrings, polygons and geometry collections.
The view map() function should be placed
into a design document using the spatial
prefix to indicate the nature of the view definition. For
example, the following design document includes the above
function as the view points
{ "spatial" : { "points" : "function(doc, meta) { if (doc.loc) { emit({ type: \"Point\", coordinates: [doc.loc[0], doc.loc[1]]}, [meta.id, doc.loc]);}}", } }
To execute the geospatial query you use the design document
format using the embedded spatial indexing. For example, if the
design document is called main within the
bucket places, the URL will be
http://localhost:8092/places/_design/main/_spatial/points.
Spatial queries include support for a number of additional arguments to the view request. The full list is provided in the following summary table.
| Method | GET /bucket/_design/design-doc/_spatial/spatial-name |
| Request Data | None |
| Response Data | JSON of the documents returned by the view |
| Authentication Required | no |
| Query Arguments | |
bbox | Specify the bounding box for a spatial query |
| Parameters: string; optional | |
limit | Limit the number of the returned documents to the specified number |
| Parameters: numeric; optional | |
skip | Skip this number of records before starting to return the results |
| Parameters: numeric; optional | |
stale | Allow the results from a stale view to be used |
| Parameters: string; optional | |
| Supported Values | |
false: Force update of the view index before results
are returned | |
ok: Allow stale views | |
update_after: Allow stale view, update view after
access |
If you do not supply a bounding box, the full dataset is returned. When querying a spatial index you can use the bounding box to specify the boundaries of the query lookup on a given value. The specification should be in the form of a comma-separated list of the coordinates to use during the query.
These coordinates are specified using the GeoJSON format, so the first two numbers are the lower left coordinates, and the last two numbers are the upper right coordinates.
For example, using the above design document:
GET http://localhost:8092/places/_design/main/_spatial/points?bbox=0,0,180,90 Content-Type: application/json
Returns the following information:
{ "update_seq" : 3, "rows" : [ { "value" : [ "oakland", [ 10.898333, 48.371667 ] ], "bbox" : [ 10.898333, 48.371667, 10.898333, 48.371667 ], "id" : "augsburg" } ] }
Note that the return data includes the value specified in the
design document view function, and the bounding box of each
individual matching document. If the spatial index includes the
bbox bounding box property as part of the
specification, then this information will be output in place of
the automatically calculated version.