Package com.google.common.geometry
Class S2TextFormat
- java.lang.Object
-
- com.google.common.geometry.S2TextFormat
-
@GwtCompatible public class S2TextFormat extends java.lang.Object
S2TextFormat contains a collection of functions for converting geometry to and from a human- readable format. It is mainly intended for testing and debugging. Be aware that the human- readable format is *not* designed to preserve the full precision of the original object, so it should not be used for data storage.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
S2TextFormat.ParseEntry
-
Constructor Summary
Constructors Constructor Description S2TextFormat()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static void
appendVertex(S2LatLng ll, java.lang.StringBuilder out)
private static void
appendVertex(S2Point p, java.lang.StringBuilder out)
private static void
appendVertices(java.lang.Iterable<S2Point> points, java.lang.StringBuilder out)
private static java.util.List<S2TextFormat.ParseEntry>
dictionaryParse(java.lang.String str)
Modeled on the DictionaryParse method of strings/serialize.ccprivate static S2Polygon
internalMakePolygon(java.lang.String str, boolean normalizeLoops)
static S2CellId
makeCellId(java.lang.String str)
As above, but does not CHECK-fail on invalid input.static S2CellId
makeCellIdOrDie(java.lang.String str)
Parses an S2CellId in the format "f/dd..d" where "f" is a digit in the range [0-5] representing the S2CellId face, and "dd..d" is a string of digits in the range [0-3] representing each child's position with respect to its parent.static S2CellUnion
makeCellUnion(java.lang.String str)
As above, but does not CHECK-fail on invalid input.static S2CellUnion
makeCellUnionOrDie(java.lang.String str)
Parses a comma-separated list of S2CellIds in the format above, and returns the corresponding S2CellUnion.static S2ShapeIndex
makeIndex(java.lang.String str)
As above, but does not CHECK-fail on invalid input.static S2ShapeIndex
makeIndexOrDie(java.lang.String str)
Returns a S2ShapeIndex containing the points, polylines, and loops (in the form of a single polygon) described by the following format:static S2LatLng
makeLatLng(java.lang.String str)
As above, but does not CHECK-fail on invalid input.static S2LatLng
makeLatLngOrDie(java.lang.String str)
Given a string in the same format as ParseLatLngs, returns a single S2LatLng.static S2LatLngRect
makeLatLngRect(java.lang.String str)
As above, but does not CHECK-fail on invalid input.(package private) S2LatLngRect
makeLatLngRectOrDie(java.lang.String str)
Given a string in the same format as ParseLatLngs, returns the minimal bounding S2LatLngRect that contains the coordinates.static S2LaxPolygonShape
makeLaxPolygon(java.lang.String str)
As above, but does not CHECK-fail on invalid input.static S2LaxPolygonShape
makeLaxPolygonOrDie(java.lang.String str)
Parses a string in the same format as MakePolygon, except that loops must be oriented so that the interior of the loop is always on the left, and polygons with degeneracies are supported.static S2LaxPolylineShape
makeLaxPolyline(java.lang.String str)
As above, but does not CHECK-fail on invalid input.static S2LaxPolylineShape
makeLaxPolylineOrDie(java.lang.String str)
Like makePolyline, but returns an S2LaxPolylineShape instead.static S2Loop
makeLoop(java.lang.String str)
As above, but does not CHECK-fail on invalid input.static S2Loop
makeLoopOrDie(java.lang.String str)
Given a string of latitude-longitude coordinates in degrees, returns a newly allocated loop.static S2Point
makePoint(java.lang.String str)
As above, but do not CHECK-fail on invalid input.static S2Point
makePointOrDie(java.lang.String str)
Returns an S2Point corresponding to the given a latitude-longitude coordinate in degrees.static S2Polygon
makePolygon(java.lang.String str)
As above, but does not CHECK-fail on invalid input.static S2Polygon
makePolygonOrDie(java.lang.String str)
Given a sequence of loops separated by semicolons, returns a newly allocated polygon.static S2Polyline
makePolyline(java.lang.String str)
As above, but does not CHECK-fail on invalid input.static S2Polyline
makePolylineOrDie(java.lang.String str)
Similar to makeLoop(), but returns an S2Polyline rather than an S2Loop.static S2Polygon
makeVerbatimPolygon(java.lang.String str)
As above, but does not CHECK-fail on invalid input.static S2Polygon
makeVerbatimPolygonOrDie(java.lang.String str)
Like MakePolygon(), except that it does not normalize loops (i.e., it gives you exactly what you asked for).(package private) static java.util.List<S2LatLng>
parseLatLngs(java.lang.String str)
As above, but does not CHECK-fail on invalid input.static java.util.List<S2LatLng>
parseLatLngsOrDie(java.lang.String str)
Parses a string of one or more latitude-longitude coordinates in degrees, and return the corresponding List of S2LatLng points.static java.util.List<S2Point>
parsePoints(java.lang.String str)
As above, but does not CHECK-fail on invalid input.static java.util.List<S2Point>
parsePointsOrDie(java.lang.String str)
Parses a string in the same format as parseLatLngs, and return the corresponding List of S2Point values.static java.lang.String
s2LatLngsToString(java.util.List<S2LatLng> latlngs)
Convert a list of S2LatLngs to the S2TextFormat string representation documented above.static java.lang.String
s2PointsToString(java.util.List<S2Point> points)
Convert a list of S2Points to the S2TextFormat string representation documented above.private static java.util.List<java.lang.String>
splitString(java.lang.String str, java.lang.String regexp)
static java.lang.String
toString(S2CellId cellId)
Convert an S2CellId to the S2TextFormat string representation documented above.static java.lang.String
toString(S2CellUnion cellUnion)
Convert an S2CellUnion to the S2TextFormat string representation documented above.static java.lang.String
toString(S2LatLng latlng)
Convert an S2LatLng to the S2TextFormat string representation documented above.static java.lang.String
toString(S2LatLngRect rect)
Convert an S2LatLngRect to the S2TextFormat string representation documented above.static java.lang.String
toString(S2LaxPolygonShape polygon)
Convert an S2LaxPolygonShape to the S2TextFormat string representation documented above.static java.lang.String
toString(S2LaxPolygonShape polygon, java.lang.String loopSeparator)
Convert an S2LaxPolygonShape to the S2TextFormat string representation documented above, using the given loopSeparator.static java.lang.String
toString(S2LaxPolylineShape polyline)
Convert an S2LaxPolylineShape to the S2TextFormat string representation documented above.static java.lang.String
toString(S2Loop loop)
Convert an S2Loop to the S2TextFormat string representation documented above.static java.lang.String
toString(S2Point s2Point)
Convert an S2Point to the S2TextFormat string representation documented above.static java.lang.String
toString(S2Polygon polygon)
Convert an S2Polygon to the S2TextFormat string representation documented above.static java.lang.String
toString(S2Polygon polygon, java.lang.String loopSeparator)
Convert an S2 polygon to the S2TextFormat string representation documented above, using the given loopSeparator between each loop.static java.lang.String
toString(S2Polyline polyline)
Convert an S2Polyline to the S2TextFormat string representation documented above.static java.lang.String
toString(S2ShapeIndex index)
Convert an S2CellUnion to the S2TextFormat string representation documented above.
-
-
-
Method Detail
-
makePointOrDie
public static S2Point makePointOrDie(java.lang.String str)
Returns an S2Point corresponding to the given a latitude-longitude coordinate in degrees. Example of the input format: "-20:150"
-
makePoint
@Nullable public static S2Point makePoint(java.lang.String str)
As above, but do not CHECK-fail on invalid input. Returns null if conversion is unsuccessful.
-
parseLatLngsOrDie
public static java.util.List<S2LatLng> parseLatLngsOrDie(java.lang.String str)
Parses a string of one or more latitude-longitude coordinates in degrees, and return the corresponding List of S2LatLng points.Examples of the input format:
"" // no points "-20:150" // one point "-20:150, -20:151, -19:150" // three points
-
parseLatLngs
@Nullable static java.util.List<S2LatLng> parseLatLngs(java.lang.String str)
As above, but does not CHECK-fail on invalid input. Returns null if conversion is unsuccessful.
-
parsePointsOrDie
public static java.util.List<S2Point> parsePointsOrDie(java.lang.String str)
Parses a string in the same format as parseLatLngs, and return the corresponding List of S2Point values.
-
parsePoints
@Nullable public static java.util.List<S2Point> parsePoints(java.lang.String str)
As above, but does not CHECK-fail on invalid input. Returns null if conversion is unsuccessful.
-
makeLatLngOrDie
public static S2LatLng makeLatLngOrDie(java.lang.String str)
Given a string in the same format as ParseLatLngs, returns a single S2LatLng.
-
makeLatLng
@Nullable public static S2LatLng makeLatLng(java.lang.String str)
As above, but does not CHECK-fail on invalid input. Returns null if conversion is unsuccessful.
-
makeLatLngRectOrDie
S2LatLngRect makeLatLngRectOrDie(java.lang.String str)
Given a string in the same format as ParseLatLngs, returns the minimal bounding S2LatLngRect that contains the coordinates.
-
makeLatLngRect
@Nullable public static S2LatLngRect makeLatLngRect(java.lang.String str)
As above, but does not CHECK-fail on invalid input. Returns null if conversion is unsuccessful.
-
makeCellIdOrDie
public static S2CellId makeCellIdOrDie(java.lang.String str)
Parses an S2CellId in the format "f/dd..d" where "f" is a digit in the range [0-5] representing the S2CellId face, and "dd..d" is a string of digits in the range [0-3] representing each child's position with respect to its parent. (Note that the latter string may be empty.)For example "4/" represents S2CellId.fromFace(4), and "3/02" represents S2CellId.fromFace(3).child(0).child(2).
This function is a wrapper for S2CellId.fromDebugString().
-
makeCellId
@Nullable public static S2CellId makeCellId(java.lang.String str)
As above, but does not CHECK-fail on invalid input. Returns null if conversion is unsuccessful.
-
makeCellUnionOrDie
public static S2CellUnion makeCellUnionOrDie(java.lang.String str)
Parses a comma-separated list of S2CellIds in the format above, and returns the corresponding S2CellUnion. (Note that S2CellUnions are automatically normalized by sorting, removing duplicates, and replacing groups of 4 child cells by their parent cell.)
-
makeCellUnion
@Nullable public static S2CellUnion makeCellUnion(java.lang.String str)
As above, but does not CHECK-fail on invalid input. Returns null if conversion is unsuccessful.
-
makeLoopOrDie
public static S2Loop makeLoopOrDie(java.lang.String str)
Given a string of latitude-longitude coordinates in degrees, returns a newly allocated loop. Example of the input format:"-20:150, 10:-120, 0.123:-170.652"
The strings "empty" or "full" create an empty or full loop respectively.
-
makeLoop
@Nullable public static S2Loop makeLoop(java.lang.String str)
As above, but does not CHECK-fail on invalid input. Returns null if conversion is unsuccessful.
-
makePolylineOrDie
public static S2Polyline makePolylineOrDie(java.lang.String str)
Similar to makeLoop(), but returns an S2Polyline rather than an S2Loop.
-
makePolyline
@Nullable public static S2Polyline makePolyline(java.lang.String str)
As above, but does not CHECK-fail on invalid input. Returns null if conversion is unsuccessful.
-
makeLaxPolylineOrDie
@GwtIncompatible("S2LaxPolylineShape") public static S2LaxPolylineShape makeLaxPolylineOrDie(java.lang.String str)
Like makePolyline, but returns an S2LaxPolylineShape instead.
-
makeLaxPolyline
@Nullable @GwtIncompatible("S2LaxPolylineShape") public static S2LaxPolylineShape makeLaxPolyline(java.lang.String str)
As above, but does not CHECK-fail on invalid input. Returns null if conversion is unsuccessful.
-
makePolygonOrDie
public static S2Polygon makePolygonOrDie(java.lang.String str)
Given a sequence of loops separated by semicolons, returns a newly allocated polygon. Loops are automatically normalized by inverting them if necessary so that they enclose at most half of the unit sphere. (Historically this was once a requirement of polygon loops. It also hides the problem that if the user thinks of the coordinates as X:Y rather than LAT:LNG, it yields a loop with the opposite orientation.)Examples of the input format:
"10:20, 90:0, 20:30" // one loop "10:20, 90:0, 20:30; 5.5:6.5, -90:-180, -15.2:20.3" // two loops "" // the empty polygon (consisting of no loops) "empty" // the empty polygon (consisting of no loops) "full" // the full polygon (consisting of one full loop).
-
makePolygon
@Nullable public static S2Polygon makePolygon(java.lang.String str)
As above, but does not CHECK-fail on invalid input. Returns null if conversion is unsuccessful.
-
makeVerbatimPolygonOrDie
public static S2Polygon makeVerbatimPolygonOrDie(java.lang.String str)
Like MakePolygon(), except that it does not normalize loops (i.e., it gives you exactly what you asked for).
-
makeVerbatimPolygon
@Nullable public static S2Polygon makeVerbatimPolygon(java.lang.String str)
As above, but does not CHECK-fail on invalid input. Returns null if conversion is unsuccessful.
-
makeLaxPolygonOrDie
@GwtIncompatible("S2LaxPolygonShape") public static S2LaxPolygonShape makeLaxPolygonOrDie(java.lang.String str)
Parses a string in the same format as MakePolygon, except that loops must be oriented so that the interior of the loop is always on the left, and polygons with degeneracies are supported. As with MakePolygon, "full" denotes the full polygon, and "" or "empty" denote the empty polygon.
-
makeLaxPolygon
@Nullable @GwtIncompatible("S2LaxPolygonShape") public static S2LaxPolygonShape makeLaxPolygon(java.lang.String str)
As above, but does not CHECK-fail on invalid input. Returns null if conversion is unsuccessful.
-
makeIndexOrDie
@GwtIncompatible("S2LaxPolylineShape, S2LaxPolygonShape") public static S2ShapeIndex makeIndexOrDie(java.lang.String str)
Returns a S2ShapeIndex containing the points, polylines, and loops (in the form of a single polygon) described by the following format:point1|point2|... # line1|line2|... # polygon1|polygon2|...
Examples:
1:2 | 2:3 # # // Two points # 0:0, 1:1, 2:2 | 3:3, 4:4 # // Two polylines # # 0:0, 0:3, 3:0; 1:1, 2:1, 1:2 // Two nested loops (one polygon) 5:5 # 6:6, 7:7 # 0:0, 0:1, 1:0 // One of each # # empty // One empty polygon # # empty | full // One empty polygon, one full polygon
Loops should be directed so that the region's interior is on the left. Loops can be degenerate (they do not need to meet S2Loop requirements).
CAVEAT: Because whitespace is ignored, empty polygons must be specified as the string "empty" rather than as the empty string ("").
-
makeIndex
@Nullable @GwtIncompatible("S2LaxPolylineShape, S2LaxPolygonShape") public static S2ShapeIndex makeIndex(java.lang.String str)
As above, but does not CHECK-fail on invalid input. Returns null if conversion is unsuccessful.
-
toString
public static java.lang.String toString(S2Point s2Point)
Convert an S2Point to the S2TextFormat string representation documented above.
-
toString
public static java.lang.String toString(S2LatLng latlng)
Convert an S2LatLng to the S2TextFormat string representation documented above.
-
toString
public static java.lang.String toString(S2LatLngRect rect)
Convert an S2LatLngRect to the S2TextFormat string representation documented above.
-
toString
public static java.lang.String toString(S2CellId cellId)
Convert an S2CellId to the S2TextFormat string representation documented above.
-
toString
public static java.lang.String toString(S2CellUnion cellUnion)
Convert an S2CellUnion to the S2TextFormat string representation documented above.
-
toString
public static java.lang.String toString(S2Loop loop)
Convert an S2Loop to the S2TextFormat string representation documented above.
-
toString
public static java.lang.String toString(S2Polyline polyline)
Convert an S2Polyline to the S2TextFormat string representation documented above.
-
toString
public static java.lang.String toString(S2Polygon polygon)
Convert an S2Polygon to the S2TextFormat string representation documented above.
-
toString
public static java.lang.String toString(S2Polygon polygon, java.lang.String loopSeparator)
Convert an S2 polygon to the S2TextFormat string representation documented above, using the given loopSeparator between each loop. Empty and Full polygons are represented as "empty" and "full" respectively.
-
s2PointsToString
public static java.lang.String s2PointsToString(java.util.List<S2Point> points)
Convert a list of S2Points to the S2TextFormat string representation documented above.
-
s2LatLngsToString
public static java.lang.String s2LatLngsToString(java.util.List<S2LatLng> latlngs)
Convert a list of S2LatLngs to the S2TextFormat string representation documented above.
-
toString
@GwtIncompatible("S2LaxPolylineShape, S2LaxPolygonShape") public static java.lang.String toString(S2LaxPolylineShape polyline)
Convert an S2LaxPolylineShape to the S2TextFormat string representation documented above.
-
toString
@GwtIncompatible("S2LaxPolylineShape, S2LaxPolygonShape") public static java.lang.String toString(S2LaxPolygonShape polygon)
Convert an S2LaxPolygonShape to the S2TextFormat string representation documented above.
-
toString
@GwtIncompatible("S2LaxPolylineShape, S2LaxPolygonShape") public static java.lang.String toString(S2LaxPolygonShape polygon, java.lang.String loopSeparator)
Convert an S2LaxPolygonShape to the S2TextFormat string representation documented above, using the given loopSeparator.
-
toString
public static java.lang.String toString(S2ShapeIndex index)
Convert an S2CellUnion to the S2TextFormat string representation documented above. The index may contain S2Shapes of any type. Shapes are reordered if necessary so that all point geometry (shapes of dimension 0) are first, followed by all polyline geometry, followed by all polygon geometry.
-
splitString
private static java.util.List<java.lang.String> splitString(java.lang.String str, java.lang.String regexp)
-
dictionaryParse
@Nullable private static java.util.List<S2TextFormat.ParseEntry> dictionaryParse(java.lang.String str)
Modeled on the DictionaryParse method of strings/serialize.cc
-
internalMakePolygon
@Nullable private static S2Polygon internalMakePolygon(java.lang.String str, boolean normalizeLoops)
-
appendVertex
private static void appendVertex(S2LatLng ll, java.lang.StringBuilder out)
-
appendVertex
private static void appendVertex(S2Point p, java.lang.StringBuilder out)
-
appendVertices
private static void appendVertices(java.lang.Iterable<S2Point> points, java.lang.StringBuilder out)
-
-