-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
e5278a7
commit f5fdacc
Showing
35 changed files
with
869 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { Feature } from 'geojson'; | ||
import { GEO_JSON_TYPE, VECTOR_SHAPE_TYPE } from './constants'; | ||
|
||
export interface VectorShapeTypeCounts { | ||
[VECTOR_SHAPE_TYPE.POINT]: number; | ||
[VECTOR_SHAPE_TYPE.LINE]: number; | ||
[VECTOR_SHAPE_TYPE.POLYGON]: number; | ||
} | ||
|
||
export function countVectorShapeTypes(features: Feature[]): VectorShapeTypeCounts { | ||
const vectorShapeTypeCounts: VectorShapeTypeCounts = { | ||
[VECTOR_SHAPE_TYPE.POINT]: 0, | ||
[VECTOR_SHAPE_TYPE.LINE]: 0, | ||
[VECTOR_SHAPE_TYPE.POLYGON]: 0, | ||
}; | ||
|
||
for (let i = 0; i < features.length; i++) { | ||
const feature: Feature = features[i]; | ||
if ( | ||
feature.geometry.type === GEO_JSON_TYPE.POINT || | ||
feature.geometry.type === GEO_JSON_TYPE.MULTI_POINT | ||
) { | ||
vectorShapeTypeCounts[VECTOR_SHAPE_TYPE.POINT] += 1; | ||
} else if ( | ||
feature.geometry.type === GEO_JSON_TYPE.LINE_STRING || | ||
feature.geometry.type === GEO_JSON_TYPE.MULTI_LINE_STRING | ||
) { | ||
vectorShapeTypeCounts[VECTOR_SHAPE_TYPE.LINE] += 1; | ||
} else if ( | ||
feature.geometry.type === GEO_JSON_TYPE.POLYGON || | ||
feature.geometry.type === GEO_JSON_TYPE.MULTI_POLYGON | ||
) { | ||
vectorShapeTypeCounts[VECTOR_SHAPE_TYPE.POLYGON] += 1; | ||
} | ||
} | ||
|
||
return vectorShapeTypeCounts; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { Feature } from 'geojson'; | ||
import { CategoryFieldMeta } from './descriptor_types'; | ||
|
||
export function pluckCategoryFieldMeta( | ||
features: Feature[], | ||
name: string, | ||
size: number | ||
): CategoryFieldMeta | null { | ||
const counts = new Map(); | ||
for (let i = 0; i < features.length; i++) { | ||
const feature = features[i]; | ||
const term = feature.properties ? feature.properties[name] : undefined; | ||
// properties object may be sparse, so need to check if the field is effectively present | ||
if (typeof term !== undefined) { | ||
if (counts.has(term)) { | ||
counts.set(term, counts.get(term) + 1); | ||
} else { | ||
counts.set(term, 1); | ||
} | ||
} | ||
} | ||
|
||
return trimCategories(counts, size); | ||
} | ||
|
||
export function trimCategories(counts: Map<string, number>, size: number): CategoryFieldMeta { | ||
const ordered = []; | ||
for (const [key, value] of counts) { | ||
ordered.push({ key, count: value }); | ||
} | ||
|
||
ordered.sort((a, b) => { | ||
return b.count - a.count; | ||
}); | ||
const truncated = ordered.slice(0, size); | ||
return { | ||
categories: truncated, | ||
} as CategoryFieldMeta; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { Feature } from 'geojson'; | ||
import { RangeFieldMeta } from './descriptor_types'; | ||
|
||
export function pluckRangeFieldMeta( | ||
features: Feature[], | ||
name: string, | ||
parseValue: (rawValue: unknown) => number | ||
): RangeFieldMeta | null { | ||
let min = Infinity; | ||
let max = -Infinity; | ||
for (let i = 0; i < features.length; i++) { | ||
const feature = features[i]; | ||
const newValue = feature.properties ? parseValue(feature.properties[name]) : NaN; | ||
if (!isNaN(newValue)) { | ||
min = Math.min(min, newValue); | ||
max = Math.max(max, newValue); | ||
} | ||
} | ||
|
||
return min === Infinity || max === -Infinity | ||
? null | ||
: ({ | ||
min, | ||
max, | ||
delta: max - min, | ||
} as RangeFieldMeta); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.