Package org.locationtech.jts.io.gml2
Class GMLWriter
- java.lang.Object
-
- org.locationtech.jts.io.gml2.GMLWriter
-
public class GMLWriter extends java.lang.Object
WritesGeometry
s as XML fragments in GML2 format. Allows specifying the XML prefix, namespace and srsName of the emitted GML. Also allows adding custom root elements to support GML extensions such as KML. With appropriate settings for prefix (none) and custom root elements this class can be used to write out geometry in KML format.An example of the output that can be generated is:
<gml:LineString xmlns:gml='http://www.opengis.net/gml' srsName='foo'> <gml:coordinates> 6.03,8.17 7.697,6.959 8.333,5.0 7.697,3.041 6.03,1.83 3.97,1.83 2.303,3.041 1.667,5.0 2.303,6.959 3.97,8.17 </gml:coordinates> </gml:LineString>
This class does not rely on any external XML libraries.
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String
coordinateSeparator
private java.lang.String[]
customElements
private boolean
emitNamespace
private java.lang.String
INDENT
private boolean
isRootTag
private int
maxCoordinatesPerLine
private java.lang.String
namespace
private java.lang.String
prefix
private java.lang.String
srsName
private int
startingIndentIndex
private static java.lang.String
tupleSeparator
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
endGeomTag(java.lang.String geometryName, java.io.Writer writer)
private java.lang.String
prefix()
void
setCustomElements(java.lang.String[] customElements)
Specifies a list of custom elements which are written after the opening tag of the root element.void
setMaxCoordinatesPerLine(int num)
Sets the number of coordinates printed per line.void
setNamespace(boolean emitNamespace)
Determines whether a GML namespace declaration will be written in the opening tag of geometries.void
setPrefix(java.lang.String prefix)
Specifies the namespace prefix to write on each GML tag.void
setSrsName(java.lang.String srsName)
Sets the value of the srsName attribute to be written into the root geometry tag.void
setStartingIndentIndex(int indent)
Sets the starting column index for pretty printingprivate void
startGeomTag(java.lang.String geometryName, Geometry g, java.io.Writer writer)
private void
startLine(int level, java.io.Writer writer)
private void
write(Coordinate[] coords, java.io.Writer writer, int level)
Takes a list of coordinates and converts it to GML.
2d and 3d aware.java.lang.String
write(Geometry geom)
Writes aGeometry
in GML2 format to a String.void
write(Geometry geom, java.io.Writer writer)
Writes aGeometry
in GML2 format into aWriter
.private void
write(Geometry geom, java.io.Writer writer, int level)
private void
writeAttributes(Geometry geom, java.io.Writer writer)
private void
writeCustomElements(Geometry geom, java.io.Writer writer)
private void
writeGeometryCollection(GeometryCollection gc, java.io.Writer writer, int level)
private void
writeLinearRing(LinearRing lr, java.io.Writer writer, int level)
private void
writeLineString(LineString ls, java.io.Writer writer, int level)
private void
writeMultiLineString(MultiLineString mls, java.io.Writer writer, int level)
private void
writeMultiPoint(MultiPoint mp, java.io.Writer writer, int level)
private void
writeMultiPolygon(MultiPolygon mp, java.io.Writer writer, int level)
private void
writePoint(Point p, java.io.Writer writer, int level)
private void
writePolygon(Polygon p, java.io.Writer writer, int level)
-
-
-
Field Detail
-
INDENT
private final java.lang.String INDENT
- See Also:
- Constant Field Values
-
startingIndentIndex
private int startingIndentIndex
-
maxCoordinatesPerLine
private int maxCoordinatesPerLine
-
emitNamespace
private boolean emitNamespace
-
isRootTag
private boolean isRootTag
-
prefix
private java.lang.String prefix
-
namespace
private java.lang.String namespace
-
srsName
private java.lang.String srsName
-
customElements
private java.lang.String[] customElements
-
coordinateSeparator
private static final java.lang.String coordinateSeparator
- See Also:
- Constant Field Values
-
tupleSeparator
private static final java.lang.String tupleSeparator
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
GMLWriter
public GMLWriter()
Creates a writer which outputs GML with default settings. The defaults are:- the namespace prefix is gml:
- no namespace prefix declaration is written
- no srsName attribute is written
-
GMLWriter
public GMLWriter(boolean emitNamespace)
Creates a writer which may emit the GML namespace prefix declaration in the geometry root element.- Parameters:
emitNamespace
- true if the GML namespace prefix declaration should be written in the geometry root element
-
-
Method Detail
-
setPrefix
public void setPrefix(java.lang.String prefix)
Specifies the namespace prefix to write on each GML tag. A null or blank prefix may be used to indicate no prefix.The default is to write gml: as the namespace prefix.
- Parameters:
prefix
- the namespace prefix to use (null or blank if none)
-
setSrsName
public void setSrsName(java.lang.String srsName)
Sets the value of the srsName attribute to be written into the root geometry tag. If the value is null or blank no srsName attribute will be written. The provided value must be a valid XML attribute value - it will not be XML-escaped.The default is not to write the srsName attribute.
- Parameters:
srsName
- the srsName attribute value
-
setNamespace
public void setNamespace(boolean emitNamespace)
Determines whether a GML namespace declaration will be written in the opening tag of geometries. Useful in XML-aware environments which parse the geometries before use, such as XSLT.- Parameters:
emitNamespace
- true if the GML namespace prefix declaration should be written in the root geometry element
-
setCustomElements
public void setCustomElements(java.lang.String[] customElements)
Specifies a list of custom elements which are written after the opening tag of the root element. The text contained in the string sequence should form valid XML markup. The specified strings are written one per line immediately after the root geometry tag line.For instance, this is useful for adding KML-specific geometry parameters such as <extrude>
- Parameters:
customElements
- a list of the custom element strings, or null if none
-
setStartingIndentIndex
public void setStartingIndentIndex(int indent)
Sets the starting column index for pretty printing- Parameters:
indent
-
-
setMaxCoordinatesPerLine
public void setMaxCoordinatesPerLine(int num)
Sets the number of coordinates printed per line.- Parameters:
num
-
-
write
public java.lang.String write(Geometry geom)
Writes aGeometry
in GML2 format to a String.- Parameters:
geom
-- Returns:
- String GML2 Encoded Geometry
-
write
public void write(Geometry geom, java.io.Writer writer) throws java.io.IOException
Writes aGeometry
in GML2 format into aWriter
.- Parameters:
geom
- Geometry to encodewriter
- Stream to encode to.- Throws:
java.io.IOException
-
write
private void write(Geometry geom, java.io.Writer writer, int level) throws java.io.IOException
- Throws:
java.io.IOException
-
writePoint
private void writePoint(Point p, java.io.Writer writer, int level) throws java.io.IOException
- Throws:
java.io.IOException
-
writeLineString
private void writeLineString(LineString ls, java.io.Writer writer, int level) throws java.io.IOException
- Throws:
java.io.IOException
-
writeLinearRing
private void writeLinearRing(LinearRing lr, java.io.Writer writer, int level) throws java.io.IOException
- Throws:
java.io.IOException
-
writePolygon
private void writePolygon(Polygon p, java.io.Writer writer, int level) throws java.io.IOException
- Throws:
java.io.IOException
-
writeMultiPoint
private void writeMultiPoint(MultiPoint mp, java.io.Writer writer, int level) throws java.io.IOException
- Throws:
java.io.IOException
-
writeMultiLineString
private void writeMultiLineString(MultiLineString mls, java.io.Writer writer, int level) throws java.io.IOException
- Throws:
java.io.IOException
-
writeMultiPolygon
private void writeMultiPolygon(MultiPolygon mp, java.io.Writer writer, int level) throws java.io.IOException
- Throws:
java.io.IOException
-
writeGeometryCollection
private void writeGeometryCollection(GeometryCollection gc, java.io.Writer writer, int level) throws java.io.IOException
- Throws:
java.io.IOException
-
write
private void write(Coordinate[] coords, java.io.Writer writer, int level) throws java.io.IOException
Takes a list of coordinates and converts it to GML.
2d and 3d aware.- Parameters:
coords
- array of coordinates- Throws:
java.io.IOException
-
startLine
private void startLine(int level, java.io.Writer writer) throws java.io.IOException
- Throws:
java.io.IOException
-
startGeomTag
private void startGeomTag(java.lang.String geometryName, Geometry g, java.io.Writer writer) throws java.io.IOException
- Throws:
java.io.IOException
-
writeAttributes
private void writeAttributes(Geometry geom, java.io.Writer writer) throws java.io.IOException
- Throws:
java.io.IOException
-
writeCustomElements
private void writeCustomElements(Geometry geom, java.io.Writer writer) throws java.io.IOException
- Throws:
java.io.IOException
-
endGeomTag
private void endGeomTag(java.lang.String geometryName, java.io.Writer writer) throws java.io.IOException
- Throws:
java.io.IOException
-
prefix
private java.lang.String prefix()
-
-