Package com.luciad.format.geojson
Interface ILcdGeoJsonShapeEncoder
public interface ILcdGeoJsonShapeEncoder
An encoder for encoding shapes to GeoJSON. This encoder can be plugged into a
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;
}
}
- Since:
- 2024.0
-
Method Summary
-
Method Details
-
canEncode
Checks whether the given shape can be encoded to GeoJSON by this encoder.- Parameters:
aShape
- the shape to encode- Returns:
- whether the given shape can be encoded by this encoder
-
encode
Encodes the given shape to an object that can directly be mapped to a JSON node. Accepted return types are:null
: this gets mapped to a null JSON node.- A primitive of type
String
,Boolean
,Long
,Integer
,Short
,Byte
,Double
orFloat
. This gets mapped to a primitive JSON node. - A
List
containing accepted return types. This gets mapped to a JSON array. - A
Map
containingString
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.- Parameters:
aShape
- the shape to encode- Returns:
- a JSON-encodable object.
- Throws:
IOException
- when the shape could not be encoded
-