-
Notifications
You must be signed in to change notification settings - Fork 8
/
InputProcessingUtils.java
648 lines (616 loc) · 25.5 KB
/
InputProcessingUtils.java
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
package org.heigit.ohsome.ohsomeapi.inputprocessing;
import java.io.Serializable;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import org.heigit.ohsome.ohsomeapi.exception.BadRequestException;
import org.heigit.ohsome.ohsomeapi.exception.ExceptionMessages;
import org.heigit.ohsome.ohsomeapi.exception.NotFoundException;
import org.heigit.ohsome.ohsomeapi.oshdb.DbConnData;
import org.heigit.ohsome.ohsomeapi.oshdb.ExtractMetadata;
import org.heigit.ohsome.ohsomeapi.utils.TimestampFormatter;
import org.heigit.ohsome.oshdb.OSHDBTag;
import org.heigit.ohsome.oshdb.api.mapreducer.MapReducer;
import org.heigit.ohsome.oshdb.filter.ChangesetIdFilterEquals;
import org.heigit.ohsome.oshdb.filter.ChangesetIdFilterEqualsAnyOf;
import org.heigit.ohsome.oshdb.filter.ChangesetIdFilterRange;
import org.heigit.ohsome.oshdb.filter.Filter;
import org.heigit.ohsome.oshdb.filter.FilterExpression;
import org.heigit.ohsome.oshdb.filter.FilterParser;
import org.heigit.ohsome.oshdb.osm.OSMType;
import org.heigit.ohsome.oshdb.util.mappable.OSHDBMapReducible;
import org.heigit.ohsome.oshdb.util.tagtranslator.TagTranslator;
import org.heigit.ohsome.oshdb.util.time.IsoDateTimeParser;
import org.heigit.ohsome.oshdb.util.time.OSHDBTimestamps;
import org.jparsec.error.ParserException;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.Lineal;
import org.locationtech.jts.geom.Polygonal;
import org.locationtech.jts.geom.Puntal;
/** Holds utility methods that are used by the input processing and executor classes. */
public class InputProcessingUtils implements Serializable {
private static final String GEOMCOLLTYPE = "GeometryCollection";
private Serializable[] boundaryIds;
private String[] toTimestamps = null;
/**
* Finds and returns the EPSG code of the given point, which is needed for {@link
* org.heigit.ohsome.ohsomeapi.inputprocessing.GeometryBuilder#createCircularPolygons(String[]
* bcircles) createCircularPolygons}.
*
* <p>Adapted code from UTMCodeFromLonLat.java class in the osmatrix project (© by Michael Auer)
*
* @param lon Longitude coordinate of the point.
* @param lat Latitude coordinate of the point.
* @return <code>String</code> representing the corresponding EPSG code.
*/
public String findEpsg(double lon, double lat) {
if (lat >= 84) {
return "EPSG:32661"; // UPS North
}
if (lat < -80) {
return "EPSG:32761"; // UPS South
}
int zoneNumber = (int) (Math.floor((lon + 180) / 6) + 1);
if (lat >= 56.0 && lat < 64.0 && lon >= 3.0 && lon < 12.0) {
zoneNumber = 32;
}
// Special zones for Svalbard
if (lat >= 72.0 && lat < 84.0) {
if (lon >= 0.0 && lon < 9.0) {
zoneNumber = 31;
} else if (lon >= 9.0 && lon < 21.0) {
zoneNumber = 33;
} else if (lon >= 21.0 && lon < 33.0) {
zoneNumber = 35;
} else if (lon >= 33.0 && lon < 42.0) {
zoneNumber = 37;
}
}
String isNorth = lat > 0 ? "6" : "7";
String zone = zoneNumber < 10 ? "0" + zoneNumber : "" + zoneNumber;
return "EPSG:32" + isNorth + zone;
}
/**
* Splits the given bounding boxes and returns them in a <code>List</code>.
*
* @param bboxes contains the given bounding boxes
* @return <code>List</code> containing the splitted bounding boxes
* @throws BadRequestException if the bboxes parameter has an invalid format
*/
public List<String> splitBboxes(String bboxes) {
String[] bboxesArray = splitOnHyphen(bboxes);
List<String> boundaryParamValues = new ArrayList<>();
boundaryIds = new Serializable[bboxesArray.length];
try {
if (bboxesArray[0].contains(":")) {
boundaryParamValues = splitBboxesWithIds(bboxesArray);
} else {
boundaryParamValues = splitBoundariesWithoutIds(bboxesArray, BoundaryType.BBOXES);
}
} catch (Exception e) {
if (e.getClass() == BadRequestException.class) {
throw e;
}
throw new BadRequestException(ExceptionMessages.BOUNDARY_PARAM_FORMAT);
}
boundaryParamValues.removeAll(Collections.singleton(null));
return boundaryParamValues;
}
/**
* Splits the given bounding circles and returns them in a <code>List</code>.
*
* @param bcircles contains the given bounding circles
* @return <code>List</code> containing the splitted bounding circles
* @throws BadRequestException if the bcircles parameter has an invalid format
*/
public List<String> splitBcircles(String bcircles) {
String[] bcirclesArray = splitOnHyphen(bcircles);
List<String> boundaryParamValues = new ArrayList<>();
boundaryIds = new Serializable[bcirclesArray.length];
try {
if (bcirclesArray[0].contains(":")) {
boundaryParamValues = splitBcirclesWithIds(bcirclesArray);
} else {
boundaryParamValues = splitBoundariesWithoutIds(bcirclesArray, BoundaryType.BCIRCLES);
}
} catch (Exception e) {
if (e.getClass() == BadRequestException.class) {
throw e;
}
throw new BadRequestException(ExceptionMessages.BOUNDARY_PARAM_FORMAT);
}
boundaryParamValues.removeAll(Collections.singleton(null));
return boundaryParamValues;
}
/**
* Splits the given bounding polygons and returns them in a <code>List</code>.
*
* @param bpolys contains the given bounding polygons
* @return <code>List</code> containing the splitted bounding polygons
* @throws BadRequestException if the bpolys parameter has an invalid format
*/
public List<String> splitBpolys(String bpolys) {
String[] bpolysArray = splitOnHyphen(bpolys);
List<String> boundaryParamValues = new ArrayList<>();
boundaryIds = new Serializable[bpolysArray.length];
try {
if (bpolysArray[0].contains(":")) {
boundaryParamValues = splitBpolysWithIds(bpolysArray);
} else if (bpolysArray[0].contains(",")) {
boundaryParamValues = splitBoundariesWithoutIds(bpolysArray, BoundaryType.BPOLYS);
} else {
throw new BadRequestException(ExceptionMessages.BOUNDARY_PARAM_FORMAT);
}
} catch (Exception e) {
if (e.getClass() == BadRequestException.class) {
throw e;
}
throw new BadRequestException(ExceptionMessages.BOUNDARY_PARAM_FORMAT);
}
boundaryParamValues.removeAll(Collections.singleton(null));
return boundaryParamValues;
}
/**
* Defines the toTimestamps for the result json object for /users responses.
*
* @param timeData contains the requested time
* @return array having only the toTimestamps
*/
public String[] defineToTimestamps(String[] timeData) {
OSHDBTimestamps timestamps;
if (timeData.length == 3 && timeData[2] != null) {
// needed to check for interval
if (timeData[2].startsWith("P")) {
timestamps = new OSHDBTimestamps(timeData[0], timeData[1], timeData[2]);
toTimestamps = timestamps.get().stream()
.map(oshdbTimestamp -> TimestampFormatter.getInstance().isoDateTime(oshdbTimestamp))
.toArray(String[]::new);
} else {
// list of timestamps
toTimestamps = getToTimestampsFromTimestamplist(timeData);
}
} else {
// list of timestamps
toTimestamps = getToTimestampsFromTimestamplist(timeData);
}
return toTimestamps;
}
/**
* Extracts the time information out of the time parameter and checks the content on its format,
* as well as <a href="https://en.wikipedia.org/wiki/ISO_8601">ISO-8601</a> conformity. This
* method is used if one datetimestring is given. Following time formats are allowed:
* <ul>
* <li><strong>YYYY-MM-DD</strong> or <strong>YYYY-MM-DDThh:mm:ss</strong>: When a timestamp
* includes 'T', hh:mm must also be given. This applies for all time formats, which use
* timestamps. If -MM-DD or only -DD is missing, '01' is used as default for month and day.</li>
* <li><strong>YYYY-MM-DD/YYYY-MM-DD</strong>: start/end timestamps</li>
* <li><strong>YYYY-MM-DD/YYYY-MM-DD/PnYnMnD</strong>: start/end/period where n refers to the size
* of the respective period</li>
* <li><strong>/YYYY-MM-DD</strong>: #/end where # equals the earliest timestamp</li>
* <li><strong>/YYYY-MM-DD/PnYnMnD</strong>: #/end/period</li>
* <li><strong>YYYY-MM-DD/</strong>: start/# where # equals the latest timestamp</li>
* <li><strong>YYYY-MM-DD//PnYnMnD</strong>: start/#/period</li>
* <li><strong>/</strong>: #/# where # equals the earliest and latest timestamp</li>
* <li><strong>//PnYnMnD</strong>: #/#/period</li>
* <li><strong>invalid</strong>: throws BadRequestException</li>
* </ul>
*
* <p>For clarification: the format YYYY-MM-DDThh:mm:ss can be applied to any format, where a
* timestamp is used and # is a replacement holder for "no value". Note that the positioning and
* using of the forward slash '/' is very important.
*
* @param time <code>String</code> holding the unparsed time information.
* @return <code>String</code> array containing the startTime at [0], the endTime at [1] and the
* period at [2].
* @throws BadRequestException if the given time parameter is not ISO-8601 conform
*/
public String[] extractIsoTime(String time) {
String[] split = time.split("/");
if (split.length == 0 && !"/".equals(time)) {
// invalid time parameter
throw new BadRequestException(ExceptionMessages.TIME_FORMAT);
}
String[] timeVals = new String[3];
if (time.startsWith("/")) {
if (time.length() == 1) {
// only /
timeVals[0] = ExtractMetadata.fromTstamp;
timeVals[1] = ExtractMetadata.toTstamp;
return timeVals;
}
if (split[0].length() == 0 && split.length == 2) {
// /YYYY-MM-DD
checkTimestampsOnIsoConformity(split[1]);
checkTemporalExtend(split[1]);
timeVals[1] = split[1];
} else if (split.length == 3 && split[0].length() == 0 && split[1].length() == 0) {
// //PnYnMnD
checkPeriodOnIsoConformity(split[2]);
timeVals[1] = ExtractMetadata.toTstamp;
timeVals[2] = split[2];
} else if (split.length == 3 && split[1].length() != 0) {
// /YYYY-MM-DD/PnYnMnD
checkTimestampsOnIsoConformity(split[1]);
checkTemporalExtend(split[1]);
checkPeriodOnIsoConformity(split[2]);
timeVals[1] = split[1];
timeVals[2] = split[2];
} else {
// invalid time parameter
throw new BadRequestException(ExceptionMessages.TIME_FORMAT);
}
timeVals[0] = ExtractMetadata.fromTstamp;
} else if (time.endsWith("/")) {
if (split.length != 1) {
// invalid time parameter
throw new BadRequestException(ExceptionMessages.TIME_FORMAT);
}
// YYYY-MM-DD/
checkTimestampsOnIsoConformity(split[0]);
checkTemporalExtend(split[0]);
timeVals[0] = split[0];
timeVals[1] = ExtractMetadata.toTstamp;
} else if (split.length == 3) {
if (split[1].length() == 0) {
// YYYY-MM-DD//PnYnMnD
checkTimestampsOnIsoConformity(split[0]);
checkTemporalExtend(split[0]);
timeVals[1] = ExtractMetadata.toTstamp;
timeVals[2] = split[2];
} else {
// YYYY-MM-DD/YYYY-MM-DD/PnYnMnD
checkTimestampsOnIsoConformity(split[0], split[1]);
checkTemporalExtend(split[0], split[1]);
timeVals[1] = split[1];
}
checkPeriodOnIsoConformity(split[2]);
timeVals[0] = split[0];
timeVals[2] = split[2];
} else if (split.length == 2) {
// YYYY-MM-DD/YYYY-MM-DD
checkTimestampsOnIsoConformity(split[0], split[1]);
checkTemporalExtend(split[0], split[1]);
timeVals[0] = split[0];
timeVals[1] = split[1];
} else if (split.length == 1) {
// YYYY-MM-DD
checkTimestampsOnIsoConformity(split[0]);
checkTemporalExtend(split[0]);
timeVals[0] = split[0];
return timeVals;
} else {
// invalid time parameter
throw new BadRequestException(ExceptionMessages.TIME_FORMAT);
}
String[] sortedTimestamps = sortTimestamps(new String[] {timeVals[0], timeVals[1]});
timeVals[0] = sortedTimestamps[0];
timeVals[1] = sortedTimestamps[1];
return timeVals;
}
/**
* Sorts the given timestamps from oldest to newest.
*
* @throws BadRequestException if the given time parameter is not ISO-8601 conform
*/
public String[] sortTimestamps(String[] timestamps) {
List<String> timeStringList = new ArrayList<>();
for (String timestamp : timestamps) {
try {
ZonedDateTime zdt = IsoDateTimeParser.parseIsoDateTime(timestamp);
checkTemporalExtend(zdt.format(DateTimeFormatter.ISO_DATE_TIME));
timeStringList.add(zdt.format(DateTimeFormatter.ISO_DATE_TIME));
} catch (Exception e) {
throw new BadRequestException(ExceptionMessages.TIME_FORMAT);
}
}
Collections.sort(timeStringList);
return timeStringList.toArray(timestamps);
}
/**
* Checks the given custom boundary id. At the moment only used if output format = csv.
*
* @throws BadRequestException if the custom ids contain semicolons
*/
public void checkCustomBoundaryId(String id) {
if (id.contains(";")) {
throw new BadRequestException("The given custom ids cannot contain semicolons, "
+ "if you want to use csv as output format.");
}
}
/**
* Checks if the given geometry is within the underlying data-polygon. Returns also true if no
* data-polygon is given.
*
* @param geom <code>Geometry</code>, which is tested against the data-polygon
* @return <code>true</code> - if inside <br>
* <code>false</code> - if not inside
*/
public boolean isWithin(Geometry geom) {
if (ExtractMetadata.dataPoly != null) {
return geom.within(ExtractMetadata.dataPoly);
}
return true;
}
/** Checks if the given String is one of the simple feature types (point, line, polygon). */
public boolean isSimpleFeatureType(String type) {
return "point".equalsIgnoreCase(type) || "line".equalsIgnoreCase(type)
|| "polygon".equalsIgnoreCase(type) || "other".equalsIgnoreCase(type);
}
/**
* Applies an entity filter using only planar relations (relations with an area) on the given
* MapReducer object. It uses the tags "type=multipolygon" and "type=boundary".
*/
public <T extends OSHDBMapReducible> MapReducer<T> filterOnPlanarRelations(MapReducer<T> mapRed) {
// further filtering to not look at all relations
TagTranslator tt = DbConnData.tagTranslator;
OSHDBTag typeMultipolygon = tt.getOSHDBTagOf("type", "multipolygon")
.orElse(new OSHDBTag(-1, -1));
OSHDBTag typeBoundary = tt.getOSHDBTagOf("type", "boundary").orElse(new OSHDBTag(-1, -1));
return mapRed.filter(Filter.byOSMEntity(entity -> !entity.getType().equals(OSMType.RELATION)
|| entity.getTags().hasTag(typeMultipolygon.getKey(), typeMultipolygon.getValue())
|| entity.getTags().hasTag(typeBoundary.getKey(), typeBoundary.getValue())));
}
/**
* Checks whether a geometry is of given feature type (Puntal|Lineal|Polygonal).
*
* @param simpleFeatureTypes a set of feature types
* @return true if the geometry matches the given simpleFeatureTypes, otherwise false
*/
public boolean checkGeometryOnSimpleFeatures(Geometry geom,
Set<SimpleFeatureType> simpleFeatureTypes) {
return simpleFeatureTypes.contains(SimpleFeatureType.POLYGON) && geom instanceof Polygonal
|| simpleFeatureTypes.contains(SimpleFeatureType.POINT) && geom instanceof Puntal
|| simpleFeatureTypes.contains(SimpleFeatureType.LINE) && geom instanceof Lineal
|| simpleFeatureTypes.contains(SimpleFeatureType.OTHER)
&& GEOMCOLLTYPE.equalsIgnoreCase(geom.getGeometryType());
}
/**
* Tries to parse the given filter using the given parser.
*
* @throws BadRequestException if the filter contains wrong syntax.
*/
public FilterExpression parseFilter(FilterParser fp, String filter) {
try {
return fp.parse(filter);
} catch (ParserException ex) {
throw new BadRequestException(ExceptionMessages.FILTER_SYNTAX + " Detailed error message: "
+ ex.getMessage().replace("\n", " "));
}
}
/**
* Returns whether a given filter is suitable for snapshots based endpoints.
*
* <p>For example, `changeset:*` filter can only be used on contribution based endpoints,
* see also <a href="https://github.com/GIScience/ohsome-api/issues/289">#289</a>.</p>
*/
public static boolean filterSuitableForSnapshots(FilterExpression filter) {
return filter.normalize().stream().flatMap(Collection::stream)
.noneMatch(f -> f instanceof ChangesetIdFilterEquals
|| f instanceof ChangesetIdFilterRange
|| f instanceof ChangesetIdFilterEqualsAnyOf);
}
/**
* Checks the provided time info on its temporal extent.
*
* @param timeInfo time information to check
* @throws NotFoundException if the given time is not completely within the timerange of the
* underlying data
* @throws BadRequestException if the timestamps are not ISO-8601 conform
* @throws RuntimeException if the Date or DateTime Format are not supported
*/
protected void checkTemporalExtend(String... timeInfo) {
long start = 0;
long end = 0;
long timestampLong = 0;
try {
start = IsoDateTimeParser.parseIsoDateTime(ExtractMetadata.fromTstamp).toEpochSecond();
end = IsoDateTimeParser.parseIsoDateTime(ExtractMetadata.toTstamp).toEpochSecond();
} catch (Exception e) {
throw new RuntimeException("The ISO 8601 Date or the combined Date-Time String cannot be"
+ " converted into a UTC based ZonedDateTime Object");
}
for (String timestamp : timeInfo) {
try {
ZonedDateTime zdt = IsoDateTimeParser.parseIsoDateTime(timestamp);
timestampLong =
DateTimeFormatter.ISO_DATE_TIME.parse(zdt.format(DateTimeFormatter.ISO_DATE_TIME))
.getLong(ChronoField.INSTANT_SECONDS);
if (timestampLong < start || timestampLong > end) {
throw new NotFoundException(
"The given time parameter is not completely within the timeframe ("
+ ExtractMetadata.fromTstamp + " to " + ExtractMetadata.toTstamp
+ ") of the underlying osh-data.");
}
} catch (NotFoundException e) {
throw e;
} catch (Exception e) {
throw new BadRequestException(ExceptionMessages.TIME_FORMAT);
}
}
}
/**
* Checks the provided time info on its ISO conformity.
*
* @param timeInfo time information to check
* @throws BadRequestException if the timestamps are not ISO-8601 conform.
*/
protected void checkTimestampsOnIsoConformity(String... timeInfo) {
for (String timestamp : timeInfo) {
try {
IsoDateTimeParser.parseIsoDateTime(timestamp);
} catch (Exception e) {
throw new BadRequestException(ExceptionMessages.TIME_FORMAT);
}
}
}
/**
* Checks the provided period on its ISO conformity.
*
* @throws BadRequestException if the interval is not ISO-8601 conform.
*/
protected void checkPeriodOnIsoConformity(String period) {
try {
IsoDateTimeParser.parseIsoPeriod(period);
} catch (Exception e) {
throw new BadRequestException(
"The interval (period) of the provided time parameter is not ISO-8601 conform.");
}
}
/**
* Splits the given boundary parameter (bboxes, bcircles, or bpolys) on '|' to seperate the
* different bounding objects.
*
* @param boundaryParam <code>String</code> that contains the boundary parameter(s)
* @return splitted boundaries
*/
private String[] splitOnHyphen(String boundaryParam) {
if (boundaryParam.contains("|")) {
return boundaryParam.split("\\|");
}
return new String[] {boundaryParam};
}
/**
* Splits the coordinates from the given boundaries array.
*
* @param boundariesArray contains the boundaries without a custom id
* @return <code>List</code> containing the splitted boundaries
*/
private List<String> splitBoundariesWithoutIds(String[] boundariesArray,
BoundaryType boundaryType) {
List<String> boundaryParamValues = new ArrayList<>();
for (int i = 0; i < boundariesArray.length; i++) {
String[] coords = boundariesArray[i].split(",");
Collections.addAll(boundaryParamValues, coords);
boundaryIds[i] = "boundary" + (i + 1);
}
checkBoundaryParamLength(boundaryParamValues, boundaryType);
return boundaryParamValues;
}
/**
* Splits the ids and the coordinates from the given bounding boxes array.
*
* @param bboxesArray contains the bounding boxes having a custom id
* @return <code>List</code> containing the splitted bounding boxes
* @throws BadRequestException if the bboxes have invalid format
*/
private List<String> splitBboxesWithIds(String[] bboxesArray) {
List<String> boundaryParamValues = new ArrayList<>();
for (int i = 0; i < bboxesArray.length; i++) {
String[] coords = bboxesArray[i].split(",");
if (coords.length != 4) {
throw new BadRequestException(ExceptionMessages.BOUNDARY_PARAM_FORMAT);
}
if (coords[0].contains(":")) {
String[] idAndCoordinate = coords[0].split(":");
// extract the id
boundaryIds[i] = idAndCoordinate[0];
// extract the coordinates
boundaryParamValues.add(idAndCoordinate[1]);
boundaryParamValues.add(coords[1]);
boundaryParamValues.add(coords[2]);
boundaryParamValues.add(coords[3]);
} else {
throw new BadRequestException(ExceptionMessages.BOUNDARY_IDS_FORMAT);
}
}
checkBoundaryParamLength(boundaryParamValues, BoundaryType.BBOXES);
return boundaryParamValues;
}
/**
* Splits the ids and the coordinates from the given bounding circles array.
*
* @param bcirclesArray contains the bounding circles having a custom id
* @return <code>List</code> containing the splitted bounding circles
* @throws BadRequestException if the bcircles have invalid format
*/
private List<String> splitBcirclesWithIds(String[] bcirclesArray) {
List<String> boundaryParamValues = new ArrayList<>();
for (int i = 0; i < bcirclesArray.length; i++) {
String[] coords = bcirclesArray[i].split(",");
if (coords.length != 3) {
throw new BadRequestException(ExceptionMessages.BOUNDARY_PARAM_FORMAT);
}
String[] idAndCoordinate = coords[0].split(":");
boundaryIds[i] = idAndCoordinate[0];
// extract the coordinate
boundaryParamValues.add(idAndCoordinate[1]);
boundaryParamValues.add(coords[1]);
// extract the radius
boundaryParamValues.add(coords[2]);
}
checkBoundaryParamLength(boundaryParamValues, BoundaryType.BCIRCLES);
return boundaryParamValues;
}
/**
* Splits the ids and the coordinates from the given bounding polygons array.
*
* @param bpolysArray contains the bounding polygons having a custom id
* @return <code>List</code> containing the splitted bounding polygons
* @throws BadRequestException if the bpolys have invalid format
*/
private List<String> splitBpolysWithIds(String[] bpolysArray) {
List<String> boundaryParamValues = new ArrayList<>();
for (int i = 0; i < bpolysArray.length; i++) {
String[] coords = bpolysArray[i].split(",");
String[] idAndCoordinate = coords[0].split(":");
// extract the id and the first coordinate
boundaryIds[i] = idAndCoordinate[0];
boundaryParamValues.add(idAndCoordinate[1]);
// extract the other coordinates
for (int j = 1; j < coords.length; j++) {
if (coords[j].contains(":")) {
throw new BadRequestException(ExceptionMessages.BOUNDARY_PARAM_FORMAT);
}
boundaryParamValues.add(coords[j]);
}
}
checkBoundaryParamLength(boundaryParamValues, BoundaryType.BPOLYS);
return boundaryParamValues;
}
/**
* Checks the given boundaries list on their length. Bounding box and polygon list must be even,
* bounding circle list must be divisable by three.
*
* @param boundaries parameter to check the length
* @throws BadRequestException if the length is not even or divisible by three
*/
private void checkBoundaryParamLength(List<String> boundaries, BoundaryType boundaryType) {
if ((boundaryType.equals(BoundaryType.BBOXES) || boundaryType.equals(BoundaryType.BPOLYS))
&& boundaries.size() % 2 != 0) {
throw new BadRequestException(ExceptionMessages.BOUNDARY_PARAM_FORMAT);
}
if (boundaryType.equals(BoundaryType.BCIRCLES) && boundaries.size() % 3 != 0) {
throw new BadRequestException(ExceptionMessages.BOUNDARY_PARAM_FORMAT);
}
}
/** Internal helper method to get the toTimestamps from a timestampList. */
private String[] getToTimestampsFromTimestamplist(String[] timeData) {
toTimestamps = new String[timeData.length];
for (int i = 0; i < timeData.length; i++) {
try {
toTimestamps[i] =
IsoDateTimeParser.parseIsoDateTime(timeData[i]).format(DateTimeFormatter.ISO_DATE_TIME);
} catch (Exception e) {
// time gets checked earlier already, so no exception should appear here
}
}
return toTimestamps;
}
public Object[] getBoundaryIds() {
return boundaryIds;
}
public String[] getToTimestamps() {
return toTimestamps;
}
public void setBoundaryIds(Serializable[] boundaryIds) {
this.boundaryIds = boundaryIds;
}
public void setToTimestamps(String[] toTimestamps) {
this.toTimestamps = toTimestamps;
}
}