|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.geo.utils; |
| 21 | + |
| 22 | +import org.elasticsearch.geo.geometry.Circle; |
| 23 | +import org.elasticsearch.geo.geometry.Geometry; |
| 24 | +import org.elasticsearch.geo.geometry.GeometryCollection; |
| 25 | +import org.elasticsearch.geo.geometry.GeometryVisitor; |
| 26 | +import org.elasticsearch.geo.geometry.Line; |
| 27 | +import org.elasticsearch.geo.geometry.LinearRing; |
| 28 | +import org.elasticsearch.geo.geometry.MultiLine; |
| 29 | +import org.elasticsearch.geo.geometry.MultiPoint; |
| 30 | +import org.elasticsearch.geo.geometry.MultiPolygon; |
| 31 | +import org.elasticsearch.geo.geometry.Point; |
| 32 | +import org.elasticsearch.geo.geometry.Polygon; |
| 33 | +import org.elasticsearch.geo.geometry.Rectangle; |
| 34 | + |
| 35 | +/** |
| 36 | + * Validator that only checks that altitude only shows up if ignoreZValue is set to true. |
| 37 | + */ |
| 38 | +public class StandardValidator implements GeometryValidator { |
| 39 | + |
| 40 | + private final boolean ignoreZValue; |
| 41 | + |
| 42 | + public StandardValidator(boolean ignoreZValue) { |
| 43 | + this.ignoreZValue = ignoreZValue; |
| 44 | + } |
| 45 | + |
| 46 | + protected void checkAltitude(double zValue) { |
| 47 | + if (ignoreZValue == false && Double.isNaN(zValue) == false) { |
| 48 | + throw new IllegalArgumentException("found Z value [" + zValue + "] but [ignore_z_value] " |
| 49 | + + "parameter is [" + ignoreZValue + "]"); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void validate(Geometry geometry) { |
| 55 | + if (ignoreZValue == false) { |
| 56 | + geometry.visit(new GeometryVisitor<Void, RuntimeException>() { |
| 57 | + |
| 58 | + @Override |
| 59 | + public Void visit(Circle circle) throws RuntimeException { |
| 60 | + checkAltitude(circle.getAlt()); |
| 61 | + return null; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public Void visit(GeometryCollection<?> collection) throws RuntimeException { |
| 66 | + for (Geometry g : collection) { |
| 67 | + g.visit(this); |
| 68 | + } |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public Void visit(Line line) throws RuntimeException { |
| 74 | + for (int i = 0; i < line.length(); i++) { |
| 75 | + checkAltitude(line.getAlt(i)); |
| 76 | + } |
| 77 | + return null; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public Void visit(LinearRing ring) throws RuntimeException { |
| 82 | + for (int i = 0; i < ring.length(); i++) { |
| 83 | + checkAltitude(ring.getAlt(i)); |
| 84 | + } |
| 85 | + return null; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public Void visit(MultiLine multiLine) throws RuntimeException { |
| 90 | + return visit((GeometryCollection<?>) multiLine); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public Void visit(MultiPoint multiPoint) throws RuntimeException { |
| 95 | + return visit((GeometryCollection<?>) multiPoint); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public Void visit(MultiPolygon multiPolygon) throws RuntimeException { |
| 100 | + return visit((GeometryCollection<?>) multiPolygon); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public Void visit(Point point) throws RuntimeException { |
| 105 | + checkAltitude(point.getAlt()); |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public Void visit(Polygon polygon) throws RuntimeException { |
| 111 | + polygon.getPolygon().visit(this); |
| 112 | + for (int i = 0; i < polygon.getNumberOfHoles(); i++) { |
| 113 | + polygon.getHole(i).visit(this); |
| 114 | + } |
| 115 | + return null; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public Void visit(Rectangle rectangle) throws RuntimeException { |
| 120 | + checkAltitude(rectangle.getMinAlt()); |
| 121 | + checkAltitude(rectangle.getMaxAlt()); |
| 122 | + return null; |
| 123 | + } |
| 124 | + }); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
0 commit comments