Modifying geo-spatial parameters in the data model

Background

This article demonstrates how to set or modify geo-spatial parameters in the data model (origin, maxdist).

Details

Geospatial search constraints are controlled using the origin and maxdist parameters.

origin and maxdist parameters are populated before the pre-process hook script runs.

This means that they can not be modified in the inputParameterMap (or rawInputParameters) in any of the available hook scripts.

To modify these values you need to set them in the additionalParameters structure.

hook_pre_process.groovy
def q = transaction.question
def near = 50 // Default 'near' value (km)
def geoRadiusThresholdPercentage = 200 // Increase maxdist radius by a particular threshold
if (q.inputParameterMap["maxdist"] != null) {
    // Store original for visualisation
    transaction.response.customData["maxdist_original"]    = q.inputParameterMap["maxdist"]

    // Increase maxdist by threshold
    transaction.response.customData["maxdist_modified"]    = (q.inputParameterMap["maxdist"].toFloat() * (geoRadiusThresholdPercentage/100)).toString()

    // Increase maxdist before processing
    q.dynamicQueryProcessorOptions.add("-maxdist=" + transaction.response.customData["maxdist_modified"])
    q.additionalParameters["maxdist"] = transaction.response.customData["maxdist_modified"]
}
else {
    // Use 'near' distance instead
    // q.dynamicQueryProcessorOptions.add("-maxdist=" + near)
    // q.additionalParameters["maxdist"] = near
}
// Set default sort order to geospatial proximity if using map/geojson form and origin available
if (((q.inputParameterMap["form"] == "map") || (q.inputParameterMap["form"] == "geojson"))
        && (q.inputParameterMap["origin"] != null)) {
    q.dynamicQueryProcessorOptions.add("-sort=prox")
    q.inputParameterMap["sort"] = "prox"
}
hook_post_process.groovy
def q = transaction.question
if (q.inputParameterMap["maxdist"] != null) {
    // Restore maxdist to original value
    q.inputParameterMap["maxdist"] = transaction.response.customData["maxdist_original"]
}