The Couchbase Search service is introducing higher dimensional geospatial indexing and querying capabilities with the Couchbase Server release 7.1.2.

Until this new feature, the Search service was limited to indexing single dimensional geopoint/location fields. Queries were limited to bounding queries like point-distance, bounded-rectangle and bounded-polygon against the indexed geopoint field.  But we know that geopoints alone won’t be effective in representing the various spatial details of real life shapes or structures.

Support of such higher dimensional spatial structures enables users to approximate a large variety of real life shapes like postal or jurisdictional region boundaries, routes for delivery vehicles or an airline carrier or the boundaries of various water bodies like a river, lake, stream etc.

Search service will now support the GeoJSON spatial data formats on spherical geodesics in this release.  This enable users to address use cases such as finding all the documents with any GeoJSON shapes that:

    • contain the query shape
    • reside within the query shape
    • intersect with the query shape

The supported GeoJSON shapes are:

In addition to the above shapes, Search also supports a couple additional custom shapes to make the spatial approximations easy for users.  The extra shapes supported are:

    • Circle
    • Envelope

The new GeoJSON types

Let’s have a quick glance over all these various shape types.

All simple geometries in GeoJSON consist of a type and a collection of coordinates.

Point 

The following specifies a GeoJSON Point field in a document:

A point is a single geographic coordinate, such as the location of a building or the current position given by any Geolocation API.

Note : The standard only supports a single way of specifying the coordinates like an array format of longitude followed by latitude. ie: [lng, lat].

Linestring

The following specifies a GeoJSON Linestring field in a document:

A linestring is defined by an array of two or more positions. By specifying only two points, the linestring will represent a straight line. Specifying more than two points creates an arbitrary path.

Polygon

The following specifies a GeoJSON Polygon field in a document:

A polygon is defined by a list of a list of points. The first and last points in each (outer) list must be the same (i.e., the polygon must be closed). And the exterior coordinates have to be in Counter Clockwise Order (CCW) in a polygon.

Polygons with holes are also supported. The holes have to follow Clockwise Order for the boundary vertices.

For Polygons with a single ring, the ring cannot self-intersect.

NOTE: The CCW order of vertices is strictly mandated for the geoshapes in the Couchbase Server release 7.1.2 and any violation of this requirement would result in unexpected search results.

MultiPoint

The following specifies a GeoJSON Multipoint field in a document:

MultiLineString

The following specifies a GeoJSON MultiLineString field in a document:

MultiPolygon

The following specifies a GeoJSON MultiPolygon field in a document:

GeometryCollection

The following specifies a GeoJSON GeometryCollection field in a document:

A GeometryCollection has a member with the name “geometries”.  The value of “geometries” is an array.  Each element of this array is a GeoJSON Geometry object.    It is possible for this array to be empty.

Unlike the other geometry types described above, a GeometryCollection can be a heterogeneous composition of smaller Geometry objects.  For example, a Geometry object in the shape of a lowercase roman “i” can be composed of one point and one LineString.

Nested  GeometryCollections are invalid.  

Circle

If the users wishes to cover a circular region over earth’s surface, then they could use this shape.

A  sample circular shape is as below.

Circle is specified over the center point coordinates along with the radius.

Example formats supported for radius are: 

“5in” , “5inch” , “7yd” , “7yards”,  “9ft” , “9feet”, “11km”, “11kilometers”, “3nm” “3nauticalmiles”, “13mm” , “13millimeters”,  “15cm”, “15centimeters”, “17mi”, “17miles” “19m” or “19meters”.

If the unit cannot be determined, the entire string is parsed and the unit of meters is assumed.

Envelope

Envelope type, which consists of coordinates for upper left and lower right points of the shape to represent a bounding rectangle in the format  [[minLon, maxLat], [maxLon, minLat]].

Indexing the spatial fields – new geoshape fieldtype

Search has introduced a field type named geoshape for representing the aforementioned spatial shapes.

If you have a field named “geometry” in the document which contains the geo shape information adhering to geoJSON format, then first you can include the geospatial field under the type mappings in the index definition like below.

Couchbase geospatial JSON indexing

Geospatial field indexing with JSON

Finally, click on the Create Index button and you are set for querying your geospatial data.

Note: The geoshape field type is capable of indexing any of the earlier mentioned spatial types including Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, GeometryCollection, Circle and Envelope.

Querying the spatial fields – new GeoShape Query

Search primarily supports three types of spatial querying capability across those heterogeneous types of geoshapes indexed.

Query Structure:

Details:

    • shapeDesc – Can be any of the 9 types like Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, GeometryCollection, Circle and Envelope.
    • relation – Can be any of the 3 types like intersects , contains and within.
Relation Result
INTERSECTS  Return all documents whose spatial field intersects the query geometry.
CONTAINS Return all documents whose spatial field contains the query geometry
WITHIN Return all documents whose spatial field is within the query geometry.

Let’s explore a few query samples. 

A point contains query returns all the matched documents with shapes that contain the given point in the query:

lineString intersects query returns all the matched documents with shapes that intersects with the linestring in the query:

 

Polygon withIn query returns all the matched documents with shapes that are residing completely within the area of the polygon in the query:

Additional Tips

The Search service uses geodesics or spherical geometry to support the advanced spatial features. That means the curvature of earth’s surface is taken into account while performing spatial filters. Please keep this in mind while observing any differences in the boundary alignment of the given shape.

These resources are available to help you understand and validate geometries in your test data:

    • Reference for the spherical mapper – gcmap
    • Reference for checking the geojson shapes – geojson.io
    • Reference for validating the vertex order or the correctness of GeoJSON – geojsonlint 

 

Author

Posted by Sreekanth Sivasankaran

Sreekanth Sivasankaran is a Principal Engineer/Senior Engineering Manager at Couchbase R&D. He leads the design and development of distributed and highly performant Search functionality. And he has over 17 years of product development experience in various domains like telecom, handsets, enterprise software, big data technologies, and distributed systems.

Leave a reply