Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Maps] geotile_grid aggregation #29477

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/ui/public/agg_types/buckets/geo_tile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import _ from 'lodash';
import { BucketAggType } from './_bucket_agg_type';
import { i18n } from '@kbn/i18n';

export const geoTileBucketAgg = new BucketAggType({
name: 'geotile_grid',
title: i18n.translate('common.ui.aggTypes.buckets.geotileGridTitle', {
defaultMessage: 'Geotile',
}),
params: [
{
name: 'field',
type: 'field',
filterFieldTypes: 'geo_point'
},
{
name: 'useGeocentroid',
default: true,
write: _.noop
},
{
name: 'precision',
default: 0,
}
],
getRequestAggs: function (agg) {
const aggs = [];
const params = agg.params;

aggs.push(agg);

if (params.useGeocentroid) {
aggs.push(agg.aggConfigs.createAggConfig({
type: 'geo_centroid',
enabled: true,
params: {
field: agg.getField()
}
}, { addToAggConfigs: false }));
}

return aggs;
}
});
4 changes: 3 additions & 1 deletion src/ui/public/agg_types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { filterBucketAgg } from './buckets/filter';
import { filtersBucketAgg } from './buckets/filters';
import { significantTermsBucketAgg } from './buckets/significant_terms';
import { geoHashBucketAgg } from './buckets/geo_hash';
import { geoTileBucketAgg } from './buckets/geo_tile';
import { bucketSumMetricAgg } from './metrics/bucket_sum';
import { bucketAvgMetricAgg } from './metrics/bucket_avg';
import { bucketMinMetricAgg } from './metrics/bucket_min';
Expand Down Expand Up @@ -86,7 +87,8 @@ const aggs = {
filterBucketAgg,
filtersBucketAgg,
significantTermsBucketAgg,
geoHashBucketAgg
geoHashBucketAgg,
geoTileBucketAgg,
]
};

Expand Down
17 changes: 0 additions & 17 deletions x-pack/plugins/gis/public/elasticsearch_geo_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,23 +99,6 @@ export function geoPointToGeometry(value) {
);
}


export function makeGeohashGridPolygon(geohashGridFeature) {
const esBbox = geohashGridFeature.properties.geohash_meta.rectangle;
return {
type: 'Polygon',
coordinates: [
[
[esBbox[0][1], esBbox[0][0]],
[esBbox[1][1], esBbox[1][0]],
[esBbox[2][1], esBbox[2][0]],
[esBbox[3][1], esBbox[3][0]],
[esBbox[0][1], esBbox[0][0]],
]
]
};
}

export function geoShapeToGeometry(value) {
if (!value) {
return [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,116 +4,109 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { decodeGeoHash } from 'ui/utils/decode_geo_hash';
import { gridDimensions } from 'ui/vis/map/grid_dimensions';
import { RENDER_AS } from './render_as';
import { getTileBoundingBox, getTileCenter } from './geo_tile_utils';

/*
* Fork of ui/public/vis/map/convert_to_geojson.js that supports multiple metrics
*/
export function convertToGeoJson(tabifiedResponse) {

let features;
const min = Infinity;
const max = -Infinity;
let geoAgg;

if (tabifiedResponse && tabifiedResponse.rows) {

const table = tabifiedResponse;
const geohashColumn = table.columns.find(column => column.aggConfig.type.dslName === 'geohash_grid');

if (!geohashColumn) {
features = [];
} else {

geoAgg = geohashColumn.aggConfig;

const metricColumns = table.columns.filter(column => {
return column.aggConfig.type.type === 'metrics'
&& column.aggConfig.type.dslName !== 'geo_centroid';
});
const geocentroidColumn = table.columns.find(column => column.aggConfig.type.dslName === 'geo_centroid');

features = table.rows.map(row => {

const geohash = row[geohashColumn.id];
if (!geohash) return false;
const geohashLocation = decodeGeoHash(geohash);

let pointCoordinates;
if (geocentroidColumn) {
const location = row[geocentroidColumn.id];
pointCoordinates = [location.lon, location.lat];
} else {
pointCoordinates = [geohashLocation.longitude[2], geohashLocation.latitude[2]];
}

const rectangle = [
[geohashLocation.latitude[0], geohashLocation.longitude[0]],
[geohashLocation.latitude[0], geohashLocation.longitude[1]],
[geohashLocation.latitude[1], geohashLocation.longitude[1]],
[geohashLocation.latitude[1], geohashLocation.longitude[0]],
];

const centerLatLng = [
geohashLocation.latitude[2],
geohashLocation.longitude[2]
];

if (geoAgg.params.useGeocentroid) {
// see https://github.com/elastic/elasticsearch/issues/24694 for why clampGrid is used
pointCoordinates[0] = clampGrid(pointCoordinates[0], geohashLocation.longitude[0], geohashLocation.longitude[1]);
pointCoordinates[1] = clampGrid(pointCoordinates[1], geohashLocation.latitude[0], geohashLocation.latitude[1]);
}

const metrics = {};
metricColumns.forEach(metricColumn => {
metrics[metricColumn.aggConfig.id] = row[metricColumn.id];
});
//const value = row[metricColumn.id];
//min = Math.min(min, value);
//max = Math.max(max, value);

return {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: pointCoordinates
},
properties: {
geohash: geohash,
geohash_meta: {
center: centerLatLng,
rectangle: rectangle
},
...metrics
}
};


}).filter(row => row);
const EMPTY_FEATURE_COLLECTION = {
type: 'FeatureCollection',
features: []
};

}
export function convertToGeoJson({ table, renderAs }) {

} else {
features = [];
if (!table || !table.rows) {
return EMPTY_FEATURE_COLLECTION;
}

const featureCollection = {
type: 'FeatureCollection',
features: features
};
const geoGridColumn = table.columns.find(column => column.aggConfig.type.dslName === 'geotile_grid');

if (!geoGridColumn) {
return {
type: 'FeatureCollection',
features: []
};
}

const metricColumns = table.columns.filter(column => {
return column.aggConfig.type.type === 'metrics'
&& column.aggConfig.type.dslName !== 'geo_centroid';
});
const geocentroidColumn = table.columns.find(column => column.aggConfig.type.dslName === 'geo_centroid');

const features = [];
table.rows.forEach(row => {
const gridKey = row[geoGridColumn.id];
if (!gridKey) {
return;
}

const properties = {};
metricColumns.forEach(metricColumn => {
properties[metricColumn.aggConfig.id] = row[metricColumn.id];
});

features.push({
type: 'Feature',
geometry: rowToGeometry({
row,
gridKey,
geocentroidColumn,
renderAs,
}),
properties
});
});

return {
featureCollection: featureCollection,
meta: {
min: min,
max: max,
geohashGridDimensionsAtEquator: geoAgg && gridDimensions(geoAgg.params.precision)
featureCollection: {
type: 'FeatureCollection',
features: features
}
};
}

function rowToGeometry({
row,
gridKey,
geocentroidColumn,
renderAs,
}) {
const { top, bottom, right, left } = getTileBoundingBox(gridKey);

if (renderAs === RENDER_AS.GRID) {
return {
type: 'Polygon',
coordinates: [
[
[right, top],
[left, top],
[left, bottom],
[right, bottom],
[right, top],
]
]
};
}

let pointCoordinates;
if (geocentroidColumn) {
const { lat, lon } = row[geocentroidColumn.id];
// see https://github.com/elastic/elasticsearch/issues/24694 for why clampGrid is used
pointCoordinates = [
clampGrid(lon, left, right),
clampGrid(lat, bottom, top)
];
} else {
const { lat, lon } = getTileCenter(gridKey);
pointCoordinates = [lon, lat];
}

return {
type: 'Point',
coordinates: pointCoordinates
};
}

function clampGrid(val, min, max) {
if (val > max) val = max;
else if (val < min) val = min;
Expand Down
Loading