View Javadoc
1   /*******************************************************************************
2    * Copyright (c) 2015 Voyager Search and MITRE
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Apache License, Version 2.0 which
5    * accompanies this distribution and is available at
6    *    http://www.apache.org/licenses/LICENSE-2.0.txt
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   * Writes a shape in the old format.
25   * <ul>
26   *   <li>Point: X Y
27   *   <br> 1.23 4.56
28   *   </li>
29   *   <li>Rect: XMin YMin XMax YMax
30   *   <br> 1.23 4.56 7.87 4.56
31   *   </li>
32   *   <li>{CIRCLE} '(' {POINT} {DISTANCE} ')' <br>
33   *   CIRCLE is "CIRCLE" or "Circle" (no other case), and POINT is "X Y" order pair of doubles, or
34   *   "Y,X" (lat,lon) pair of doubles, and DISTANCE is "d=RADIUS" or "distance=RADIUS" where RADIUS
35   *   is a double that is the distance radius in degrees.
36   *   </li>
37   * </ul>
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     * Writes a shape to a String, in a format that can be read by
50     * {@link LegacyShapeReader#readShapeOrNull(String, SpatialContext)}
51     * @param shape Not null.
52     * @return Not null.
53     */
54    public static String writeShape(Shape shape) {
55      return writeShape(shape, makeNumberFormat(6));
56    }
57  
58    /** Overloaded to provide a number format. */
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     * A convenience method to create a suitable NumberFormat for writing numbers.
85     */
86    public static NumberFormat makeNumberFormat(int fractionDigits) {
87      NumberFormat nf = NumberFormat.getInstance(Locale.ROOT);//not thread-safe
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 }