Skip to content

Commit

Permalink
Support other geopoint formats (#144)
Browse files Browse the repository at this point in the history
Support Geopoint as
GeoJSON
String
Array of lon and lat
WKT and Geohash will be supported in later release.

Signed-off-by: Vijayan Balasubramanian <[email protected]>
  • Loading branch information
VijayanB authored Dec 28, 2022
1 parent 0cccef5 commit 9651f76
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 12 deletions.
30 changes: 18 additions & 12 deletions maps_dashboards/public/model/documentLayerFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { Map as Maplibre, Popup, MapGeoJSONFeature } from 'maplibre-gl';
import { createPopup, getPopupLngLat } from '../components/tooltip/create_tooltip';
import { DocumentLayerSpecification } from './mapLayerType';
import { convertGeoPointToGeoJSON, isGeoJSON } from '../utils/geo_formater';

interface MaplibreRef {
current: Maplibre | null;
Expand Down Expand Up @@ -60,16 +61,18 @@ const getGeoFieldName = (layerConfig: DocumentLayerSpecification) => {
};

const buildGeometry = (fieldType: string, location: any) => {
if (fieldType === 'geo_point') {
if (isGeoJSON(location)) {
return {
type: 'Point',
coordinates: [location.lon, location.lat],
type: openSearchGeoJSONMap.get(location.type?.toLowerCase()),
coordinates: location.coordinates,
};
}
return {
type: openSearchGeoJSONMap.get(location.type),
coordinates: location.coordinates,
};
if (fieldType === 'geo_point') {
// convert other supported formats to GeoJSON
return convertGeoPointToGeoJSON(location);
}
// We don't support non-geo-json format for geo_shape yet
return undefined;
};

const buildProperties = (document: any, fields: string[]) => {
Expand All @@ -92,11 +95,14 @@ const getLayerSource = (data: any, layerConfig: DocumentLayerSpecification) => {
const featureList: any = [];
data.forEach((item: any) => {
const geoFieldValue = getFieldValue(item._source, geoFieldName);
const feature = {
geometry: buildGeometry(geoFieldType, geoFieldValue),
properties: buildProperties(item, layerConfig.source.tooltipFields),
};
featureList.push(feature);
const geometry = buildGeometry(geoFieldType, geoFieldValue);
if (geometry) {
const feature = {
geometry,
properties: buildProperties(item, layerConfig.source.tooltipFields),
};
featureList.push(feature);
}
});
return {
type: 'FeatureCollection',
Expand Down
55 changes: 55 additions & 0 deletions maps_dashboards/public/utils/geo_formater.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

const geoJSONTypes: string[] = [
'point',
'linestring',
'polygon',
'multipoint',
'multilinestring',
'multipolygon',
'geometrycollection',
];

export function isGeoJSON(value: { type: any; coordinates: any }) {
if (!value) return false;
if (!value.type || !value.coordinates) {
return false;
}
const geoJSONType = value.type;
if (geoJSONTypes.includes(geoJSONType.toLowerCase())) {
return true;
}
return false;
}

function buildGeoJSONOfTypePoint(lon: number, lat: number) {
return {
type: 'Point',
coordinates: [lon, lat],
};
}

export function convertGeoPointToGeoJSON(location: any) {
// An object with 'lat' and 'lon' properties
if (location?.lat && location?.lon) {
return buildGeoJSONOfTypePoint(location?.lon, location?.lat);
}
// Geopoint as an array && support either (lon/lat) or (lon/lat/z)
if (Array.isArray(location) && (location.length === 2 || location.length === 3)) {
return buildGeoJSONOfTypePoint(location[0], location[1]);
}

if (typeof location !== 'string') {
return undefined;
}
// Geopoint as a string && support either (lat,lon) or (lat, lon, z)
const values = location.trim().split(',');
if (values && (values.length === 2 || values.length === 3)) {
return buildGeoJSONOfTypePoint(parseFloat(values[1].trim()), parseFloat(values[0].trim()));
}
// TODO Geopoint as geohash & WKT Format
return undefined;
}

0 comments on commit 9651f76

Please sign in to comment.