Package org.geotools.data.geojson
Class GeoJSONWriter
- Object
-
- GeoJSONWriter
-
- All Implemented Interfaces:
AutoCloseable
public class GeoJSONWriter extends Object implements AutoCloseable
Wrapper to handle writing GeoJSON FeatureCollections- Author:
- ian
-
-
Field Summary
Fields Modifier and Type Field Description static StringDEFAULT_DATE_FORMATDate format (ISO 8601)static FastDateFormatDEFAULT_DATE_FORMATTERstatic TimeZoneDEFAULT_TIME_ZONEDefault time zone, GMT
-
Constructor Summary
Constructors Constructor Description GeoJSONWriter(OutputStream outputStream)Prepares a writer over the target output stream.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()StringgetDatePattern()Returns the date formatter pattern.intgetMaxDecimals()Returns the max number of decimals used for encodingTimeZonegetTimeZone(TimeZone tz)Returns the timezone used to format dates.booleanisEncodeFeatureBounds()booleanisEncodeFeatureCollectionCRS()booleanisPrettyPrinting()Returns true if pretty printing is enabled, false otherwise.booleanisSingleFeature()Returns true if the generated JSON is for a single feature, without a feature collection wrapper around it.voidsetBounds(ReferencedEnvelope bbox)voidsetDatePattern(String pattern)Sets the date format for time encodingvoidsetEncodeFeatureBounds(boolean encodeFeatureCollectionBounds)voidsetEncodeFeatureCollectionCRS(boolean b)Set to true to encode the feature collection CRS field.voidsetMaxDecimals(int number)Set how many decimals should be used in writing out the coordinates.voidsetPrettyPrinting(boolean prettyPrint)Enables/disables pretty printing.voidsetSingleFeature(boolean singleFeature)Turns on and off the single feature mode.voidsetTimeZone(TimeZone tz)Sets the Timezone used to format the date fields.static StringtoGeoJSON(SimpleFeature f)Utility encoding a singleSimpleFeature, and returning it as a stringstatic StringtoGeoJSON(SimpleFeatureCollection fc)Utility encoding a @linkSimpleFeatureCollection}, and returning it as a stringstatic StringtoGeoJSON(Geometry geometry)Utility encoding a single JTS geometry in GeoJSON, and returning it as a stringstatic StringtoGeoJSON(Geometry geometry, int maxDecimals)Utility encoding a single JTS geometry in GeoJSON with configurable max decimals, and returning it as a stringvoidwrite(SimpleFeature currentFeature)Writes a single feature onto the output.voidwriteFeatureCollection(SimpleFeatureCollection features)Encodes the whole feature collection onto the output
-
-
-
Field Detail
-
DEFAULT_DATE_FORMAT
public static final String DEFAULT_DATE_FORMAT
Date format (ISO 8601)- See Also:
- Constant Field Values
-
DEFAULT_TIME_ZONE
public static final TimeZone DEFAULT_TIME_ZONE
Default time zone, GMT
-
DEFAULT_DATE_FORMATTER
public static final FastDateFormat DEFAULT_DATE_FORMATTER
-
-
Constructor Detail
-
GeoJSONWriter
public GeoJSONWriter(OutputStream outputStream) throws IOException
Prepares a writer over the target output stream.- Parameters:
outputStream-- Throws:
IOException
-
-
Method Detail
-
write
public void write(SimpleFeature currentFeature) throws IOException
Writes a single feature onto the output.- Parameters:
currentFeature-- Throws:
IOException
-
close
public void close() throws IOException- Specified by:
closein interfaceAutoCloseable- Throws:
IOException
-
isEncodeFeatureBounds
public boolean isEncodeFeatureBounds()
- Returns:
- the encodeFeatureCollectionBounds
-
setEncodeFeatureBounds
public void setEncodeFeatureBounds(boolean encodeFeatureCollectionBounds)
- Parameters:
encodeFeatureCollectionBounds- the encodeFeatureCollectionBounds to set
-
toGeoJSON
public static String toGeoJSON(Geometry geometry)
Utility encoding a single JTS geometry in GeoJSON, and returning it as a string
-
toGeoJSON
public static String toGeoJSON(Geometry geometry, int maxDecimals)
Utility encoding a single JTS geometry in GeoJSON with configurable max decimals, and returning it as a string
-
toGeoJSON
public static String toGeoJSON(SimpleFeature f)
Utility encoding a singleSimpleFeature, and returning it as a string
-
toGeoJSON
public static String toGeoJSON(SimpleFeatureCollection fc)
Utility encoding a @linkSimpleFeatureCollection}, and returning it as a string
-
setEncodeFeatureCollectionCRS
public void setEncodeFeatureCollectionCRS(boolean b)
Set to true to encode the feature collection CRS field. Defaults to false.
-
isEncodeFeatureCollectionCRS
public boolean isEncodeFeatureCollectionCRS()
- Returns:
- true if the feature collections CRS will be encoded in the output
-
getMaxDecimals
public int getMaxDecimals()
Returns the max number of decimals used for encoding- Returns:
-
setMaxDecimals
public void setMaxDecimals(int number)
Set how many decimals should be used in writing out the coordinates. Users should consult https://xkcd.com/2170/ before changing this value, unless they are using a CRS different from 4326.- Parameters:
number- - the number of digits after the decimal place marker
-
writeFeatureCollection
public void writeFeatureCollection(SimpleFeatureCollection features) throws IOException
Encodes the whole feature collection onto the output- Throws:
IOException
-
setBounds
public void setBounds(ReferencedEnvelope bbox)
-
isSingleFeature
public boolean isSingleFeature()
Returns true if the generated JSON is for a single feature, without a feature collection wrapper around it.
-
setSingleFeature
public void setSingleFeature(boolean singleFeature)
Turns on and off the single feature mode. In single feature mode the feature collection wrapper elements won't be emitted. Defaults to false.- Parameters:
singleFeature-
-
setPrettyPrinting
public void setPrettyPrinting(boolean prettyPrint)
Enables/disables pretty printing.
-
isPrettyPrinting
public boolean isPrettyPrinting()
Returns true if pretty printing is enabled, false otherwise.
-
setTimeZone
public void setTimeZone(TimeZone tz)
Sets the Timezone used to format the date fields.nullis a valid value, the JVM local timezone will be used in that case.
-
getTimeZone
public TimeZone getTimeZone(TimeZone tz)
Returns the timezone used to format dates. Defaults to GMT.
-
setDatePattern
public void setDatePattern(String pattern)
Sets the date format for time encoding- Parameters:
pattern-SimpleDateFormatcompatible * pattern
-
getDatePattern
public String getDatePattern()
Returns the date formatter pattern. Defaults to DEFAULT_DATE_FORMAT
-
-