public interface ILcdGeoJsonShapeEncoder
TLcdGeoJsonModelEncoder
, for
encoding shapes that the encoder does not support by default, or to override the default encoding behaviour.
An example implementation is shown below. This implementation can be used for adding support for
ILcdCircle
shape types to the TLcdGeoJsonModelEncoder
:
public static class CircleEncoder implements ILcdGeoJsonShapeEncoder {
@Override
public boolean canEncode(ILcdShape aShape) {
return aShape instanceof ILcdCircle;
}
@Override
public Object encode(ILcdShape aShape) throws IOException {
if (!canEncode(aShape)) {
throw new IOException("This encoder only supports circles.");
}
ILcdCircle circle = (ILcdCircle) aShape;
Map<String, Object> map = new LinkedHashMap<>();
map.put("type", "Circle");
map.put("center", Arrays.asList(circle.getCenter().getX(), circle.getCenter().getY()));
map.put("radius", circle.getRadius());
// The resulting JSON geometry will be:
// { "type": "Circle", "center": [ <x>, <y> ], "radius": <radius> }
return map;
}
}
Modifier and Type | Method and Description |
---|---|
boolean |
canEncode(ILcdShape aShape)
Checks whether the given shape can be encoded to GeoJSON by this encoder.
|
Object |
encode(ILcdShape aShape)
Encodes the given shape to an object that can directly be mapped to a JSON node.
|
boolean canEncode(ILcdShape aShape)
aShape
- the shape to encodeObject encode(ILcdShape aShape) throws IOException
null
: this gets mapped to a null JSON node.String
, Boolean
, Long
, Integer
, Short
,
Byte
, Double
or Float
. This gets mapped to a primitive JSON node.List
containing accepted return types. This gets mapped to a JSON array.Map
containing String
keys and accepted return types for values. This gets mapped
to a JSON object node, with the map entries as properties.ILcdGeoJsonShapeEncoder
class javadoc for an example implementation.aShape
- the shape to encodeIOException
- when the shape could not be encoded