Debian Codegraph @

src/main/org/h2/util/geometry/EWKTUtils.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.util.geometry;
7
8 import static org.h2.util.geometry.GeometryUtils.DIMENSION_SYSTEM_XY;
9 import static org.h2.util.geometry.GeometryUtils.DIMENSION_SYSTEM_XYM;
10 import static org.h2.util.geometry.GeometryUtils.DIMENSION_SYSTEM_XYZ;
11 import static org.h2.util.geometry.GeometryUtils.DIMENSION_SYSTEM_XYZM;
12 import static org.h2.util.geometry.GeometryUtils.GEOMETRY_COLLECTION;
13 import static org.h2.util.geometry.GeometryUtils.LINE_STRING;
14 import static org.h2.util.geometry.GeometryUtils.M;
15 import static org.h2.util.geometry.GeometryUtils.MULTI_LINE_STRING;
16 import static org.h2.util.geometry.GeometryUtils.MULTI_POINT;
17 import static org.h2.util.geometry.GeometryUtils.MULTI_POLYGON;
18 import static org.h2.util.geometry.GeometryUtils.POINT;
19 import static org.h2.util.geometry.GeometryUtils.POLYGON;
20 import static org.h2.util.geometry.GeometryUtils.X;
21 import static org.h2.util.geometry.GeometryUtils.Y;
22 import static org.h2.util.geometry.GeometryUtils.Z;
23
24 import java.io.ByteArrayOutputStream;
25 import java.util.ArrayList;
26
27 import org.h2.util.StringUtils;
28 import org.h2.util.geometry.EWKBUtils.EWKBTarget;
29 import org.h2.util.geometry.GeometryUtils.Target;
30
31 /**
32 * EWKT format support for GEOMETRY data type.
33 *
34 * <p>
35 * This class provides limited support of EWKT. EWKT is based on Well-known Text
36 * Representation (WKT) from OGC 06-103r4 and includes additional <a href=
37 * "https://postgis.net/docs/using_postgis_dbmanagement.html#EWKB_EWKT">PostGIS
38 * extensions</a>. SRID support from EWKT is implemented.
39 * </p>
40 */
41 public final class EWKTUtils {
42
43 /**
44 * 0-based type names of geometries, subtract 1 from type code to get index
45 * in this array.
46 */
47 static final String[] TYPES = { //
48 "POINT", //
49 "LINESTRING", //
50 "POLYGON", //
51 "MULTIPOINT", //
52 "MULTILINESTRING", //
53 "MULTIPOLYGON", //
54 "GEOMETRYCOLLECTION", //
55 };
56
57 /**
58 * Names of dimension systems.
59 */
60 private static final String[] DIMENSION_SYSTEMS = { //
61 "XY", //
62 "Z", //
63 "M", //
64 "ZM", //
65 };
66
67 /**
68 * Converter output target that writes a EWKT.
69 */
70 public static final class EWKTTarget extends Target {
71
72 private final StringBuilder output;
73
74 private final int dimensionSystem;
75
76 private int type;
77
78 private boolean inMulti;
79
80 /**
81 * Creates a new EWKT output target.
82 *
83 * @param output
84 * output stream
85 * @param dimensionSystem
86 * dimension system to use
87 */
88 public EWKTTarget(StringBuilder output, int dimensionSystem) {
89 this.output = output;
90 this.dimensionSystem = dimensionSystem;
91 }
92
93 @Override
94 protected void init(int srid) {
95 if (srid != 0) {
96 output.append("SRID=").append(srid).append(';');
97 }
98 }
99
100 @Override
101 protected void startPoint() {
102 writeHeader(POINT);
103 }
104
105 @Override
106 protected void startLineString(int numPoints) {
107 writeHeader(LINE_STRING);
108 if (numPoints == 0) {
109 output.append("EMPTY");
110 }
111 }
112
113 @Override
114 protected void startPolygon(int numInner, int numPoints) {
115 writeHeader(POLYGON);
116 if (numPoints == 0) {
117 output.append("EMPTY");
118 } else {
119 output.append('(');
120 }
121 }
122
123 @Override
124 protected void startPolygonInner(int numInner) {
125 output.append(numInner > 0 ? ", " : ", EMPTY");
126 }
127
128 @Override
129 protected void endNonEmptyPolygon() {
130 output.append(')');
131 }
132
133 @Override
134 protected void startCollection(int type, int numItems) {
135 writeHeader(type);
136 if (numItems == 0) {
137 output.append("EMPTY");
138 }
139 if (type != GEOMETRY_COLLECTION) {
140 inMulti = true;
141 }
142 }
143
144 private void writeHeader(int type) {
145 this.type = type;
146 if (inMulti) {
147 return;
148 }
149 output.append(TYPES[type - 1]);
150 switch (dimensionSystem) {
151 case DIMENSION_SYSTEM_XYZ:
152 output.append(" Z");
153 break;
154 case DIMENSION_SYSTEM_XYM:
155 output.append(" M");
156 break;
157 case DIMENSION_SYSTEM_XYZM:
158 output.append(" ZM");
159 }
160 output.append(' ');
161 }
162
163 @Override
164 protected Target startCollectionItem(int index, int total) {
165 if (index == 0) {
166 output.append('(');
167 } else {
168 output.append(", ");
169 }
170 return this;
171 }
172
173 @Override
174 protected void endCollectionItem(Target target, int type, int index, int total) {
175 if (index + 1 == total) {
176 output.append(')');
177 }
178 }
179
180 @Override
181 protected void endObject(int type) {
182 switch (type) {
183 case MULTI_POINT:
184 case MULTI_LINE_STRING:
185 case MULTI_POLYGON:
186 inMulti = false;
187 }
188 }
189
190 @Override
191 protected void addCoordinate(double x, double y, double z, double m, int index, int total) {
192 if (type == POINT && Double.isNaN(x) && Double.isNaN(y) && Double.isNaN(z) && Double.isNaN(m)) {
193 output.append("EMPTY");
194 return;
195 }
196 if (index == 0) {
197 output.append('(');
198 } else {
199 output.append(", ");
200 }
201 writeDouble(x);
202 output.append(' ');
203 writeDouble(y);
204 if ((dimensionSystem & DIMENSION_SYSTEM_XYZ) != 0) {
205 output.append(' ');
206 writeDouble(z);
207 }
208 if ((dimensionSystem & DIMENSION_SYSTEM_XYM) != 0) {
209 output.append(' ');
210 writeDouble(m);
211 }
212 if (index + 1 == total) {
213 output.append(')');
214 }
215 }
216
217 private void writeDouble(double v) {
218 String s = Double.toString(GeometryUtils.checkFinite(v));
219 if (s.endsWith(".0")) {
220 output.append(s, 0, s.length() - 2);
221 } else {
222 int idx = s.indexOf(".0E");
223 if (idx < 0) {
224 output.append(s);
225 } else {
226 output.append(s, 0, idx).append(s, idx + 2, s.length());
227 }
228 }
229 }
230
231 }
232
233 /**
234 * Helper source object for EWKT reading.
235 */
236 private static final class EWKTSource {
237 private final String ewkt;
238
239 private int offset;
240
241 EWKTSource(String ewkt) {
242 this.ewkt = ewkt;
243 }
244
245 int readSRID() {
246 skipWS();
247 int srid;
248 if (ewkt.regionMatches(true, offset, "SRID=", 0, 5)) {
249 offset += 5;
250 int idx = ewkt.indexOf(';', 5);
251 if (idx < 0) {
252 throw new IllegalArgumentException();
253 }
254 int end = idx;
255 while (ewkt.charAt(end - 1) <= ' ') {
256 end--;
257 }
258 srid = Integer.parseInt(StringUtils.trimSubstring(ewkt, offset, end));
259 offset = idx + 1;
260 } else {
261 srid = 0;
262 }
263 return srid;
264 }
265
266 void read(char symbol) {
267 skipWS();
268 int len = ewkt.length();
269 if (offset >= len) {
270 throw new IllegalArgumentException();
271 }
272 if (ewkt.charAt(offset) != symbol) {
273 throw new IllegalArgumentException();
274 }
275 offset++;
276 }
277
278 int readType() {
279 skipWS();
280 int len = ewkt.length();
281 if (offset >= len) {
282 throw new IllegalArgumentException();
283 }
284 int result = 0;
285 char ch = ewkt.charAt(offset);
286 switch (ch) {
287 case 'P':
288 case 'p':
289 result = match("POINT", POINT);
290 if (result == 0) {
291 result = match("POLYGON", POLYGON);
292 }
293 break;
294 case 'L':
295 case 'l':
296 result = match("LINESTRING", LINE_STRING);
297 break;
298 case 'M':
299 case 'm':
300 if (match("MULTI", 1) != 0) {
301 result = match("POINT", MULTI_POINT);
302 if (result == 0) {
303 result = match("POLYGON", MULTI_POLYGON);
304 if (result == 0) {
305 result = match("LINESTRING", MULTI_LINE_STRING);
306 }
307 }
308 }
309 break;
310 case 'G':
311 case 'g':
312 result = match("GEOMETRYCOLLECTION", GEOMETRY_COLLECTION);
313 break;
314 }
315 if (result == 0) {
316 throw new IllegalArgumentException();
317 }
318 return result;
319 }
320
321 int readDimensionSystem() {
322 int o = offset;
323 skipWS();
324 int len = ewkt.length();
325 if (offset >= len) {
326 throw new IllegalArgumentException();
327 }
328 int result;
329 char ch = ewkt.charAt(offset);
330 switch (ch) {
331 case 'M':
332 case 'm':
333 result = DIMENSION_SYSTEM_XYM;
334 offset++;
335 break;
336 case 'Z':
337 case 'z':
338 offset++;
339 if (offset >= len) {
340 result = DIMENSION_SYSTEM_XYZ;
341 } else {
342 ch = ewkt.charAt(offset);
343 if (ch == 'M' || ch == 'm') {
344 offset++;
345 result = DIMENSION_SYSTEM_XYZM;
346 } else {
347 result = DIMENSION_SYSTEM_XYZ;
348 }
349 }
350 break;
351 default:
352 result = DIMENSION_SYSTEM_XY;
353 if (o != offset) {
354 // Token is already terminated by a whitespace
355 return result;
356 }
357 }
358 checkStringEnd(len);
359 return result;
360 }
361
362 boolean readEmpty() {
363 skipWS();
364 int len = ewkt.length();
365 if (offset >= len) {
366 throw new IllegalArgumentException();
367 }
368 if (ewkt.charAt(offset) == '(') {
369 offset++;
370 return false;
371 }
372 if (match("EMPTY", 1) != 0) {
373 checkStringEnd(len);
374 return true;
375 }
376 throw new IllegalArgumentException();
377 }
378
379 private int match(String token, int code) {
380 int l = token.length();
381 if (offset <= ewkt.length() - l && ewkt.regionMatches(true, offset, token, 0, l)) {
382 offset += l;
383 } else {
384 code = 0;
385 }
386 return code;
387 }
388
389 private void checkStringEnd(int len) {
390 if (offset < len) {
391 char ch = ewkt.charAt(offset);
392 if (ch > ' ' && ch != '(' && ch != ')' && ch != ',') {
393 throw new IllegalArgumentException();
394 }
395 }
396 }
397
398 public boolean hasCoordinate() {
399 skipWS();
400 if (offset >= ewkt.length()) {
401 return false;
402 }
403 return isNumberStart(ewkt.charAt(offset));
404 }
405
406 public double readCoordinate() {
407 skipWS();
408 int len = ewkt.length();
409 if (offset >= len) {
410 throw new IllegalArgumentException();
411 }
412 char ch = ewkt.charAt(offset);
413 if (!isNumberStart(ch)) {
414 throw new IllegalArgumentException();
415 }
416 int start = offset++;
417 while (offset < len && isNumberPart(ch = ewkt.charAt(offset))) {
418 offset++;
419 }
420 if (offset < len) {
421 if (ch > ' ' && ch != ')' && ch != ',') {
422 throw new IllegalArgumentException();
423 }
424 }
425 Double d = Double.parseDouble(ewkt.substring(start, offset));
426 return d == 0 ? 0 : d;
427 }
428
429 private static boolean isNumberStart(char ch) {
430 if (ch >= '0' && ch <= '9') {
431 return true;
432 }
433 switch (ch) {
434 case '+':
435 case '-':
436 case '.':
437 return true;
438 default:
439 return false;
440 }
441 }
442
443 private static boolean isNumberPart(char ch) {
444 if (ch >= '0' && ch <= '9') {
445 return true;
446 }
447 switch (ch) {
448 case '+':
449 case '-':
450 case '.':
451 case 'E':
452 case 'e':
453 return true;
454 default:
455 return false;
456 }
457 }
458
459 public boolean hasMoreCoordinates() {
460 skipWS();
461 if (offset >= ewkt.length()) {
462 throw new IllegalArgumentException();
463 }
464 switch (ewkt.charAt(offset)) {
465 case ',':
466 offset++;
467 return true;
468 case ')':
469 offset++;
470 return false;
471 default:
472 throw new IllegalArgumentException();
473 }
474 }
475
476 boolean hasData() {
477 skipWS();
478 return offset < ewkt.length();
479 }
480
481 int getItemCount() {
482 int result = 1;
483 int offset = this.offset, level = 0, len = ewkt.length();
484 while (offset < len) {
485 switch (ewkt.charAt(offset++)) {
486 case ',':
487 if (level == 0) {
488 result++;
489 }
490 break;
491 case '(':
492 level++;
493 break;
494 case ')':
495 if (--level < 0) {
496 return result;
497 }
498 }
499 }
500 throw new IllegalArgumentException();
501 }
502
503 private void skipWS() {
504 for (int len = ewkt.length(); offset < len && ewkt.charAt(offset) <= ' '; offset++) {
505 }
506 }
507
508 @Override
509 public String toString() {
510 return new StringBuilder(ewkt.length() + 3).append(ewkt, 0, offset).append("<*>")
511 .append(ewkt, offset, ewkt.length()).toString();
512 }
513
514 }
515
516 /**
517 * Converts EWKB to EWKT.
518 *
519 * @param ewkb
520 * source EWKB
521 * @return EWKT representation
522 */
523 public static String ewkb2ewkt(byte[] ewkb) {
524 return ewkb2ewkt(ewkb, EWKBUtils.getDimensionSystem(ewkb));
525 }
526
527 /**
528 * Converts EWKB to EWKT.
529 *
530 * @param ewkb
531 * source EWKB
532 * @param dimensionSystem
533 * dimension system
534 * @return EWKT representation
535 */
536 public static String ewkb2ewkt(byte[] ewkb, int dimensionSystem) {
537 StringBuilder output = new StringBuilder();
538 EWKBUtils.parseEWKB(ewkb, new EWKTTarget(output, dimensionSystem));
539 return output.toString();
540 }
541
542 /**
543 * Converts EWKT to EWKB.
544 *
545 * @param ewkt
546 * source EWKT
547 * @return EWKB representation
548 */
549 public static byte[] ewkt2ewkb(String ewkt) {
550 return ewkt2ewkb(ewkt, getDimensionSystem(ewkt));
551 }
552
553 /**
554 * Converts EWKT to EWKB.
555 *
556 * @param ewkt
557 * source EWKT
558 * @param dimensionSystem
559 * dimension system
560 * @return EWKB representation
561 */
562 public static byte[] ewkt2ewkb(String ewkt, int dimensionSystem) {
563 ByteArrayOutputStream output = new ByteArrayOutputStream();
564 EWKBTarget target = new EWKBTarget(output, dimensionSystem);
565 parseEWKT(ewkt, target);
566 return output.toByteArray();
567 }
568
569 /**
570 * Parses a EWKT.
571 *
572 * @param ewkt
573 * source EWKT
574 * @param target
575 * output target
576 */
577 public static void parseEWKT(String ewkt, Target target) {
578 parseEWKT(new EWKTSource(ewkt), target, 0, 0);
579 }
580
581 /**
582 * Parses geometry type and dimension system from the given string.
583 *
584 * @param s
585 * string to parse
586 * @return geometry type and dimension system in OGC geometry code format
587 * (type + dimensionSystem * 1000)
588 * @throws IllegalArgumentException
589 * if input is not valid
590 */
591 public static int parseGeometryType(String s) {
592 EWKTSource source = new EWKTSource(s);
593 int type = source.readType();
594 int dimensionSystem = 0;
595 if (source.hasData()) {
596 dimensionSystem = source.readDimensionSystem();
597 if (source.hasData()) {
598 throw new IllegalArgumentException();
599 }
600 }
601 return dimensionSystem * 1_000 + type;
602 }
603
604 /**
605 * Parses a dimension system from the given string.
606 *
607 * @param s
608 * string to parse
609 * @return dimension system, one of XYZ, XYM, or XYZM
610 * @see GeometryUtils#DIMENSION_SYSTEM_XYZ
611 * @see GeometryUtils#DIMENSION_SYSTEM_XYM
612 * @see GeometryUtils#DIMENSION_SYSTEM_XYZM
613 * @throws IllegalArgumentException
614 * if input is not valid
615 */
616 public static int parseDimensionSystem(String s) {
617 EWKTSource source = new EWKTSource(s);
618 int dimensionSystem = source.readDimensionSystem();
619 if (source.hasData() || dimensionSystem == DIMENSION_SYSTEM_XY) {
620 throw new IllegalArgumentException();
621 }
622 return dimensionSystem;
623 }
624
625 /**
626 * Formats type and dimension system as a string.
627 *
628 * @param builder
629 * string builder
630 * @param type
631 * OGC geometry code format (type + dimensionSystem * 1000)
632 * @return the specified string builder
633 * @throws IllegalArgumentException
634 * if type is not valid
635 */
636 public static StringBuilder formatGeometryTypeAndDimensionSystem(StringBuilder builder, int type) {
637 int t = type % 1_000, d = type / 1_000;
638 if (t < POINT || t > GEOMETRY_COLLECTION || d < DIMENSION_SYSTEM_XY || d > DIMENSION_SYSTEM_XYZM) {
639 throw new IllegalArgumentException();
640 }
641 builder.append(TYPES[t - 1]);
642 if (d != DIMENSION_SYSTEM_XY) {
643 builder.append(' ').append(DIMENSION_SYSTEMS[d]);
644 }
645 return builder;
646 }
647
648 /**
649 * Parses a EWKB.
650 *
651 * @param source
652 * EWKT source
653 * @param target
654 * output target
655 * @param parentType
656 * type of parent geometry collection, or 0 for the root geometry
657 * @param dimensionSystem
658 * dimension system of parent geometry
659 */
660 private static void parseEWKT(EWKTSource source, Target target, int parentType, int dimensionSystem) {
661 if (parentType == 0) {
662 target.init(source.readSRID());
663 }
664 int type;
665 switch (parentType) {
666 default: {
667 type = source.readType();
668 dimensionSystem = source.readDimensionSystem();
669 break;
670 }
671 case MULTI_POINT:
672 type = POINT;
673 break;
674 case MULTI_LINE_STRING:
675 type = LINE_STRING;
676 break;
677 case MULTI_POLYGON:
678 type = POLYGON;
679 break;
680 }
681 target.dimensionSystem(dimensionSystem);
682 switch (type) {
683 case POINT: {
684 if (parentType != 0 && parentType != MULTI_POINT && parentType != GEOMETRY_COLLECTION) {
685 throw new IllegalArgumentException();
686 }
687 boolean empty = source.readEmpty();
688 target.startPoint();
689 if (empty) {
690 target.addCoordinate(Double.NaN, Double.NaN, Double.NaN, Double.NaN, 0, 1);
691 } else {
692 addCoordinate(source, target, dimensionSystem, 0, 1);
693 source.read(')');
694 }
695 break;
696 }
697 case LINE_STRING: {
698 if (parentType != 0 && parentType != MULTI_LINE_STRING && parentType != GEOMETRY_COLLECTION) {
699 throw new IllegalArgumentException();
700 }
701 boolean empty = source.readEmpty();
702 if (empty) {
703 target.startLineString(0);
704 } else {
705 ArrayList<double[]> coordinates = new ArrayList<>();
706 do {
707 coordinates.add(readCoordinate(source, dimensionSystem));
708 } while (source.hasMoreCoordinates());
709 int numPoints = coordinates.size();
710 if (numPoints < 0 || numPoints == 1) {
711 throw new IllegalArgumentException();
712 }
713 target.startLineString(numPoints);
714 for (int i = 0; i < numPoints; i++) {
715 double[] c = coordinates.get(i);
716 target.addCoordinate(c[X], c[Y], c[Z], c[M], i, numPoints);
717 }
718 }
719 break;
720 }
721 case POLYGON: {
722 if (parentType != 0 && parentType != MULTI_POLYGON && parentType != GEOMETRY_COLLECTION) {
723 throw new IllegalArgumentException();
724 }
725 boolean empty = source.readEmpty();
726 if (empty) {
727 target.startPolygon(0, 0);
728 } else {
729 ArrayList<double[]> outer = readRing(source, dimensionSystem);
730 ArrayList<ArrayList<double[]>> inner = new ArrayList<>();
731 while (source.hasMoreCoordinates()) {
732 inner.add(readRing(source, dimensionSystem));
733 }
734 int numInner = inner.size();
735 int size = outer.size();
736 // Size may be 0 (EMPTY) or 4+
737 if (size >= 1 && size <= 3) {
738 throw new IllegalArgumentException();
739 }
740 if (size == 0 && numInner > 0) {
741 throw new IllegalArgumentException();
742 }
743 target.startPolygon(numInner, size);
744 if (size > 0) {
745 addRing(outer, target);
746 for (int i = 0; i < numInner; i++) {
747 ArrayList<double[]> ring = inner.get(i);
748 size = ring.size();
749 // Size may be 0 (EMPTY) or 4+
750 if (size >= 1 && size <= 3) {
751 throw new IllegalArgumentException();
752 }
753 target.startPolygonInner(size);
754 addRing(ring, target);
755 }
756 target.endNonEmptyPolygon();
757 }
758 }
759 break;
760 }
761 case MULTI_POINT:
762 case MULTI_LINE_STRING:
763 case MULTI_POLYGON:
764 parseCollection(source, target, type, parentType, dimensionSystem);
765 break;
766 case GEOMETRY_COLLECTION:
767 parseCollection(source, target, GEOMETRY_COLLECTION, parentType, 0);
768 break;
769 default:
770 throw new IllegalArgumentException();
771 }
772 target.endObject(type);
773 if (parentType == 0 && source.hasData()) {
774 throw new IllegalArgumentException();
775 }
776 }
777
778 private static void parseCollection(EWKTSource source, Target target, int type, int parentType,
779 int dimensionSystem) {
780 if (parentType != 0 && parentType != GEOMETRY_COLLECTION) {
781 throw new IllegalArgumentException();
782 }
783 if (source.readEmpty()) {
784 target.startCollection(type, 0);
785 } else {
786 if (type == MULTI_POINT && source.hasCoordinate()) {
787 parseMultiPointAlternative(source, target, dimensionSystem);
788 } else {
789 int numItems = source.getItemCount();
790 target.startCollection(type, numItems);
791 for (int i = 0; i < numItems; i++) {
792 if (i > 0) {
793 source.read(',');
794 }
795 Target innerTarget = target.startCollectionItem(i, numItems);
796 parseEWKT(source, innerTarget, type, dimensionSystem);
797 target.endCollectionItem(innerTarget, type, i, numItems);
798 }
799 source.read(')');
800 }
801 }
802 }
803
804 private static void parseMultiPointAlternative(EWKTSource source, Target target, int dimensionSystem) {
805 // Special case for MULTIPOINT (1 2, 3 4)
806 ArrayList<double[]> points = new ArrayList<>();
807 do {
808 points.add(readCoordinate(source, dimensionSystem));
809 } while (source.hasMoreCoordinates());
810 int numItems = points.size();
811 target.startCollection(MULTI_POINT, numItems);
812 for (int i = 0; i < points.size(); i++) {
813 Target innerTarget = target.startCollectionItem(i, numItems);
814 target.startPoint();
815 double[] c = points.get(i);
816 target.addCoordinate(c[X], c[Y], c[Z], c[M], 0, 1);
817 target.endCollectionItem(innerTarget, MULTI_POINT, i, numItems);
818 }
819 }
820
821 private static ArrayList<double[]> readRing(EWKTSource source, int dimensionSystem) {
822 if (source.readEmpty()) {
823 return new ArrayList<>(0);
824 }
825 ArrayList<double[]> result = new ArrayList<>();
826 double[] c = readCoordinate(source, dimensionSystem);
827 double startX = c[X], startY = c[Y];
828 result.add(c);
829 while (source.hasMoreCoordinates()) {
830 result.add(readCoordinate(source, dimensionSystem));
831 }
832 int size = result.size();
833 if (size < 4) {
834 throw new IllegalArgumentException();
835 }
836 c = result.get(size - 1);
837 double endX = c[X], endY = c[Y];
838 /*
839 * TODO OGC 06-103r4 determines points as equal if they have the same X
840 * and Y coordinates. Should we check Z and M here too?
841 */
842 if (startX != endX || startY != endY) {
843 throw new IllegalArgumentException();
844 }
845 return result;
846 }
847
848 private static void addRing(ArrayList<double[]> ring, Target target) {
849 for (int i = 0, size = ring.size(); i < size; i++) {
850 double[] coordinates = ring.get(i);
851 target.addCoordinate(coordinates[X], coordinates[Y], coordinates[Z], coordinates[M], i, size);
852 }
853 }
854
855 private static void addCoordinate(EWKTSource source, Target target, int dimensionSystem, int index, int total) {
856 double x = source.readCoordinate();
857 double y = source.readCoordinate();
858 double z = (dimensionSystem & DIMENSION_SYSTEM_XYZ) != 0 ? source.readCoordinate() : Double.NaN;
859 double m = (dimensionSystem & DIMENSION_SYSTEM_XYM) != 0 ? source.readCoordinate() : Double.NaN;
860 target.addCoordinate(x, y, z, m, index, total);
861 }
862
863 private static double[] readCoordinate(EWKTSource source, int dimensionSystem) {
864 double x = source.readCoordinate();
865 double y = source.readCoordinate();
866 double z = (dimensionSystem & DIMENSION_SYSTEM_XYZ) != 0 ? source.readCoordinate() : Double.NaN;
867 double m = (dimensionSystem & DIMENSION_SYSTEM_XYM) != 0 ? source.readCoordinate() : Double.NaN;
868 return new double[] { x, y, z, m };
869 }
870
871 /**
872 * Reads the dimension system from EWKT.
873 *
874 * @param ewkt
875 * EWKT source
876 * @return the dimension system
877 */
878 public static int getDimensionSystem(String ewkt) {
879 EWKTSource source = new EWKTSource(ewkt);
880 source.readSRID();
881 source.readType();
882 return source.readDimensionSystem();
883 }
884
885 private EWKTUtils() {
886 }
887
888 }