geoFence and geoFenceCircle in aerospike not returning any match in result

The code of query is as follow -

geoWithin: {
                    bin: 'geoFence',
                    lat: parseFloat(payload.lat.toString()),
                    lng: parseFloat(payload.lng.toString()),
                },

There is latitude and longitude matching the above query in set but the same is not showing in the output.

Can anyone help with same ?

Hey @Saksham_Patro, if I’m understanding what you are trying to do, you have records with latitude and longitude that you want to return from a query that identifies them as existing within a geoFence, is that correct?

I believe you’re using the Node client, so I’ve put together a small example that does just that:

const Aerospike = require('aerospike');
const GeoJSON = Aerospike.GeoJSON;
const batchType = Aerospike.batchType;
const op = Aerospike.operations

// Define host configuration
const config = {hosts: '127.0.0.1:3000'};

const geoWrite = async () => {
    // Create a list of points, Lng | Lat
    let places = [
        {name: "Mount Diablo", point: new GeoJSON.Point(-122.276156, 37.7467434)},
        {name: "Golden Gate", point: new GeoJSON.Point(-122.4602678, 37.7620894)},
        {name: "Big Basin", point: new GeoJSON.Point(-122.2337089, 37.4992656)},
        {name: "Pinnacles", point: new GeoJSON.Point(-121.5499273, 36.5451009)},
        {name: "Muir Beach", point: new GeoJSON.Point(-122.5958625, 37.8664261)},
        {name: "Point Molate Beach", point: new GeoJSON.Point(-122.389274, 37.9636355)}
    ]

    let batchRecords = []
    // Write the records
    for(let i = 0; i < places.length; i++){
        batchRecords.push({
            type: batchType.BATCH_WRITE,
            key: new Aerospike.Key('sandbox', 'test', i + 1),
            ops: [
                op.write('name', places[i].name),
                op.write('point', places[i].point),
            ]
        })
    }

    // Connect to server
    let client = await Aerospike.connect(config);
    
    // Write the records
    await client.batchWrite(batchRecords);
    console.info("Records created");

    // Close the connection to the server
    client.close();
}

const geoQuery = async () => {
    // Create geo region,  Lng | Lat | Radius in meters
    let region  = new GeoJSON.Circle(-122.0988349, 37.4212823, 50000);
    
    // Connect to server
    let client = await Aerospike.connect(config);
    
    // Create index job
    let indexJob = await client.createGeo2DSphereIndex({
        ns: 'sandbox', // namespace
        set: 'test', // set name
        bin: 'point', // bin name
        index: 'geo_idx', // index name
    });

    // Wait for the task to complete
    await indexJob.wait()
    console.info('Index created');

    // Create the query
    let query = client.query('sandbox', 'test');

    // Set the query filter
    query.where(Aerospike.filter.geoWithinGeoJSONRegion('point', region));

    //Execute the query
    return new Promise(async (resolve, reject) => {
        await query.foreach(null,
            (record) => console.info("Place: %o\n", record.bins),
            (err) => reject(err),
            () => {client.close(); resolve();}
        );
    }).catch(err => console.log(err))
}

geoWrite().then(() => geoQuery())

The geoWrite() function writes a handful of records that have names and coordinates, and the geoQuery() function creates the geoIndex and queries the data with a circular region 100km wide (50km radius). You can try to run this in the sandbox as well.

Hope this helps!

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.