public final class GeoJson extends Object
This class uses the Point
and Point2D
classes to represent
GeoJSON positions. To assist in converting raw (x, y) coordinates the
p(double, double)
and p(int, int)
methods are provides to
quickly construct the Point instances.
As an example of using this class consider the following Polygon with a hole from the GeoJSON specification:
The equivalent BSON document can be constructed via:{ "type": "Polygon", "coordinates": [ [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ], [ [100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2] ] ] }
import static com.allanbank.mongodb.builder.GeoJson.polygon; import static com.allanbank.mongodb.builder.GeoJson.p; Document geoJsonPolygon = polygon( Arrays.asList( p(100.0, 0.0), p(101.0, 0.0), p(101.0, 1.0), p(100.0, 1.0), p(100.0, 0.0) ), Arrays.asList( p(100.2, 0.2), p(100.8, 0.2), p(100.8, 0.8), p(100.2, 0.8), p(100.2, 0.2) ) );
Modifier and Type | Method and Description |
---|---|
protected static void |
add(ArrayBuilder coordinates,
List<? extends Point2D> positions)
Adds a positions to the coordinates array.
|
protected static void |
add(ArrayBuilder coordinates,
Point2D position)
Adds a position to the coordinates array.
|
protected static void |
addRaw(ArrayBuilder arrayBuilder,
Point2D position)
Adds the (x,y) coordinates from the point directly to the array provided.
|
protected static void |
lineRing(ArrayBuilder positionArray,
List<? extends Point2D> positions)
Fills in the LineRing coordinates.
|
static Document |
lineString(List<? extends Point2D> points)
Constructs a GeoJSON 'LineString' document from the coordinates provided.
|
static Document |
lineString(Point2D p1,
Point2D p2,
Point2D... remaining)
Constructs a GeoJSON 'LineString' document from the coordinates provided.
|
static Document |
multiLineString(List<? extends Point2D> firstLineString,
List<? extends Point2D>... additionalLineStrings)
Constructs a GeoJSON 'MultiLineString' document from the coordinates
provided.
|
static Document |
multiPoint(List<? extends Point2D> positions)
Constructs a GeoJSON 'MultiPoint' document from the positions provided.
|
static Document |
multiPoint(Point2D firstPosition,
Point2D... additionalPositions)
Constructs a GeoJSON 'MultiPoint' document from the positions provided.
|
static Point2D |
p(double x,
double y)
Helper method to construct a
Point2D from the (x, y) coordinates. |
static Point |
p(int x,
int y)
Helper method to construct a
Point from the (x, y) coordinates. |
static Document |
point(Point2D position)
Constructs a GeoJSON 'Point' document from the coordinates provided.
|
static Document |
polygon(List<? extends Point2D> boundary)
Constructs a GeoJSON 'Polygon' document from the coordinates provided.
|
static Document |
polygon(List<? extends Point2D> boundary,
List<? extends Point2D>... holes)
Constructs a GeoJSON 'Polygon' document from the coordinates provided.
|
public static Document lineString(List<? extends Point2D> points) throws IllegalArgumentException
points
- The positions in the line string. There should be at least 2
positions for the document to be a valid LineString.IllegalArgumentException
- If the list does not contain at least 2 points.public static Document lineString(Point2D p1, Point2D p2, Point2D... remaining)
p1
- The first position in the line string.p2
- The second position in the line string.remaining
- The remaining positions in the line string.public static Document multiLineString(List<? extends Point2D> firstLineString, List<? extends Point2D>... additionalLineStrings)
Note: You will need to add a @SuppressWarnings("unchecked")
to
uses of this method as Java does not like mixing generics and varargs.
The @SafeVarargs
annotation is not available in Java 1.6, the
minimum version for the driver.
firstLineString
- The first line string.additionalLineStrings
- The remaining line strings.public static Document multiPoint(List<? extends Point2D> positions)
positions
- The positionspublic static Document multiPoint(Point2D firstPosition, Point2D... additionalPositions)
firstPosition
- The first positionadditionalPositions
- The other positionspublic static Point2D p(double x, double y)
Point2D
from the (x, y) coordinates.x
- The point's x positiony
- The point's y positionpublic static Point p(int x, int y)
Point
from the (x, y) coordinates.x
- The point's x positiony
- The point's y positionpublic static Document point(Point2D position)
position
- The point's positionpublic static Document polygon(List<? extends Point2D> boundary) throws IllegalArgumentException
boundary
- The boundary positions for the polygon.IllegalArgumentException
- If the line ring does not have at least 4 positions or the
first and last positions are not equivalent.public static Document polygon(List<? extends Point2D> boundary, List<? extends Point2D>... holes) throws IllegalArgumentException
Note: You will need to add a @SuppressWarnings("unchecked")
to
uses of this method as Java does not like mixing generics and varargs.
The @SafeVarargs
annotation is not available in Java 1.6, the
minimum version for the driver.
boundary
- The boundary positions for the polygon.holes
- The positions for the holes within the polygon.IllegalArgumentException
- If the line ring does not have at least 4 positions or the
first and last positions are not equivalent.protected static void add(ArrayBuilder coordinates, List<? extends Point2D> positions)
coordinates
- The array to add the position to.positions
- The positions to add.protected static void add(ArrayBuilder coordinates, Point2D position)
coordinates
- The array to add the position to.position
- The point to add.protected static void addRaw(ArrayBuilder arrayBuilder, Point2D position)
arrayBuilder
- The builder to append the (x,y) coordinates to.position
- The (x,y) coordinates.protected static void lineRing(ArrayBuilder positionArray, List<? extends Point2D> positions) throws IllegalArgumentException
positionArray
- The array to fill with the positions.positions
- The positions defining the LineRing.IllegalArgumentException
- If the line ring does not have at least 4 positions or the
first and last positions are not equivalent.Copyright © 2011-2013 Allanbank Consulting, Inc.. All Rights Reserved.