-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support other geopoint formats (#144)
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
Showing
2 changed files
with
73 additions
and
12 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
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; | ||
} |