Skip to content

Geo: Refactors libs/geo parser to provide serialization logic as well #43717

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

Merged
merged 1 commit into from
Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.geo.geometry.Geometry;

import java.io.IOException;
import java.text.ParseException;

/**
* Geometry serializer/deserializer
*/
public interface GeometryFormat {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we have these formats be required to provide their name via something like a String getName()? or type?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have any particular use for that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

guess not!


/**
* Parser JSON representation of a geometry
*/
Geometry fromXContent(XContentParser parser) throws IOException, ParseException;

/**
* Serializes the geometry into its JSON representation
*/
XContentBuilder toXContent(Geometry geometry, XContentBuilder builder, ToXContent.Params params) throws IOException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package org.elasticsearch.common.geo;

import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.geo.geometry.Geometry;
import org.elasticsearch.geo.utils.GeographyValidator;
Expand Down Expand Up @@ -47,14 +49,64 @@ public GeometryParser(boolean rightOrientation, boolean coerce, boolean ignoreZV
/**
* Parses supplied XContent into Geometry
*/
public Geometry parse(XContentParser parser) throws IOException,
ParseException {
public Geometry parse(XContentParser parser) throws IOException, ParseException {
return geometryFormat(parser).fromXContent(parser);
}

/**
* Returns a geometry format object that can parse and then serialize the object back to the same format.
*/
public GeometryFormat geometryFormat(XContentParser parser) {
if (parser.currentToken() == XContentParser.Token.VALUE_NULL) {
return null;
return new GeometryFormat() {
@Override
public Geometry fromXContent(XContentParser parser) throws IOException {
return null;
}

@Override
public XContentBuilder toXContent(Geometry geometry, XContentBuilder builder, ToXContent.Params params) throws IOException {
if (geometry != null) {
// We don't know the format of the original geometry - so going with default
return GeoJson.toXContent(geometry, builder, params);
} else {
return builder.nullValue();
}
}
};
} else if (parser.currentToken() == XContentParser.Token.START_OBJECT) {
return geoJsonParser.fromXContent(parser);
return new GeometryFormat() {
@Override
public Geometry fromXContent(XContentParser parser) throws IOException {
return geoJsonParser.fromXContent(parser);
}

@Override
public XContentBuilder toXContent(Geometry geometry, XContentBuilder builder, ToXContent.Params params) throws IOException {
if (geometry != null) {
return GeoJson.toXContent(geometry, builder, params);
} else {
return builder.nullValue();
}
}
};
} else if (parser.currentToken() == XContentParser.Token.VALUE_STRING) {
return wellKnownTextParser.fromWKT(parser.text());
return new GeometryFormat() {
@Override
public Geometry fromXContent(XContentParser parser) throws IOException, ParseException {
return wellKnownTextParser.fromWKT(parser.text());
}

@Override
public XContentBuilder toXContent(Geometry geometry, XContentBuilder builder, ToXContent.Params params) throws IOException {
if (geometry != null) {
return builder.value(wellKnownTextParser.toWKT(geometry));
} else {
return builder.nullValue();
}
}
};

}
throw new ElasticsearchParseException("shape must be an object consisting of type and coordinates");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package org.elasticsearch.common.geo;

import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParseException;
Expand All @@ -44,7 +46,11 @@ public void testGeoJsonParsing() throws Exception {

try (XContentParser parser = createParser(pointGeoJson)) {
parser.nextToken();
assertEquals(new Point(0, 100), new GeometryParser(true, randomBoolean(), randomBoolean()).parse(parser));
GeometryFormat format = new GeometryParser(true, randomBoolean(), randomBoolean()).geometryFormat(parser);
assertEquals(new Point(0, 100), format.fromXContent(parser));
XContentBuilder newGeoJson = XContentFactory.jsonBuilder();
format.toXContent(new Point(10, 100), newGeoJson, ToXContent.EMPTY_PARAMS);
assertEquals("{\"type\":\"Point\",\"coordinates\":[100.0,10.0]}", Strings.toString(newGeoJson));
}

XContentBuilder pointGeoJsonWithZ = XContentFactory.jsonBuilder()
Expand Down Expand Up @@ -77,7 +83,7 @@ public void testGeoJsonParsing() throws Exception {
.endArray()
.endObject();

Polygon p = new Polygon(new LinearRing(new double[] {1d, 1d, 0d, 0d, 1d}, new double[] {100d, 101d, 101d, 100d, 100d}));
Polygon p = new Polygon(new LinearRing(new double[]{1d, 1d, 0d, 0d, 1d}, new double[]{100d, 101d, 101d, 100d, 100d}));
try (XContentParser parser = createParser(polygonGeoJson)) {
parser.nextToken();
// Coerce should automatically close the polygon
Expand All @@ -101,7 +107,12 @@ public void testWKTParsing() throws Exception {
parser.nextToken(); // Start object
parser.nextToken(); // Field Name
parser.nextToken(); // Field Value
assertEquals(new Point(0, 100), new GeometryParser(true, randomBoolean(), randomBoolean()).parse(parser));
GeometryFormat format = new GeometryParser(true, randomBoolean(), randomBoolean()).geometryFormat(parser);
assertEquals(new Point(0, 100), format.fromXContent(parser));
XContentBuilder newGeoJson = XContentFactory.jsonBuilder().startObject().field("val");
format.toXContent(new Point(10, 100), newGeoJson, ToXContent.EMPTY_PARAMS);
newGeoJson.endObject();
assertEquals("{\"val\":\"point (100.0 10.0)\"}", Strings.toString(newGeoJson));
}
}

Expand All @@ -115,7 +126,20 @@ public void testNullParsing() throws Exception {
parser.nextToken(); // Start object
parser.nextToken(); // Field Name
parser.nextToken(); // Field Value
assertNull(new GeometryParser(true, randomBoolean(), randomBoolean()).parse(parser));
GeometryFormat format = new GeometryParser(true, randomBoolean(), randomBoolean()).geometryFormat(parser);
assertNull(format.fromXContent(parser));

XContentBuilder newGeoJson = XContentFactory.jsonBuilder().startObject().field("val");
// if we serialize non-null value - it should be serialized as geojson
format.toXContent(new Point(10, 100), newGeoJson, ToXContent.EMPTY_PARAMS);
newGeoJson.endObject();
assertEquals("{\"val\":{\"type\":\"Point\",\"coordinates\":[100.0,10.0]}}", Strings.toString(newGeoJson));

newGeoJson = XContentFactory.jsonBuilder().startObject().field("val");
format.toXContent(null, newGeoJson, ToXContent.EMPTY_PARAMS);
newGeoJson.endObject();
assertEquals("{\"val\":null}", Strings.toString(newGeoJson));

}
}

Expand Down