summaryrefslogtreecommitdiffstats
path: root/petascope/src/petascope/wcps/server/core/Metadata.java
blob: 175830ae8fe03a8e654eb25279a8cc474d3a12b8 (plain)
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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
/*
 * This file is part of rasdaman community.
 *
 * Rasdaman community is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Rasdaman community is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
 rasdaman GmbH.
 *
 * For more information please see <http://www.rasdaman.org>
 * or contact Peter Baumann via <baumann@rasdaman.com>.
 */
package petascope.wcps.server.core;

//~--- JDK imports ------------------------------------------------------------

import petascope.wcps.server.exceptions.InvalidMetadataException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import petascope.wcs.server.core.TimeString;

/**

 * This class implements WCPS metadata. For information on what each field

 * means, see the WCPS standard. This class provides extensive error cheching

 * and well as various utility functions.

 */
public class Metadata implements Cloneable {

    private List<CellDomainElement> cellDomain;
    private String coverageName;
    private int coverageId = -1;    // Used when reading metadata from the DB

    private List<DomainElement> domain;
    private InterpolationMethod interpolationDefault;
    private Set<InterpolationMethod> interpolationSet;
    private String nullDefault;
    private Set<String> nullSet;
    private List<RangeElement> range;
    private String titleStr = "";
    private String abstractStr = "";
    private String keywordsStr = "";
    private Wgs84Crs crs = null;
    private CellDomainElement cellX, cellY, cellT;
    private DomainElement domX, domY, domT;

    public Metadata(List<CellDomainElement> cellDomain, List<RangeElement> range, Set<String> nullSet, String nullDefault, Set<InterpolationMethod> interpolationSet, InterpolationMethod interpolationDefault, String coverageName, List<DomainElement> domain, Wgs84Crs crs, String title, String abstr, String keywords) throws InvalidMetadataException {
        this(cellDomain, range, nullSet, nullDefault, interpolationSet, interpolationDefault, coverageName, domain, crs);
        this.titleStr = title;
        this.abstractStr = abstr;
        this.keywordsStr = keywords;
    }

    public Metadata(List<CellDomainElement> cellDomain, List<RangeElement> range, Set<String> nullSet, String nullDefault, Set<InterpolationMethod> interpolationSet, InterpolationMethod interpolationDefault, String coverageName, List<DomainElement> domain, Wgs84Crs crs) throws InvalidMetadataException {
        if ((cellDomain == null) || (range == null) || (coverageName == null) || (nullSet == null) || (interpolationSet == null)) {
            throw new InvalidMetadataException("Cell domain, range list, "
                    + "coverage name, null set, and interpolation set cannot be null for coverage " + coverageName);
        }

        if (cellDomain.size() == 0) {
            throw new InvalidMetadataException("Invalid cell domain: At least "
                    + "one element is required for coverage " + coverageName);
        }

        this.cellDomain = cellDomain;

        if (range.size() == 0) {
            throw new InvalidMetadataException("At least one range element is "
                    + "required for coverage " + coverageName);
        }

        this.range = new ArrayList<RangeElement>(range.size());
        Iterator<RangeElement> ir = range.iterator();

        while (ir.hasNext()) {
            RangeElement next = ir.next();
            Iterator<RangeElement> j = this.range.iterator();

            while (j.hasNext()) {
                if (j.next().getName().equals(next.getName())) {
                    throw new InvalidMetadataException("Duplicate range element"
                            + " name encountered for coverage " + coverageName);
                }
            }

            this.range.add(next);
        }

        if (nullSet.size() == 0) {
            throw new InvalidMetadataException("Invalid null set: At least one "
                    + "null value is required for coverage " + coverageName);
        }

        if (nullDefault == null) {
            throw new InvalidMetadataException("Invalid null default: Null "
                    + "default cannot be null for coverage " + coverageName);
        }

        if (!nullSet.contains(nullDefault)) {
            throw new InvalidMetadataException("Invalid null default: Default "
                    + "null value " + nullDefault + " is not part of the null set"
                    + " for coverage " + coverageName);
        }

        Iterator<String> ns = nullSet.iterator();

        while (ns.hasNext()) {
            String nullVal = ns.next();
            List<String> nulls = SDU.str2string(nullVal);

            if (nulls.size() != range.size()) {
                throw new InvalidMetadataException("Invalid null value: "
                        + nullVal + " must have " + range.size() + " axes "
                        + "according to the range specified for coverage " + coverageName);
            }

            Iterator<String> i = nulls.iterator();
            Iterator<RangeElement> j = range.iterator();

            while (j.hasNext()) {
                RangeElement re = j.next();

                if (re.isBoolean()) {
                    SDU.str2boolean(i.next());
                } else if (re.isIntegral()) {
                    SDU.str2integer(i.next());
                } else if (re.isFloating()) {
                    SDU.str2double(i.next());
                } else if (re.isComplex()) {
                    SDU.str2complex(i.next());
                }
            }
        }

        this.nullSet = nullSet;
        this.nullDefault = nullDefault;

        if (interpolationSet.size() == 0) {
            throw new InvalidMetadataException("Invalid interpolation set: "
                    + "At least one interpolation method is required for "
                    + "coverage " + coverageName);
        }

        if (interpolationDefault == null) {
            interpolationDefault = new InterpolationMethod("none", "none");
        }

        boolean defaultContainedInSet = false;
        Iterator<InterpolationMethod> is = interpolationSet.iterator();

        while (is.hasNext()) {
            if (interpolationDefault.equals(is.next())) {
                defaultContainedInSet = true;
            }
        }

        if (!defaultContainedInSet) {
            throw new InvalidMetadataException("Invalid interpolation default:"
                    + " Default interpolation method ("
                    + interpolationDefault.getInterpolationType() + ","
                    + interpolationDefault.getNullResistance() + ") is not part "
                    + "of the interpolation set for coverage " + coverageName);
        }

        this.interpolationSet = interpolationSet;
        this.interpolationDefault = interpolationDefault;
        this.crs = crs;

        this.coverageName = coverageName;

        if (domain != null) {
            if (domain.size() != cellDomain.size()) {
                throw new InvalidMetadataException("Domain and cell domain "
                        + "must have equal number of elements for coverage " + coverageName);
            }

            this.domain = new ArrayList<DomainElement>(domain.size());
            Iterator<DomainElement> i = domain.iterator();
            Iterator<CellDomainElement> ci = cellDomain.iterator();

            while (i.hasNext() && ci.hasNext()) {
                DomainElement next = i.next();
                CellDomainElement cell = ci.next();
                Iterator<DomainElement> j = this.domain.iterator();
                if (next.getType().equals("x")) {
                    cellX = cell;
                }
                if (next.getType().equals("y")) {
                    cellY = cell;
                }
                if (next.getType().equals("t") //                        || next.getType().equals("temporal")

                        ) {
                    cellT = cell;
                    domT = next;
                }

                while (j.hasNext()) {
                    DomainElement previous = j.next();

                    // don't compare same objects

                    if (next == previous) {
                        continue;
                    }

                    if (previous.getName().equals(next.getName())) {
                        throw new InvalidMetadataException("Duplicate domain "
                                + "element name encountered for coverage " + coverageName);
                    }

                    if (previous.getType().equals("temporal") && next.getType().equals("temporal")) {
                        throw new InvalidMetadataException("Domain can contain"
                                + " at most one temporal axis for coverage " + coverageName);
                    }

                    if (previous.getType().equals("elevation") && next.getType().equals("elevation")) {
                        throw new InvalidMetadataException("Domain can contain"
                                + " at most one elevation axis for coverage " + coverageName);
                    }

                    if (previous.getType().equals("x") && next.getType().equals("x")) {
                        throw new InvalidMetadataException("Domain can contain"
                                + " at most one x axis for coverage " + coverageName);
                    }

                    if (previous.getType().equals("y") && next.getType().equals("y")) {
                        throw new InvalidMetadataException("Domain can contain"
                                + " at most one y axis for coverage " + coverageName);
                    }

                    if (next.getType().equals("x")) {
                        boolean l = false;
                        Iterator<DomainElement> k = domain.iterator();

                        while (k.hasNext()) {
                            if (k.next().getType().equals("y")) {
                                l = true;
                            }
                        }

                        if (l == false) {
                            throw new InvalidMetadataException("If domain "
                                    + "contains a x axis, it must contain a y "
                                    + "axis as well for coverage " + coverageName);
                        }
                    } else if (next.getType().equals("y")) {
                        boolean l = false;
                        Iterator<DomainElement> k = domain.iterator();

                        while (k.hasNext()) {
                            if (k.next().getType().equals("x")) {
                                l = true;
                            }
                        }

                        if (l == false) {
                            throw new InvalidMetadataException("If domain "
                                    + "contains a y axis, it must contain a x "
                                    + "axis as well for coverage " + coverageName);
                        }
                    }
                }

                this.domain.add(next);
            }
        }

    }

    @Override
    public Metadata clone() {
        try {
            List<CellDomainElement> cd = new ArrayList<CellDomainElement>(cellDomain.size());
            Iterator<CellDomainElement> i = cellDomain.iterator();

            while (i.hasNext()) {
                cd.add(i.next().clone());
            }

            List<RangeElement> r = new ArrayList<RangeElement>(range.size());
            Iterator<RangeElement> j = range.iterator();

            while (j.hasNext()) {
                r.add(j.next().clone());
            }

            List<DomainElement> d = new ArrayList<DomainElement>(domain.size());
            Iterator<DomainElement> k = domain.iterator();

            while (k.hasNext()) {
                d.add(k.next().clone());
            }

            Set<String> ns = new HashSet<String>(nullSet.size());
            Iterator<String> l = nullSet.iterator();

            while (l.hasNext()) {
                ns.add(new String(l.next()));
            }

            Set<InterpolationMethod> is = new HashSet<InterpolationMethod>(interpolationSet.size());
            Iterator<InterpolationMethod> m = interpolationSet.iterator();

            while (m.hasNext()) {
                is.add(m.next().clone());
            }

            return new Metadata(cd, r, ns, new String(nullDefault), is, interpolationDefault.clone(), new String(coverageName), d, crs, getAbstract(), getTitle(), getKeywords());
        } catch (InvalidMetadataException ime) {
            throw new RuntimeException("Invalid metadata while cloning "
                    + "Metadata. This is a software bug in WCPS.", ime);
        }

    }

//  public CellDomainElement getCellDomain( int index ) {

//

//      return cellDomain.get( index );

//

//  }

    protected void setCoverageId(int id) {
        this.coverageId = id;
    }

    public int getCoverageId() {
        return coverageId;
    }

    public String getCoverageName() {
        return coverageName;
    }

    public String getAbstract() {
        return abstractStr;
    }

    public String getTitle() {
        return titleStr;
    }

    public String getKeywords() {
        return keywordsStr;
    }

    public Iterator<CellDomainElement> getCellDomainIterator() {
        return cellDomain.iterator();

    }

    public Iterator<DomainElement> getDomainIterator() {
        return domain.iterator();
    }

    public Iterator<RangeElement> getRangeIterator() {
        return range.iterator();
    }

    public Iterator<InterpolationMethod> getInterpolationMethodIterator() {
        return interpolationSet.iterator();
    }

//    public Iterator<CrsName> getCrsSetIterator()

//    {

//        return crsset.iterator();

//    }

    public Iterator<String> getNullSetIterator() {
        return nullSet.iterator();
    }

    public int getDimension() {
        return cellDomain.size();
    }

    public int getDomainIndexByName(String name) {
        Iterator<DomainElement> i = domain.iterator();

        for (int index = 0; i.hasNext(); index++) {
            if (i.next().getName().equals(name)) {
                return index;
            }
        }

        return -1;
    }

    public DomainElement getDomainByName(String name) {
        Iterator<DomainElement> i = domain.iterator();

        for (int index = 0; i.hasNext(); index++) {
            DomainElement dom = i.next();
            if (dom.getName().equals(name)) {
                return dom;
            }
        }

        return null;
    }

    public String getNullDefault() {
        return nullDefault;

    }

    public Set<String> getNullSet() {
        return nullSet;

    }

    public boolean isRangeBoolean() {
        Iterator<RangeElement> i = range.iterator();

        while (i.hasNext()) {
            if (!i.next().isBoolean()) {
                return false;
            }
        }

        return true;

    }

    public boolean isRangeComplex() {
        Iterator<RangeElement> i = range.iterator();

        while (i.hasNext()) {
            if (!i.next().isComplex()) {
                return false;
            }
        }

        return true;

    }

    public boolean isRangeIntegral() {
        Iterator<RangeElement> i = range.iterator();

        while (i.hasNext()) {
            if (!i.next().isIntegral()) {
                return false;
            }
        }

        return true;

    }

    public boolean isRangeFloating() {
        Iterator<RangeElement> i = range.iterator();

        while (i.hasNext()) {
            if (!i.next().isFloating()) {
                return false;
            }
        }

        return true;

    }

    public boolean isRangeNumeric() {
        Iterator<RangeElement> i = range.iterator();

        while (i.hasNext()) {
            if (!i.next().isNumeric()) {
                return false;
            }
        }

        return true;

    }

    public void setCoverageName(String coverageName) throws InvalidMetadataException {
        if (coverageName == null) {
            throw new InvalidMetadataException("Metadata transformation: Coverage name cannot be null");
        }

        this.coverageName = coverageName;

    }

//  public void setCrs( String crs ) throws InvalidMetadataException {

//

//      if( crs == null ) {

//          throw new InvalidMetadataException( "Metadata transformation: CRS cannot be null" );

//      }

//      if( !(crs.equals( "" ) || crs.startsWith( "urn:ogc:def:crs:EPSG::" )) ) {

//          throw new InvalidMetadataException( "Metadata transformation: Invalid CRS" );

//      }

//      this.crs = crs;

//

//  }

//  public void setInterpolationMethodList( List<String> interpolationMethodList ) throws InvalidMetadataException {

//

//      this.interpolationMethodList = interpolationMethodList;

//      Iterator<String> is = interpolationMethodList.iterator();

//      while( is.hasNext() ) {

//          String next = is.next();

//          if( !(next.equals( "nearest neighbor" ) || next.equals( "bilinear" ) || next.equals( "bicubic" ) || next.equals( "lost area" ) || next.equals( "barycentric" )) ) {

//              throw new InvalidMetadataException( "Metadata transformation: Invalid interpolation method" );

//          }

//      }

//

//  }

//  public void setNullValue( String nullValue ) {

//

//      this.nullValue = nullValue;

//

//  }

    public void setRangeType(String type) throws InvalidMetadataException {
        Iterator<RangeElement> i = range.iterator();

        while (i.hasNext()) {
            i.next().setType(type);
        }

    }

//  public void setRangeType( List<String> types ) throws InvalidMetadataException {

//

//      if( types.size() != range.size() ) {

//          throw new InvalidMetadataException( "Invalid range type: New type has " + types.size() + " elements, but range has " + range.size() + " elements" );

//      }

//      Iterator<RangeElement> i = range.iterator();

//      Iterator<String> j = types.iterator();

//      while( i.hasNext() ) {

//          i.next().setType( j.next() );

//      }

//

//  }

//  public void updateCellDomain( int index, BigInteger lo, BigInteger hi ) throws InvalidMetadataException {

//

//      cellDomain.set( index, new CellDomainElement( lo, hi ) );

//

//  }

//  public void updateDomain( int index, Double numLo, Double numHi, String strLo, String strHi ) throws InvalidMetadataException {

//

//      DomainElement old = domain.get( index );

//      domain.set( index, new DomainElement( old.getName(), old.getType(), numLo, numHi, strLo, strHi ) );

//

//  }

    public void updateNulls(Set<String> nullSet, String nullDefault) throws InvalidMetadataException {
        if (nullSet.size() == 0) {
            throw new InvalidMetadataException("Invalid null set: At least one null value is required");
        }

        if (nullDefault == null) {
            nullDefault = "0";
        }

        if (!nullSet.contains(nullDefault)) {
            throw new InvalidMetadataException("Invalid null default: Default null value " + nullDefault + " is not part of the null set");
        }

        this.nullSet = nullSet;
        this.nullDefault = nullDefault;

    }

    public String getInterpolationDefault() {
        return interpolationDefault.getInterpolationType();
    }

    public String getNullResistanceDefault() {
        return interpolationDefault.getNullResistance();
    }

    public Wgs84Crs getCrs() {
        return crs;
    }

    /**

     * @return the X if it exists

     */
    public CellDomainElement getXCellDomain() {
        return cellX;
    }

    /**

     * @return the Y if it exists

     */
    public CellDomainElement getYCellDomain() {
        return cellY;
    }

    /**

     * @return the T if it exists

     */
    public CellDomainElement getTCellDomain() {
        return cellT;
    }

    /**

     * @param titleStr the titleStr to set

     */
    public void setTitle(String titleStr) {
        this.titleStr = titleStr;
    }

    /**

     * @param abstractStr the abstractStr to set

     */
    public void setAbstract(String abstractStr) {
        this.abstractStr = abstractStr;
    }

    /**

     * @param keywordsStr the keywordsStr to set

     */
    public void setKeywords(String keywordsStr) {
        this.keywordsStr = keywordsStr;
    }

    /**

     * @param cellDomain the cellDomain to set

     */
    public void setCellDomain(List<CellDomainElement> cellDomain) {
        this.cellDomain = cellDomain;
    }

    /**

     * @param domain the domain to set

     */
    public void setDomain(List<DomainElement> domain) {
        this.domain = domain;
    }

    /**

     * @param range the range to set

     */
    public void setRange(List<RangeElement> range) {
        this.range = range;
    }

    /**

     * @param interpolationSet the interpolationSet to set

     */
    public void setInterpolationSet(Set<InterpolationMethod> interpolationSet) {
        this.interpolationSet = interpolationSet;
    }

    /**

     * @param interpolationDefault the interpolationDefault to set

     */
    public void setDefaultInterpolation(InterpolationMethod interpolationDefault) {
        this.interpolationDefault = interpolationDefault;
    }

    /**

     *  Returns the maximal time position of the current coverage in ISO 8601 format, as string.

     * If there is no time-axis, returns null

     */
    public String getTimePeriodBeginning() {
        if (domT == null) {
            return null;
        }
        return domT.getStrLo();
    }

    /**

     *  Returns the minimal time position of the current coverage in ISO 8601 format, as string.

     * If there is no time-axis, returns null

     */
    public String getTimePeriodEnd() {
        if (domT == null) {
            return null;
        }
        return domT.getStrHi();
    }

    /**

     * Returns the time span of the current coverage, as described in the metadata (in miliseconds).

     * If there is no metadata, returns -1.

     * Note that this function returns the absolute difference. It is the administrator's

     * responsibility to make sure that the metadata values are correct.

     */
    public long getTimeSpan() {
        if (domT == null) {
            return -1;
        }
        long result = TimeString.difference(getTimePeriodEnd(), getTimePeriodBeginning());
        return Math.abs(result);
    }

    /* Returns the difference between the maximum and the minimum time axis index.

    Returns -1 if there is no metadata. */
    public long getTimeIndexesSpan() {
        if (cellT == null) {
            return -1;
        }
        BigInteger big = cellT.getHi().subtract(cellT.getLo());
        return big.longValue();
    }
}