Package org.locationtech.jts.io.kml
Class KMLWriter
- java.lang.Object
-
- org.locationtech.jts.io.kml.KMLWriter
-
public class KMLWriter extends java.lang.ObjectWrites a formatted string containing the KML representation of a JTSGeometry. The output is KML fragments which can be substituted wherever the KML Geometry abstract element can be used.Output elements are indented to provide a nicely-formatted representation. An output line prefix and maximum number of coordinates per line can be specified.
The Z ordinate value output can be forced to be a specific value. The
extrudeandaltitudeModemodes can be set. If set, the corresponding sub-elements will be output.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringALTITUDE_MODE_ABSOLUTEThe KML standard valueabsolutefor use insetAltitudeMode(String).static java.lang.StringALTITUDE_MODE_CLAMPTOGROUNDThe KML standard valueclampToGroundfor use insetAltitudeMode(String).static java.lang.StringALTITUDE_MODE_RELATIVETOGROUNDThe KML standard valuerelativeToGroundfor use insetAltitudeMode(String).private java.lang.StringaltitudeModeprivate static java.lang.StringCOORDINATE_SEPARATORprivate booleanextrudeprivate intINDENT_SIZEprivate java.lang.StringlinePrefixprivate intmaxCoordinatesPerLineprivate java.text.DecimalFormatnumberFormatterprivate booleantesselateprivate static java.lang.StringTUPLE_SEPARATORprivate doublezVal
-
Constructor Summary
Constructors Constructor Description KMLWriter()Creates a new writer.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static java.text.DecimalFormatcreateFormatter(int precision)Creates theDecimalFormatused to writedoubles with a sufficient number of decimal places.private java.lang.StringgeometryTag(java.lang.String geometryName, java.lang.String attributes)voidsetAltitudeMode(java.lang.String altitudeMode)Sets the value output in thealtitudeModeelement.voidsetExtrude(boolean extrude)Sets the flag to be output in theextrudeelement.voidsetLinePrefix(java.lang.String linePrefix)Sets a tag string which is prefixed to every emitted text line.voidsetMaximumCoordinatesPerLine(int maxCoordinatesPerLine)Sets the maximum number of coordinates to output per line.voidsetPrecision(int precision)Sets the maximum number of decimal places to output in ordinate values.voidsetTesselate(boolean tesselate)Sets the flag to be output in thetesselateelement.voidsetZ(double zVal)Sets the Z value to be output for all coordinates.private voidstartLine(java.lang.String text, int level, java.lang.StringBuffer buf)private voidwrite(double num, java.lang.StringBuffer buf)private voidwrite(Coordinate[] coords, int level, java.lang.StringBuffer buf)Takes a list of coordinates and converts it to KML.
2d and 3d aware.private voidwrite(Coordinate p, java.lang.StringBuffer buf)java.lang.Stringwrite(Geometry geom)Writes aGeometryin KML format as a string.voidwrite(Geometry geometry, java.io.Writer writer)Writes the KML representation of aGeometryto aWriter.voidwrite(Geometry geometry, java.lang.StringBuffer buf)Appends the KML representation of aGeometryto aStringBuffer.static java.lang.StringwriteGeometry(Geometry geometry, double z)Writes a Geometry as KML to a string, using a specified Z value.static java.lang.StringwriteGeometry(Geometry geometry, double z, int precision, boolean extrude, java.lang.String altitudeMode)Writes a Geometry as KML to a string, using a specified Z value, precision, extrude flag, and altitude mode code.private voidwriteGeometry(Geometry g, int level, java.lang.StringBuffer buf)private voidwriteGeometryCollection(GeometryCollection gc, java.lang.String attributes, int level, java.lang.StringBuffer buf)private voidwriteLinearRing(LinearRing lr, java.lang.String attributes, boolean writeModifiers, int level, java.lang.StringBuffer buf)private voidwriteLineString(LineString ls, java.lang.String attributes, int level, java.lang.StringBuffer buf)private voidwriteModifiers(int level, java.lang.StringBuffer buf)private voidwritePoint(Point p, java.lang.String attributes, int level, java.lang.StringBuffer buf)private voidwritePolygon(Polygon p, java.lang.String attributes, int level, java.lang.StringBuffer buf)
-
-
-
Field Detail
-
ALTITUDE_MODE_CLAMPTOGROUND
public static java.lang.String ALTITUDE_MODE_CLAMPTOGROUND
The KML standard valueclampToGroundfor use insetAltitudeMode(String).
-
ALTITUDE_MODE_RELATIVETOGROUND
public static java.lang.String ALTITUDE_MODE_RELATIVETOGROUND
The KML standard valuerelativeToGroundfor use insetAltitudeMode(String).
-
ALTITUDE_MODE_ABSOLUTE
public static java.lang.String ALTITUDE_MODE_ABSOLUTE
The KML standard valueabsolutefor use insetAltitudeMode(String).
-
INDENT_SIZE
private final int INDENT_SIZE
- See Also:
- Constant Field Values
-
COORDINATE_SEPARATOR
private static final java.lang.String COORDINATE_SEPARATOR
- See Also:
- Constant Field Values
-
TUPLE_SEPARATOR
private static final java.lang.String TUPLE_SEPARATOR
- See Also:
- Constant Field Values
-
linePrefix
private java.lang.String linePrefix
-
maxCoordinatesPerLine
private int maxCoordinatesPerLine
-
zVal
private double zVal
-
extrude
private boolean extrude
-
tesselate
private boolean tesselate
-
altitudeMode
private java.lang.String altitudeMode
-
numberFormatter
private java.text.DecimalFormat numberFormatter
-
-
Method Detail
-
writeGeometry
public static java.lang.String writeGeometry(Geometry geometry, double z)
Writes a Geometry as KML to a string, using a specified Z value.- Parameters:
geometry- the geometry to writez- the Z value to use- Returns:
- a string containing the KML geometry representation
-
writeGeometry
public static java.lang.String writeGeometry(Geometry geometry, double z, int precision, boolean extrude, java.lang.String altitudeMode)
Writes a Geometry as KML to a string, using a specified Z value, precision, extrude flag, and altitude mode code.- Parameters:
geometry- the geometry to writez- the Z value to useprecision- the maximum number of decimal places to writeextrude- the extrude flag to writealtitudeMode- the altitude model code to write- Returns:
- a string containing the KML geometry representation
-
setLinePrefix
public void setLinePrefix(java.lang.String linePrefix)
Sets a tag string which is prefixed to every emitted text line. This can be used to indent the geometry text in a containing document.- Parameters:
linePrefix- the tag string
-
setMaximumCoordinatesPerLine
public void setMaximumCoordinatesPerLine(int maxCoordinatesPerLine)
Sets the maximum number of coordinates to output per line.- Parameters:
maxCoordinatesPerLine- the maximum number of coordinates to output
-
setZ
public void setZ(double zVal)
Sets the Z value to be output for all coordinates. This overrides any Z value present in the Geometry coordinates.- Parameters:
zVal- the Z value to output
-
setExtrude
public void setExtrude(boolean extrude)
Sets the flag to be output in theextrudeelement.- Parameters:
extrude- the extrude flag to output
-
setTesselate
public void setTesselate(boolean tesselate)
Sets the flag to be output in thetesselateelement.- Parameters:
tesselate- the tesselate flag to output
-
setAltitudeMode
public void setAltitudeMode(java.lang.String altitudeMode)
Sets the value output in thealtitudeModeelement.- Parameters:
altitudeMode- string representing the altitude mode
-
setPrecision
public void setPrecision(int precision)
Sets the maximum number of decimal places to output in ordinate values. Useful for limiting output size.- Parameters:
precision- the number of decimal places to output
-
write
public java.lang.String write(Geometry geom)
Writes aGeometryin KML format as a string.- Parameters:
geom- the geometry to write- Returns:
- a string containing the KML geometry representation
-
write
public void write(Geometry geometry, java.io.Writer writer) throws java.io.IOException
Writes the KML representation of aGeometryto aWriter.- Parameters:
geometry- the geometry to writewriter- the Writer to write to- Throws:
java.io.IOException- if an I/O error occurred
-
write
public void write(Geometry geometry, java.lang.StringBuffer buf)
Appends the KML representation of aGeometryto aStringBuffer.- Parameters:
geometry- the geometry to writebuf- the buffer to write into
-
writeGeometry
private void writeGeometry(Geometry g, int level, java.lang.StringBuffer buf)
-
startLine
private void startLine(java.lang.String text, int level, java.lang.StringBuffer buf)
-
geometryTag
private java.lang.String geometryTag(java.lang.String geometryName, java.lang.String attributes)
-
writeModifiers
private void writeModifiers(int level, java.lang.StringBuffer buf)
-
writePoint
private void writePoint(Point p, java.lang.String attributes, int level, java.lang.StringBuffer buf)
-
writeLineString
private void writeLineString(LineString ls, java.lang.String attributes, int level, java.lang.StringBuffer buf)
-
writeLinearRing
private void writeLinearRing(LinearRing lr, java.lang.String attributes, boolean writeModifiers, int level, java.lang.StringBuffer buf)
-
writePolygon
private void writePolygon(Polygon p, java.lang.String attributes, int level, java.lang.StringBuffer buf)
-
writeGeometryCollection
private void writeGeometryCollection(GeometryCollection gc, java.lang.String attributes, int level, java.lang.StringBuffer buf)
-
write
private void write(Coordinate[] coords, int level, java.lang.StringBuffer buf)
Takes a list of coordinates and converts it to KML.
2d and 3d aware. Terminates the coordinate output with a newline.- Parameters:
cs- array of coordinates
-
write
private void write(Coordinate p, java.lang.StringBuffer buf)
-
write
private void write(double num, java.lang.StringBuffer buf)
-
createFormatter
private static java.text.DecimalFormat createFormatter(int precision)
Creates theDecimalFormatused to writedoubles with a sufficient number of decimal places.- Parameters:
precisionModel- thePrecisionModelused to determine the number of decimal places to write.- Returns:
- a
DecimalFormatthat writedoubles without scientific notation.
-
-