summaryrefslogtreecommitdiff
path: root/core/src/main/java/org/elasticsearch/common/geo/builders/PointCollection.java
blob: b48aacd857bf7a7a9415423344ce7872be5a0d14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * 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.builders;

import com.vividsolutions.jts.geom.Coordinate;
import org.elasticsearch.common.xcontent.XContentBuilder;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

/**
 * The {@link PointCollection} is an abstract base implementation for all GeoShapes. It simply handles a set of points.
 */
public abstract class PointCollection<E extends PointCollection<E>> extends ShapeBuilder {

    protected final ArrayList<Coordinate> points;

    protected PointCollection() {
        this(new ArrayList<Coordinate>());
    }

    protected PointCollection(ArrayList<Coordinate> points) {
        this.points = points;
    }

    @SuppressWarnings("unchecked")
    private E thisRef() {
        return (E)this;
    }

    /**
     * Add a new point to the collection
     * @param longitude longitude of the coordinate
     * @param latitude latitude of the coordinate
     * @return this
     */
    public E point(double longitude, double latitude) {
        return this.point(coordinate(longitude, latitude));
    }

    /**
     * Add a new point to the collection
     * @param coordinate coordinate of the point
     * @return this
     */
    public E point(Coordinate coordinate) {
        this.points.add(coordinate);
        return thisRef();
    }

    /**
     * Add a array of points to the collection
     *
     * @param coordinates array of {@link Coordinate}s to add
     * @return this
     */
    public E points(Coordinate...coordinates) {
        return this.points(Arrays.asList(coordinates));
    }

    /**
     * Add a collection of points to the collection
     *
     * @param coordinates array of {@link Coordinate}s to add
     * @return this
     */
    public E points(Collection<? extends Coordinate> coordinates) {
        this.points.addAll(coordinates);
        return thisRef();
    }

    /**
     * Copy all points to a new Array
     *
     * @param closed if set to true the first point of the array is repeated as last element
     * @return Array of coordinates
     */
    protected Coordinate[] coordinates(boolean closed) {
        Coordinate[] result = points.toArray(new Coordinate[points.size() + (closed?1:0)]);
        if(closed) {
            result[result.length-1] = result[0];
        }
        return result;
    }

    /**
     * builds an array of coordinates to a {@link XContentBuilder}
     *
     * @param builder builder to use
     * @param closed repeat the first point at the end of the array if it's not already defines as last element of the array
     * @return the builder
     */
    protected XContentBuilder coordinatesToXcontent(XContentBuilder builder, boolean closed) throws IOException {
        builder.startArray();
        for(Coordinate point : points) {
            toXContent(builder, point);
        }
        if(closed) {
            Coordinate start = points.get(0);
            Coordinate end = points.get(points.size()-1);
            if(start.x != end.x || start.y != end.y) {
                toXContent(builder, points.get(0));
            }
        }
        builder.endArray();
        return builder;
    }
}