Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
/*
* 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.
*/
/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
package org.opensearch.common.geo;
import org.opensearch.OpenSearchException;
import org.opensearch.OpenSearchParseException;
import org.opensearch.common.ParseField;
import org.opensearch.common.geo.parsers.ShapeParser;
import org.opensearch.common.unit.DistanceUnit;
import org.opensearch.common.xcontent.ConstructingObjectParser;
import org.opensearch.common.xcontent.ObjectParser;
import org.opensearch.common.xcontent.ToXContent;
import org.opensearch.common.xcontent.ToXContentObject;
import org.opensearch.common.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.XContentParser;
import org.opensearch.common.xcontent.XContentSubParser;
import org.opensearch.geometry.Circle;
import org.opensearch.geometry.Geometry;
import org.opensearch.geometry.GeometryCollection;
import org.opensearch.geometry.GeometryVisitor;
import org.opensearch.geometry.Line;
import org.opensearch.geometry.LinearRing;
import org.opensearch.geometry.MultiLine;
import org.opensearch.geometry.MultiPoint;
import org.opensearch.geometry.MultiPolygon;
import org.opensearch.geometry.Point;
import org.opensearch.geometry.Polygon;
import org.opensearch.geometry.Rectangle;
import org.opensearch.geometry.ShapeType;
import org.opensearch.geometry.utils.GeometryValidator;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import static org.opensearch.common.xcontent.ConstructingObjectParser.constructorArg;
import static org.opensearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg;
/**
* Utility class for converting libs/geo shapes to and from GeoJson
*/
public final class GeoJson {
private static final ParseField FIELD_TYPE = new ParseField("type");
private static final ParseField FIELD_COORDINATES = new ParseField("coordinates");
private static final ParseField FIELD_GEOMETRIES = new ParseField("geometries");
private static final ParseField FIELD_ORIENTATION = new ParseField("orientation");
private static final ParseField FIELD_RADIUS = new ParseField("radius");
private final boolean rightOrientation;
private final boolean coerce;
private final GeometryValidator validator;
public GeoJson(boolean rightOrientation, boolean coerce, GeometryValidator validator) {
this.rightOrientation = rightOrientation;
this.coerce = coerce;
this.validator = validator;
}
public Geometry fromXContent(XContentParser parser) throws IOException {
try (XContentSubParser subParser = new XContentSubParser(parser)) {
Geometry geometry = PARSER.apply(subParser, this);
validator.validate(geometry);
return geometry;
}
}
public static XContentBuilder toXContent(Geometry geometry, XContentBuilder builder, ToXContent.Params params) throws IOException {
builder.startObject();
builder.field(FIELD_TYPE.getPreferredName(), getGeoJsonName(geometry));
geometry.visit(new GeometryVisitor() {
@Override
public XContentBuilder visit(Circle circle) throws IOException {
builder.field(FIELD_RADIUS.getPreferredName(), DistanceUnit.METERS.toString(circle.getRadiusMeters()));
builder.field(ShapeParser.FIELD_COORDINATES.getPreferredName());
return coordinatesToXContent(circle.getY(), circle.getX(), circle.getZ());
}
@Override
public XContentBuilder visit(GeometryCollection> collection) throws IOException {
builder.startArray(FIELD_GEOMETRIES.getPreferredName());
for (Geometry g : collection) {
toXContent(g, builder, params);
}
return builder.endArray();
}
@Override
public XContentBuilder visit(Line line) throws IOException {
builder.field(ShapeParser.FIELD_COORDINATES.getPreferredName());
return coordinatesToXContent(line);
}
@Override
public XContentBuilder visit(LinearRing ring) {
throw new UnsupportedOperationException("linearRing cannot be serialized using GeoJson");
}
@Override
public XContentBuilder visit(MultiLine multiLine) throws IOException {
builder.field(ShapeParser.FIELD_COORDINATES.getPreferredName());
builder.startArray();
for (int i = 0; i < multiLine.size(); i++) {
coordinatesToXContent(multiLine.get(i));
}
return builder.endArray();
}
@Override
public XContentBuilder visit(MultiPoint multiPoint) throws IOException {
builder.startArray(ShapeParser.FIELD_COORDINATES.getPreferredName());
for (int i = 0; i < multiPoint.size(); i++) {
Point p = multiPoint.get(i);
builder.startArray().value(p.getX()).value(p.getY());
if (p.hasZ()) {
builder.value(p.getZ());
}
builder.endArray();
}
return builder.endArray();
}
@Override
public XContentBuilder visit(MultiPolygon multiPolygon) throws IOException {
builder.startArray(ShapeParser.FIELD_COORDINATES.getPreferredName());
for (int i = 0; i < multiPolygon.size(); i++) {
builder.startArray();
coordinatesToXContent(multiPolygon.get(i));
builder.endArray();
}
return builder.endArray();
}
@Override
public XContentBuilder visit(Point point) throws IOException {
builder.field(ShapeParser.FIELD_COORDINATES.getPreferredName());
return coordinatesToXContent(point.getY(), point.getX(), point.getZ());
}
@Override
public XContentBuilder visit(Polygon polygon) throws IOException {
builder.startArray(ShapeParser.FIELD_COORDINATES.getPreferredName());
coordinatesToXContent(polygon.getPolygon());
for (int i = 0; i < polygon.getNumberOfHoles(); i++) {
coordinatesToXContent(polygon.getHole(i));
}
return builder.endArray();
}
@Override
public XContentBuilder visit(Rectangle rectangle) throws IOException {
builder.startArray(ShapeParser.FIELD_COORDINATES.getPreferredName());
coordinatesToXContent(rectangle.getMaxY(), rectangle.getMinX(), rectangle.getMinZ()); // top left
coordinatesToXContent(rectangle.getMinY(), rectangle.getMaxX(), rectangle.getMaxZ()); // bottom right
return builder.endArray();
}
private XContentBuilder coordinatesToXContent(double lat, double lon, double alt) throws IOException {
builder.startArray().value(lon).value(lat);
if (Double.isNaN(alt) == false) {
builder.value(alt);
}
return builder.endArray();
}
private XContentBuilder coordinatesToXContent(Line line) throws IOException {
builder.startArray();
for (int i = 0; i < line.length(); i++) {
builder.startArray().value(line.getX(i)).value(line.getY(i));
if (line.hasZ()) {
builder.value(line.getZ(i));
}
builder.endArray();
}
return builder.endArray();
}
private XContentBuilder coordinatesToXContent(Polygon polygon) throws IOException {
coordinatesToXContent(polygon.getPolygon());
for (int i = 0; i < polygon.getNumberOfHoles(); i++) {
coordinatesToXContent(polygon.getHole(i));
}
return builder;
}
});
return builder.endObject();
}
/**
* Produces that same GeoJSON as toXContent only in parsed map form
*/
public static Map toMap(Geometry geometry) {
Map root = new HashMap<>();
root.put(FIELD_TYPE.getPreferredName(), getGeoJsonName(geometry));
geometry.visit(new GeometryVisitor() {
@Override
public Void visit(Circle circle) {
root.put(FIELD_RADIUS.getPreferredName(), DistanceUnit.METERS.toString(circle.getRadiusMeters()));
root.put(ShapeParser.FIELD_COORDINATES.getPreferredName(), coordinatesToList(circle.getY(), circle.getX(), circle.getZ()));
return null;
}
@Override
public Void visit(GeometryCollection> collection) {
List