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.Arrays;
38
import java.util.TreeSet;
40
import com.vividsolutions.jts.util.Assert;
43
* Basic implementation of <code>GeometryCollection</code>.
47
public class GeometryCollection extends Geometry {
48
// With contributions from Markus Schaber [schabios@logi-track.com] 2004-03-26
49
private static final long serialVersionUID = -5694727726395021467L;
51
* Internal representation of this <code>GeometryCollection</code>.
53
protected Geometry[] geometries;
55
/** @deprecated Use GeometryFactory instead */
56
public GeometryCollection(Geometry[] geometries, PrecisionModel precisionModel, int SRID) {
57
this(geometries, new GeometryFactory(precisionModel, SRID));
63
* the <code>Geometry</code>s for this <code>GeometryCollection</code>,
64
* or <code>null</code> or an empty array to create the empty
65
* geometry. Elements may be empty <code>Geometry</code>s,
66
* but not <code>null</code>s.
68
public GeometryCollection(Geometry[] geometries, GeometryFactory factory) {
70
if (geometries == null) {
71
geometries = new Geometry[]{};
73
if (hasNullElements(geometries)) {
74
throw new IllegalArgumentException("geometries must not contain null elements");
76
this.geometries = geometries;
79
public Coordinate getCoordinate() {
80
if (isEmpty()) return null;
81
return geometries[0].getCoordinate();
85
* Collects all coordinates of all subgeometries into an Array.
87
* Note that while changes to the coordinate objects themselves
88
* may modify the Geometries in place, the returned Array as such
89
* is only a temporary container which is not synchronized back.
91
* @return the collected coordinates
93
public Coordinate[] getCoordinates() {
94
Coordinate[] coordinates = new Coordinate[getNumPoints()];
96
for (int i = 0; i < geometries.length; i++) {
97
Coordinate[] childCoordinates = geometries[i].getCoordinates();
98
for (int j = 0; j < childCoordinates.length; j++) {
100
coordinates[k] = childCoordinates[j];
106
public boolean isEmpty() {
107
for (int i = 0; i < geometries.length; i++) {
108
if (!geometries[i].isEmpty()) {
115
public int getDimension() {
116
int dimension = Dimension.FALSE;
117
for (int i = 0; i < geometries.length; i++) {
118
dimension = Math.max(dimension, geometries[i].getDimension());
123
public int getBoundaryDimension() {
124
int dimension = Dimension.FALSE;
125
for (int i = 0; i < geometries.length; i++) {
126
dimension = Math.max(dimension, ((Geometry) geometries[i]).getBoundaryDimension());
131
public int getNumGeometries() {
132
return geometries.length;
135
public Geometry getGeometryN(int n) {
136
return geometries[n];
139
public int getNumPoints() {
141
for (int i = 0; i < geometries.length; i++) {
142
numPoints += ((Geometry) geometries[i]).getNumPoints();
147
public String getGeometryType() {
148
return "GeometryCollection";
151
public boolean isSimple() {
152
checkNotGeometryCollection(this);
153
Assert.shouldNeverReachHere();
157
public Geometry getBoundary() {
158
checkNotGeometryCollection(this);
159
Assert.shouldNeverReachHere();
164
* Returns the area of this <code>GeometryCollection</code>
166
*@return the area of the polygon
168
public double getArea()
171
for (int i = 0; i < geometries.length; i++) {
172
area += geometries[i].getArea();
177
public double getLength()
180
for (int i = 0; i < geometries.length; i++) {
181
sum += (geometries[i]).getLength();
186
public boolean equalsExact(Geometry other, double tolerance) {
187
if (!isEquivalentClass(other)) {
190
GeometryCollection otherCollection = (GeometryCollection) other;
191
if (geometries.length != otherCollection.geometries.length) {
194
for (int i = 0; i < geometries.length; i++) {
195
if (!((Geometry) geometries[i]).equalsExact(otherCollection.geometries[i], tolerance)) {
202
public void apply(CoordinateFilter filter) {
203
for (int i = 0; i < geometries.length; i++) {
204
geometries[i].apply(filter);
208
public void apply(GeometryFilter filter) {
210
for (int i = 0; i < geometries.length; i++) {
211
geometries[i].apply(filter);
215
public void apply(GeometryComponentFilter filter) {
217
for (int i = 0; i < geometries.length; i++) {
218
geometries[i].apply(filter);
222
public Object clone() {
223
GeometryCollection gc = (GeometryCollection) super.clone();
224
gc.geometries = new Geometry[geometries.length];
225
for (int i = 0; i < geometries.length; i++) {
226
gc.geometries[i] = (Geometry) geometries[i].clone();
228
return gc;// return the clone
231
public void normalize() {
232
for (int i = 0; i < geometries.length; i++) {
233
geometries[i].normalize();
235
Arrays.sort(geometries);
238
protected Envelope computeEnvelopeInternal() {
239
Envelope envelope = new Envelope();
240
for (int i = 0; i < geometries.length; i++) {
241
envelope.expandToInclude(geometries[i].getEnvelopeInternal());
246
protected int compareToSameClass(Object o) {
247
TreeSet theseElements = new TreeSet(Arrays.asList(geometries));
248
TreeSet otherElements = new TreeSet(Arrays.asList(((GeometryCollection) o).geometries));
249
return compare(theseElements, otherElements);