'Generate a Query from a URL boundary box query; supports multiple
bboxes, but optimised for the usual case of just 1
@param resource: the resource
@param get_vars: the URL GET vars'

@staticmethod
def parse_bbox_query(resource, get_vars):

    tablenames = ('gis_location', 'gis_feature_query', 'gis_layer_shapefile')
    POLYGON = 'POLYGON((%s    %s,    %s    %s,    %s    %s,    %s    %s,    %s    %s))'
    query = None
    joins = {}
    if get_vars:
        table = resource.table
        tablename = resource.tablename
        fields = table.fields
        introspect = (tablename not in tablenames)
        for (k, v) in get_vars.items():
            if (k[:4] == 'bbox'):
                if (type(v) is list):
                    v = v[(-1)]
                try:
                    (minLon, minLat, maxLon, maxLat) = v.split(',')
                except ValueError:
                    continue
                field = None
                rfield = None
                alias = False
                if (k.find('.') != (-1)):
                    fname = k.split('.')[1]
                    if (fname not in fields):
                        continue
                    field = table[fname]
                    if ((query is not None) or ('bbox' in get_vars)):
                        alias = True
                elif introspect:
                    context = resource.get_config('context')
                    if (context and ('location' in context)):
                        try:
                            rfield = resource.resolve_selector('(location)$lat')
                        except (SyntaxError, AttributeError):
                            rfield = None
                        else:
                            if ((not rfield.field) or (rfield.tname != 'gis_location')):
                                rfield = None
                    if (rfield is None):
                        fname = None
                        for f in fields:
                            ftype = str(table[f].type)
                            if (ftype[:22] == 'reference    gis_location'):
                                fname = f
                                break
                            elif ((not fname) and (ftype[:18] == 'reference    org_site')):
                                fname = f
                        field = (table[fname] if fname else None)
                    if ((not rfield) and (not field)):
                        continue
                gtable = current.s3db.gis_location
                if rfield:
                    joins.update(rfield.left)
                elif field:
                    fname = field.name
                    gtable = current.s3db.gis_location
                    if alias:
                        gtable = gtable.with_alias(('gis_%s_location' % fname))
                    tname = str(gtable)
                    ftype = str(field.type)
                    if (ftype == 'reference    gis_location'):
                        joins[tname] = [gtable.on((gtable.id == field))]
                    elif (ftype == 'reference    org_site'):
                        stable = current.s3db.org_site
                        if alias:
                            stable = stable.with_alias(('org_%s_site' % fname))
                        joins[tname] = [stable.on((stable.site_id == field)), gtable.on((gtable.id == stable.location_id))]
                    elif introspect:
                        continue
                elif (tablename in ('gis_location', 'gis_feature_query')):
                    gtable = table
                elif (tablename == 'gis_layer_shapefile'):
                    gtable = resource.components.items()[0][1].table
                bbox_filter = None
                if current.deployment_settings.get_gis_spatialdb():
                    minLon = float(minLon)
                    maxLon = float(maxLon)
                    minLat = float(minLat)
                    maxLat = float(maxLat)
                    bbox = (POLYGON % (minLon, minLat, minLon, maxLat, maxLon, maxLat, maxLon, minLat, minLon, minLat))
                    try:
                        bbox_filter = gtable.the_geom.st_intersects(bbox)
                    except:
                        pass
                if (bbox_filter is None):
                    bbox_filter = ((((gtable.lon > float(minLon)) & (gtable.lon < float(maxLon))) & (gtable.lat > float(minLat))) & (gtable.lat < float(maxLat)))
                if (query is None):
                    query = bbox_filter
                else:
                    query = (query & bbox_filter)
    return (query, joins)
