-
Notifications
You must be signed in to change notification settings - Fork 24.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add shape-type metadata to geo_shape's doc-value #50104
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
833b646
add shape-type metadata to geo_shape's doc-value
talevy bab4654
add extra dimensional information
talevy 4ed17ee
add highestDimension to the GeoValue interface
talevy 1648e29
Merge remote-tracking branch 'elastic/geoshape-doc-values' into gdv-s…
talevy cd77db6
fix extent assertions
talevy 70b81e2
Revert "fix extent assertions"
talevy c0e7521
Merge remote-tracking branch 'elastic/geoshape-doc-values' into gdv-s…
talevy d21c1bb
remove isGeometryCollection
talevy c8909a4
separate concerns of dimensionalshapetype
talevy f28f53e
Merge remote-tracking branch 'elastic/geoshape-doc-values' into gdv-s…
talevy 16adc72
clear up DimensionalShapeType serialization
talevy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
server/src/main/java/org/elasticsearch/common/geo/DimensionalShapeType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.elasticsearch.common.geo; | ||
|
||
import org.apache.lucene.store.ByteArrayDataInput; | ||
import org.apache.lucene.store.ByteBuffersDataOutput; | ||
import org.elasticsearch.geometry.Circle; | ||
import org.elasticsearch.geometry.Geometry; | ||
import org.elasticsearch.geometry.GeometryCollection; | ||
import org.elasticsearch.geometry.GeometryVisitor; | ||
import org.elasticsearch.geometry.Line; | ||
import org.elasticsearch.geometry.LinearRing; | ||
import org.elasticsearch.geometry.MultiLine; | ||
import org.elasticsearch.geometry.MultiPoint; | ||
import org.elasticsearch.geometry.MultiPolygon; | ||
import org.elasticsearch.geometry.Point; | ||
import org.elasticsearch.geometry.Polygon; | ||
import org.elasticsearch.geometry.Rectangle; | ||
import org.elasticsearch.geometry.ShapeType; | ||
|
||
import java.util.Comparator; | ||
|
||
/** | ||
* Like {@link ShapeType} but has specific | ||
* types for when the geometry is a {@link GeometryCollection} and | ||
* more information about what the highest-dimensional sub-shape | ||
* is. | ||
*/ | ||
public enum DimensionalShapeType { | ||
POINT, | ||
MULTIPOINT, | ||
LINESTRING, | ||
MULTILINESTRING, | ||
POLYGON, | ||
MULTIPOLYGON, | ||
GEOMETRYCOLLECTION_POINTS, // highest-dimensional shapes are Points | ||
GEOMETRYCOLLECTION_LINES, // highest-dimensional shapes are Lines | ||
GEOMETRYCOLLECTION_POLYGONS; // highest-dimensional shapes are Polygons | ||
|
||
private static DimensionalShapeType[] values = values(); | ||
|
||
private static Comparator<DimensionalShapeType> COMPARATOR = Comparator.comparingInt(DimensionalShapeType::centroidDimension); | ||
|
||
public static DimensionalShapeType max(DimensionalShapeType s1, DimensionalShapeType s2) { | ||
if (s1 == null) { | ||
return s2; | ||
} else if (s2 == null) { | ||
return s1; | ||
} | ||
return COMPARATOR.compare(s1, s2) >= 0 ? s1 : s2; | ||
} | ||
|
||
public void writeTo(ByteBuffersDataOutput out) { | ||
out.writeByte((byte) ordinal()); | ||
} | ||
|
||
public static DimensionalShapeType readFrom(ByteArrayDataInput in) { | ||
return values[Byte.toUnsignedInt(in.readByte())]; | ||
} | ||
|
||
public static DimensionalShapeType forGeometry(Geometry geometry) { | ||
return geometry.visit(new GeometryVisitor<>() { | ||
private DimensionalShapeType st = null; | ||
|
||
@Override | ||
public DimensionalShapeType visit(Circle circle) { | ||
st = DimensionalShapeType.max(st, DimensionalShapeType.POLYGON); | ||
return st; | ||
} | ||
|
||
@Override | ||
public DimensionalShapeType visit(Line line) { | ||
st = DimensionalShapeType.max(st, DimensionalShapeType.LINESTRING); | ||
return st; | ||
} | ||
|
||
@Override | ||
public DimensionalShapeType visit(LinearRing ring) { | ||
throw new UnsupportedOperationException("should not visit LinearRing"); | ||
} | ||
|
||
@Override | ||
public DimensionalShapeType visit(MultiLine multiLine) { | ||
st = DimensionalShapeType.max(st, DimensionalShapeType.MULTILINESTRING); | ||
return st; | ||
} | ||
|
||
@Override | ||
public DimensionalShapeType visit(MultiPoint multiPoint) { | ||
st = DimensionalShapeType.max(st, DimensionalShapeType.MULTIPOINT); | ||
return st; | ||
} | ||
|
||
@Override | ||
public DimensionalShapeType visit(MultiPolygon multiPolygon) { | ||
st = DimensionalShapeType.max(st, DimensionalShapeType.MULTIPOLYGON); | ||
return st; | ||
} | ||
|
||
@Override | ||
public DimensionalShapeType visit(Point point) { | ||
st = DimensionalShapeType.max(st, DimensionalShapeType.POINT); | ||
return st; | ||
} | ||
|
||
@Override | ||
public DimensionalShapeType visit(Polygon polygon) { | ||
st = DimensionalShapeType.max(st, DimensionalShapeType.POLYGON); | ||
return st; | ||
} | ||
|
||
@Override | ||
public DimensionalShapeType visit(Rectangle rectangle) { | ||
st = DimensionalShapeType.max(st, DimensionalShapeType.POLYGON); | ||
return st; | ||
} | ||
|
||
@Override | ||
public DimensionalShapeType visit(GeometryCollection<?> collection) { | ||
for (Geometry shape : collection) { | ||
shape.visit(this); | ||
} | ||
int dimension = st.centroidDimension(); | ||
if (dimension == 0) { | ||
return DimensionalShapeType.GEOMETRYCOLLECTION_POINTS; | ||
} else if (dimension == 1) { | ||
return DimensionalShapeType.GEOMETRYCOLLECTION_LINES; | ||
} else { | ||
return DimensionalShapeType.GEOMETRYCOLLECTION_POLYGONS; | ||
} | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* The integer representation of the dimension for the specific | ||
* dimensional shape type. This is to be used by the centroid | ||
* calculation to determine whether to add a sub-shape's centroid | ||
* to the overall shape calculation. | ||
* | ||
* @return 0 for points, 1 for lines, 2 for polygons | ||
*/ | ||
private int centroidDimension() { | ||
switch (this) { | ||
case POINT: | ||
case MULTIPOINT: | ||
case GEOMETRYCOLLECTION_POINTS: | ||
return 0; | ||
case LINESTRING: | ||
case MULTILINESTRING: | ||
case GEOMETRYCOLLECTION_LINES: | ||
return 1; | ||
case POLYGON: | ||
case MULTIPOLYGON: | ||
case GEOMETRYCOLLECTION_POLYGONS: | ||
return 2; | ||
default: | ||
throw new IllegalStateException("dimension calculation of DimensionalShapeType [" + this + "] is not supported"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
server/src/test/java/org/elasticsearch/common/geo/DimensionalShapeTypeTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.common.geo; | ||
|
||
import org.apache.lucene.store.ByteArrayDataInput; | ||
import org.apache.lucene.store.ByteBuffersDataOutput; | ||
import org.elasticsearch.test.ESTestCase; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class DimensionalShapeTypeTests extends ESTestCase { | ||
|
||
public void testValidOrdinals() { | ||
assertThat(DimensionalShapeType.values().length, equalTo(9)); | ||
assertThat(DimensionalShapeType.POINT.ordinal(), equalTo(0)); | ||
assertThat(DimensionalShapeType.MULTIPOINT.ordinal(), equalTo(1)); | ||
assertThat(DimensionalShapeType.LINESTRING.ordinal(), equalTo(2)); | ||
assertThat(DimensionalShapeType.MULTILINESTRING.ordinal(), equalTo(3)); | ||
assertThat(DimensionalShapeType.POLYGON.ordinal(), equalTo(4)); | ||
assertThat(DimensionalShapeType.MULTIPOLYGON.ordinal(), equalTo(5)); | ||
assertThat(DimensionalShapeType.GEOMETRYCOLLECTION_POINTS.ordinal(), equalTo(6)); | ||
assertThat(DimensionalShapeType.GEOMETRYCOLLECTION_LINES.ordinal(), equalTo(7)); | ||
assertThat(DimensionalShapeType.GEOMETRYCOLLECTION_POLYGONS.ordinal(), equalTo(8)); | ||
} | ||
|
||
public void testSerialization() { | ||
for (DimensionalShapeType type : DimensionalShapeType.values()) { | ||
ByteBuffersDataOutput out = new ByteBuffersDataOutput(); | ||
type.writeTo(out); | ||
ByteArrayDataInput input = new ByteArrayDataInput(out.toArrayCopy()); | ||
assertThat(DimensionalShapeType.readFrom(input), equalTo(type)); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add a comment where this 9 bytes are coming from.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will update this in the follow-up PR when this is modified to include the centroid weight!