1
2
3
4
5
6
7
8
9 package org.locationtech.spatial4j.io;
10
11 import java.io.IOException;
12 import java.io.Reader;
13 import java.text.ParseException;
14 import java.util.StringTokenizer;
15
16 import org.locationtech.spatial4j.context.SpatialContext;
17 import org.locationtech.spatial4j.context.SpatialContextFactory;
18 import org.locationtech.spatial4j.exception.InvalidShapeException;
19 import org.locationtech.spatial4j.shape.Point;
20 import org.locationtech.spatial4j.shape.Shape;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 @Deprecated
39 public class LegacyShapeReader implements ShapeReader {
40
41 final SpatialContext ctx;
42
43 public LegacyShapeReader(SpatialContext ctx, SpatialContextFactory factory) {
44 this.ctx = ctx;
45 }
46
47
48
49
50
51
52
53
54 public static Shape readShapeOrNull(String str, SpatialContext ctx) throws InvalidShapeException {
55 if (str == null || str.length() == 0) {
56 throw new InvalidShapeException(str);
57 }
58
59 if (Character.isLetter(str.charAt(0))) {
60 if (str.startsWith("Circle(") || str.startsWith("CIRCLE(")) {
61 int idx = str.lastIndexOf(')');
62 if (idx > 0) {
63 String body = str.substring("Circle(".length(), idx);
64 StringTokenizer st = new StringTokenizer(body, " ");
65 String token = st.nextToken();
66 Point pt;
67 if (token.indexOf(',') != -1) {
68 pt = readLatCommaLonPoint(token, ctx);
69 } else {
70 double x = Double.parseDouble(token);
71 double y = Double.parseDouble(st.nextToken());
72 pt = ctx.makePoint(x, y);
73 }
74 Double d = null;
75
76 String arg = st.nextToken();
77 idx = arg.indexOf('=');
78 if (idx > 0) {
79 String k = arg.substring(0, idx);
80 if (k.equals("d") || k.equals("distance")) {
81 d = Double.parseDouble(arg.substring(idx + 1));
82 } else {
83 throw new InvalidShapeException("unknown arg: " + k + " :: " + str);
84 }
85 } else {
86 d = Double.parseDouble(arg);
87 }
88 if (st.hasMoreTokens()) {
89 throw new InvalidShapeException("Extra arguments: " + st.nextToken() + " :: " + str);
90 }
91 if (d == null) {
92 throw new InvalidShapeException("Missing Distance: " + str);
93 }
94
95 return ctx.makeCircle(pt, d);
96 }
97 }
98 return null;
99 }
100
101 if (str.indexOf(',') != -1)
102 return readLatCommaLonPoint(str, ctx);
103 StringTokenizer st = new StringTokenizer(str, " ");
104 double p0 = Double.parseDouble(st.nextToken());
105 double p1 = Double.parseDouble(st.nextToken());
106 if (st.hasMoreTokens()) {
107 double p2 = Double.parseDouble(st.nextToken());
108 double p3 = Double.parseDouble(st.nextToken());
109 if (st.hasMoreTokens())
110 throw new InvalidShapeException("Only 4 numbers supported (rect) but found more: " + str);
111 return ctx.makeRectangle(p0, p2, p1, p3);
112 }
113 return ctx.makePoint(p0, p1);
114 }
115
116
117 private static Point readLatCommaLonPoint(String value, SpatialContext ctx) throws InvalidShapeException {
118 double[] latLon = ParseUtils.parseLatitudeLongitude(value);
119 return ctx.makePoint(latLon[1], latLon[0]);
120 }
121
122
123
124 @Override
125 public String getFormatName() {
126 return ShapeIO.LEGACY;
127 }
128
129 @Override
130 public Shape read(Object value) throws IOException, ParseException, InvalidShapeException {
131 Shape shape = readShapeOrNull(value.toString(), ctx);
132 if(shape==null) {
133 throw new ParseException("unable to read shape: "+value, 0);
134 }
135 return readShapeOrNull(value.toString(), ctx);
136 }
137
138 @Override
139 public Shape readIfSupported(Object value) throws InvalidShapeException {
140 return readShapeOrNull(value.toString(), ctx);
141 }
142
143 @Override
144 public Shape read(Reader reader) throws IOException, ParseException, InvalidShapeException {
145 return read(WKTReader.readString(reader));
146 }
147 }