|
| 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 checks that lats are between -90 and +90 and lons are between -180 and +180 and altitude is present only if |
| 37 | + * ignoreZValue is set to true |
| 38 | + */ |
| 39 | +public class GeographyValidator implements GeometryValidator { |
| 40 | + |
| 41 | + /** |
| 42 | + * Minimum longitude value. |
| 43 | + */ |
| 44 | + private static final double MIN_LON_INCL = -180.0D; |
| 45 | + |
| 46 | + /** |
| 47 | + * Maximum longitude value. |
| 48 | + */ |
| 49 | + private static final double MAX_LON_INCL = 180.0D; |
| 50 | + |
| 51 | + /** |
| 52 | + * Minimum latitude value. |
| 53 | + */ |
| 54 | + private static final double MIN_LAT_INCL = -90.0D; |
| 55 | + |
| 56 | + /** |
| 57 | + * Maximum latitude value. |
| 58 | + */ |
| 59 | + private static final double MAX_LAT_INCL = 90.0D; |
| 60 | + |
| 61 | + private final boolean ignoreZValue; |
| 62 | + |
| 63 | + public GeographyValidator(boolean ignoreZValue) { |
| 64 | + this.ignoreZValue = ignoreZValue; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * validates latitude value is within standard +/-90 coordinate bounds |
| 69 | + */ |
| 70 | + protected void checkLatitude(double latitude) { |
| 71 | + if (Double.isNaN(latitude) || latitude < MIN_LAT_INCL || latitude > MAX_LAT_INCL) { |
| 72 | + throw new IllegalArgumentException( |
| 73 | + "invalid latitude " + latitude + "; must be between " + MIN_LAT_INCL + " and " + MAX_LAT_INCL); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * validates longitude value is within standard +/-180 coordinate bounds |
| 79 | + */ |
| 80 | + protected void checkLongitude(double longitude) { |
| 81 | + if (Double.isNaN(longitude) || longitude < MIN_LON_INCL || longitude > MAX_LON_INCL) { |
| 82 | + throw new IllegalArgumentException( |
| 83 | + "invalid longitude " + longitude + "; must be between " + MIN_LON_INCL + " and " + MAX_LON_INCL); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + protected void checkAltitude(double zValue) { |
| 88 | + if (ignoreZValue == false && Double.isNaN(zValue) == false) { |
| 89 | + throw new IllegalArgumentException("found Z value [" + zValue + "] but [ignore_z_value] " |
| 90 | + + "parameter is [" + ignoreZValue + "]"); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void validate(Geometry geometry) { |
| 96 | + geometry.visit(new GeometryVisitor<Void, RuntimeException>() { |
| 97 | + |
| 98 | + @Override |
| 99 | + public Void visit(Circle circle) throws RuntimeException { |
| 100 | + checkLatitude(circle.getLat()); |
| 101 | + checkLongitude(circle.getLon()); |
| 102 | + checkAltitude(circle.getAlt()); |
| 103 | + return null; |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public Void visit(GeometryCollection<?> collection) throws RuntimeException { |
| 108 | + for (Geometry g : collection) { |
| 109 | + g.visit(this); |
| 110 | + } |
| 111 | + return null; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public Void visit(Line line) throws RuntimeException { |
| 116 | + for (int i = 0; i < line.length(); i++) { |
| 117 | + checkLatitude(line.getLat(i)); |
| 118 | + checkLongitude(line.getLon(i)); |
| 119 | + checkAltitude(line.getAlt(i)); |
| 120 | + } |
| 121 | + return null; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public Void visit(LinearRing ring) throws RuntimeException { |
| 126 | + for (int i = 0; i < ring.length(); i++) { |
| 127 | + checkLatitude(ring.getLat(i)); |
| 128 | + checkLongitude(ring.getLon(i)); |
| 129 | + checkAltitude(ring.getAlt(i)); |
| 130 | + } |
| 131 | + return null; |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public Void visit(MultiLine multiLine) throws RuntimeException { |
| 136 | + return visit((GeometryCollection<?>) multiLine); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public Void visit(MultiPoint multiPoint) throws RuntimeException { |
| 141 | + return visit((GeometryCollection<?>) multiPoint); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public Void visit(MultiPolygon multiPolygon) throws RuntimeException { |
| 146 | + return visit((GeometryCollection<?>) multiPolygon); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public Void visit(Point point) throws RuntimeException { |
| 151 | + checkLatitude(point.getLat()); |
| 152 | + checkLongitude(point.getLon()); |
| 153 | + checkAltitude(point.getAlt()); |
| 154 | + return null; |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public Void visit(Polygon polygon) throws RuntimeException { |
| 159 | + polygon.getPolygon().visit(this); |
| 160 | + for (int i = 0; i < polygon.getNumberOfHoles(); i++) { |
| 161 | + polygon.getHole(i).visit(this); |
| 162 | + } |
| 163 | + return null; |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public Void visit(Rectangle rectangle) throws RuntimeException { |
| 168 | + checkLatitude(rectangle.getMinLat()); |
| 169 | + checkLatitude(rectangle.getMaxLat()); |
| 170 | + checkLongitude(rectangle.getMinLon()); |
| 171 | + checkLongitude(rectangle.getMaxLon()); |
| 172 | + checkAltitude(rectangle.getMinAlt()); |
| 173 | + checkAltitude(rectangle.getMaxAlt()); |
| 174 | + return null; |
| 175 | + } |
| 176 | + }); |
| 177 | + } |
| 178 | +} |
0 commit comments