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 String
DEFAULT_DATE_FORMAT
Date format (ISO 8601)static FastDateFormat
DEFAULT_DATE_FORMATTER
static TimeZone
DEFAULT_TIME_ZONE
Default 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 void
close()
String
getDatePattern()
Returns the date formatter pattern.int
getMaxDecimals()
Returns the max number of decimals used for encodingTimeZone
getTimeZone(TimeZone tz)
Returns the timezone used to format dates.boolean
isEncodeFeatureBounds()
boolean
isEncodeFeatureCollectionCRS()
boolean
isPrettyPrinting()
Returns true if pretty printing is enabled, false otherwise.boolean
isSingleFeature()
Returns true if the generated JSON is for a single feature, without a feature collection wrapper around it.void
setBounds(ReferencedEnvelope bbox)
void
setDatePattern(String pattern)
Sets the date format for time encodingvoid
setEncodeFeatureBounds(boolean encodeFeatureCollectionBounds)
void
setEncodeFeatureCollectionCRS(boolean b)
Set to true to encode the feature collection CRS field.void
setMaxDecimals(int number)
Set how many decimals should be used in writing out the coordinates.void
setPrettyPrinting(boolean prettyPrint)
Enables/disables pretty printing.void
setSingleFeature(boolean singleFeature)
Turns on and off the single feature mode.void
setTimeZone(TimeZone tz)
Sets the Timezone used to format the date fields.static String
toGeoJSON(SimpleFeature f)
Utility encoding a singleSimpleFeature
, and returning it as a stringstatic String
toGeoJSON(SimpleFeatureCollection fc)
Utility encoding a @linkSimpleFeatureCollection
}, and returning it as a stringstatic String
toGeoJSON(Geometry geometry)
Utility encoding a single JTS geometry in GeoJSON, and returning it as a stringstatic String
toGeoJSON(Geometry geometry, int maxDecimals)
Utility encoding a single JTS geometry in GeoJSON with configurable max decimals, and returning it as a stringvoid
write(SimpleFeature currentFeature)
Writes a single feature onto the output.void
writeFeatureCollection(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:
close
in 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.null
is 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
-SimpleDateFormat
compatible * pattern
-
getDatePattern
public String getDatePattern()
Returns the date formatter pattern. Defaults to DEFAULT_DATE_FORMAT
-
-