-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Parser.java
1202 lines (1160 loc) · 49.8 KB
/
Parser.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
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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* GeoTools - The Open Source Java GIS Toolkit
* http://geotools.org
*
* (C) 2002-2008, Open Source Geospatial Foundation (OSGeo)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License.
*
* This library 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
* Lesser General Public License for more details.
*/
package org.geotools.referencing.wkt;
import static java.util.Collections.singletonMap;
import java.io.BufferedReader;
import java.text.ParseException;
import java.text.ParsePosition;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.measure.Quantity;
import javax.measure.Unit;
import javax.measure.quantity.Angle;
import javax.measure.quantity.Length;
import org.geotools.measure.Units;
import org.geotools.metadata.i18n.ErrorKeys;
import org.geotools.metadata.i18n.Errors;
import org.geotools.metadata.iso.citation.Citations;
import org.geotools.referencing.NamedIdentifier;
import org.geotools.referencing.ReferencingFactoryFinder;
import org.geotools.referencing.cs.AbstractCS;
import org.geotools.referencing.cs.DefaultCoordinateSystemAxis;
import org.geotools.referencing.cs.DirectionAlongMeridian;
import org.geotools.referencing.datum.BursaWolfParameters;
import org.geotools.referencing.datum.DefaultGeodeticDatum;
import org.geotools.referencing.datum.DefaultPrimeMeridian;
import org.geotools.referencing.datum.DefaultVerticalDatum;
import org.geotools.referencing.factory.ReferencingFactoryContainer;
import org.geotools.referencing.factory.epsg.CartesianAuthorityFactory;
import org.geotools.referencing.operation.DefiningConversion;
import org.geotools.util.Arguments;
import org.opengis.metadata.citation.Citation;
import org.opengis.parameter.ParameterNotFoundException;
import org.opengis.parameter.ParameterValue;
import org.opengis.parameter.ParameterValueGroup;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.IdentifiedObject;
import org.opengis.referencing.NoSuchIdentifierException;
import org.opengis.referencing.crs.CRSFactory;
import org.opengis.referencing.crs.CompoundCRS;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.crs.DerivedCRS;
import org.opengis.referencing.crs.EngineeringCRS;
import org.opengis.referencing.crs.GeocentricCRS;
import org.opengis.referencing.crs.GeographicCRS;
import org.opengis.referencing.crs.ProjectedCRS;
import org.opengis.referencing.crs.VerticalCRS;
import org.opengis.referencing.cs.AxisDirection;
import org.opengis.referencing.cs.CSFactory;
import org.opengis.referencing.cs.CoordinateSystem;
import org.opengis.referencing.cs.CoordinateSystemAxis;
import org.opengis.referencing.cs.EllipsoidalCS;
import org.opengis.referencing.datum.Datum;
import org.opengis.referencing.datum.DatumFactory;
import org.opengis.referencing.datum.Ellipsoid;
import org.opengis.referencing.datum.EngineeringDatum;
import org.opengis.referencing.datum.GeodeticDatum;
import org.opengis.referencing.datum.PrimeMeridian;
import org.opengis.referencing.datum.VerticalDatum;
import org.opengis.referencing.datum.VerticalDatumType;
import org.opengis.referencing.operation.Conversion;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.MathTransformFactory;
import org.opengis.referencing.operation.NoninvertibleTransformException;
import org.opengis.referencing.operation.OperationMethod;
import si.uom.NonSI;
import si.uom.SI;
import tech.units.indriya.AbstractUnit;
/**
* Parser for <A
* HREF="http://geoapi.sourceforge.net/snapshot/javadoc/org/opengis/referencing/doc-files/WKT.html"><cite>Well
* Known Text</cite> (WKT)</A>. This parser can parse {@linkplain MathTransform math transform}
* objects as well, which is part of the WKT's {@code FITTED_CS} element.
*
* @since 2.0
* @version $Id$
* @author Remi Eve
* @author Martin Desruisseaux (IRD)
* @see <A
* HREF="http://geoapi.sourceforge.net/snapshot/javadoc/org/opengis/referencing/doc-files/WKT.html">Well
* Know Text specification</A>
* @see <A HREF="http://gdal.org/wktproblems.html">OGC WKT Coordinate System Issues</A>
*/
public class Parser extends MathTransformParser {
/** For cross-version compatibility. */
private static final long serialVersionUID = -144097689843465085L;
/**
* {@code true} in order to allows the non-standard Oracle syntax. Oracle put the Bursa-Wolf
* parameters straight into the {@code DATUM} elements, without enclosing them in a {@code
* TOWGS84} element.
*/
private static final boolean ALLOW_ORACLE_SYNTAX = true;
/**
* The mapping between WKT element name and the object class to be created. Will be created by
* {@link #getTypeMap} only when first needed. Keys must be upper case.
*/
private static Map<String, Class<?>> TYPES;
/** The factory to use for creating {@linkplain Datum datum}. */
protected final DatumFactory datumFactory;
/** The factory to use for creating {@linkplain CoordinateSystem coordinate systems}. */
protected final CSFactory csFactory;
/**
* The factory to use for creating {@linkplain CoordinateReferenceSystem coordinate reference
* systems}.
*/
protected final CRSFactory crsFactory;
/** The list of {@linkplain AxisDirection axis directions} from their name. */
private final Map<String, AxisDirection> directions;
/** Constructs a parser using the default set of symbols and factories. */
public Parser() {
this(Symbols.DEFAULT);
}
/**
* Constructs a parser for the specified set of symbols using default factories.
*
* @param symbols The symbols for parsing and formatting numbers.
* @todo Pass hints in argument.
*/
public Parser(final Symbols symbols) {
this(
symbols,
ReferencingFactoryFinder.getDatumFactory(null),
ReferencingFactoryFinder.getCSFactory(null),
ReferencingFactoryFinder.getCRSFactory(null),
ReferencingFactoryFinder.getMathTransformFactory(null));
}
/**
* Constructs a parser for the specified set of symbols using the specified set of factories.
*
* @param symbols The symbols for parsing and formatting numbers.
* @param factories The factories to use.
*/
public Parser(final Symbols symbols, final ReferencingFactoryContainer factories) {
this(
symbols,
factories.getDatumFactory(),
factories.getCSFactory(),
factories.getCRSFactory(),
factories.getMathTransformFactory());
}
/**
* Constructs a parser for the specified set of symbols using the specified factories.
*
* @param symbols The symbols for parsing and formatting numbers.
* @param datumFactory The factory to use for creating {@linkplain Datum datum}.
* @param csFactory The factory to use for creating {@linkplain CoordinateSystem coordinate
* systems}.
* @param crsFactory The factory to use for creating {@linkplain CoordinateReferenceSystem
* coordinate reference systems}.
* @param mtFactory The factory to use for creating {@linkplain MathTransform math transform}
* objects.
*/
public Parser(
final Symbols symbols,
final DatumFactory datumFactory,
final CSFactory csFactory,
final CRSFactory crsFactory,
final MathTransformFactory mtFactory) {
super(symbols, mtFactory);
this.datumFactory = datumFactory;
this.csFactory = csFactory;
this.crsFactory = crsFactory;
final AxisDirection[] values = AxisDirection.values();
directions = new HashMap<>((int) Math.ceil((values.length + 1) / 0.75f), 0.75f);
for (AxisDirection value : values) {
directions.put(value.name().trim().toUpperCase(), value);
}
}
/**
* Parses a coordinate reference system element.
*
* @param text The text to be parsed.
* @return The coordinate reference system.
* @throws ParseException if the string can't be parsed.
*/
public CoordinateReferenceSystem parseCoordinateReferenceSystem(final String text)
throws ParseException {
final Element element = getTree(text, new ParsePosition(0));
final CoordinateReferenceSystem crs = parseCoordinateReferenceSystem(element);
element.close();
return crs;
}
/**
* Parses a coordinate reference system element.
*
* @return element The next element as a {@link CoordinateReferenceSystem} object.
* @throws ParseException if the next element can't be parsed.
*/
private CoordinateReferenceSystem parseCoordinateReferenceSystem(final Element element)
throws ParseException {
final Object key = element.peek();
if (key instanceof Element) {
final String keyword = ((Element) key).keyword.trim().toUpperCase(symbols.locale);
CoordinateReferenceSystem r = null;
try {
if ("GEOGCS".equals(keyword)) return r = parseGeoGCS(element);
if ("PROJCS".equals(keyword)) return r = parseProjCS(element);
if ("GEOCCS".equals(keyword)) return r = parseGeoCCS(element);
if ("VERT_CS".equals(keyword)) return r = parseVertCS(element);
if ("LOCAL_CS".equals(keyword)) return r = parseLocalCS(element);
if ("COMPD_CS".equals(keyword)) return r = parseCompdCS(element);
if ("FITTED_CS".equals(keyword)) return r = parseFittedCS(element);
} finally {
// Work around for simulating post-conditions in Java.
assert isValid(r, keyword) : element;
}
}
throw element.parseFailed(null, Errors.format(ErrorKeys.UNKNOW_TYPE_$1, key));
}
/**
* Parses the next element in the specified <cite>Well Know Text</cite> (WKT) tree.
*
* @param element The element to be parsed.
* @return The object.
* @throws ParseException if the element can't be parsed.
* @todo All sequences of <code>if ("FOO".equals(keyword))</code> in this method and other
* methods of this class and subclasses, could be optimized with a {@code switch} statement.
*/
@Override
protected Object parse(final Element element) throws ParseException {
final Object key = element.peek();
if (key instanceof Element) {
final String keyword = ((Element) key).keyword.trim().toUpperCase(symbols.locale);
Object r = null;
try {
if ("AXIS".equals(keyword)) return r = parseAxis(element, SI.METRE, true);
if ("PRIMEM".equals(keyword)) return r = parsePrimem(element, NonSI.DEGREE_ANGLE);
if ("TOWGS84".equals(keyword)) return r = parseToWGS84(element);
if ("SPHEROID".equals(keyword)) return r = parseSpheroid(element);
if ("VERT_DATUM".equals(keyword)) return r = parseVertDatum(element);
if ("LOCAL_DATUM".equals(keyword)) return r = parseLocalDatum(element);
if ("DATUM".equals(keyword))
return r = parseDatum(element, DefaultPrimeMeridian.GREENWICH);
r = parseMathTransform(element, false);
if (r != null) {
return r;
}
} finally {
// Work around for simulating post-conditions in Java.
assert isValid(r, keyword) : element;
}
}
return parseCoordinateReferenceSystem(element);
}
/**
* Checks if the parsed object is of the expected type. This is also a way to check the
* consistency of the {@link #TYPES} map.
*/
private static boolean isValid(final Object parsed, final String keyword) {
if (parsed == null) {
// Required in order to avoid AssertionError in place of ParseException.
return true;
}
final Class type = getClassOf(keyword);
return type != null && type.isInstance(parsed);
}
/**
* Returns the properties to be given to the parsed object. This method is invoked automatically
* by the parser for the {@linkplain Element#isRoot root element} only. This method expect on
* input the properties parsed from the {@code AUTHORITY} element, and returns on output the
* properties to give to the object to be created. The default implementation returns the {@code
* properties} map unchanged. Subclasses may override this method in order to add or change
* properties.
*
* <p><strong>Example:</strong> if a subclass want to add automatically an authority code when
* no {@code AUTHORITY} element was explicitly set in the WKT, then it may test for the {@link
* IdentifiedObject#IDENTIFIERS_KEY} key and add automatically an entry if this key was missing.
*
* @param properties The properties parsed from the WKT file. Entries can be added, removed or
* modified directly in this map.
* @return The properties to be given to the parsed object. This is usually {@code properties}
* (maybe after modifications), but could also be a new map.
* @since 2.3
*/
protected Map<String, Object> alterProperties(final Map<String, Object> properties) {
return properties;
}
/**
* Parses an <strong>optional</strong> "AUTHORITY" element. This element has the following
* pattern:
*
* <blockquote>
*
* <code>
* AUTHORITY["<name>", "<code>"]
* </code>
*
* </blockquote>
*
* or even
*
* <blockquote>
*
* <code>
* AUTHORITY["<name>", <code>]
* </code>
*
* </blockquote>
*
* @param parent The parent element.
* @param name The name of the parent object being parsed.
* @return A properties map with the parent name and the optional autority code.
* @throws ParseException if the "AUTHORITY" can't be parsed.
*/
private Map<String, Object> parseAuthority(final Element parent, final String name)
throws ParseException {
final boolean isRoot = parent.isRoot();
final Element element = parent.pullOptionalElement("AUTHORITY");
Map<String, Object> properties;
if (element == null) {
if (isRoot) {
properties = new HashMap<>(4);
properties.put(IdentifiedObject.NAME_KEY, name);
} else {
properties = singletonMap(IdentifiedObject.NAME_KEY, name);
}
} else {
final String auth = element.pullString("name");
// the code can be annotation marked but could be a number to
String code = element.pullOptionalString("code");
if (code == null) {
int codeNumber = element.pullInteger("code");
code = String.valueOf(codeNumber);
}
element.close();
final Citation authority = Citations.fromName(auth);
properties = new HashMap<>(4);
properties.put(IdentifiedObject.NAME_KEY, new NamedIdentifier(authority, name));
properties.put(IdentifiedObject.IDENTIFIERS_KEY, new NamedIdentifier(authority, code));
}
if (isRoot) {
properties = alterProperties(properties);
}
return properties;
}
/**
* Parses an "UNIT" element. This element has the following pattern:
*
* <blockquote>
*
* <code>
* UNIT["<name>", <conversion factor> {,<authority>}]
* </code>
*
* </blockquote>
*
* @param parent The parent element.
* @param unit The contextual unit. Usually {@link SI#METRE} or {@link SI#RADIAN}.
* @return The "UNIT" element as an {@link Unit} object.
* @throws ParseException if the "UNIT" can't be parsed.
* @todo Authority code is currently ignored. We may consider to create a subclass of {@link
* Unit} which implements {@link IdentifiedObject} in a future version.
*/
private <T extends Quantity<T>> Unit<T> parseUnit(final Element parent, final Unit<T> unit)
throws ParseException {
final Element element = parent.pullElement("UNIT");
final String name = element.pullString("name");
final double factor = element.pullDouble("factor");
parseAuthority(element, name);
element.close();
Unit<T> finalUnit = (factor != 1) ? unit.multiply(factor) : unit;
return Units.autoCorrect(finalUnit);
}
/**
* Parses an "AXIS" element. This element has the following pattern:
*
* <blockquote>
*
* <code>
* AXIS["<name>", NORTH | SOUTH | EAST | WEST | UP | DOWN | OTHER]
* </code>
*
* </blockquote>
*
* Note: there is no AUTHORITY element for AXIS element in OGC specification. However, we accept
* it anyway in order to make the parser more tolerant to non-100% compliant WKT. Note that AXIS
* is really the only element without such AUTHORITY clause and the EPSG database provides
* authority code for all axis.
*
* @param parent The parent element.
* @param unit The contextual unit. Usually {@link NonSI#DEGREE_ANGLE} or {@link SI#METRE}.
* @param required {@code true} if the axis is mandatory, or {@code false} if it is optional.
* @return The "AXIS" element as a {@link CoordinateSystemAxis} object, or {@code null} if the
* axis was not required and there is no axis object.
* @throws ParseException if the "AXIS" element can't be parsed.
*/
private CoordinateSystemAxis parseAxis(
final Element parent, final Unit<?> unit, final boolean required)
throws ParseException {
final Element element;
if (required) {
element = parent.pullElement("AXIS");
} else {
element = parent.pullOptionalElement("AXIS");
if (element == null) {
return null;
}
}
final String name = element.pullString("name");
final Element orientation = element.pullOptionalVoidElement();
final AxisDirection direction;
if (orientation != null) {
direction = directions.get(orientation.keyword.trim().toUpperCase());
} else {
String directionName = element.pullString("orientation");
direction = DirectionAlongMeridian.parse(directionName).getDirection();
}
final Map<String, ?> properties = parseAuthority(element, name); // See javadoc
element.close();
if (direction == null) {
throw element.parseFailed(null, Errors.format(ErrorKeys.UNKNOW_TYPE_$1, orientation));
}
try {
return createAxis(properties, name, direction, unit);
} catch (FactoryException exception) {
throw element.parseFailed(exception, null);
}
}
/**
* Creates an axis. If the name matches one of pre-defined axis, the pre-defined one will be
* returned. This replacement help to get more success when comparing a CS built from WKT
* against a CS built from one of Geotools's constants.
*
* @param properties Name and other properties to give to the new object. If {@code null}, the
* abbreviation will be used as the axis name.
* @param abbreviation The coordinate axis abbreviation.
* @param direction The axis direction.
* @param unit The coordinate axis unit.
* @throws FactoryException if the axis can't be created.
*/
private CoordinateSystemAxis createAxis(
Map<String, ?> properties,
final String abbreviation,
final AxisDirection direction,
final Unit<?> unit)
throws FactoryException {
final CoordinateSystemAxis candidate =
DefaultCoordinateSystemAxis.getPredefined(abbreviation, direction);
if (candidate != null && unit.equals(candidate.getUnit())) {
return candidate;
}
if (properties == null) {
properties = singletonMap(IdentifiedObject.NAME_KEY, abbreviation);
}
return csFactory.createCoordinateSystemAxis(properties, abbreviation, direction, unit);
}
/**
* Parses a "PRIMEM" element. This element has the following pattern:
*
* <blockquote>
*
* <code>
* PRIMEM["<name>", <longitude> {,<authority>}]
* </code>
*
* </blockquote>
*
* @param parent The parent element.
* @param angularUnit The contextual unit.
* @return The "PRIMEM" element as a {@link PrimeMeridian} object.
* @throws ParseException if the "PRIMEM" element can't be parsed.
*/
private PrimeMeridian parsePrimem(final Element parent, final Unit<Angle> angularUnit)
throws ParseException {
final Element element = parent.pullElement("PRIMEM");
final String name = element.pullString("name");
final double longitude = element.pullDouble("longitude");
final Map<String, ?> properties = parseAuthority(element, name);
element.close();
try {
return datumFactory.createPrimeMeridian(properties, longitude, angularUnit);
} catch (FactoryException exception) {
throw element.parseFailed(exception, null);
}
}
/**
* Parses an <strong>optional</strong> "TOWGS84" element. This element has the following
* pattern:
*
* <blockquote>
*
* <code>
* TOWGS84[<dx>, <dy>, <dz>, <ex>, <ey>, <ez>, <ppm>]
* </code>
*
* </blockquote>
*
* @param parent The parent element.
* @return The "TOWGS84" element as a {@link BursaWolfParameters} object, or {@code null} if no
* "TOWGS84" has been found.
* @throws ParseException if the "TOWGS84" can't be parsed.
*/
private static BursaWolfParameters parseToWGS84(final Element parent) throws ParseException {
final Element element = parent.pullOptionalElement("TOWGS84");
if (element == null) {
return null;
}
final BursaWolfParameters info = new BursaWolfParameters(DefaultGeodeticDatum.WGS84);
info.dx = element.pullDouble("dx");
info.dy = element.pullDouble("dy");
info.dz = element.pullDouble("dz");
if (element.peek() != null) {
info.ex = element.pullDouble("ex");
info.ey = element.pullDouble("ey");
info.ez = element.pullDouble("ez");
info.ppm = element.pullDouble("ppm");
}
element.close();
return info;
}
/**
* Parses a "SPHEROID" element. This element has the following pattern:
*
* <blockquote>
*
* <code>
* SPHEROID["<name>", <semi-major axis>, <inverse flattening> {,<authority>}]
* </code>
*
* </blockquote>
*
* @param parent The parent element.
* @return The "SPHEROID" element as an {@link Ellipsoid} object.
* @throws ParseException if the "SPHEROID" element can't be parsed.
*/
private Ellipsoid parseSpheroid(final Element parent) throws ParseException {
Element element = parent.pullElement("SPHEROID");
String name = element.pullString("name");
double semiMajorAxis = element.pullDouble("semiMajorAxis");
double inverseFlattening = element.pullDouble("inverseFlattening");
Map<String, ?> properties = parseAuthority(element, name);
element.close();
if (inverseFlattening == 0) {
// Inverse flattening null is an OGC convention for a sphere.
inverseFlattening = Double.POSITIVE_INFINITY;
}
try {
return datumFactory.createFlattenedSphere(
properties, semiMajorAxis, inverseFlattening, SI.METRE);
} catch (FactoryException exception) {
throw element.parseFailed(exception, null);
}
}
/**
* Parses a "PROJECTION" element. This element has the following pattern:
*
* <blockquote>
*
* <code>
* PROJECTION["<name>" {,<authority>}]
* </code>
*
* </blockquote>
*
* @param parent The parent element.
* @param ellipsoid The ellipsoid, or {@code null} if none.
* @param linearUnit The linear unit of the parent PROJCS element, or {@code null}.
* @param angularUnit The angular unit of the parent GEOCS element, or {@code null}.
* @return The "PROJECTION" element as a {@link ParameterValueGroup} object.
* @throws ParseException if the "PROJECTION" element can't be parsed.
*/
private ParameterValueGroup parseProjection(
final Element parent,
final Ellipsoid ellipsoid,
final Unit<Length> linearUnit,
final Unit<Angle> angularUnit)
throws ParseException {
final Element element = parent.pullElement("PROJECTION");
final String classification = element.pullString("name");
parseAuthority(element, classification);
element.close();
/*
* Set the list of parameters. NOTE: Parameters are defined in
* the parent Element (usually a "PROJCS" element), not in this
* "PROJECTION" element.
*
* We will set the semi-major and semi-minor parameters from the
* ellipsoid first. If those values were explicitly specified in
* a "PARAMETER" statement, they will overwrite the values inferred
* from the ellipsoid.
*/
final ParameterValueGroup parameters;
try {
parameters = mtFactory.getDefaultParameters(classification);
} catch (NoSuchIdentifierException exception) {
throw element.parseFailed(exception, null);
}
Element param = parent;
try {
if (ellipsoid != null) {
final Unit<Length> axisUnit = ellipsoid.getAxisUnit();
parameters.parameter("semi_major").setValue(ellipsoid.getSemiMajorAxis(), axisUnit);
parameters.parameter("semi_minor").setValue(ellipsoid.getSemiMinorAxis(), axisUnit);
}
while ((param = parent.pullOptionalElement("PARAMETER")) != null) {
final String paramName = param.pullString("name");
final double paramValue = param.pullDouble("value");
final ParameterValue<?> parameter = parameters.parameter(paramName);
final Unit<?> expected = parameter.getDescriptor().getUnit();
if (expected != null && !AbstractUnit.ONE.equals(expected)) {
if (linearUnit != null && SI.METRE.isCompatible(expected)) {
parameter.setValue(paramValue, linearUnit);
continue;
}
if (angularUnit != null && SI.RADIAN.isCompatible(expected)) {
parameter.setValue(paramValue, angularUnit);
continue;
}
}
parameter.setValue(paramValue);
}
} catch (ParameterNotFoundException exception) {
throw param.parseFailed(
exception,
Errors.format(ErrorKeys.UNEXPECTED_PARAMETER_$1, exception.getParameterName()));
}
return parameters;
}
/**
* Parses a "DATUM" element. This element has the following pattern:
*
* <blockquote>
*
* <code>
* DATUM["<name>", <spheroid> {,<to wgs84>} {,<authority>}]
* </code>
*
* </blockquote>
*
* @param parent The parent element.
* @param meridian the prime meridian.
* @return The "DATUM" element as a {@link GeodeticDatum} object.
* @throws ParseException if the "DATUM" element can't be parsed.
*/
private GeodeticDatum parseDatum(final Element parent, final PrimeMeridian meridian)
throws ParseException {
Element element = parent.pullElement("DATUM");
String name = element.pullString("name");
Ellipsoid ellipsoid = parseSpheroid(element);
BursaWolfParameters toWGS84 = parseToWGS84(element); // Optional; may be null.
Map<String, Object> properties = parseAuthority(element, name);
if (ALLOW_ORACLE_SYNTAX && (toWGS84 == null) && (element.peek() instanceof Number)) {
toWGS84 = new BursaWolfParameters(DefaultGeodeticDatum.WGS84);
toWGS84.dx = element.pullDouble("dx");
toWGS84.dy = element.pullDouble("dy");
toWGS84.dz = element.pullDouble("dz");
toWGS84.ex = element.pullDouble("ex");
toWGS84.ey = element.pullDouble("ey");
toWGS84.ez = element.pullDouble("ez");
toWGS84.ppm = element.pullDouble("ppm");
}
element.close();
if (toWGS84 != null) {
if (!(properties instanceof HashMap)) {
properties = new HashMap<>(properties);
}
properties.put(DefaultGeodeticDatum.BURSA_WOLF_KEY, toWGS84);
}
try {
return datumFactory.createGeodeticDatum(properties, ellipsoid, meridian);
} catch (FactoryException exception) {
throw element.parseFailed(exception, null);
}
}
/**
* Parses a "VERT_DATUM" element. This element has the following pattern:
*
* <blockquote>
*
* <code>
* VERT_DATUM["<name>", <datum type> {,<authority>}]
* </code>
*
* </blockquote>
*
* @param parent The parent element.
* @return The "VERT_DATUM" element as a {@link VerticalDatum} object.
* @throws ParseException if the "VERT_DATUM" element can't be parsed.
*/
private VerticalDatum parseVertDatum(final Element parent) throws ParseException {
final Element element = parent.pullElement("VERT_DATUM");
final String name = element.pullString("name");
final int datum = element.pullInteger("datum");
final Map<String, ?> properties = parseAuthority(element, name);
element.close();
final VerticalDatumType type =
DefaultVerticalDatum.getVerticalDatumTypeFromLegacyCode(datum);
if (type == null) {
throw element.parseFailed(null, Errors.format(ErrorKeys.UNKNOW_TYPE_$1, datum));
}
try {
return datumFactory.createVerticalDatum(properties, type);
} catch (FactoryException exception) {
throw element.parseFailed(exception, null);
}
}
/**
* Parses a "LOCAL_DATUM" element. This element has the following pattern:
*
* <blockquote>
*
* <code>
* LOCAL_DATUM["<name>", <datum type> {,<authority>}]
* </code>
*
* </blockquote>
*
* @param parent The parent element.
* @return The "LOCAL_DATUM" element as an {@link EngineeringDatum} object.
* @throws ParseException if the "LOCAL_DATUM" element can't be parsed.
* @todo The vertical datum type is currently ignored.
*/
private EngineeringDatum parseLocalDatum(final Element parent) throws ParseException {
final Element element = parent.pullElement("LOCAL_DATUM");
final String name = element.pullString("name");
element.pullInteger("datum");
final Map<String, ?> properties = parseAuthority(element, name);
element.close();
try {
return datumFactory.createEngineeringDatum(properties);
} catch (FactoryException exception) {
throw element.parseFailed(exception, null);
}
}
/**
* Parses a "LOCAL_CS" element. This element has the following pattern:
*
* <blockquote>
*
* <code>
* LOCAL_CS["<name>", <local datum>, <unit>, <axis>, {,<axis>}* {,<authority>}]
* </code>
*
* </blockquote>
*
* @param parent The parent element.
* @return The "LOCAL_CS" element as an {@link EngineeringCRS} object.
* @throws ParseException if the "LOCAL_CS" element can't be parsed.
* @todo The coordinate system used is always a Geotools implementation, since we don't know
* which method to invokes in the {@link CSFactory} (is it a cartesian coordinate system? a
* spherical one? etc.).
*/
static final String WILDCARD_CODE = "EPSG:" + CartesianAuthorityFactory.GENERIC_2D_CODE;
private EngineeringCRS parseLocalCS(final Element parent) throws ParseException {
Element element = parent.pullElement("LOCAL_CS");
String name = element.pullString("name");
EngineeringDatum datum = parseLocalDatum(element);
Unit<Length> linearUnit = parseUnit(element, SI.METRE);
CoordinateSystemAxis axis = parseAxis(element, linearUnit, true);
List<CoordinateSystemAxis> list = new ArrayList<>();
do {
list.add(axis);
axis = parseAxis(element, linearUnit, false);
} while (axis != null);
final Map<String, ?> properties = parseAuthority(element, name);
element.close();
final CoordinateSystem cs;
//
if (properties.containsKey("identifiers")
&& properties.get("identifiers").toString().equalsIgnoreCase(WILDCARD_CODE)) {
return CartesianAuthorityFactory.GENERIC_2D;
} else {
cs =
new AbstractCS(
singletonMap("name", name),
list.toArray(new CoordinateSystemAxis[list.size()]));
}
try {
return crsFactory.createEngineeringCRS(properties, datum, cs);
} catch (FactoryException exception) {
throw element.parseFailed(exception, null);
}
}
/**
* Parses a "GEOCCS" element. This element has the following pattern:
*
* <blockquote>
*
* <code>
* GEOCCS["<name>", <datum>, <prime meridian>, <linear unit>
* {,<axis> ,<axis> ,<axis>} {,<authority>}]
* </code>
*
* </blockquote>
*
* @param parent The parent element.
* @return The "GEOCCS" element as a {@link GeocentricCRS} object.
* @throws ParseException if the "GEOCCS" element can't be parsed.
*/
private GeocentricCRS parseGeoCCS(final Element parent) throws ParseException {
final Element element = parent.pullElement("GEOCCS");
final String name = element.pullString("name");
final Map<String, ?> properties = parseAuthority(element, name);
final PrimeMeridian meridian = parsePrimem(element, NonSI.DEGREE_ANGLE);
final GeodeticDatum datum = parseDatum(element, meridian);
final Unit<Length> linearUnit = parseUnit(element, SI.METRE);
CoordinateSystemAxis axis1, axis2;
CoordinateSystemAxis axis0 = parseAxis(element, linearUnit, false);
try {
if (axis0 != null) {
axis1 = parseAxis(element, linearUnit, true);
axis2 = parseAxis(element, linearUnit, true);
} else {
// Those default values are part of WKT specification.
axis0 = createAxis(null, "X", AxisDirection.OTHER, linearUnit);
axis1 = createAxis(null, "Y", AxisDirection.EAST, linearUnit);
axis2 = createAxis(null, "Z", AxisDirection.NORTH, linearUnit);
}
element.close();
return crsFactory.createGeocentricCRS(
properties,
datum,
csFactory.createCartesianCS(properties, axis0, axis1, axis2));
} catch (FactoryException exception) {
throw element.parseFailed(exception, null);
}
}
/**
* Parses an <strong>optional</strong> "VERT_CS" element. This element has the following
* pattern:
*
* <blockquote>
*
* <code>
* VERT_CS["<name>", <vert datum>, <linear unit>, {<axis>,} {,<authority>}]
* </code>
*
* </blockquote>
*
* @param parent The parent element.
* @return The "VERT_CS" element as a {@link VerticalCRS} object.
* @throws ParseException if the "VERT_CS" element can't be parsed.
*/
private VerticalCRS parseVertCS(final Element parent) throws ParseException {
final Element element = parent.pullElement("VERT_CS");
if (element == null) {
return null;
}
String name = element.pullString("name");
VerticalDatum datum = parseVertDatum(element);
Unit<Length> linearUnit = parseUnit(element, SI.METRE);
CoordinateSystemAxis axis = parseAxis(element, linearUnit, false);
Map<String, ?> properties = parseAuthority(element, name);
element.close();
try {
if (axis == null) {
axis = createAxis(null, "Z", AxisDirection.UP, linearUnit);
}
return crsFactory.createVerticalCRS(
properties,
datum,
csFactory.createVerticalCS(singletonMap("name", name), axis));
} catch (FactoryException exception) {
throw element.parseFailed(exception, null);
}
}
/**
* Parses a "GEOGCS" element. This element has the following pattern:
*
* <blockquote>
*
* <code>
* GEOGCS["<name>", <datum>, <prime meridian>, <angular unit> {,<twin axes>} {,<authority>}]
* </code>
*
* </blockquote>
*
* @param parent The parent element.
* @return The "GEOGCS" element as a {@link GeographicCRS} object.
* @throws ParseException if the "GEOGCS" element can't be parsed.
*/
private GeographicCRS parseGeoGCS(final Element parent) throws ParseException {
Element element = parent.pullElement("GEOGCS");
String name = element.pullString("name");
Map<String, ?> properties = parseAuthority(element, name);
Unit<Angle> angularUnit = parseUnit(element, SI.RADIAN);
PrimeMeridian meridian = parsePrimem(element, angularUnit);
GeodeticDatum datum = parseDatum(element, meridian);
CoordinateSystemAxis axis0 = parseAxis(element, angularUnit, false);
CoordinateSystemAxis axis1;
CoordinateSystemAxis axis2 = null;
try {
if (axis0 != null) {
axis1 = parseAxis(element, angularUnit, true);
if (axis1 != null) {
axis2 = parseAxis(element, SI.METRE, false);
}
} else {
// Those default values are part of WKT specification.
axis0 = createAxis(null, "Lon", AxisDirection.EAST, angularUnit);
axis1 = createAxis(null, "Lat", AxisDirection.NORTH, angularUnit);
}
element.close();
EllipsoidalCS ellipsoidalCS;
if (axis2 != null) {
ellipsoidalCS = csFactory.createEllipsoidalCS(properties, axis0, axis1, axis2);
} else {
ellipsoidalCS = csFactory.createEllipsoidalCS(properties, axis0, axis1);
}
return crsFactory.createGeographicCRS(properties, datum, ellipsoidalCS);
} catch (FactoryException exception) {
throw element.parseFailed(exception, null);
}
}
/**
* Parses a "PROJCS" element. This element has the following pattern:
*
* <blockquote>
*
* <code>
* PROJCS["<name>", <geographic cs>, <projection>, {<parameter>,}*,
* <linear unit> {,<twin axes>}{,<authority>}]
* </code>
*
* </blockquote>
*
* @param parent The parent element.
* @return The "PROJCS" element as a {@link ProjectedCRS} object.
* @throws ParseException if the "GEOGCS" element can't be parsed.
*/
private ProjectedCRS parseProjCS(final Element parent) throws ParseException {
Element element = parent.pullElement("PROJCS");
String name = element.pullString("name");
Map<String, ?> properties = parseAuthority(element, name);
GeographicCRS geoCRS = parseGeoGCS(element);
Ellipsoid ellipsoid = geoCRS.getDatum().getEllipsoid();
Unit<Length> linearUnit = parseUnit(element, SI.METRE);
Unit<Angle> angularUnit =
geoCRS.getCoordinateSystem().getAxis(0).getUnit().asType(Angle.class);
ParameterValueGroup projection =
parseProjection(element, ellipsoid, linearUnit, angularUnit);
CoordinateSystemAxis axis0 = parseAxis(element, linearUnit, false);
CoordinateSystemAxis axis1;
try {
if (axis0 != null) {
axis1 = parseAxis(element, linearUnit, true);
} else {
// Those default values are part of WKT specification.
axis0 = createAxis(null, "X", AxisDirection.EAST, linearUnit);
axis1 = createAxis(null, "Y", AxisDirection.NORTH, linearUnit);
}
element.pullOptionalElement(
"EXTENSION"); // application specific extensions can be ignored
element.close();
final Conversion conversion = new DefiningConversion(name, projection);
return crsFactory.createProjectedCRS(
properties,