|
| 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 | +package org.elasticsearch.common.geo; |
| 20 | + |
| 21 | +import org.apache.lucene.store.ByteArrayDataInput; |
| 22 | +import org.apache.lucene.store.ByteBuffersDataOutput; |
| 23 | +import org.elasticsearch.geometry.Circle; |
| 24 | +import org.elasticsearch.geometry.Geometry; |
| 25 | +import org.elasticsearch.geometry.GeometryCollection; |
| 26 | +import org.elasticsearch.geometry.GeometryVisitor; |
| 27 | +import org.elasticsearch.geometry.Line; |
| 28 | +import org.elasticsearch.geometry.LinearRing; |
| 29 | +import org.elasticsearch.geometry.MultiLine; |
| 30 | +import org.elasticsearch.geometry.MultiPoint; |
| 31 | +import org.elasticsearch.geometry.MultiPolygon; |
| 32 | +import org.elasticsearch.geometry.Point; |
| 33 | +import org.elasticsearch.geometry.Polygon; |
| 34 | +import org.elasticsearch.geometry.Rectangle; |
| 35 | +import org.elasticsearch.geometry.ShapeType; |
| 36 | + |
| 37 | +import java.util.Comparator; |
| 38 | + |
| 39 | +/** |
| 40 | + * Like {@link ShapeType} but has specific |
| 41 | + * types for when the geometry is a {@link GeometryCollection} and |
| 42 | + * more information about what the highest-dimensional sub-shape |
| 43 | + * is. |
| 44 | + */ |
| 45 | +public enum DimensionalShapeType { |
| 46 | + POINT, |
| 47 | + MULTIPOINT, |
| 48 | + LINESTRING, |
| 49 | + MULTILINESTRING, |
| 50 | + POLYGON, |
| 51 | + MULTIPOLYGON, |
| 52 | + GEOMETRYCOLLECTION_POINTS, // highest-dimensional shapes are Points |
| 53 | + GEOMETRYCOLLECTION_LINES, // highest-dimensional shapes are Lines |
| 54 | + GEOMETRYCOLLECTION_POLYGONS; // highest-dimensional shapes are Polygons |
| 55 | + |
| 56 | + private static DimensionalShapeType[] values = values(); |
| 57 | + |
| 58 | + private static Comparator<DimensionalShapeType> COMPARATOR = Comparator.comparingInt(DimensionalShapeType::centroidDimension); |
| 59 | + |
| 60 | + public static DimensionalShapeType max(DimensionalShapeType s1, DimensionalShapeType s2) { |
| 61 | + if (s1 == null) { |
| 62 | + return s2; |
| 63 | + } else if (s2 == null) { |
| 64 | + return s1; |
| 65 | + } |
| 66 | + return COMPARATOR.compare(s1, s2) >= 0 ? s1 : s2; |
| 67 | + } |
| 68 | + |
| 69 | + public void writeTo(ByteBuffersDataOutput out) { |
| 70 | + out.writeByte((byte) ordinal()); |
| 71 | + } |
| 72 | + |
| 73 | + public static DimensionalShapeType readFrom(ByteArrayDataInput in) { |
| 74 | + return values[Byte.toUnsignedInt(in.readByte())]; |
| 75 | + } |
| 76 | + |
| 77 | + public static DimensionalShapeType forGeometry(Geometry geometry) { |
| 78 | + return geometry.visit(new GeometryVisitor<>() { |
| 79 | + private DimensionalShapeType st = null; |
| 80 | + |
| 81 | + @Override |
| 82 | + public DimensionalShapeType visit(Circle circle) { |
| 83 | + st = DimensionalShapeType.max(st, DimensionalShapeType.POLYGON); |
| 84 | + return st; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public DimensionalShapeType visit(Line line) { |
| 89 | + st = DimensionalShapeType.max(st, DimensionalShapeType.LINESTRING); |
| 90 | + return st; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public DimensionalShapeType visit(LinearRing ring) { |
| 95 | + throw new UnsupportedOperationException("should not visit LinearRing"); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public DimensionalShapeType visit(MultiLine multiLine) { |
| 100 | + st = DimensionalShapeType.max(st, DimensionalShapeType.MULTILINESTRING); |
| 101 | + return st; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public DimensionalShapeType visit(MultiPoint multiPoint) { |
| 106 | + st = DimensionalShapeType.max(st, DimensionalShapeType.MULTIPOINT); |
| 107 | + return st; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public DimensionalShapeType visit(MultiPolygon multiPolygon) { |
| 112 | + st = DimensionalShapeType.max(st, DimensionalShapeType.MULTIPOLYGON); |
| 113 | + return st; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public DimensionalShapeType visit(Point point) { |
| 118 | + st = DimensionalShapeType.max(st, DimensionalShapeType.POINT); |
| 119 | + return st; |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public DimensionalShapeType visit(Polygon polygon) { |
| 124 | + st = DimensionalShapeType.max(st, DimensionalShapeType.POLYGON); |
| 125 | + return st; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public DimensionalShapeType visit(Rectangle rectangle) { |
| 130 | + st = DimensionalShapeType.max(st, DimensionalShapeType.POLYGON); |
| 131 | + return st; |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public DimensionalShapeType visit(GeometryCollection<?> collection) { |
| 136 | + for (Geometry shape : collection) { |
| 137 | + shape.visit(this); |
| 138 | + } |
| 139 | + int dimension = st.centroidDimension(); |
| 140 | + if (dimension == 0) { |
| 141 | + return DimensionalShapeType.GEOMETRYCOLLECTION_POINTS; |
| 142 | + } else if (dimension == 1) { |
| 143 | + return DimensionalShapeType.GEOMETRYCOLLECTION_LINES; |
| 144 | + } else { |
| 145 | + return DimensionalShapeType.GEOMETRYCOLLECTION_POLYGONS; |
| 146 | + } |
| 147 | + } |
| 148 | + }); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * The integer representation of the dimension for the specific |
| 153 | + * dimensional shape type. This is to be used by the centroid |
| 154 | + * calculation to determine whether to add a sub-shape's centroid |
| 155 | + * to the overall shape calculation. |
| 156 | + * |
| 157 | + * @return 0 for points, 1 for lines, 2 for polygons |
| 158 | + */ |
| 159 | + private int centroidDimension() { |
| 160 | + switch (this) { |
| 161 | + case POINT: |
| 162 | + case MULTIPOINT: |
| 163 | + case GEOMETRYCOLLECTION_POINTS: |
| 164 | + return 0; |
| 165 | + case LINESTRING: |
| 166 | + case MULTILINESTRING: |
| 167 | + case GEOMETRYCOLLECTION_LINES: |
| 168 | + return 1; |
| 169 | + case POLYGON: |
| 170 | + case MULTIPOLYGON: |
| 171 | + case GEOMETRYCOLLECTION_POLYGONS: |
| 172 | + return 2; |
| 173 | + default: |
| 174 | + throw new IllegalStateException("dimension calculation of DimensionalShapeType [" + this + "] is not supported"); |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments