Skip to content

Commit 8316eec

Browse files
committed
extended geometry collection support
1 parent 36937a7 commit 8316eec

File tree

10 files changed

+194
-8
lines changed

10 files changed

+194
-8
lines changed

src/main/java/mil/nga/sf/GeometryCollection.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ public GeometryType getCollectionType() {
167167
case MULTIPOLYGON:
168168
break;
169169
case GEOMETRYCOLLECTION:
170+
case MULTICURVE:
171+
case MULTISURFACE:
170172
if (isMultiPoint()) {
171173
geometryType = GeometryType.MULTIPOINT;
172174
} else if (isMultiLineString()) {
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package mil.nga.sf.extended;
2+
3+
import mil.nga.sf.Geometry;
4+
import mil.nga.sf.GeometryCollection;
5+
import mil.nga.sf.GeometryType;
6+
import mil.nga.sf.util.SFException;
7+
8+
/**
9+
* Extended Geometry Collection providing abstract geometry collection type
10+
* support
11+
*
12+
* @author osbornb
13+
*
14+
* @param <T>
15+
* geometry type
16+
*/
17+
public class ExtendedGeometryCollection<T extends Geometry> extends
18+
GeometryCollection<T> {
19+
20+
/**
21+
* Extended geometry collection geometry type
22+
*/
23+
private GeometryType geometryType = GeometryType.GEOMETRYCOLLECTION;
24+
25+
/**
26+
* Constructor, wraps a geometry collection as extended
27+
*
28+
* @param geometryCollection
29+
*/
30+
public ExtendedGeometryCollection(GeometryCollection<T> geometryCollection) {
31+
super(GeometryType.GEOMETRYCOLLECTION, geometryCollection.hasZ(),
32+
geometryCollection.hasM());
33+
setGeometries(geometryCollection.getGeometries());
34+
updateGeometryType();
35+
}
36+
37+
/**
38+
* Copy Constructor
39+
*
40+
* @param extendedGeometryCollection
41+
* extended geometry collection to copy
42+
*/
43+
public ExtendedGeometryCollection(
44+
ExtendedGeometryCollection<T> extendedGeometryCollection) {
45+
super(GeometryType.GEOMETRYCOLLECTION, extendedGeometryCollection
46+
.hasZ(), extendedGeometryCollection.hasM());
47+
for (T geometry : extendedGeometryCollection.getGeometries()) {
48+
@SuppressWarnings("unchecked")
49+
T geometryCopy = (T) geometry.copy();
50+
addGeometry(geometryCopy);
51+
}
52+
geometryType = extendedGeometryCollection.getGeometryType();
53+
}
54+
55+
/**
56+
* Update the extended geometry type based upon the contained geometries
57+
*/
58+
public void updateGeometryType() {
59+
GeometryType geometryType = getCollectionType();
60+
switch (geometryType) {
61+
case GEOMETRYCOLLECTION:
62+
case MULTICURVE:
63+
case MULTISURFACE:
64+
break;
65+
case MULTIPOINT:
66+
geometryType = GeometryType.GEOMETRYCOLLECTION;
67+
break;
68+
case MULTILINESTRING:
69+
geometryType = GeometryType.MULTICURVE;
70+
break;
71+
case MULTIPOLYGON:
72+
geometryType = GeometryType.MULTISURFACE;
73+
break;
74+
default:
75+
throw new SFException(
76+
"Unsupported extended geometry collection geometry type: "
77+
+ geometryType);
78+
}
79+
this.geometryType = geometryType;
80+
}
81+
82+
/**
83+
* {@inheritDoc}
84+
*/
85+
@Override
86+
public GeometryType getGeometryType() {
87+
return geometryType;
88+
}
89+
90+
/**
91+
* {@inheritDoc}
92+
*/
93+
@Override
94+
public Geometry copy() {
95+
return new ExtendedGeometryCollection<T>(this);
96+
}
97+
98+
/**
99+
* {@inheritDoc}
100+
*/
101+
@Override
102+
public int hashCode() {
103+
final int prime = 31;
104+
int result = super.hashCode();
105+
result = prime * result
106+
+ ((geometryType == null) ? 0 : geometryType.hashCode());
107+
return result;
108+
}
109+
110+
/**
111+
* {@inheritDoc}
112+
*/
113+
@Override
114+
public boolean equals(Object obj) {
115+
if (this == obj)
116+
return true;
117+
if (!super.equals(obj))
118+
return false;
119+
if (getClass() != obj.getClass())
120+
return false;
121+
@SuppressWarnings("unchecked")
122+
ExtendedGeometryCollection<T> other = (ExtendedGeometryCollection<T>) obj;
123+
if (geometryType != other.geometryType)
124+
return false;
125+
return true;
126+
}
127+
128+
}

src/main/java/mil/nga/sf/util/GeometryEnvelopeBuilder.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ public static void buildEnvelope(Geometry geometry,
100100
addPolygon(envelope, (Triangle) geometry);
101101
break;
102102
case GEOMETRYCOLLECTION:
103+
case MULTICURVE:
104+
case MULTISURFACE:
103105
updateHasZandM(envelope, geometry);
104106
@SuppressWarnings("unchecked")
105107
GeometryCollection<Geometry> geomCollection = (GeometryCollection<Geometry>) geometry;

src/main/java/mil/nga/sf/util/GeometryPrinter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ public static String getGeometryString(Geometry geometry) {
7878
addPolygonMessage(message, (Triangle) geometry);
7979
break;
8080
case GEOMETRYCOLLECTION:
81+
case MULTICURVE:
82+
case MULTISURFACE:
8183
@SuppressWarnings("unchecked")
8284
GeometryCollection<Geometry> geomCollection = (GeometryCollection<Geometry>) geometry;
8385
message.append("Geometries: " + geomCollection.numGeometries());

src/main/java/mil/nga/sf/util/GeometryUtils.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ public static int getDimension(Geometry geometry) {
7171
dimension = 2;
7272
break;
7373
case GEOMETRYCOLLECTION:
74+
case MULTICURVE:
75+
case MULTISURFACE:
7476
@SuppressWarnings("unchecked")
7577
GeometryCollection<Geometry> geomCollection = (GeometryCollection<Geometry>) geometry;
7678
List<Geometry> geometries = geomCollection.getGeometries();
@@ -184,6 +186,8 @@ public static void minimizeGeometry(Geometry geometry, double maxX) {
184186
minimize((Triangle) geometry, maxX);
185187
break;
186188
case GEOMETRYCOLLECTION:
189+
case MULTICURVE:
190+
case MULTISURFACE:
187191
@SuppressWarnings("unchecked")
188192
GeometryCollection<Geometry> geomCollection = (GeometryCollection<Geometry>) geometry;
189193
for (Geometry subGeometry : geomCollection.getGeometries()) {
@@ -378,6 +382,8 @@ public static void normalizeGeometry(Geometry geometry, double maxX) {
378382
normalize((Triangle) geometry, maxX);
379383
break;
380384
case GEOMETRYCOLLECTION:
385+
case MULTICURVE:
386+
case MULTISURFACE:
381387
@SuppressWarnings("unchecked")
382388
GeometryCollection<Geometry> geomCollection = (GeometryCollection<Geometry>) geometry;
383389
for (Geometry subGeometry : geomCollection.getGeometries()) {

src/main/java/mil/nga/sf/util/centroid/CentroidCurve.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ public void add(Geometry geometry) {
7272
addLineStrings(compoundCurve.getLineStrings());
7373
break;
7474
case GEOMETRYCOLLECTION:
75+
case MULTICURVE:
76+
case MULTISURFACE:
7577
@SuppressWarnings("unchecked")
7678
GeometryCollection<Geometry> geomCollection = (GeometryCollection<Geometry>) geometry;
7779
List<Geometry> geometries = geomCollection.getGeometries();

src/main/java/mil/nga/sf/util/centroid/CentroidPoint.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ public void add(Geometry geometry) {
6464
}
6565
break;
6666
case GEOMETRYCOLLECTION:
67+
case MULTICURVE:
68+
case MULTISURFACE:
6769
@SuppressWarnings("unchecked")
6870
GeometryCollection<Geometry> geomCollection = (GeometryCollection<Geometry>) geometry;
6971
List<Geometry> geometries = geomCollection.getGeometries();

src/main/java/mil/nga/sf/util/centroid/CentroidSurface.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ public void add(Geometry geometry) {
8686
add(polyhedralSurface.getPolygons());
8787
break;
8888
case GEOMETRYCOLLECTION:
89+
case MULTICURVE:
90+
case MULTISURFACE:
8991
@SuppressWarnings("unchecked")
9092
GeometryCollection<Geometry> geomCollection = (GeometryCollection<Geometry>) geometry;
9193
List<Geometry> geometries = geomCollection.getGeometries();
@@ -158,8 +160,7 @@ private void add(CurvePolygon<Curve> curvePolygon) {
158160
add((LineString) curve);
159161
break;
160162
default:
161-
throw new SFException("Unexpected Curve Type: "
162-
+ curveGeometryType);
163+
throw new SFException("Unexpected Curve Type: " + curveGeometryType);
163164
}
164165

165166
for (int i = 1; i < rings.size(); i++) {

src/test/java/mil/nga/sf/test/GeometryCollectionTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import mil.nga.sf.Point;
1717
import mil.nga.sf.Polygon;
1818
import mil.nga.sf.Surface;
19+
import mil.nga.sf.extended.ExtendedGeometryCollection;
1920

2021
import org.junit.Test;
2122

@@ -82,6 +83,15 @@ public void testMultiPoint() throws IOException {
8283
TestCase.assertEquals(geometryCollection2,
8384
geometryCollection.getAsGeometryCollection());
8485

86+
ExtendedGeometryCollection<Geometry> extendedGeometryCollection = new ExtendedGeometryCollection<>(
87+
geometryCollection);
88+
TestCase.assertEquals(GeometryType.GEOMETRYCOLLECTION,
89+
extendedGeometryCollection.getGeometryType());
90+
TestCase.assertEquals(GeometryType.MULTIPOINT,
91+
extendedGeometryCollection.getCollectionType());
92+
TestCase.assertEquals(extendedGeometryCollection,
93+
new ExtendedGeometryCollection<>(geometryCollection));
94+
8595
}
8696

8797
@Test
@@ -143,6 +153,15 @@ public void testMultiLineString() throws IOException {
143153
.getAsMultiCurve();
144154
TestCase.assertEquals(multiCurve, multiCurve2);
145155

156+
ExtendedGeometryCollection<Geometry> extendedGeometryCollection = new ExtendedGeometryCollection<>(
157+
geometryCollection);
158+
TestCase.assertEquals(GeometryType.MULTICURVE,
159+
extendedGeometryCollection.getGeometryType());
160+
TestCase.assertEquals(GeometryType.MULTILINESTRING,
161+
extendedGeometryCollection.getCollectionType());
162+
TestCase.assertEquals(extendedGeometryCollection,
163+
new ExtendedGeometryCollection<>(geometryCollection));
164+
146165
}
147166

148167
@Test
@@ -202,6 +221,14 @@ public void testMultiPolygon() throws IOException {
202221
.getAsMultiSurface();
203222
TestCase.assertEquals(multiSurface, multiSurface2);
204223

224+
ExtendedGeometryCollection<Geometry> extendedGeometryCollection = new ExtendedGeometryCollection<>(
225+
geometryCollection);
226+
TestCase.assertEquals(GeometryType.MULTISURFACE,
227+
extendedGeometryCollection.getGeometryType());
228+
TestCase.assertEquals(GeometryType.MULTIPOLYGON,
229+
extendedGeometryCollection.getCollectionType());
230+
TestCase.assertEquals(extendedGeometryCollection,
231+
new ExtendedGeometryCollection<>(geometryCollection));
205232
}
206233

207234
@Test
@@ -257,6 +284,15 @@ public void testMultiCurve() throws IOException {
257284
TestCase.assertEquals(geometryCollection2,
258285
geometryCollection.getAsGeometryCollection());
259286

287+
ExtendedGeometryCollection<Geometry> extendedGeometryCollection = new ExtendedGeometryCollection<>(
288+
geometryCollection);
289+
TestCase.assertEquals(GeometryType.MULTICURVE,
290+
extendedGeometryCollection.getGeometryType());
291+
TestCase.assertEquals(GeometryType.MULTICURVE,
292+
extendedGeometryCollection.getCollectionType());
293+
TestCase.assertEquals(extendedGeometryCollection,
294+
new ExtendedGeometryCollection<>(geometryCollection));
295+
260296
}
261297

262298
@Test
@@ -313,6 +349,15 @@ public void testMultiSurface() throws IOException {
313349
TestCase.assertEquals(geometryCollection2,
314350
geometryCollection.getAsGeometryCollection());
315351

352+
ExtendedGeometryCollection<Surface> extendedGeometryCollection = new ExtendedGeometryCollection<>(
353+
multiSurface);
354+
TestCase.assertEquals(GeometryType.MULTISURFACE,
355+
extendedGeometryCollection.getGeometryType());
356+
TestCase.assertEquals(GeometryType.MULTISURFACE,
357+
extendedGeometryCollection.getCollectionType());
358+
TestCase.assertEquals(extendedGeometryCollection,
359+
new ExtendedGeometryCollection<>(geometryCollection));
360+
316361
}
317362

318363
}

src/test/java/mil/nga/sf/test/SFTestUtils.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ public static void compareGeometries(Geometry expected, Geometry actual) {
9393
(MultiPolygon) actual);
9494
break;
9595
case GEOMETRYCOLLECTION:
96+
case MULTICURVE:
97+
case MULTISURFACE:
9698
compareGeometryCollection((GeometryCollection<?>) expected,
9799
(GeometryCollection<?>) actual);
98100
break;
@@ -108,12 +110,6 @@ public static void compareGeometries(Geometry expected, Geometry actual) {
108110
compareCurvePolygon((CurvePolygon<?>) expected,
109111
(CurvePolygon<?>) actual);
110112
break;
111-
case MULTICURVE:
112-
TestCase.fail("Unexpected Geometry Type of "
113-
+ geometryType.name() + " which is abstract");
114-
case MULTISURFACE:
115-
TestCase.fail("Unexpected Geometry Type of "
116-
+ geometryType.name() + " which is abstract");
117113
case CURVE:
118114
TestCase.fail("Unexpected Geometry Type of "
119115
+ geometryType.name() + " which is abstract");

0 commit comments

Comments
 (0)