1
2
3
4
5
6
7
8
9 package org.locationtech.spatial4j.context;
10
11 import org.locationtech.spatial4j.distance.CartesianDistCalc;
12 import org.locationtech.spatial4j.distance.DistanceCalculator;
13 import org.locationtech.spatial4j.distance.GeodesicSphereDistCalc;
14 import org.locationtech.spatial4j.exception.InvalidShapeException;
15 import org.locationtech.spatial4j.io.BinaryCodec;
16 import org.locationtech.spatial4j.io.LegacyShapeWriter;
17 import org.locationtech.spatial4j.io.SupportedFormats;
18 import org.locationtech.spatial4j.io.WKTReader;
19 import org.locationtech.spatial4j.shape.*;
20 import org.locationtech.spatial4j.shape.impl.RectangleImpl;
21
22 import java.text.ParseException;
23 import java.util.List;
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 public class SpatialContext {
39
40
41 public static final SpatialContext GEO = new SpatialContext(new SpatialContextFactory());
42
43
44 private final boolean geo;
45 private final ShapeFactory shapeFactory;
46 private final DistanceCalculator calculator;
47 private final Rectangle worldBounds;
48 private final BinaryCodec binaryCodec;
49 private final SupportedFormats formats;
50
51
52
53
54
55
56
57
58 @Deprecated
59 public SpatialContext(boolean geo, DistanceCalculator calculator, Rectangle worldBounds) {
60 this(initFromLegacyConstructor(geo, calculator, worldBounds));
61 }
62
63 private static SpatialContextFactory initFromLegacyConstructor(boolean geo,
64 DistanceCalculator calculator,
65 Rectangle worldBounds) {
66 SpatialContextFactory factory = new SpatialContextFactory();
67 factory.geo = geo;
68 factory.distCalc = calculator;
69 factory.worldBounds = worldBounds;
70 return factory;
71 }
72
73 @Deprecated
74 public SpatialContext(boolean geo) {
75 this(initFromLegacyConstructor(geo, null, null));
76 }
77
78
79
80
81 public SpatialContext(SpatialContextFactory factory) {
82 this.geo = factory.geo;
83
84 this.shapeFactory = factory.makeShapeFactory(this);
85
86 if (factory.distCalc == null) {
87 this.calculator = isGeo()
88 ? new GeodesicSphereDistCalc.Haversine()
89 : new CartesianDistCalc();
90 } else {
91 this.calculator = factory.distCalc;
92 }
93
94
95 Rectangle bounds = factory.worldBounds;
96 if (bounds == null) {
97 this.worldBounds = isGeo()
98 ? new RectangleImpl(-180, 180, -90, 90, this)
99 : new RectangleImpl(-Double.MAX_VALUE, Double.MAX_VALUE,
100 -Double.MAX_VALUE, Double.MAX_VALUE, this);
101 } else {
102 if (isGeo() && !bounds.equals(new RectangleImpl(-180, 180, -90, 90, this)))
103 throw new IllegalArgumentException("for geo (lat/lon), bounds must be " + GEO.getWorldBounds());
104 if (bounds.getMinX() > bounds.getMaxX())
105 throw new IllegalArgumentException("worldBounds minX should be <= maxX: "+ bounds);
106 if (bounds.getMinY() > bounds.getMaxY())
107 throw new IllegalArgumentException("worldBounds minY should be <= maxY: "+ bounds);
108
109 this.worldBounds = new RectangleImpl(bounds, this);
110 }
111
112 this.binaryCodec = factory.makeBinaryCodec(this);
113
114 factory.checkDefaultFormats();
115 this.formats = factory.makeFormats(this);
116 }
117
118
119 public ShapeFactory getShapeFactory() {
120 return shapeFactory;
121 }
122
123 public SupportedFormats getFormats() {
124 return formats;
125 }
126
127 public DistanceCalculator getDistCalc() {
128 return calculator;
129 }
130
131
132 public double calcDistance(Point p, double x2, double y2) {
133 return getDistCalc().distance(p, x2, y2);
134 }
135
136
137 public double calcDistance(Point p, Point p2) {
138 return getDistCalc().distance(p, p2);
139 }
140
141
142
143
144
145 public Rectangle getWorldBounds() {
146 return worldBounds;
147 }
148
149
150
151 @Deprecated
152 public boolean isNormWrapLongitude() {
153 return shapeFactory.isNormWrapLongitude();
154 }
155
156
157
158
159 public boolean isGeo() {
160 return geo;
161 }
162
163
164
165 @Deprecated
166 public double normX(double x) {
167 return shapeFactory.normX(x);
168 }
169
170
171
172 @Deprecated
173 public double normY(double y) { return shapeFactory.normY(y); }
174
175
176
177 @Deprecated
178 public void verifyX(double x) {
179 shapeFactory.verifyX(x);
180 }
181
182
183
184 @Deprecated
185 public void verifyY(double y) {
186 shapeFactory.verifyY(y);
187 }
188
189
190 @Deprecated
191 public Point makePoint(double x, double y) {
192 return shapeFactory.pointXY(x, y);
193 }
194
195
196 @Deprecated
197 public Rectangle makeRectangle(Point lowerLeft, Point upperRight) {
198 return shapeFactory.rect(lowerLeft, upperRight);
199 }
200
201
202
203
204
205
206 @Deprecated
207 public Rectangle makeRectangle(double minX, double maxX, double minY, double maxY) {
208 return shapeFactory.rect(minX, maxX, minY, maxY);
209 }
210
211
212 @Deprecated
213 public Circle makeCircle(double x, double y, double distance) {
214 return shapeFactory.circle(x, y, distance);
215 }
216
217
218 @Deprecated
219 public Circle makeCircle(Point point, double distance) {
220 return shapeFactory.circle(point, distance);
221 }
222
223
224
225 @Deprecated
226 public Shape makeLineString(List<Point> points) {
227 return shapeFactory.lineString(points, 0);
228 }
229
230
231
232
233 @Deprecated
234 public Shape makeBufferedLineString(List<Point> points, double buf) {
235 return shapeFactory.lineString(points, buf);
236 }
237
238
239 @Deprecated
240 public <S extends Shape> ShapeCollection<S> makeCollection(List<S> coll) {
241 return shapeFactory.multiShape(coll);
242 }
243
244
245 @Deprecated
246 public WKTReader getWktShapeParser() {
247 return (WKTReader)formats.getWktReader();
248 }
249
250
251
252
253
254
255
256 @Deprecated
257 public Shape readShapeFromWkt(String wkt) throws ParseException, InvalidShapeException {
258 return getWktShapeParser().parse(wkt);
259 }
260
261 public BinaryCodec getBinaryCodec() { return binaryCodec; }
262
263
264
265
266
267
268 @Deprecated
269 public Shape readShape(String value) throws InvalidShapeException {
270 return formats.read(value);
271 }
272
273
274
275
276
277
278
279
280 @Deprecated
281 public String toString(Shape shape) {
282 return LegacyShapeWriter.writeShape(shape);
283 }
284
285 @Override
286 public String toString() {
287 if (this.equals(GEO)) {
288 return GEO.getClass().getSimpleName()+".GEO";
289 } else {
290 return getClass().getSimpleName()+"{" +
291 "geo=" + geo +
292 ", calculator=" + calculator +
293 ", worldBounds=" + worldBounds +
294 '}';
295 }
296 }
297 }