Class FeatureJSON


  • public class FeatureJSON
    extends Object
    Reads and writes feature objects to and from geojson.

     SimpleFeature feature = ...;
    
     FeatureJSON io = new FeatureJSON();
     io.writeFeature(feature, "feature.json"));
    
     Iterator features = io.streamFeatureCollection("features.json");
     while(features.hasNext()) {
       feature = features.next();
       ...
     }
     
    Author:
    Justin Deoliveira, OpenGeo
    • Constructor Detail

      • FeatureJSON

        public FeatureJSON()
    • Method Detail

      • setFeatureType

        public void setFeatureType​(SimpleFeatureType featureType)
        Sets the target feature type for parsing.

        Setting the target feature type will help the geojson parser determine the type of feature properties during properties. When the type is not around all properties are returned as a string.

        Parameters:
        featureType - The feature type. Parsed features will reference this feature type.
      • setEncodeFeatureBounds

        public void setEncodeFeatureBounds​(boolean encodeFeatureBounds)
        Sets the flag controlling whether feature bounds are encoded.
        See Also:
        isEncodeFeatureBounds()
      • isEncodeFeatureBounds

        public boolean isEncodeFeatureBounds()
        The flag controlling whether feature bounds are encoded.

        When set each feature object will contain a "bbox" attribute whose value is an array containing the elements of the bounding box (in x1,y1,x2,y2 order) of the feature

      • setEncodeFeatureCollectionBounds

        public void setEncodeFeatureCollectionBounds​(boolean encodeFeatureCollectionBounds)
        Sets the flag controlling whether feature collection bounds are encoded.
        See Also:
        isEncodeFeatureCollectionBounds()
      • isEncodeFeatureCollectionBounds

        public boolean isEncodeFeatureCollectionBounds()
        The flag controlling whether feature collection bounds are encoded.

        When set the feature collection object will contain a "bbox" attribute whose value is an array containing elements of the bounding box (in x1,y1,x2,y2 order) of the feature collection.

      • setEncodeFeatureCRS

        public void setEncodeFeatureCRS​(boolean encodeFeatureCRS)
        Sets the flag controlling whether feature coordinate reference systems are encoded.
        See Also:
        isEncodeFeatureCRS()
      • isEncodeFeatureCRS

        public boolean isEncodeFeatureCRS()
        The flag controlling whether feature coordinate reference systems are encoded.

        When set each feature object will contain a "crs" attribute describing the coordinate reference system of the feature.

      • setEncodeFeatureCollectionCRS

        public void setEncodeFeatureCollectionCRS​(boolean encodeFeatureCollectionCRS)
        Sets the flag controlling whether feature collection coordinate reference systems are encoded.
        See Also:
        isEncodeFeatureCollectionCRS()
      • isEncodeFeatureCollectionCRS

        public boolean isEncodeFeatureCollectionCRS()
        The flag controlling whether feature collection coordinate reference systems are encoded.

        When set the feature collection object will contain a "crs" attribute describing the coordinate reference system of the feature collection.

      • setEncodeNullValues

        public void setEncodeNullValues​(boolean encodeNullValues)
        Sets the flag controlling whether properties with null values are encoded.
        See Also:
        isEncodeNullValues()
      • isEncodeNullValues

        public boolean isEncodeNullValues()
        The flag controlling whether properties with null values are encoded.

        When set, properties with null values are encoded as null in the GeoJSON document.

      • writeFeature

        public void writeFeature​(SimpleFeature feature,
                                 Object output)
                          throws IOException
        Writes a feature as GeoJSON.
        Parameters:
        feature - The feature.
        output - The output. See GeoJSONUtil.toWriter(Object) for details.
        Throws:
        IOException
      • writeFeature

        public void writeFeature​(SimpleFeature feature,
                                 OutputStream output)
                          throws IOException
        Writes a feature as GeoJSON.

        This method calls through to #writeFeature(FeatureCollection, Object)

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

        public String toString​(SimpleFeature feature)
                        throws IOException
        Writes a feature as GeoJSON returning the result as a string.
        Parameters:
        feature - The feature
        Returns:
        The geometry encoded as GeoJSON
        Throws:
        IOException
      • readFeature

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

        public SimpleFeature readFeature​(InputStream input)
                                  throws IOException
        Reads a feature from GeoJSON.

        This method calls through to readFeature(Object)

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

        public void writeFeatureCollection​(FeatureCollection features,
                                           Object output)
                                    throws IOException
        Writes a feature collection as GeoJSON.
        Parameters:
        features - The feature collection.
        output - The output. See GeoJSONUtil.toWriter(Object) for details.
        Throws:
        IOException
      • writeFeatureCollection

        public void writeFeatureCollection​(FeatureCollection features,
                                           OutputStream output)
                                    throws IOException
        Writes a feature collection as GeoJSON.

        This method calls through to writeFeatureCollection(FeatureCollection, Object)

        Parameters:
        features - The feature collection.
        output - The output strema to write to.
        Throws:
        IOException
      • readFeatureCollection

        public FeatureCollection readFeatureCollection​(Object input)
                                                throws IOException
        Reads a feature collection from GeoJSON.

        Warning that this method will load the entire feature collection into memory. For large feature collections streamFeatureCollection(Object) should be used.

        Parameters:
        input - The input. See GeoJSONUtil.toReader(Object) for details.
        Returns:
        The feature collection.
        Throws:
        IOException - In the event of a parsing error or if the input json is invalid.
      • readFeatureCollection

        public FeatureCollection readFeatureCollection​(InputStream input)
                                                throws IOException
        Reads a feature collection from GeoJSON.

        Warning that this method will load the entire feature collection into memory. For large feature collections streamFeatureCollection(Object) should be used.

        This method calls through to readFeatureCollection(Object).

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

        public FeatureIterator<SimpleFeature> streamFeatureCollection​(Object input)
                                                               throws IOException
        Reads a feature collection from GeoJSON streaming back the contents via an iterator.
        Parameters:
        input - The input. See GeoJSONUtil.toReader(Object) for details.
        Returns:
        A feature iterator.
        Throws:
        IOException - In the event of a parsing error or if the input json is invalid.
      • toString

        public String toString​(FeatureCollection features)
                        throws IOException
        Writes a feature collection as GeoJSON returning the result as a string.
        Parameters:
        features - The feature collection.
        Returns:
        The feature collection encoded as GeoJSON
        Throws:
        IOException
      • writeCRS

        public void writeCRS​(CoordinateReferenceSystem crs,
                             Object output)
                      throws IOException
        Writes a coordinate reference system as GeoJSON.
        Parameters:
        crs - The coordinate reference system.
        output - The output. See GeoJSONUtil.toWriter(Object) for details.
        Throws:
        IOException
      • readCRS

        public CoordinateReferenceSystem readCRS​(Object input)
                                          throws IOException
        Reads a coordinate reference system from GeoJSON.

        This method only handles named coordinate reference system objects.

        Parameters:
        input - The input. See GeoJSONUtil.toReader(Object) for details.
        Returns:
        The coordinate reference system.
        Throws:
        IOException - In the event of a parsing error or if the input json is invalid.
      • readCRS

        public CoordinateReferenceSystem readCRS​(InputStream input)
                                          throws IOException
        Reads a coordinate reference system from GeoJSON.

        This method only handles named coordinate reference system objects.

        This method calls through to readCRS(Object)

        Parameters:
        input - The input. See GeoJSONUtil.toReader(Object) for details.
        Returns:
        The coordinate reference system.
        Throws:
        IOException - In the event of a parsing error or if the input json is invalid.
      • readFeatureCollectionSchema

        public SimpleFeatureType readFeatureCollectionSchema​(Object input,
                                                             boolean nullValuesEncoded)
                                                      throws IOException
        Reads the SimpleFeatureType of a GeoJSON feature collection. In the worst case, it will parse all features searching for attributes not present in previous features.
        Parameters:
        input - The input. See GeoJSONUtil.toReader(Object) for details.
        nullValuesEncoded - if the input has null values encoded. If this flag is set to true and the GeoJSON doesn't actually encode null values, only the first feature attributes will be discovered.
        Returns:
        The feature collection schema
        Throws:
        IOException - In the event of a parsing error or if the input json is invalid.
      • readFeatureCollectionSchema

        public SimpleFeatureType readFeatureCollectionSchema​(InputStream input,
                                                             boolean nullValuesEncoded)
                                                      throws IOException
        Reads the SimpleFeatureType of a GeoJSON feature collection. In the worst case, it will parse all features searching for attributes not present in previous features.
        Parameters:
        input - The input. See GeoJSONUtil.toReader(Object) for details.
        nullValuesEncoded - if the input has null values encoded. If this flag is set to true and the GeoJSON doesn't actually encode null values, only the first feature attributes will be discovered.
        Returns:
        The feature collection schema
        Throws:
        IOException - In the event of a parsing error or if the input json is invalid.
      • toString

        public String toString​(CoordinateReferenceSystem crs)
                        throws IOException
        Writes a coordinate reference system as GeoJSON returning the result as a string.
        Parameters:
        crs - The coordinate reference system.
        Returns:
        The coordinate reference system encoded as GeoJSON
        Throws:
        IOException