1
2
3
4
5
6
7
8
9 package org.locationtech.spatial4j.io;
10
11 import java.io.IOException;
12 import java.io.Writer;
13 import java.text.NumberFormat;
14 import java.util.Locale;
15
16 import org.locationtech.spatial4j.context.SpatialContext;
17 import org.locationtech.spatial4j.context.SpatialContextFactory;
18 import org.locationtech.spatial4j.shape.Circle;
19 import org.locationtech.spatial4j.shape.Point;
20 import org.locationtech.spatial4j.shape.Rectangle;
21 import org.locationtech.spatial4j.shape.Shape;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 @Deprecated
40 public class LegacyShapeWriter implements ShapeWriter {
41
42 final SpatialContext ctx;
43
44 public LegacyShapeWriter(SpatialContext ctx, SpatialContextFactory factory) {
45 this.ctx = ctx;
46 }
47
48
49
50
51
52
53
54 public static String writeShape(Shape shape) {
55 return writeShape(shape, makeNumberFormat(6));
56 }
57
58
59 public static String writeShape(Shape shape, NumberFormat nf) {
60 if (shape instanceof Point) {
61 Point point = (Point) shape;
62 return nf.format(point.getX()) + " " + nf.format(point.getY());
63 }
64 else if (shape instanceof Rectangle) {
65 Rectangle rect = (Rectangle)shape;
66 return
67 nf.format(rect.getMinX()) + " " +
68 nf.format(rect.getMinY()) + " " +
69 nf.format(rect.getMaxX()) + " " +
70 nf.format(rect.getMaxY());
71 }
72 else if (shape instanceof Circle) {
73 Circle c = (Circle) shape;
74 return "Circle(" +
75 nf.format(c.getCenter().getX()) + " " +
76 nf.format(c.getCenter().getY()) + " " +
77 "d=" + nf.format(c.getRadius()) +
78 ")";
79 }
80 return shape.toString();
81 }
82
83
84
85
86 public static NumberFormat makeNumberFormat(int fractionDigits) {
87 NumberFormat nf = NumberFormat.getInstance(Locale.ROOT);
88 nf.setGroupingUsed(false);
89 nf.setMaximumFractionDigits(fractionDigits);
90 nf.setMinimumFractionDigits(0);
91 return nf;
92 }
93
94 @Override
95 public String getFormatName() {
96 return ShapeIO.LEGACY;
97 }
98
99 @Override
100 public void write(Writer output, Shape shape) throws IOException {
101 output.append(writeShape(shape));
102 }
103
104 @Override
105 public String toString(Shape shape) {
106 return writeShape(shape);
107 }
108 }