Spartial Query Node JS

In the Couchbase View, is this correct for the spartial index?

function (doc) {
emit({ “type”: “Point”, “coordinates”: [doc.longitude, doc.latitude]}, doc);
}

When sending the bbox coordinates to the view. What are the 4 values? Bottom Left (Lon,Lat), Top Right (Lon, Lat)?

The documentation does not define it correctly.

Thank you in advance,
Marty

Hello Marty,

there are multiple ways to emit the data into a Spatial View. The first one would be to pass GeoJSON:

function (doc) {
  if ( typeof doc.geo !== 'undefined’ ) {

    var geojson = {};
        geojson.type = "Point";
        geojson.coordinates = [ doc.geo.lon, doc.geo.lat ];
   
    emit(geojson, doc.name);
  }
}

The other way would be to emit a flat array:

function (doc) {
  if ( typeof doc.geo !== 'undefined’ ) {
    
    var geojson = {};
    geojson.coordinates = [ doc.geo.lon, doc.geo.lat ];

    emit(geojson.coordinates, doc.name);
  }
}

The following uses the min-max array and is the most generic way to do it:

function (doc) {
  if ( typeof doc.geo !== 'undefined’ ) {
    
    // [x_min, x_max], [y_min, y_max]
    // x_min == x_max && y_min == y_may  [x_min, y_min] 
   
   emit([ [doc.geo.lon, doc.geo.lon ], 
                 [doc.geo.lat, doc.geo.lat] ], 
                 doc.name);
  }
}

In case of a Point is x_min and x_max falling into the same point, which you used in second case. So the second case is a shorter version of the third one. As you can see we define these regions by using Min-Max arrays. So one dimension is given by a range. This should help you to understand the way to query it more.

So the start_range and end_range parameters define a point in the n-dimensional space. If you indexed only on Geo-Data then you have two dimensions (x,y) which means that you provide the start vector as a 2 dimensional one and the end_range vector, e.g.:smile:

start_range = [x_min, y_min]
end_range = [ x_max, y_max]

It’s easy to see that this defines a box. So the box with has one side which is defined by the vector [x_min, x_max] and one other one which is defined by [y_min, y_max].

If you would index on more dimensions then it would be:

start_range = [x_min, y_min, z_min]
end_range = [ x_max, y_max, z_max]

… and so on.

The following shows you how to perform a query with an open end (even if not infinite because you indexed with finite boundaries) range for the 3rd dimension:

http://192.168.7.160:8092/beer-sample/_design/dev_spatials/_spatial/by_region?stale=false&start_range=[-180,0,0]&end_range=[180,90,null]

Hope this helps.

Regards,
David