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

fix(geo): ensure ogc filter accept today and now #1622

Merged
merged 9 commits into from
May 15, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,14 @@ export class OgcFilterTimeComponent implements OnInit {
const interval = filter.match(/years|months|weeks|days|hours|seconds/);
if (filter.match(/\+/)) {
const intervalInt = parseInt(
filter.substring(filter.search('+') + 1, interval.index),
filter.substring(filter.indexOf('+') + 1, interval.index),
10
);
return moment().add(intervalInt, interval[0]).toDate();
}
if (filter.match(/\-/)) {
const intervalInt = parseInt(
filter.substring(filter.search('-') + 1, interval.index),
filter.substring(filter.indexOf('-') + 1, interval.index),
10
);
return moment().subtract(intervalInt, interval[0]).toDate();
Expand All @@ -162,14 +162,14 @@ export class OgcFilterTimeComponent implements OnInit {
const interval = filter.match(/years|months|weeks|days|hours|seconds/);
if (filter.match(/\+/)) {
const intervalInt = parseInt(
filter.substring(filter.search('+') + 1, interval.index),
filter.substring(filter.indexOf('+') + 1, interval.index),
10
);
return moment(_now).add(intervalInt, interval[0]).toDate();
}
if (filter.match(/\-/)) {
const intervalInt = parseInt(
filter.substring(filter.search('-') + 1, interval.index),
filter.substring(filter.indexOf('-') + 1, interval.index),
10
);
return moment(_now).subtract(intervalInt, interval[0]).toDate();
Expand Down Expand Up @@ -631,16 +631,16 @@ export class OgcFilterTimeComponent implements OnInit {
return this.currentFilter.begin
? this.currentFilter.begin
: this.datasource.options.minDate
? this.datasource.options.minDate
: this._defaultMin;
? this.datasource.options.minDate
: this._defaultMin;
}

public handleMax() {
return this.currentFilter.end
? this.currentFilter.end
: this.datasource.options.maxDate
? this.datasource.options.maxDate
: this._defaultMax;
? this.datasource.options.maxDate
: this._defaultMax;
}

changePropertyByPass(event) {
Expand Down
52 changes: 48 additions & 4 deletions packages/geo/src/lib/filter/shared/ogc-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,8 +594,8 @@ export class OgcFilterWriter {
const srsName = igoOgcFilterObject.hasOwnProperty('srsName')
? igoOgcFilterObject.srsName
: proj
? proj.getCode()
: 'EPSG:3857';
? proj.getCode()
: 'EPSG:3857';

return Object.assign(
{},
Expand Down Expand Up @@ -913,12 +913,56 @@ export class OgcFilterWriter {
public parseFilterOptionDate(value: string, defaultValue?: string): string {
if (!value) {
return defaultValue;
} else if (value === 'today') {
return undefined;
} else if (
value.toLowerCase().includes('now') ||
value.toLowerCase().includes('today')
) {
return this.parseStringDate(value);
} else if (moment(value).isValid()) {
return value;
} else {
return undefined;
}
}
/**
* this function to parse date with specific format
* exemple 'today + 1 days' or 'now + 1 years'
* @param value string
* @returns date
*/
private parseStringDate(value: string): string {
const sValue = value.toLowerCase().split(' ');
alecarn marked this conversation as resolved.
Show resolved Hide resolved
const time = sValue[0];
const operators = ['+', '-'].includes(sValue[1]) ? sValue[1] : undefined;
const numberOfunits = /^[0-9]*$/.test(sValue[2]) ? sValue[2] : undefined;
const unit: any = [
alecarn marked this conversation as resolved.
Show resolved Hide resolved
'years',
'months',
'weeks',
'days',
'hours',
'seconds'
].includes(sValue[3])
? sValue[3]
: undefined;

if (!operators || !unit || !numberOfunits) {
return time === 'now'
? moment().format()
: moment().endOf('day').format();
}

if (operators === '+') {
return time === 'now'
? moment().add(parseInt(numberOfunits, 10), unit).format()
: moment().endOf('day').add(parseInt(numberOfunits, 10), unit).format();
} else {
return time === 'now'
? moment().subtract(parseInt(numberOfunits, 10), unit).format()
: moment()
.endOf('day')
.subtract(parseInt(numberOfunits, 10), unit)
.format();
}
}
}
Loading