1 /*
2 * WKB4J - WKB reader for geographical mapping toolkits
3 * (C) 2002,2003, David Garnier, dgarnier@users.sourceforge.net
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation,
8 * version 2.1 of the License.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * or visit the web to http://www.gnu.org.
19 *
20 */
21
22 package org.wkb4j.postgis;
23
24 import java.sql.SQLException;
25
26 import org.postgis.Geometry;
27 import org.postgis.LineString;
28 import org.postgis.MultiLineString;
29 import org.postgis.MultiPoint;
30 import org.postgis.MultiPolygon;
31 import org.postgis.Point;
32 import org.postgis.Polygon;
33 import org.postgresql.util.PGtokenizer;
34
35 /***
36 * @author David Garnier
37 * @version $Revision $ $Date $
38 */
39 public class GeometryCollection extends Geometry {
40
41 Geometry[] geometries;
42
43 public GeometryCollection() {
44 type = GEOMETRYCOLLECTION;
45 }
46
47 public GeometryCollection(Geometry[] _geometries) {
48 this();
49 this.geometries = _geometries;
50 try {
51
52 dimension = _geometries[0].dimension;
53 } catch (ArrayIndexOutOfBoundsException aioobe) {
54 aioobe.printStackTrace();
55 }
56 }
57
58 public GeometryCollection(String value) throws SQLException {
59 this();
60 value = value.trim();
61 if (value.indexOf("GEOMETRYCOLLECTION") == 0) {
62 PGtokenizer t =
63 new PGtokenizer(
64 PGtokenizer.removePara(
65 value.substring("GEOMETRYCOLLECTION".length()).trim()),
66 ',');
67 int nGeometries = t.getSize();
68 geometries = new Geometry[nGeometries];
69
70 for (int p = 0; p < nGeometries; p++) {
71 try {
72 String geomString = t.getToken(p);
73
74 if (geomString.startsWith("MULTILINESTRING")) {
75 geometries[p] = new MultiLineString(geomString);
76 } else if (geomString.startsWith("LINESTRING")) {
77 geometries[p] = new LineString(geomString);
78 } else if (geomString.startsWith("POLYGON")) {
79 geometries[p] = new Polygon(geomString);
80 } else if (geomString.startsWith("MULTIPOLYGON")) {
81 geometries[p] = new MultiPolygon(geomString);
82 } else if (geomString.startsWith("POINT")) {
83 geometries[p] = new Point(geomString);
84 } else if (geomString.startsWith("MULTIPOINT")) {
85 geometries[p] = new MultiPoint(geomString);
86 } else if (geomString.startsWith("GEOMETRYCOLLECTION")) {
87 geometries[p] = new GeometryCollection(geomString);
88 } else {
89
90 System.out.println(
91 "Unknown geometry type starting with: "
92 + geomString);
93 geometries[p] = null;
94 }
95
96 } catch (NullPointerException npe) {
97 int d = 0;
98 }
99 }
100 dimension = geometries[0].dimension;
101 } else {
102 throw new SQLException("postgis.geometrycollectiongeometry");
103 }
104 }
105
106 public String toString() {
107 return "GEOMETRYCOLLECTION " + getValue();
108 }
109
110 public String getValue() {
111 StringBuffer b = new StringBuffer("(");
112 for (int p = 0; p < geometries.length; p++) {
113 if (p > 0)
114 b.append(",");
115
116 String value = null;
117 switch (geometries[p].getType()) {
118
119 case (MULTILINESTRING) :
120 value = ((MultiLineString) geometries[p]).toString();
121 break;
122 case (LINESTRING) :
123 value = ((LineString) geometries[p]).toString();
124 break;
125 case (POLYGON) :
126 value = ((Polygon) geometries[p]).toString();
127 break;
128 case (MULTIPOLYGON) :
129 value = ((MultiPolygon) geometries[p]).toString();
130 break;
131 case (POINT) :
132 value = ((Point) geometries[p]).toString();
133 break;
134 case (MULTIPOINT) :
135 value = ((MultiPoint) geometries[p]).toString();
136 break;
137 case (GEOMETRYCOLLECTION) :
138 value = ((GeometryCollection) geometries[p]).toString();
139 break;
140 default :
141 System.out.println(
142 "Unknown geometry type: " + geometries[p].getType());
143 }
144
145 b.append(value);
146 }
147 b.append(")");
148 return b.toString();
149 }
150
151 public int numGeometries() {
152 return geometries.length;
153 }
154
155 public Geometry getGeometry(int idx) {
156 if (idx >= 0 & idx < geometries.length) {
157 return geometries[idx];
158 } else {
159 return null;
160 }
161 }
162
163 }
This page was automatically generated by Maven