Implementation of shape_query and bb_query functions