function views_geojson_bbox_argument::query in Views GeoJSON 6
Same name and namespace in other branches
- 7 views/views_geojson_bbox_argument.inc \views_geojson_bbox_argument::query()
File
- views/views_geojson_bbox_argument.inc, line 90
Class
- views_geojson_bbox_argument
- Custom filter to return only points within a provided bounding box.
Code
function query() {
$this
->ensure_my_table();
if (!empty($this->argument) && $this->argument !== TRUE) {
$bbox = $this
->_explode_bbox_coords($this->argument);
}
else {
$bbox = $this
->_get_bbox_coords();
}
if (!isset($bbox['left']) || !isset($bbox['bottom']) || !isset($bbox['right']) || !isset($bbox['top'])) {
return;
}
if (isset($this->view->display_handler->display->display_options['style_options'])) {
$data_source = $this->view->display_handler->display->display_options['style_options']['data_source'];
}
else {
$data_source = $this->view->display['default']->display_options['style_options']['data_source'];
}
if ($data_source['value'] == 'latlon') {
$lat_field_obj = $this->view->field[$data_source['latitude']];
$lon_field_obj = $this->view->field[$data_source['longitude']];
$lat_field_table = $lat_field_obj->table;
$lon_field_table = $lon_field_obj->table;
}
elseif ($data_source['value'] == 'geofield') {
$lat_field_obj = $lon_field_obj = $this->view->field[$data_source['geofield']];
$lat_field_table = $lon_field_table = $lat_field_obj->table;
}
else {
return;
}
if (isset($lat_field_obj->field_info) && $lat_field_obj->field_info['type'] == 'geofield') {
$lat_field_name = $lat_field_obj->field_info['storage']['details']['sql']['FIELD_LOAD_CURRENT'][$lat_field_table]['lat'];
}
else {
$lat_field_name = $lat_field_obj->field;
}
if (isset($lon_field_obj->field_info) && $lon_field_obj->field_info['type'] == 'geofield') {
$lon_field_name = $lon_field_obj->field_info['storage']['details']['sql']['FIELD_LOAD_CURRENT'][$lon_field_table]['lon'];
}
else {
$lon_field_name = $lon_field_obj->field;
}
$this->query
->add_table($lat_field_table);
if ($lon_field_table != $lat_field_table) {
$this->query
->add_table($lon_field_table);
}
$left = $bbox['left'];
$right = $bbox['right'];
if ($right - $left < 360) {
$group = $this->query
->set_where_group('AND');
if ($left > -180) {
$this->query
->add_where($group, "{$lon_field_table}.{$lon_field_name}", $left, '>=');
}
else {
$this->query
->set_where_group('OR', $group);
$left = -1 * ($left % 180);
$this->query
->add_where($group, "{$lon_field_table}.{$lon_field_name}", array(
$left,
0,
), 'BETWEEN');
}
if ($right < 180) {
$this->query
->add_where($group, "{$lon_field_table}.{$lon_field_name}", $right, '<=');
}
else {
$this->query
->set_where_group('OR', $group);
$right = -1 * ($right % 180);
$this->query
->add_where($group, "{$lon_field_table}.{$lon_field_name}", array(
$right,
0,
), 'BETWEEN');
}
}
$bottom = $bbox['bottom'];
$top = $bbox['top'];
$group = $this->query
->set_where_group('AND');
$this->query
->add_where($group, "{$lat_field_table}.{$lat_field_name}", $bottom, '>=');
$this->query
->add_where($group, "{$lat_field_table}.{$lat_field_name}", $top, '<=');
}