Class GeometryJSON


  • public class GeometryJSON
    extends Object
    Reads and writes geometry objects to and from geojson.

     Point point = new Point(1,2);
    
     GeometryJSON g = new GeometryJSON();
     g.writePoint(point, "point.json"));
     Point point2 = g.readPoint("point.json");
    
     Geometry geometry = ...;
     g.write(geometry, new File("geometry.json"));
     geometry = g.read("geometry.json");
    
     
    Author:
    Justin Deoliveira, OpenGeo
    • Constructor Summary

      Constructors 
      Constructor Description
      GeometryJSON()
      Constructs a geometry json instance.
      GeometryJSON​(int decimals)
      Constructs a geometry json instance specifying the number of decimals to use when encoding floating point numbers.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean isTrace()
      Tracing flag.
      Geometry read​(InputStream input)
      Reads a Geometry instance from GeoJSON.
      Geometry read​(Object input)
      Reads a Geometry instance from GeoJSON.
      GeometryCollection readGeometryCollection​(InputStream input)
      Reads a GeometryCollection from GeoJSON.
      GeometryCollection readGeometryCollection​(Object input)
      Reads a GeometryCollection from GeoJSON.
      LineString readLine​(InputStream input)
      Reads a LineString from GeoJSON.
      LineString readLine​(Object input)
      Reads a LineString from GeoJSON.
      MultiLineString readMultiLine​(InputStream input)
      Reads a MultiLineString from GeoJSON.
      MultiLineString readMultiLine​(Object input)
      Reads a MultiLineString from GeoJSON.
      MultiPoint readMultiPoint​(InputStream input)
      Reads a MultiPoint from GeoJSON.
      MultiPoint readMultiPoint​(Object input)
      Reads a MultiPoint from GeoJSON.
      MultiPolygon readMultiPolygon​(InputStream input)
      Reads a MultiPolygon from GeoJSON.
      MultiPolygon readMultiPolygon​(Object input)
      Reads a MultiPolygon from GeoJSON.
      Point readPoint​(InputStream input)
      Reads a Point from GeoJSON.
      Point readPoint​(Object input)
      Reads a Point from GeoJSON.
      Polygon readPolygon​(InputStream input)
      Reads a Polygon from GeoJSON.
      Polygon readPolygon​(Object input)
      Reads a Polygon from GeoJSON.
      void setTrace​(boolean trace)
      Sets trace flag.
      String toString​(BoundingBox bbox)
      Writes an BoundingBox instance as GeoJSON returning the result as a string.
      String toString​(Envelope e)
      Writes an Envelope instance as GeoJSON returning the result as a string.
      String toString​(Geometry geometry)
      Writes a Geometry instance as GeoJSON returning the result as a string.
      void write​(Geometry geometry, OutputStream output)
      Writes a Geometry instance as GeoJSON.
      void write​(Geometry geometry, Object output)
      Writes a Geometry instance as GeoJSON.
      void writeGeometryCollection​(GeometryCollection gcol, OutputStream output)
      Writes a GeometryCollection as GeoJSON.
      void writeGeometryCollection​(GeometryCollection gcol, Object output)
      Writes a GeometryCollection as GeoJSON.
      void writeLine​(LineString line, OutputStream output)
      Writes a LineString as GeoJSON.
      void writeLine​(LineString line, Object output)
      Writes a LineString as GeoJSON.
      void writeMultiLine​(MultiLineString mline, OutputStream output)
      Writes a MultiLineString as GeoJSON.
      void writeMultiLine​(MultiLineString mline, Object output)
      Writes a MultiLineString as GeoJSON.
      void writeMultiPoint​(MultiPoint mpoint, OutputStream output)
      Writes a MultiPoint as GeoJSON.
      void writeMultiPoint​(MultiPoint mpoint, Object output)
      Writes a MultiPoint as GeoJSON.
      void writeMultiPolygon​(MultiPolygon mpoly, OutputStream output)
      Writes a MultiPolygon as GeoJSON.
      void writeMultiPolygon​(MultiPolygon mpoly, Object output)
      Writes a MultiPolygon as GeoJSON.
      void writePoint​(Point point, OutputStream output)
      Writes a Point as GeoJSON.
      void writePoint​(Point point, Object output)
      Writes a Point as GeoJSON.
      void writePolygon​(Polygon poly, OutputStream output)
      Writes a Polygon as GeoJSON.
      void writePolygon​(Polygon poly, Object output)
      Writes a Polygon as GeoJSON.
      • Methods inherited from class Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • GeometryJSON

        public GeometryJSON()
        Constructs a geometry json instance.
      • GeometryJSON

        public GeometryJSON​(int decimals)
        Constructs a geometry json instance specifying the number of decimals to use when encoding floating point numbers.
    • Method Detail

      • setTrace

        public void setTrace​(boolean trace)
        Sets trace flag.
      • isTrace

        public boolean isTrace()
        Tracing flag.

        When this flag is set parsed documents will be echoed to stdout during parsing.

      • write

        public void write​(Geometry geometry,
                          Object output)
                   throws IOException
        Writes a Geometry instance as GeoJSON.
        Parameters:
        geometry - The geometry.
        output - The output. See GeoJSONUtil.toWriter(Object) for details.
        Throws:
        IOException
      • write

        public void write​(Geometry geometry,
                          OutputStream output)
                   throws IOException
        Writes a Geometry instance as GeoJSON.

        This method calls through to write(Geometry, Object)

        Parameters:
        geometry - The geometry.
        output - The output stream.
        Throws:
        IOException
      • toString

        public String toString​(Geometry geometry)
        Writes a Geometry instance as GeoJSON returning the result as a string.
        Parameters:
        geometry - The geometry.
        Returns:
        The geometry encoded as GeoJSON
      • read

        public Geometry read​(Object input)
                      throws IOException
        Reads a Geometry instance from GeoJSON.
        Parameters:
        input - The input. See GeoJSONUtil.toReader(Object) for details.
        Returns:
        The geometry instance.
        Throws:
        IOException
      • read

        public Geometry read​(InputStream input)
                      throws IOException
        Reads a Geometry instance from GeoJSON.

        This method calls through to read(Object)

        Parameters:
        input - The input stream.
        Returns:
        The geometry instance.
        Throws:
        IOException
      • writePoint

        public void writePoint​(Point point,
                               Object output)
                        throws IOException
        Writes a Point as GeoJSON.
        Parameters:
        point - The point.
        output - The output. See GeoJSONUtil.toWriter(Object) for details.
        Throws:
        IOException - In the event of a parsing error or if the input json is invalid.
      • writePoint

        public void writePoint​(Point point,
                               OutputStream output)
                        throws IOException
        Writes a Point as GeoJSON.

        This method calls through to writePoint(Point, Object)

        Parameters:
        point - The point.
        output - The output stream.
        Throws:
        IOException - In the event of a parsing error or if the input json is invalid.
      • readPoint

        public Point readPoint​(Object input)
                        throws IOException
        Reads a Point from GeoJSON.
        Parameters:
        input - The input. See GeoJSONUtil.toReader(Object) for details.
        Returns:
        The point.
        Throws:
        IOException - In the event of a parsing error or if the input json is invalid.
      • readPoint

        public Point readPoint​(InputStream input)
                        throws IOException
        Reads a Point from GeoJSON.

        This method calls through to readPoint(Object)

        Parameters:
        input - The input stream.
        Returns:
        The point.
        Throws:
        IOException - In the event of a parsing error or if the input json is invalid.
      • writeLine

        public void writeLine​(LineString line,
                              Object output)
                       throws IOException
        Writes a LineString as GeoJSON.
        Parameters:
        line - The line string.
        output - The output. See GeoJSONUtil.toWriter(Object) for details.
        Throws:
        IOException
      • writeLine

        public void writeLine​(LineString line,
                              OutputStream output)
                       throws IOException
        Writes a LineString as GeoJSON.

        This method calls through to writeLine(LineString, Object)

        Parameters:
        line - The line string.
        output - The output stream.
        Throws:
        IOException
      • readLine

        public LineString readLine​(Object input)
                            throws IOException
        Reads a LineString from GeoJSON.
        Parameters:
        input - The input. See GeoJSONUtil.toReader(Object) for details.
        Returns:
        The line string.
        Throws:
        IOException - In the event of a parsing error or if the input json is invalid.
      • readLine

        public LineString readLine​(InputStream input)
                            throws IOException
        Reads a LineString from GeoJSON.

        This method calls through to readLine(Object)

        Parameters:
        input - The input stream.
        Returns:
        The line string.
        Throws:
        IOException - In the event of a parsing error or if the input json is invalid.
      • writePolygon

        public void writePolygon​(Polygon poly,
                                 Object output)
                          throws IOException
        Writes a Polygon as GeoJSON.
        Parameters:
        poly - The polygon.
        output - The output. See GeoJSONUtil.toWriter(Object) for details.
        Throws:
        IOException
      • writePolygon

        public void writePolygon​(Polygon poly,
                                 OutputStream output)
                          throws IOException
        Writes a Polygon as GeoJSON.

        This method calls through to writePolygon(Polygon, Object)

        Parameters:
        poly - The polygon.
        output - The output stream.
        Throws:
        IOException
      • readPolygon

        public Polygon readPolygon​(Object input)
                            throws IOException
        Reads a Polygon from GeoJSON.
        Parameters:
        input - The input. See GeoJSONUtil.toReader(Object) for details.
        Returns:
        The polygon.
        Throws:
        IOException - In the event of a parsing error or if the input json is invalid.
      • readPolygon

        public Polygon readPolygon​(InputStream input)
                            throws IOException
        Reads a Polygon from GeoJSON.

        This method calls through to readPolygon(Object)

        Parameters:
        input - The input stream.
        Returns:
        The polygon.
        Throws:
        IOException - In the event of a parsing error or if the input json is invalid.
      • writeMultiPoint

        public void writeMultiPoint​(MultiPoint mpoint,
                                    Object output)
                             throws IOException
        Writes a MultiPoint as GeoJSON.
        Parameters:
        mpoint - The multi point.
        output - The output. See GeoJSONUtil.toWriter(Object) for details.
        Throws:
        IOException
      • writeMultiPoint

        public void writeMultiPoint​(MultiPoint mpoint,
                                    OutputStream output)
                             throws IOException
        Writes a MultiPoint as GeoJSON.

        This method calls through to writeMultiPoint(MultiPoint, Object)

        Parameters:
        mpoint - The multi point.
        output - The output stream.
        Throws:
        IOException
      • readMultiPoint

        public MultiPoint readMultiPoint​(Object input)
                                  throws IOException
        Reads a MultiPoint from GeoJSON.
        Parameters:
        input - The input. See GeoJSONUtil.toReader(Object) for details.
        Returns:
        The multi point.
        Throws:
        IOException - In the event of a parsing error or if the input json is invalid.
      • readMultiPoint

        public MultiPoint readMultiPoint​(InputStream input)
                                  throws IOException
        Reads a MultiPoint from GeoJSON.

        This method calls through to readMultiPoint(Object)

        Parameters:
        input - The input stream.
        Returns:
        The multi point.
        Throws:
        IOException - In the event of a parsing error or if the input json is invalid.
      • writeMultiLine

        public void writeMultiLine​(MultiLineString mline,
                                   Object output)
                            throws IOException
        Writes a MultiLineString as GeoJSON.
        Parameters:
        mline - The multi line string.
        output - The output. See GeoJSONUtil.toWriter(Object) for details.
        Throws:
        IOException
      • writeMultiLine

        public void writeMultiLine​(MultiLineString mline,
                                   OutputStream output)
                            throws IOException
        Writes a MultiLineString as GeoJSON.

        This method calls through to writeMultiLine(MultiLineString, Object)

        Parameters:
        mline - The multi line string.
        output - The output stream.
        Throws:
        IOException
      • readMultiLine

        public MultiLineString readMultiLine​(Object input)
                                      throws IOException
        Reads a MultiLineString from GeoJSON.
        Parameters:
        input - The input. See GeoJSONUtil.toReader(Object) for details.
        Returns:
        The multi line string.
        Throws:
        IOException - In the event of a parsing error or if the input json is invalid.
      • readMultiLine

        public MultiLineString readMultiLine​(InputStream input)
                                      throws IOException
        Reads a MultiLineString from GeoJSON.

        This method calls through to readMultiLine(Object)

        Parameters:
        input - The input stream.
        Returns:
        The multi line string.
        Throws:
        IOException - In the event of a parsing error or if the input json is invalid.
      • writeMultiPolygon

        public void writeMultiPolygon​(MultiPolygon mpoly,
                                      Object output)
                               throws IOException
        Writes a MultiPolygon as GeoJSON.
        Parameters:
        mpoly - The multi polygon.
        output - The output. See GeoJSONUtil.toWriter(Object) for details.
        Throws:
        IOException
      • writeMultiPolygon

        public void writeMultiPolygon​(MultiPolygon mpoly,
                                      OutputStream output)
                               throws IOException
        Writes a MultiPolygon as GeoJSON.

        This method calls through to writeMultiPolygon(MultiPolygon, Object)

        Parameters:
        mpoly - The multi polygon.
        output - The output stream.
        Throws:
        IOException
      • readMultiPolygon

        public MultiPolygon readMultiPolygon​(Object input)
                                      throws IOException
        Reads a MultiPolygon from GeoJSON.
        Parameters:
        input - The input. See GeoJSONUtil.toReader(Object) for details.
        Returns:
        The multi polygon.
        Throws:
        IOException - In the event of a parsing error or if the input json is invalid.
      • readMultiPolygon

        public MultiPolygon readMultiPolygon​(InputStream input)
                                      throws IOException
        Reads a MultiPolygon from GeoJSON.

        This method calls through to readMultiPolygon(Object)

        Parameters:
        input - The input stream.
        Returns:
        The multi polygon.
        Throws:
        IOException - In the event of a parsing error or if the input json is invalid.
      • writeGeometryCollection

        public void writeGeometryCollection​(GeometryCollection gcol,
                                            Object output)
                                     throws IOException
        Writes a GeometryCollection as GeoJSON.
        Parameters:
        gcol - The geometry collection.
        output - The output. See GeoJSONUtil.toWriter(Object) for details.
        Throws:
        IOException
      • writeGeometryCollection

        public void writeGeometryCollection​(GeometryCollection gcol,
                                            OutputStream output)
                                     throws IOException
        Writes a GeometryCollection as GeoJSON.

        This method calls through to writeGeometryCollection(GeometryCollection, Object)

        Parameters:
        gcol - The geometry collection.
        output - The output stream.
        Throws:
        IOException
      • readGeometryCollection

        public GeometryCollection readGeometryCollection​(Object input)
                                                  throws IOException
        Reads a GeometryCollection from GeoJSON.
        Parameters:
        input - The input. See GeoJSONUtil.toReader(Object) for details.
        Returns:
        The geometry collection.
        Throws:
        IOException - In the event of a parsing error or if the input json is invalid.
      • readGeometryCollection

        public GeometryCollection readGeometryCollection​(InputStream input)
                                                  throws IOException
        Reads a GeometryCollection from GeoJSON.

        This method calls through to readGeometryCollection(Object)

        Parameters:
        input - The input stream.
        Returns:
        The geometry collection.
        Throws:
        IOException - In the event of a parsing error or if the input json is invalid.
      • toString

        public String toString​(BoundingBox bbox)
        Writes an BoundingBox instance as GeoJSON returning the result as a string.
        Parameters:
        bbox - The bounding box.
        Returns:
        The bounding box encoded as GeoJSON
      • toString

        public String toString​(Envelope e)
        Writes an Envelope instance as GeoJSON returning the result as a string.
        Parameters:
        e - The envelope
        Returns:
        The envelope encoded as GeoJSON