-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWKTReader.java
314 lines (299 loc) · 9.36 KB
/
WKTReader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/******************************************************************************
* Author: Deniz Ece Aktan
* Compilation: javac WKTReader.java
* Execution: java WKTReader
*
* Dependencies: com.sinergise.geometry
*
* This class Tread takes a WKT formatted String,
* transformes in into a Geometry object and returns it.
*
******************************************************************************/
import java.io.IOException;
import java.io.StreamTokenizer;
import java.io.StringReader;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import com.sinergise.geometry.Geometry;
import com.sinergise.geometry.GeometryCollection;
import com.sinergise.geometry.LineString;
import com.sinergise.geometry.MultiLineString;
import com.sinergise.geometry.MultiPoint;
import com.sinergise.geometry.MultiPolygon;
import com.sinergise.geometry.Point;
import com.sinergise.geometry.Polygon;
import util.Coordinate;
public class WKTReader {
private StreamTokenizer tokenizer;
private static final String EMPTY = "EMPTY";
private static final String COMMA = ",";
private static final String L_P = "(";
private static final String R_P = ")";
private HashMap<String, String> geoMap;
public WKTReader()
{
geoMap = new HashMap<String, String>();
geoMap.put("POINT", "com.sinergise.geometry.Point");
geoMap.put("LINESTRING", "com.sinergise.geometry.LineString");
geoMap.put("POLYGON", "com.sinergise.geometry.Poligon");
geoMap.put("MULTILINESTRING",
"com.sinergise.geometry.MultiLineString");
geoMap.put("MULTIPOINT", "com.sinergise.geometry.MultiPoint");
geoMap.put("MULTIPOLYGON",
"com.sinergise.geometry.MultiPoligon");
}
/**
* Transforms the input WKT-formatted String into Geometry object
* @throws ParseException
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public Geometry read(String wktString) throws ParseException,
IOException, InstantiationException,
IllegalAccessException, ClassNotFoundException {
StringReader reader = new StringReader(wktString);
tokenizer = new StreamTokenizer(reader);
tokenizer.resetSyntax();
tokenizer.wordChars('a', 'z');
tokenizer.wordChars('A', 'Z');
tokenizer.wordChars(128 + 32, 255);
tokenizer.wordChars('0', '9');
tokenizer.wordChars('-', '-');
tokenizer.wordChars('+', '+');
tokenizer.wordChars('.', '.');
tokenizer.whitespaceChars(0, ' ');
tokenizer.commentChar('#');
Geometry geoObj = readFormattedGeoString("");
return geoObj;
}
private Geometry readFormattedGeoString(String type_inp)
throws IOException, ParseException,
InstantiationException, IllegalAccessException,
ClassNotFoundException {
String type = "";
String nextToken;
Geometry output = null;
if (!type_inp.isEmpty())
type = type_inp;
else {
type = getNextWord();
nextToken = getNextEmptyOrOpener();
if (nextToken.equals(EMPTY)) {
output = (Geometry) Class.forName(geoMap.get(type)).newInstance();
return output;
}
}
if (type.equals("POINT")) {
Coordinate coord = new Coordinate(getNextNumber(),getNextNumber());
Point point = new Point(coord.getX(),coord.getY());
getNextCloser();
output = point;
}
else if (type.equalsIgnoreCase("LINESTRING")) {
Coordinate [] cs = getCoordinates();
ArrayList<Double> list = new ArrayList<>();
for (int i = 0; i < cs.length; i++) {
list.add(cs[i].getX());
list.add(cs[i].getY());
}
double[] arr = new double[list.size()];
for (int i = 0; i < arr.length; i++) {
arr[i] = list.get(i);
}
output = new LineString(arr);
}
else if (type.equalsIgnoreCase("POLYGON")) {
ArrayList<LineString> holes = new ArrayList<>();
getNextEmptyOrOpener();
LineString shell = (LineString) readFormattedGeoString("LINESTRING");
nextToken = getNextCloserOrComma();
LineString[] array = null;
if (!nextToken.equals(R_P))
{
while (nextToken.equals(COMMA)) {
nextToken = getNextEmptyOrOpener();
LineString hole = (LineString) readFormattedGeoString("LINESTRING");
holes.add(hole);
nextToken = getNextCloserOrComma();
}
array = new LineString[holes.size()];
for (int i = 0; i < holes.size(); i++) {
array[i] = holes.get(i);
}
}
output = new Polygon(shell, array);
}
else if (type.equalsIgnoreCase("MULTIPOINT")) {
ArrayList<Point> points = new ArrayList<>();
nextToken = getNextEmptyOrOpener();
points.add((Point)readFormattedGeoString("POINT"));
nextToken = getNextCloserOrComma();
while (nextToken.equals(COMMA)) {
nextToken = getNextEmptyOrOpener();
Point p = (Point) readFormattedGeoString("POINT");
points.add(p);
nextToken = getNextCloserOrComma();
}
Point[] array = new Point[points.size()];
for (int i = 0; i < points.size(); i++) {
array[i] = points.get(i);
}
output = new MultiPoint(array);
}
else if (type.equalsIgnoreCase("MULTILINESTRING")) {
ArrayList<LineString> lineStrings = new ArrayList<>();
getNextEmptyOrOpener();
LineString lineString = (LineString) readFormattedGeoString("LINESTRING");
lineStrings.add(lineString);
nextToken = getNextCloserOrComma();
while (nextToken.equals(COMMA)) {
nextToken = getNextEmptyOrOpener();
lineString = (LineString) readFormattedGeoString("LINESTRING");
lineStrings.add(lineString);
nextToken = getNextCloserOrComma();
}
LineString[] array = new LineString[lineStrings.size()];
for (int i = 0; i < lineStrings.size(); i++) {
array[i] = lineStrings.get(i);
}
output = new MultiLineString(array);
}
else if (type.equalsIgnoreCase("MULTIPOLYGON")) {
ArrayList<Polygon> polygons = new ArrayList<>();
getNextEmptyOrOpener();
Polygon polygon =(Polygon) readFormattedGeoString("POLYGON");
polygons.add(polygon);
nextToken = getNextCloserOrComma();
while (nextToken.equals(COMMA)) {
nextToken = getNextEmptyOrOpener();
polygon = (Polygon) readFormattedGeoString("POLYGON");
polygons.add(polygon);
nextToken = getNextCloserOrComma();
}
Polygon[]array = new Polygon[polygons.size()];
for (int i = 0; i < polygons.size(); i++) {
array[i] = polygons.get(i);
}
output = new MultiPolygon(array);
}
else if (type.equalsIgnoreCase("GEOMETRYCOLLECTION")) {
ArrayList<Geometry> geos = new ArrayList<>();
Geometry geo = readFormattedGeoString("");
geos.add(geo);
nextToken = getNextCloserOrComma();
while (nextToken.equals(COMMA)) {
geo = readFormattedGeoString("");
geos.add(geo);
nextToken = getNextCloserOrComma();
}
Geometry[]array = new Geometry[geos.size()];
for (int i = 0; i < geos.size(); i++) {
array[i] = geos.get(i);
}
output = new GeometryCollection<Geometry>(array);
}
return output;
}
private String getNextWord() throws ParseException,
IOException {
int type = tokenizer.nextToken();
switch (type) {
case StreamTokenizer.TT_WORD:
String word = tokenizer.sval;
if (word.equalsIgnoreCase(EMPTY))
return EMPTY;
return word;
case '(': return L_P;
case ')': return R_P;
case ',': return COMMA;
}
parseError("word");
return null;
}
private void parseError(String expected)
throws ParseException {
String tokenStr = tokenString();
throw new ParseException("Expected " + expected + " but found " + tokenStr, 0);
}
private String tokenString() {
switch (tokenizer.ttype) {
case StreamTokenizer.TT_NUMBER:
return "<NUMBER>";
case StreamTokenizer.TT_EOL:
return "End-of-Line";
case StreamTokenizer.TT_EOF:
return "End-of-Stream";
case StreamTokenizer.TT_WORD:
return "'" + tokenizer.sval + "'";
default:
}
return "'" + (char) tokenizer.ttype + "'";
}
private String getNextEmptyOrOpener()
throws IOException, ParseException {
String nextWord = getNextWord();
if (nextWord.equals(EMPTY) || nextWord.equals(L_P)) {
return nextWord;
}
parseError(EMPTY + " or " + L_P);
return null;
}
private double getNextNumber()
throws IOException, ParseException {
int type = tokenizer.nextToken();
switch (type) {
case StreamTokenizer.TT_WORD:
{
try {
return Double.parseDouble(tokenizer.sval);
}
catch (NumberFormatException ex) {
throw new ParseException("Invalid number: " + tokenizer.sval,0);
}
}
}
parseError("number");
return 0.0;
}
private String getNextCloser()
throws IOException, ParseException {
String nextWord = getNextWord();
if (nextWord.equals(R_P)) {
return nextWord;
}
parseError(R_P);
return null;
}
private String getNextCloserOrComma()
throws IOException, ParseException {
String nextWord = getNextWord();
if (nextWord.equals(COMMA) || nextWord.equals(R_P)) {
return nextWord;
}
parseError(COMMA + " or " + R_P);
return null;
}
private Coordinate getPreciseCoordinate()
throws IOException, ParseException
{
Coordinate coord = new Coordinate();
coord.setX(getNextNumber());
coord.setY(getNextNumber());
return coord;
}
private Coordinate[] getCoordinates()
throws IOException, ParseException {
String nextToken ="";
ArrayList<Coordinate> coordinates = new ArrayList<Coordinate>();
coordinates.add(getPreciseCoordinate());
nextToken = getNextCloserOrComma();
while (nextToken.equals(COMMA)) {
coordinates.add(getPreciseCoordinate());
nextToken = getNextCloserOrComma();
}
Coordinate[] array = new Coordinate[coordinates.size()];
return (Coordinate[]) coordinates.toArray(array);
}
}