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

Support other geopoint formats #144

Merged

Conversation

VijayanB
Copy link
Member

@VijayanB VijayanB commented Dec 26, 2022

Description

Support Geopoint as

  1. GeoJSON
  2. String
  3. Array of lon and lat

WKT and Geohash will be supported in later release.

Signed-off-by: Vijayan Balasubramanian balasvij@amazon.com

Issues Resolved

List any issues this PR will resolve, e.g. Closes [...].

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@VijayanB VijayanB requested a review from a team December 26, 2022 23:20
@VijayanB VijayanB self-assigned this Dec 26, 2022
* SPDX-License-Identifier: Apache-2.0
*/

const geoJSONTypes = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const geoJSONTypes = [
const geoJSONTypes: string[] = [

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

'geometrycollection',
];

export function isGeoJSON(value: any) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about change the value type to value: unknown or to a more specific type, like value: object?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

@VijayanB VijayanB force-pushed the geopoint-support-format branch from 220e615 to 0a34e04 Compare December 27, 2022 18:09
@VijayanB VijayanB requested a review from junqiu-lei December 27, 2022 18:09
coordinates: location.coordinates,
};
// Geopoint as an array
if (Array.isArray(location) && location.length === 2) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't the length be 3?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Let me check

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated to include 2/3, but, we will skip 3rd value

// Geopoint as a string
if (typeof location === 'string') {
const values = location.trim().split(',');
if (values && values.length === 2) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. The length could be 3?

coordinates: location.coordinates,
};
}
if (fieldType !== 'geo_point') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, better to clear out the invalid case on top?

const buildGeometry = (fieldType: string, location: any) => {
  if (fileType !== 'geo_point' && !isGeoJSON(location)) {
    return undefined;
  }

  // Rest of the logics

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactored in such a way, we can extend the logic to geo_shape later.

if (geometry) {
const feature = {
geometry,
properties: buildProperties(item, layerConfig.source.tooltipFields),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comma is not necessary here I guess?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be mandatory from ES-lint. I will check again.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is required to have comma at end by ES-lint

}

export function isLonLatObject(value: any) {
if (value?.lat && value?.lon) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be simplified like

Suggested change
if (value?.lat && value?.lon) {
return value?.lat && value?.lon;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactored

}

export function wktPointToGeoJSON(value: any) {
if (typeof value === 'string') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to reduce the level of nested blocks.

if (typeof value !== 'string') {
  return undefined;
}
...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactored

];

export function isGeoJSON(value: { type: any; coordinates: any }) {
if (!value) return false;
Copy link
Collaborator

@heemin32 heemin32 Dec 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be simplified like

  return value && 
         value.type && 
         value.coordinates && 
         geoJSONTypes.includes(value.type.toLowerCase());

return false;
}

export function isLonLatObject(value: any) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export function isLonLatObject(value: any) {
export function isLonLatObject(value: any) {
return value?.lat && value?.lon;
}

}

export function wktPointToGeoJSON(value: any) {
if (typeof value === 'string') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can reduce nested block like

if (typeof value !== 'string') {
  return undefined;
}
... more code ...

export function wktPointToGeoJSON(value: any) {
if (typeof value === 'string') {
const removedType = value.trim().replace('POINT', '');
const coordinatesExpression = RegExp('[(](?<longitude>.+) (?<latitude>.+)[)]');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this will cover WKT point format comprehensively. It will even parse POINT (1a 3b).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I was thinking about adding lat/lon check after extraction. However, this will be called after geo_point data is retrieved from OpenSearch. I believe users won't be able to save POINT( 1a 3b) to OpenSearch.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will not be supporting WKT for now.

WKT and Geohash is not supported yet.

Signed-off-by: Vijayan Balasubramanian <balasvij@amazon.com>
@VijayanB VijayanB force-pushed the geopoint-support-format branch from 0a34e04 to ccd2e76 Compare December 27, 2022 23:35
@VijayanB VijayanB requested a review from heemin32 December 27, 2022 23:39
return buildGeoJSONOfTypePoint(location[0], location[1]);
}

if (typeof location !== 'string') {
Copy link
Collaborator

@heemin32 heemin32 Dec 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this code is highly related with the next few lines. By combining them, there will be no ordering among three condition checks which will enhance readability imo.

if (type of location === 'string') {
  const values = location.trim().split(',');
  if (values && (values.length === 2 || values.length === 3)) {
    return buildGeoJSONOfTypePoint(parseFloat(values[1].trim()), parseFloat(values[0].trim()));
  }
}

@VijayanB VijayanB merged commit 9651f76 into opensearch-project:feature/new-maps Dec 28, 2022
@junqiu-lei junqiu-lei added feature v2.5.0 'Issues and PRs related to version v2.5.0' enhancement New feature or request and removed feature labels Jan 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request v2.5.0 'Issues and PRs related to version v2.5.0'
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants