mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Geomap: applying multiple line builders should keep each segment (#46563)
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { ArrayVector, Field, FieldConfig, FieldType } from '@grafana/data';
|
import { ArrayVector, Field, FieldConfig, FieldType } from '@grafana/data';
|
||||||
import { getCenterPoint } from 'app/features/transformers/spatial/utils';
|
import { getCenterPoint } from 'app/features/transformers/spatial/utils';
|
||||||
import { Geometry, LineString, Point } from 'ol/geom';
|
import { Geometry, GeometryCollection, LineString, Point } from 'ol/geom';
|
||||||
import { fromLonLat } from 'ol/proj';
|
import { fromLonLat } from 'ol/proj';
|
||||||
import { Gazetteer } from '../gazetteer/gazetteer';
|
import { Gazetteer } from '../gazetteer/gazetteer';
|
||||||
import { decodeGeohash } from './geohash';
|
import { decodeGeohash } from './geohash';
|
||||||
@@ -50,6 +50,40 @@ export function getGeoFieldFromGazetteer(gaz: Gazetteer, field: Field<string>):
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function createGeometryCollection(
|
||||||
|
src: Field<Geometry | undefined>,
|
||||||
|
dest: Field<Geometry | undefined>
|
||||||
|
): Field<Geometry | undefined> {
|
||||||
|
const v0 = src.values.toArray();
|
||||||
|
const v1 = dest.values.toArray();
|
||||||
|
if (!v0 || !v1) {
|
||||||
|
throw 'missing src/dest';
|
||||||
|
}
|
||||||
|
if (v0.length !== v1.length) {
|
||||||
|
throw 'Source and destination field lengths do not match';
|
||||||
|
}
|
||||||
|
|
||||||
|
const count = src.values.length!;
|
||||||
|
const geo = new Array<Geometry | undefined>(count);
|
||||||
|
for (let i = 0; i < count; i++) {
|
||||||
|
const a = v0[i];
|
||||||
|
const b = v1[i];
|
||||||
|
if (a && b) {
|
||||||
|
geo[i] = new GeometryCollection([a, b]);
|
||||||
|
} else if (a) {
|
||||||
|
geo[i] = a;
|
||||||
|
} else if (b) {
|
||||||
|
geo[i] = b;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
name: 'Geometry',
|
||||||
|
type: FieldType.geo,
|
||||||
|
values: new ArrayVector(geo),
|
||||||
|
config: hiddenTooltipField,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export function createLineBetween(
|
export function createLineBetween(
|
||||||
src: Field<Geometry | undefined>,
|
src: Field<Geometry | undefined>,
|
||||||
dest: Field<Geometry | undefined>
|
dest: Field<Geometry | undefined>
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { ArrayVector, DataFrame, DataTransformerID, DataTransformerInfo, FieldType } from '@grafana/data';
|
import { ArrayVector, DataFrame, DataTransformerID, DataTransformerInfo, FieldType } from '@grafana/data';
|
||||||
import { createLineBetween } from 'app/features/geo/format/utils';
|
import { createGeometryCollection, createLineBetween } from 'app/features/geo/format/utils';
|
||||||
import { getGeometryField, getLocationMatchers } from 'app/features/geo/utils/location';
|
import { getGeometryField, getLocationMatchers } from 'app/features/geo/utils/location';
|
||||||
import { mergeMap, from } from 'rxjs';
|
import { mergeMap, from } from 'rxjs';
|
||||||
import { SpatialOperation, SpatialAction, SpatialTransformOptions } from './models.gen';
|
import { SpatialOperation, SpatialAction, SpatialTransformOptions } from './models.gen';
|
||||||
@@ -26,10 +26,17 @@ async function doSetGeometry(frames: DataFrame[], options: SpatialTransformOptio
|
|||||||
const src = getGeometryField(frame, location);
|
const src = getGeometryField(frame, location);
|
||||||
const target = getGeometryField(frame, targetLocation);
|
const target = getGeometryField(frame, targetLocation);
|
||||||
if (src.field && target.field) {
|
if (src.field && target.field) {
|
||||||
|
const fields = [...frame.fields];
|
||||||
const line = createLineBetween(src.field, target.field);
|
const line = createLineBetween(src.field, target.field);
|
||||||
|
const first = fields[0];
|
||||||
|
if (first.type === FieldType.geo && first !== src.field && first !== target.field) {
|
||||||
|
fields[0] = createGeometryCollection(first, line); //
|
||||||
|
} else {
|
||||||
|
fields.unshift(line);
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
...frame,
|
...frame,
|
||||||
fields: [line, ...frame.fields],
|
fields,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return frame;
|
return frame;
|
||||||
|
|||||||
Reference in New Issue
Block a user