4
* The JTS Topology Suite is a collection of Java classes that
5
* implement the fundamental operations required to validate a given
6
* geo-spatial data set to a known topological specification.
8
* Copyright (C) 2001 Vivid Solutions
10
* This library is free software; you can redistribute it and/or
11
* modify it under the terms of the GNU Lesser General Public
12
* License as published by the Free Software Foundation; either
13
* version 2.1 of the License, or (at your option) any later version.
15
* This library is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
* Lesser General Public License for more details.
20
* You should have received a copy of the GNU Lesser General Public
21
* License along with this library; if not, write to the Free Software
22
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
* For more information, contact:
28
* 2328 Government Street
33
* www.vividsolutions.com
35
package com.vividsolutions.jts.geom;
37
import java.util.List;
40
* Useful utility functions for handling Coordinate arrays
44
public class CoordinateArrays {
46
private final static Coordinate[] coordArrayType = new Coordinate[0];
49
* Creates a deep copy of the argument {@link Coordinate) array.
51
* @param coordinates an array of Coordinates
52
* @return a deep copy of the input
54
public static Coordinate[] copyDeep(Coordinate[] coordinates) {
55
Coordinate[] copy = new Coordinate[coordinates.length];
56
for (int i = 0; i < coordinates.length; i++) {
57
copy[i] = new Coordinate(coordinates[i]);
63
* Converts the given List of Coordinates into a Coordinate array.
65
public static Coordinate[] toCoordinateArray(List coordList)
67
return (Coordinate[]) coordList.toArray(coordArrayType);
71
* Returns whether #equals returns true for any two consecutive Coordinates
74
public static boolean hasRepeatedPoints(Coordinate[] coord)
76
for (int i = 1; i < coord.length; i++) {
77
if (coord[i - 1].equals(coord[i]) ) {
85
* Returns either the given coordinate array if its length is greater than the
86
* given amount, or an empty coordinate array.
88
public static Coordinate[] atLeastNCoordinatesOrNothing(int n, Coordinate[] c) {
89
return c.length >= n ? c : new Coordinate[] { };
93
* If the coordinate array argument has repeated points,
94
* constructs a new array containing no repeated points.
95
* Otherwise, returns the argument.
96
* @see #hasRepeatedPoints(Coordinate[])
98
public static Coordinate[] removeRepeatedPoints(Coordinate[] coord)
100
if (! hasRepeatedPoints(coord)) return coord;
101
CoordinateList coordList = new CoordinateList(coord, false);
102
return coordList.toCoordinateArray();
106
* Reverses the coordinates in an array in-place.
108
public static void reverse(Coordinate[] coord)
110
int last = coord.length - 1;
112
for (int i = 0; i <= mid; i++) {
113
Coordinate tmp = coord[i];
114
coord[i] = coord[last - i];
115
coord[last - i] = tmp;
120
* Returns true if the two arrays are identical, both null, or pointwise
121
* equal (as compared using Coordinate#equals)
122
* @see Coordinate#equals(Object)
124
public static boolean equals(
128
if (coord1 == coord2) return true;
129
if (coord1 == null || coord2 == null) return false;
130
if (coord1.length != coord2.length) return false;
131
for (int i = 0; i < coord1.length; i++) {
132
if (! coord1[i].equals(coord2[i])) return false;
138
* Returns the minimum coordinate, using the usual lexicographic comparison.
140
*@param coordinates the array to search
141
*@return the minimum coordinate in the array, found using <code>compareTo</code>
142
*@see Coordinate#compareTo(Object)
144
public static Coordinate minCoordinate(Coordinate[] coordinates)
146
Coordinate minCoord = null;
147
for (int i = 0; i < coordinates.length; i++) {
148
if (minCoord == null || minCoord.compareTo(coordinates[i]) > 0) {
149
minCoord = coordinates[i];
155
* Shifts the positions of the coordinates until <code>firstCoordinate</code>
158
*@param coordinates the array to rearrange
159
*@param firstCoordinate the coordinate to make first
161
public static void scroll(Coordinate[] coordinates, Coordinate firstCoordinate) {
162
int i = indexOf(firstCoordinate, coordinates);
164
Coordinate[] newCoordinates = new Coordinate[coordinates.length];
165
System.arraycopy(coordinates, i, newCoordinates, 0, coordinates.length - i);
166
System.arraycopy(coordinates, 0, newCoordinates, coordinates.length - i, i);
167
System.arraycopy(newCoordinates, 0, coordinates, 0, coordinates.length);
171
* Returns the index of <code>coordinate</code> in <code>coordinates</code>.
172
* The first position is 0; the second, 1; etc.
174
*@param coordinate the <code>Coordinate</code> to search for
175
*@param coordinates the array to search
176
*@return the position of <code>coordinate</code>, or -1 if it is
179
public static int indexOf(Coordinate coordinate, Coordinate[] coordinates) {
180
for (int i = 0; i < coordinates.length; i++) {
181
if (coordinate.equals(coordinates[i])) {