~ubuntu-branches/ubuntu/natty/jts/natty

« back to all changes in this revision

Viewing changes to src/com/vividsolutions/jts/geom/GeometryCollection.java

  • Committer: Bazaar Package Importer
  • Author(s): Wolfgang Baer
  • Date: 2005-08-07 14:12:35 UTC
  • Revision ID: james.westby@ubuntu.com-20050807141235-7hy3ll3xpq79djcb
Tags: upstream-1.6
ImportĀ upstreamĀ versionĀ 1.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
 
 
3
/*
 
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.
 
7
 *
 
8
 * Copyright (C) 2001 Vivid Solutions
 
9
 *
 
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.
 
14
 *
 
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.
 
19
 *
 
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
 
23
 *
 
24
 * For more information, contact:
 
25
 *
 
26
 *     Vivid Solutions
 
27
 *     Suite #1A
 
28
 *     2328 Government Street
 
29
 *     Victoria BC  V8T 5G5
 
30
 *     Canada
 
31
 *
 
32
 *     (250)385-6040
 
33
 *     www.vividsolutions.com
 
34
 */
 
35
package com.vividsolutions.jts.geom;
 
36
 
 
37
import java.util.Arrays;
 
38
import java.util.TreeSet;
 
39
 
 
40
import com.vividsolutions.jts.util.Assert;
 
41
 
 
42
/**
 
43
 *  Basic implementation of <code>GeometryCollection</code>.
 
44
 *
 
45
 *@version 1.6
 
46
 */
 
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;
 
50
  /**
 
51
   *  Internal representation of this <code>GeometryCollection</code>.
 
52
   */
 
53
  protected Geometry[] geometries;
 
54
 
 
55
  /** @deprecated Use GeometryFactory instead */
 
56
  public GeometryCollection(Geometry[] geometries, PrecisionModel precisionModel, int SRID) {
 
57
      this(geometries, new GeometryFactory(precisionModel, SRID));
 
58
  }
 
59
 
 
60
 
 
61
  /**
 
62
   * @param geometries
 
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.
 
67
   */
 
68
  public GeometryCollection(Geometry[] geometries, GeometryFactory factory) {
 
69
    super(factory);
 
70
    if (geometries == null) {
 
71
      geometries = new Geometry[]{};
 
72
    }
 
73
    if (hasNullElements(geometries)) {
 
74
      throw new IllegalArgumentException("geometries must not contain null elements");
 
75
    }
 
76
    this.geometries = geometries;
 
77
  }
 
78
 
 
79
  public Coordinate getCoordinate() {
 
80
    if (isEmpty()) return null;
 
81
    return geometries[0].getCoordinate();
 
82
  }
 
83
 
 
84
  /**
 
85
   * Collects all coordinates of all subgeometries into an Array.
 
86
   *
 
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.
 
90
   *
 
91
   * @return the collected coordinates
 
92
   *    */
 
93
  public Coordinate[] getCoordinates() {
 
94
    Coordinate[] coordinates = new Coordinate[getNumPoints()];
 
95
    int k = -1;
 
96
    for (int i = 0; i < geometries.length; i++) {
 
97
      Coordinate[] childCoordinates = geometries[i].getCoordinates();
 
98
      for (int j = 0; j < childCoordinates.length; j++) {
 
99
        k++;
 
100
        coordinates[k] = childCoordinates[j];
 
101
      }
 
102
    }
 
103
    return coordinates;
 
104
  }
 
105
 
 
106
  public boolean isEmpty() {
 
107
    for (int i = 0; i < geometries.length; i++) {
 
108
      if (!geometries[i].isEmpty()) {
 
109
        return false;
 
110
      }
 
111
    }
 
112
    return true;
 
113
  }
 
114
 
 
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());
 
119
    }
 
120
    return dimension;
 
121
  }
 
122
 
 
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());
 
127
    }
 
128
    return dimension;
 
129
  }
 
130
 
 
131
  public int getNumGeometries() {
 
132
    return geometries.length;
 
133
  }
 
134
 
 
135
  public Geometry getGeometryN(int n) {
 
136
    return geometries[n];
 
137
  }
 
138
 
 
139
  public int getNumPoints() {
 
140
    int numPoints = 0;
 
141
    for (int i = 0; i < geometries.length; i++) {
 
142
      numPoints += ((Geometry) geometries[i]).getNumPoints();
 
143
    }
 
144
    return numPoints;
 
145
  }
 
146
 
 
147
  public String getGeometryType() {
 
148
    return "GeometryCollection";
 
149
  }
 
150
 
 
151
  public boolean isSimple() {
 
152
    checkNotGeometryCollection(this);
 
153
    Assert.shouldNeverReachHere();
 
154
    return false;
 
155
  }
 
156
 
 
157
  public Geometry getBoundary() {
 
158
    checkNotGeometryCollection(this);
 
159
    Assert.shouldNeverReachHere();
 
160
    return null;
 
161
  }
 
162
 
 
163
  /**
 
164
   *  Returns the area of this <code>GeometryCollection</code>
 
165
   *
 
166
   *@return the area of the polygon
 
167
   */
 
168
  public double getArea()
 
169
  {
 
170
    double area = 0.0;
 
171
    for (int i = 0; i < geometries.length; i++) {
 
172
      area += geometries[i].getArea();
 
173
    }
 
174
    return area;
 
175
  }
 
176
 
 
177
  public double getLength()
 
178
  {
 
179
    double sum = 0.0;
 
180
    for (int i = 0; i < geometries.length; i++) {
 
181
      sum += (geometries[i]).getLength();
 
182
    }
 
183
    return sum;
 
184
  }
 
185
 
 
186
  public boolean equalsExact(Geometry other, double tolerance) {
 
187
    if (!isEquivalentClass(other)) {
 
188
      return false;
 
189
    }
 
190
    GeometryCollection otherCollection = (GeometryCollection) other;
 
191
    if (geometries.length != otherCollection.geometries.length) {
 
192
      return false;
 
193
    }
 
194
    for (int i = 0; i < geometries.length; i++) {
 
195
      if (!((Geometry) geometries[i]).equalsExact(otherCollection.geometries[i], tolerance)) {
 
196
        return false;
 
197
      }
 
198
    }
 
199
    return true;
 
200
  }
 
201
 
 
202
  public void apply(CoordinateFilter filter) {
 
203
    for (int i = 0; i < geometries.length; i++) {
 
204
      geometries[i].apply(filter);
 
205
    }
 
206
  }
 
207
 
 
208
  public void apply(GeometryFilter filter) {
 
209
    filter.filter(this);
 
210
    for (int i = 0; i < geometries.length; i++) {
 
211
      geometries[i].apply(filter);
 
212
    }
 
213
  }
 
214
 
 
215
  public void apply(GeometryComponentFilter filter) {
 
216
    filter.filter(this);
 
217
    for (int i = 0; i < geometries.length; i++) {
 
218
      geometries[i].apply(filter);
 
219
    }
 
220
  }
 
221
 
 
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();
 
227
    }
 
228
    return gc;// return the clone
 
229
  }
 
230
 
 
231
  public void normalize() {
 
232
    for (int i = 0; i < geometries.length; i++) {
 
233
      geometries[i].normalize();
 
234
    }
 
235
    Arrays.sort(geometries);
 
236
  }
 
237
 
 
238
  protected Envelope computeEnvelopeInternal() {
 
239
    Envelope envelope = new Envelope();
 
240
    for (int i = 0; i < geometries.length; i++) {
 
241
      envelope.expandToInclude(geometries[i].getEnvelopeInternal());
 
242
    }
 
243
    return envelope;
 
244
  }
 
245
 
 
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);
 
250
  }
 
251
}
 
252