A LuciadFusion WFS service typically serves data in the GML, GeoJSON or SHAPE-ZIP format.
If you want to customize the encoding or add your own vector output format,
you can do so by implementing an ILcdWFSClientModelEncoderFactory and plugging it into LuciadFusion.
It’s easiest to start by extending from the default implementation, TLcdWFSClientModelEncoderFactory.
If you want to add another vector output format, you must extend the number of output formats, define an identifier for the format, and register its encoder. The sample code includes a full example that adds a KML output format:
package samples.fusion.wfs;
import java.io.IOException;
import com.luciad.datamodel.ILcdDataObject;
import com.luciad.datamodel.TLcdDataProperty;
import com.luciad.datamodel.TLcdDataType;
import com.luciad.format.kml22.model.extendeddata.TLcdKML22Schema;
import com.luciad.format.kml22.model.feature.TLcdKML22Placemark;
import com.luciad.format.kml22.xml.TLcdKML22ModelEncoder;
import com.luciad.io.ILcdOutputStreamFactory;
import com.luciad.model.ILcdDataModelDescriptor;
import com.luciad.model.ILcdModel;
import com.luciad.model.ILcdModelEncoder;
import com.luciad.ogc.wfs.ILcdWFSClientModelEncoderFactory;
import com.luciad.ogc.wfs.ILcdWFSModelEncoder;
import com.luciad.ogc.wfs.TLcdWFSClientModelEncoderFactory;
import com.luciad.ogc.wfs.TLcdWFSModelEncoder;
import com.luciad.ogc.wfs.TLcdWFSRequestContext;
import com.luciad.util.service.LcdService;
import samples.encoder.kml22.converter.IKML22PlacemarkConverter;
import samples.encoder.kml22.converter.KML22Converter;
import samples.encoder.kml22.converter.KML22FeatureUtil;
import samples.encoder.kml22.converter.KML22ModelBuilder;
import samples.encoder.kml22.converter.KML22ShapeConverter;
/**
* Adds a "KML" output format to the WFS server, on top of the default GML, GeoJSON and SHAPE-ZIP output
* formats.
* <p>
* WFS features are generic {@link ILcdDataObject} instances: KML doesn't know about them, so this sample
* converts every feature into a {@link TLcdKML22Placemark} before handing it off to
* {@link TLcdKML22ModelEncoder}. That conversion is done with the KML sample converter framework in
* {@code samples.encoder.kml22.converter} (see {@link KML22Converter}):
* <ul>
* <li>{@link KML22ShapeConverter} converts the feature's geometry (point, polyline, polygon, ...) into a KML
* geometry.</li>
* <li>{@link DataObjectPlacemarkConverter}, below, converts the feature's properties into the placemark's name
* and {@code ExtendedData}.</li>
* </ul>
* <p>
* {@link DataObjectPlacemarkConverter#findDisplayName} picks a generic, "good enough" placemark name. Depending
* on your feature types, you'll likely want to replace it with something tailored to your own data, for example
* picking a specific, known property, or combining a few of them.
*
* @see com.luciad.ogc.wfs.ILcdWFSClientModelEncoderFactory
* @see com.luciad.format.kml22.xml.TLcdKML22ModelEncoder
*/
@LcdService(service = ILcdWFSClientModelEncoderFactory.class)
public class WFSKMLClientModelEncoderFactory extends TLcdWFSClientModelEncoderFactory {
private static final String KML_FORMAT = "KML";
private static final String KML_MIME_TYPE = "application/vnd.google-earth.kml+xml";
@Override
public int getSupportedOutputFormatCount(TLcdWFSRequestContext aContext) {
return super.getSupportedOutputFormatCount(aContext) + 1;
}
@Override
public String getSupportedOutputFormat(int aIndex, TLcdWFSRequestContext aContext) {
if (aIndex == getSupportedOutputFormatCount(aContext) - 1) {
return KML_FORMAT;
}
return super.getSupportedOutputFormat(aIndex, aContext);
}
@Override
public ILcdWFSModelEncoder createModelEncoder(ILcdOutputStreamFactory aOutputStreamFactory, String aOutputFormat, TLcdWFSRequestContext aContext) {
if (KML_FORMAT.equalsIgnoreCase(aOutputFormat)) {
return new TLcdWFSModelEncoder(new WFSKMLModelEncoder(aOutputStreamFactory), KML_MIME_TYPE);
}
return super.createModelEncoder(aOutputStreamFactory, aOutputFormat, aContext);
}
/**
* Converts a WFS feature model into KML placemarks, and encodes the result with
* {@link TLcdKML22ModelEncoder}.
*/
private static class WFSKMLModelEncoder implements ILcdModelEncoder {
private final ILcdOutputStreamFactory fOutputStreamFactory;
WFSKMLModelEncoder(ILcdOutputStreamFactory aOutputStreamFactory) {
fOutputStreamFactory = aOutputStreamFactory;
}
@Override
public String getDisplayName() {
return "WFS KML encoder";
}
@Override
public boolean canSave(ILcdModel aModel) {
return false;
}
@Override
public void save(ILcdModel aModel) throws IllegalArgumentException, IOException {
throw new UnsupportedOperationException("KML encoding only supports export, not save.");
}
@Override
public boolean canExport(ILcdModel aModel, String aDestinationName) {
return aModel.getModelDescriptor() instanceof ILcdDataModelDescriptor;
}
@Override
public void export(ILcdModel aModel, String aDestinationName) throws IllegalArgumentException, IOException {
KML22Converter converter = new KML22Converter();
converter.addShapeConverter(new KML22ShapeConverter());
converter.addPlacemarkConverter(new DataObjectPlacemarkConverter());
ILcdModel kmlModel = converter.convert(aModel);
TLcdKML22ModelEncoder encoder = new TLcdKML22ModelEncoder();
encoder.setOutputStreamFactory(fOutputStreamFactory);
encoder.export(kmlModel, aDestinationName);
}
}
/**
* Converts any {@link ILcdDataObject} feature into a {@link TLcdKML22Placemark}, using its properties for
* the placemark's name and {@code ExtendedData}. The actual geometry is added separately, by a
* {@link samples.encoder.kml22.converter.IKML22ShapeConverter}.
*/
private static class DataObjectPlacemarkConverter implements IKML22PlacemarkConverter {
@Override
public boolean canConvertIntoPlacemark(ILcdModel aModel, Object aObject) {
return aObject instanceof ILcdDataObject;
}
@Override
public TLcdKML22Placemark convertIntoPlacemark(ILcdModel aModel, Object aObject, KML22ModelBuilder aKMLBuilder) {
ILcdDataObject dataObject = (ILcdDataObject) aObject;
TLcdDataType dataType = dataObject.getDataType();
TLcdKML22Placemark placemark = KML22FeatureUtil.createPlacemark(findDisplayName(dataObject));
// Register one schema per feature type, and describe every property of the feature as ExtendedData
// on the placemark, so that the properties survive the round trip to KML.
String schemaId = dataType.getName();
TLcdKML22Schema schema = aKMLBuilder.getSchema(schemaId);
if (schema == null) {
schema = KML22FeatureUtil.createSchema(dataType);
aKMLBuilder.addSchema(schema);
}
placemark.setExtendedData(KML22FeatureUtil.createExtendedData(dataObject, "#" + schemaId));
return placemark;
}
/**
* Picks a name for the placemark: the value of the first property that looks like an identifier or
* display name ("name" or "id", case-insensitively), or the feature type's name as a fallback.
*/
private static String findDisplayName(ILcdDataObject aDataObject) {
for (TLcdDataProperty property : aDataObject.getDataType().getProperties()) {
String propertyName = property.getName();
if ("name".equalsIgnoreCase(propertyName) || "id".equalsIgnoreCase(propertyName)) {
Object value = aDataObject.getValue(property);
if (value != null) {
return value.toString();
}
}
}
return aDataObject.getDataType().getDisplayName();
}
}
}
WFSKMLClientModelEncoderFactory extends TLcdWFSClientModelEncoderFactory and:
-
Increases the number of supported output formats by one, and defines
KMLas the identifier for that extra format. This identifier gets listed in the WFS capabilities, and is what clients pass as theoutputFormatparameter of aGetFeaturerequest (outputFormat=KML). -
Registers an encoder for that format, which converts the requested features into KML.
Unlike GML, GeoJSON and SHAPE-ZIP, KML doesn’t have a generic notion of an arbitrary feature: it works with placemarks, each
with their own geometry, name and, optionally, extended data.
This means that adding a KML output format requires you to convert your WFS features, which are ILcdDataObject instances, into placemarks yourself.
The sample does so using the KML sample converter framework, samples.encoder.kml22.converter:
-
KML22ShapeConverterconverts a feature’s geometry into a KML geometry. -
DataObjectPlacemarkConverter, a small customIKML22PlacemarkConverter, converts a feature’s properties into the placemark name andExtendedData.
|
|
To plug in your implementation, you register it with the service loader using the @LcdService annotation.
You don’t have to specify a priority in the @LcdService.
By default, your implementation already has a higher priority than the pre-configured TLcdWFSClientModelEncoderFactory.
For more information about the services mechanism, see Working with the services mechanism.