-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCql2Linq.cs
451 lines (390 loc) · 24.3 KB
/
Cql2Linq.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text.RegularExpressions;
using GeoJSON.Net.Geometry;
using Itenso.TimePeriod;
using NetTopologySuite.Geometries;
using Stac.Api.Interfaces;
using Stac.Api.Models.Cql2;
using Stac.Api.Services.Filtering;
using Stac.Api.Services.Queryable;
using Stars.Geometry.NTS;
namespace Stac.Api.Models.Cql2
{
public static class Cql2Linq
{
public static IEnumerable<TSource> Boolean<TSource>(this IEnumerable<TSource> source, BooleanExpression booleanExpression) where TSource : IStacObject
{
if ( booleanExpression == null)
return source;
return source.AsQueryable().Boolean(booleanExpression);
}
public static IQueryable<TSource> Boolean<TSource>(this IQueryable<TSource> source, BooleanExpression booleanExpression) where TSource : IStacObject
{
// This method creates a lambda expression that return a boolean value
// As we start a lambda expression, we need to create a parameter
ParameterExpression itemParameter = Expression.Parameter(typeof(TSource), "item");
ParameterExpression providerParameter = Expression.Parameter(typeof(IStacQueryProvider), "provider");
// We need to create a body for the lambda expression with all the rest of the filter
Expression body = CQL2BooleanToExpression<TSource>(booleanExpression, itemParameter, providerParameter);
var lambda = Expression.Lambda<Func<TSource, bool>>(body, itemParameter);
// Get the Where method from the IQueryable interface
var whereMethod = typeof(Queryable).GetMethods()
.Where(m => m.Name == nameof(Queryable.Where)
&& m.GetParameters().Length == 2)
.Where(p => p.GetParameters().First().ParameterType.GetGenericTypeDefinition() == typeof(IQueryable<>)
&& p.GetParameters().Last().ParameterType.GetGenericTypeDefinition() == typeof(Expression<>)
// Ensure Func has 2 args
&& p.GetParameters().Last().ParameterType.GetGenericArguments().First().GetGenericArguments().Length == 2)
.Single();
// Call the Where method with the lambda expression
var callExpression = Expression.Call(
null,
whereMethod.MakeGenericMethod(typeof(TSource)),
new Expression[] { source.Expression, Expression.Quote(lambda) }
);
// Return a new StacQueryable with the new expression
return new StacQueryable<TSource>(source.Provider as IStacQueryProvider,
callExpression);
}
public static Expression CQL2BooleanToExpression<TSource>(BooleanExpression booleanExpression, ParameterExpression itemParameter, ParameterExpression providerParameter) where TSource : IStacObject
{
AndOrExpression andOrExpression = booleanExpression.AndOrExpression();
if (andOrExpression != null)
{
return CQL2AndOrToExpression<TSource>(andOrExpression, itemParameter, providerParameter);
}
NotExpression notExpression = booleanExpression.NotExpression();
if (notExpression != null)
{
return CQL2NotToExpression<TSource>(notExpression, itemParameter, providerParameter);
}
ComparisonPredicate comparisonPredicate = booleanExpression.Comparison();
if (comparisonPredicate != null)
{
return CQL2ComparisonToExpression<TSource>(comparisonPredicate, itemParameter, providerParameter);
}
throw new NotImplementedException();
}
public static Expression CQL2AndOrToExpression<TSource>(AndOrExpression andOrExpression, ParameterExpression itemParameter, ParameterExpression providerParameter) where TSource : IStacObject
{
// check the arguments
if (andOrExpression.Args.Count < 2)
{
throw new ArgumentException("AndOrExpression must have at least 2 arguments");
}
// We pass the parameter to the CQL2BooleanToExpression method to get left and right expressions
var left = CQL2BooleanToExpression<TSource>(andOrExpression.Args[0], itemParameter, providerParameter);
var right = CQL2BooleanToExpression<TSource>(andOrExpression.Args[1], itemParameter, providerParameter);
switch (andOrExpression.Op)
{
case AndOrExpressionOp.And:
return Expression.AndAlso(left, right);
case AndOrExpressionOp.Or:
return Expression.OrElse(left, right);
default:
throw new NotImplementedException();
}
}
public static Expression CQL2NotToExpression<TSource>(NotExpression notExpression, ParameterExpression itemParameter, ParameterExpression providerParameter) where TSource : IStacObject
{
// check the arguments
if (notExpression.Args.Count != 1)
{
throw new ArgumentException("NotExpression must have exactly 1 argument");
}
// We pass the parameter to the CQL2BooleanToExpression method to get the negated expression
var negated = CQL2BooleanToExpression<TSource>(notExpression.Args[0], itemParameter, providerParameter);
return Expression.Not(negated);
}
private static Expression CQL2ComparisonToExpression<TSource>(ComparisonPredicate binaryComparisonPredicate, ParameterExpression itemParameter, ParameterExpression providerParameter) where TSource : IStacObject
{
switch (binaryComparisonPredicate)
{
case BinaryComparisonPredicate binaryComparisonPredicate1:
return CQL2BinaryComparisonToExpression<TSource>(binaryComparisonPredicate1, itemParameter, providerParameter);
case IsLikePredicate isLikePredicate:
return CQL2ToIsLikeToExpression<TSource>(isLikePredicate, itemParameter, providerParameter);
case IsBetweenPredicate isBetweenPredicate:
return CQL2ToIsBetweenToExpression<TSource>(isBetweenPredicate, itemParameter, providerParameter);
case IsInListPredicate isInListPredicate:
return CQL2ToIsInListToExpression<TSource>(isInListPredicate, itemParameter, providerParameter);
case IsNullPredicate isNullPredicate:
return CQL2ToIsNullToExpression<TSource>(isNullPredicate, itemParameter, providerParameter);
case SpatialPredicate spatialPredicate:
return CQL2ToSpatialToExpression<TSource>(spatialPredicate, itemParameter, providerParameter);
case TemporalPredicate temporalPredicate:
return CQL2ToTemporalToExpression<TSource>(temporalPredicate, itemParameter, providerParameter);
default:
throw new NotImplementedException(binaryComparisonPredicate.GetType().Name);
}
}
private static BinaryExpression CQL2BinaryComparisonToExpression<TSource>(BinaryComparisonPredicate binaryComparisonPredicate, ParameterExpression itemParameter, ParameterExpression providerParameter) where TSource : IStacObject
{
// check the arguments
if (binaryComparisonPredicate.Args.Count != 2)
{
throw new ArgumentException("BinaryComparisonPredicate must have exactly 2 arguments");
}
// We pass the parameter to the CQL2ToExpression method to get the scalar expression value
var left = CQL2ToExpression<TSource>(binaryComparisonPredicate.Args[0], itemParameter, providerParameter);
var right = CQL2ToExpression<TSource>(binaryComparisonPredicate.Args[1], itemParameter, providerParameter);
var methodInfo = typeof(IComparable).GetMethod("CompareTo", new Type[] { typeof(object) });
var compareExpression = Expression.Call(left, methodInfo, right);
var zeroConst = Expression.Constant(0, typeof(Int32));
switch (binaryComparisonPredicate.Op)
{
case ComparisonPredicateOp.Eq:
return Expression.Equal(compareExpression, zeroConst);
case ComparisonPredicateOp.Diff:
return Expression.NotEqual(compareExpression, zeroConst);
case ComparisonPredicateOp.Lt:
return Expression.LessThan(compareExpression, zeroConst);
case ComparisonPredicateOp.Le:
return Expression.LessThanOrEqual(compareExpression, zeroConst);
case ComparisonPredicateOp.Gt:
return Expression.GreaterThan(compareExpression, zeroConst);
case ComparisonPredicateOp.Ge:
return Expression.GreaterThanOrEqual(compareExpression, zeroConst);
}
throw new NotImplementedException();
}
private static Expression CQL2ToIsLikeToExpression<TSource>(IsLikePredicate isLikeComparisonPredicate, ParameterExpression itemParameter, ParameterExpression providerParameter) where TSource : IStacObject
{
// We pass the parameter to the CQL2ToExpression method to get the scalar expression value
var left = CQL2ToExpression<TSource>(isLikeComparisonPredicate.Args[0], itemParameter, providerParameter);
var right = CQL2ToExpression<TSource>(isLikeComparisonPredicate.Args[1], itemParameter, providerParameter);
var methodInfo = typeof(Regex).GetMethod("IsMatch", new Type[] { typeof(string) });
var isLikeExpression = Expression.Call(null, typeof(ItemFiltersExtensions).GetMethod("IsLike", new Type[] { typeof(object), typeof(IComparable) }), left, right);
return isLikeExpression;
}
private static BinaryExpression CQL2ToIsBetweenToExpression<TSource>(IsBetweenPredicate isBetweenPredicate, ParameterExpression itemParameter, ParameterExpression providerParameter) where TSource : IStacObject
{
// We pass the parameter to the CQL2ToExpression method to get the scalar expression value
var main = CQL2ToExpression<TSource>(isBetweenPredicate.Args[0], itemParameter, providerParameter);
var min = CQL2ToExpression<TSource>(isBetweenPredicate.Args[1], itemParameter, providerParameter);
var max = CQL2ToExpression<TSource>(isBetweenPredicate.Args[2], itemParameter, providerParameter);
var methodInfo = typeof(IComparable).GetMethod("CompareTo", new Type[] { typeof(object) });
var minCompareExpression = Expression.Call(main, methodInfo, min);
var maxCompareExpression = Expression.Call(main, methodInfo, max);
var zeroConst = Expression.Constant(0, typeof(Int32));
return Expression.AndAlso(Expression.GreaterThanOrEqual(minCompareExpression, zeroConst), Expression.LessThanOrEqual(maxCompareExpression, zeroConst));
}
private static BinaryExpression CQL2ToIsInListToExpression<TSource>(IsInListPredicate isInListPredicate, ParameterExpression itemParameter, ParameterExpression providerParameter) where TSource : IStacObject
{
// We pass the parameter to the CQL2ToExpression method to get the scalar expression value
var main = CQL2ToExpression<TSource>(isInListPredicate.Args[0], itemParameter, providerParameter);
var methodInfo = typeof(IComparable).GetMethod("CompareTo", new Type[] { typeof(object) });
var zeroConst = Expression.Constant(0, typeof(Int32));
var orExpressions = new List<Expression>();
foreach (var arg in isInListPredicate.Args.Skip(1))
{
var argExpression = CQL2ToExpression<TSource>(arg, itemParameter, providerParameter);
var compareExpression = Expression.Call(main, methodInfo, argExpression);
orExpressions.Add(Expression.Equal(compareExpression, zeroConst));
}
return Expression.OrElse(orExpressions[0], orExpressions[1]);
}
private static Expression CQL2ToIsNullToExpression<TSource>(IsNullPredicate isNullPredicate, ParameterExpression itemParameter, ParameterExpression providerParameter) where TSource : IStacObject
{
// We pass the parameter to the CQL2ToExpression method to get the scalar expression value
var main = CQL2ToExpression<TSource>(isNullPredicate.Args, itemParameter, providerParameter);
var methodInfo = typeof(object).GetMethod("Equals", new Type[] { typeof(object) });
var nullConst = Expression.Constant(null, typeof(object));
var compareExpression = Expression.Call(main, methodInfo, nullConst);
return Expression.IsTrue(compareExpression);
}
private static Expression CQL2ToSpatialToExpression<TSource>(SpatialPredicate spatialPredicate, ParameterExpression itemParameter, ParameterExpression providerParameter) where TSource : IStacObject
{
// We pass the parameter to the CQL2ToExpression method to get the scalar expression value
var left = CQL2ToExpression<TSource>(spatialPredicate.Args[0], itemParameter, providerParameter);
var right = CQL2ToExpression<TSource>(spatialPredicate.Args[1], itemParameter, providerParameter);
MethodInfo methodInfo = GetSpatialMethod(spatialPredicate.Op);
var spatialExpression = Expression.Call(providerParameter, methodInfo, left, right);
return spatialExpression;
}
private static MethodInfo GetSpatialMethod(SpatialPredicateOp op)
{
switch (op)
{
case SpatialPredicateOp.S_contains:
return typeof(IStacQueryProvider).GetMethod("SpatialContains", new Type[] { typeof(Geometry), typeof(Geometry) });
case SpatialPredicateOp.S_crosses:
return typeof(IStacQueryProvider).GetMethod("SpatialCrosses", new Type[] { typeof(Geometry), typeof(Geometry) });
case SpatialPredicateOp.S_disjoint:
return typeof(IStacQueryProvider).GetMethod("SpatialDisjoint", new Type[] { typeof(Geometry), typeof(Geometry) });
case SpatialPredicateOp.S_equals:
return typeof(IStacQueryProvider).GetMethod("SpatialEquals", new Type[] { typeof(Geometry), typeof(Geometry) });
case SpatialPredicateOp.S_intersects:
return typeof(IStacQueryProvider).GetMethod("SpatialIntersects", new Type[] { typeof(Geometry), typeof(Geometry) });
case SpatialPredicateOp.S_overlaps:
return typeof(IStacQueryProvider).GetMethod("SpatialOverlaps", new Type[] { typeof(Geometry), typeof(Geometry) });
case SpatialPredicateOp.S_touches:
return typeof(IStacQueryProvider).GetMethod("SpatialTouches", new Type[] { typeof(Geometry), typeof(Geometry) });
}
throw new NotImplementedException(op.GetType().ToString());
}
private static Expression CQL2ToTemporalToExpression<TSource>(TemporalPredicate temporalPredicate, ParameterExpression itemParameter, ParameterExpression providerParameter) where TSource : IStacObject
{
// We pass the parameter to the CQL2ToExpression method to get the scalar expression value
var left = CQL2ToExpression<TSource>(temporalPredicate.Args[0], itemParameter, providerParameter);
var right = CQL2ToExpression<TSource>(temporalPredicate.Args[1], itemParameter, providerParameter);
MethodInfo methodInfo = GetTemporalMethod(temporalPredicate.Op);
var temporalExpression = Expression.Call(providerParameter, methodInfo, left, right);
return temporalExpression;
}
private static MethodInfo GetTemporalMethod(TemporalPredicateOp op)
{
switch (op)
{
case TemporalPredicateOp.T_before:
return typeof(IStacQueryProvider).GetMethod("TemporalBefore", new Type[] { typeof(ITimePeriod), typeof(ITimePeriod) });
case TemporalPredicateOp.T_during:
return typeof(IStacQueryProvider).GetMethod("TemporalDuring", new Type[] { typeof(ITimePeriod), typeof(ITimePeriod) });
case TemporalPredicateOp.T_equals:
return typeof(IStacQueryProvider).GetMethod("TemporalEquals", new Type[] { typeof(ITimePeriod), typeof(ITimePeriod) });
case TemporalPredicateOp.T_meets:
return typeof(IStacQueryProvider).GetMethod("TemporalMeets", new Type[] { typeof(ITimePeriod), typeof(ITimePeriod) });
case TemporalPredicateOp.T_overlaps:
return typeof(IStacQueryProvider).GetMethod("TemporalOverlaps", new Type[] { typeof(ITimePeriod), typeof(ITimePeriod) });
case TemporalPredicateOp.T_starts:
return typeof(IStacQueryProvider).GetMethod("TemporalStarts", new Type[] { typeof(ITimePeriod), typeof(ITimePeriod) });
case TemporalPredicateOp.T_intersects:
return typeof(IStacQueryProvider).GetMethod("TemporalIntersects", new Type[] { typeof(ITimePeriod), typeof(ITimePeriod) });
}
throw new NotImplementedException(op.ToString());
}
public static Expression CQL2ToExpression<TSource>(IScalarExpression scalarExpression, ParameterExpression item, ParameterExpression providerParameter) where TSource : IStacObject
{
if (scalarExpression is IScalarLiteral scalarLiteral)
{
return Expression.Constant(scalarLiteral.Value, typeof(IComparable));
}
if (scalarExpression is CharExpression charExpression)
{
return CQL2ToExpression<TSource>(charExpression, item, providerParameter);
}
throw new NotImplementedException();
}
public static Expression CQL2ToExpression<TSource>(IInListOperand inListOperand, ParameterExpression item, ParameterExpression providerParameter) where TSource : IStacObject
{
if (inListOperand is IScalarExpression scalarExpression)
{
return CQL2ToExpression<TSource>(scalarExpression, item, providerParameter);
}
throw new NotImplementedException();
}
public static Expression CQL2ToExpression<TSource>(IIsNullOperand isNullOperand, ParameterExpression item, ParameterExpression providerParameter) where TSource : IStacObject
{
if (isNullOperand is CharExpression charExpression)
{
return CQL2ToExpression<TSource>(charExpression, item, providerParameter);
}
if (isNullOperand is Number number)
{
return Expression.Constant(number.Value, typeof(IComparable));
}
if (isNullOperand is ITemporalExpression temporalExpression)
{
return CQL2ToExpression<TSource>(temporalExpression, item, providerParameter);
}
if (isNullOperand is BooleanExpression booleanExpression)
{
return CQL2BooleanToExpression<TSource>(booleanExpression, item, providerParameter);
}
if (isNullOperand is IGeomExpression geomExpression)
{
return CQL2ToExpression<TSource>(geomExpression, item, providerParameter);
}
throw new NotImplementedException();
}
public static Expression CQL2ToExpression<TSource>(INumericExpression numericExpression, ParameterExpression item, ParameterExpression providerParameter) where TSource : IStacObject
{
if (numericExpression is Number number)
{
return Expression.Constant(number.Value, typeof(IComparable));
}
if (numericExpression is CharExpression charExpression)
{
return CQL2ToExpression<TSource>(charExpression, item, providerParameter);
}
throw new NotImplementedException();
}
public static Expression CQL2ToExpression<TSource>(ITemporalExpression temporalExpression, ParameterExpression item, ParameterExpression providerParameter) where TSource : IStacObject
{
if (temporalExpression is ITemporalLiteral temporalLiteral)
{
return CQL2ToExpression<TSource>(temporalLiteral, item, providerParameter);
}
if (temporalExpression is PropertyRef propertyRef)
{
var method = typeof(IStacQueryProvider).GetMethod("GetStacObjectDateTime").MakeGenericMethod(typeof(TSource));
return Expression.Call(providerParameter,
method,
item, Expression.Constant(propertyRef.Property));
}
throw new NotImplementedException();
}
public static Expression CQL2ToExpression<TSource>(ITemporalLiteral temporalLiteral, ParameterExpression item, ParameterExpression providerParameter) where TSource : IStacObject
{
if (temporalLiteral is InstantLiteral instantLiteral)
{
return Expression.Constant(new TimeInterval(instantLiteral.DateTime.DateTime, instantLiteral.DateTime.DateTime));
}
if (temporalLiteral is IntervalLiteral intervalLiteral)
{
return Expression.Constant(intervalLiteral.TimeInterval);
}
throw new NotImplementedException();
}
public static Expression CQL2ToExpression<TSource>(CharExpression charExpression, ParameterExpression item, ParameterExpression providerParameter) where TSource : IStacObject
{
PropertyRef propertyRef = charExpression.Property();
if (propertyRef != null)
{
var method = typeof(IStacQueryProvider).GetMethod("GetStacObjectProperty").MakeGenericMethod(typeof(TSource));
return Expression.Call(providerParameter,
method,
item, Expression.Constant(propertyRef.Property));
}
String str = charExpression.String();
if (str != null)
{
return Expression.Constant(str.Str, typeof(IComparable));
}
throw new NotImplementedException(charExpression.GetType().Name);
}
public static Expression CQL2ToExpression<TSource>(IGeomExpression geomExpression, ParameterExpression item, ParameterExpression providerParameter) where TSource : IStacObject
{
if (geomExpression is ISpatialLiteral spatialLiteral)
{
return CQL2ToExpression<TSource>(spatialLiteral, item, providerParameter);
}
if (geomExpression is PropertyRef propertyRef)
{
var method = typeof(IStacQueryProvider).GetMethod("GetStacObjectGeometry").MakeGenericMethod(typeof(TSource));
return Expression.Call(providerParameter,
method,
item, Expression.Constant(propertyRef.Property));
}
throw new NotImplementedException(geomExpression.GetType().Name);
}
public static Expression CQL2ToExpression<TSource>(ISpatialLiteral spatialLiteral, ParameterExpression item, ParameterExpression providerParameter) where TSource : IStacObject
{
if (spatialLiteral is GeometryLiteral geometryLiteral)
{
return Expression.Constant(geometryLiteral.GeometryObject.ToNTSGeometry(), typeof(Geometry));
}
if (spatialLiteral is EnvelopeLiteral envelopeLiteral)
{
return Expression.Constant(new Envelope(envelopeLiteral.Bbox[0], envelopeLiteral.Bbox[2], envelopeLiteral.Bbox[1], envelopeLiteral.Bbox[3]), typeof(Geometry));
}
throw new NotImplementedException(spatialLiteral.GetType().Name);
}
}
}