toGeometry method

Geometry toGeometry(
  1. Envelope envelope
)

Creates a {@link Geometry} with the same extent as the given envelope. The Geometry returned is guaranteed to be valid. To provide this behaviour, the following cases occur:

If the Envelope is:

  • null : returns an empty {@link Point}
  • a point : returns a non-empty {@link Point}
  • a line : returns a two-point {@link LineString}
  • a rectangle : returns a {@link Polygon} whose points are (minx, miny), (minx, maxy), (maxx, maxy), (maxx, miny), (minx, miny).

@param envelope the Envelope to convert @return an empty Point (for null Envelopes), a Point (when min x = max x and min y = max y) or a Polygon (in all other cases)

Implementation

Geometry toGeometry(Envelope envelope) {
  // null envelope - return empty point geometry
  if (envelope.isNull()) {
    return createPointEmpty();
  }

  // point?
  if (envelope.getMinX() == envelope.getMaxX() &&
      envelope.getMinY() == envelope.getMaxY()) {
    return createPoint(
        new Coordinate(envelope.getMinX(), envelope.getMinY()));
  }

  // vertical or horizontal line?
  if (envelope.getMinX() == envelope.getMaxX() ||
      envelope.getMinY() == envelope.getMaxY()) {
    return createLineString([
      new Coordinate(envelope.getMinX(), envelope.getMinY()),
      new Coordinate(envelope.getMaxX(), envelope.getMaxY())
    ]);
  }

  // create a CW ring for the polygon
  return createPolygon(
      createLinearRing([
        new Coordinate(envelope.getMinX(), envelope.getMinY()),
        new Coordinate(envelope.getMinX(), envelope.getMaxY()),
        new Coordinate(envelope.getMaxX(), envelope.getMaxY()),
        new Coordinate(envelope.getMaxX(), envelope.getMinY()),
        new Coordinate(envelope.getMinX(), envelope.getMinY())
      ]),
      null);
}