Class Geometry

java.lang.Object
dev.avelar.mapnik.Geometry
Direct Known Subclasses:
Geometry.Collection, Geometry.LineString, Geometry.MultiLineString, Geometry.MultiPoint, Geometry.MultiPolygon, Geometry.Point, Geometry.Polygon

public abstract class Geometry extends Object
A 2D geometry: a point, line, polygon, a multi version of those, or a collection. Geometries are immutable and hold no native memory. Build them with the factories (point(double, double), polygon(double[], double[]...) and so on) or read them from WKT, WKB or GeoJSON, and write them back out in any of the three.

Coordinates are plain numbers in whatever projection your data uses. Only 2D is supported: a third value in GeoJSON is ignored, and WKT or WKB with Z or M is rejected.

  • Method Details

    • kind

      public abstract GeometryKind kind()
      What kind of geometry this is.
    • isEmpty

      public abstract boolean isEmpty()
      True if it has no coordinates, for example an empty collection.
    • envelope

      public Box2d envelope()
      The bounding box, or null if the geometry is empty.
    • toWkt

      public String toWkt()
      As WKT, for example POINT(1 2).
    • toGeoJson

      public String toGeoJson()
      As a GeoJSON geometry object, for example {"type":"Point","coordinates":[1,2]}.
    • toWkb

      public byte[] toWkb()
      As little-endian 2D WKB.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • centroid

      public Point2d centroid()
      The centre of mass. Throws MapnikException for an empty geometry.
    • interiorPoint

      public Point2d interiorPoint()
      A point that lies inside a polygon, as far from its edges as reasonably possible (a good place for a label, unlike the centroid of a C shape). For a multi polygon it uses the polygon with the largest bounding box. Throws IllegalStateException for other kinds.
    • closestPoint

      public Geometry.Nearest closestPoint(double x, double y)
      The point of this geometry nearest to (x, y), with its distance. Throws MapnikException if empty.
    • isValid

      public boolean isValid()
      True if the geometry is valid: rings do not cross themselves, holes lie inside the exterior and do not overlap each other, and so on. For a ring that crosses itself Mapnik reports the problem as a wrong orientation, because a crossing ring has no single winding.
    • validityReason

      public String validityReason()
      Why the geometry is invalid, in Mapnik's words, or a sentence saying it is valid.
    • isSimple

      public boolean isSimple()
      True if the geometry has no self-intersections or repeated points that make it complex.
    • corrected

      public Geometry corrected()
      A copy with polygon rings wound consistently: exteriors counter-clockwise, holes clockwise. Either winding counts as valid for an ordinary polygon, but a consistent one is what GeoJSON and many other tools expect. It does not repair a ring that crosses itself.
    • simplify

      public Geometry simplify(SimplifyAlgorithm algorithm, double tolerance)
      A copy with fewer points, keeping the overall shape. tolerance is in the geometry's units and means a different distance for each algorithm. Polygon rings are never collapsed to fewer than four points, and points and multi points are unchanged.
    • offset

      public Geometry offset(double distance)
      A parallel copy of a line, distance to its left (positive) or right (negative). For line strings, multi line strings and collections of them. Throws MapnikException for points and polygons.
    • reproject

      public Geometry reproject(CoordinateTransform transform)
      A copy with every coordinate moved from the transform's source projection to its destination. Throws MapnikException if any point cannot be transformed.
    • point

      public static Geometry.Point point(double x, double y)
    • lineString

      public static Geometry.LineString lineString(double... xy)
      A line through the points given as x0, y0, x1, y1, ... At least two points.
    • lineString

      public static Geometry.LineString lineString(List<Point2d> points)
    • polygon

      public static Geometry.Polygon polygon(double[] exterior, double[]... holes)
      A polygon from an exterior ring and any holes, each given as x0, y0, x1, y1, ... The rings must be closed (the last point repeats the first) and have at least four points.
    • rectangle

      public static Geometry.Polygon rectangle(Box2d box)
      A rectangle covering box.
    • multiPoint

      public static Geometry.MultiPoint multiPoint(double... xy)
    • multiLineString

      public static Geometry.MultiLineString multiLineString(Geometry.LineString... lines)
    • multiPolygon

      public static Geometry.MultiPolygon multiPolygon(Geometry.Polygon... polygons)
    • collection

      public static Geometry.Collection collection(Geometry... geometries)
    • empty

      public static Geometry.Collection empty()
      A geometry with nothing in it: an empty collection.
    • fromWkb

      public static Geometry fromWkb(byte[] data)
      Read 2D WKB of either byte order. Throws IllegalArgumentException if it is malformed or has Z or M.
    • fromWkt

      public static Geometry fromWkt(String wkt)
      Read WKT, such as POINT(1 2) or POLYGON((0 0,1 0,1 1,0 0)). Type names are case-insensitive. EMPTY is accepted for the multi types, collections and lines. Throws IllegalArgumentException with the position of the first problem.
    • fromGeoJson

      public static Geometry fromGeoJson(String json)
      Read a GeoJSON geometry object. A third (elevation) value in a position is ignored. A null geometry reads as empty().