public KMLRasterTranslator(ContentHandler handler) {
super(handler);
}
public void encode(Object o) throws IllegalArgumentException {
MapLayer mapLayer = (MapLayer) o;
int mapLayerOrder = mapContext.indexOf(mapLayer);
if ( isStandAlone() ) {
start( "kml" );
}
//start("Document");
//element("name", mapLayer.getTitle());
//start the folder naming it 'layer_<mapLayerOrder>', this is
// necessary for a GroundOverlay
start("Folder");
element("name", "layer_" + mapLayerOrder);
element("description", mapLayer.getTitle());
start("GroundOverlay");
//element( "name", feature.getID() );
element("name", mapLayer.getTitle());
element("drawOrder", Integer.toString(mapLayerOrder));
//encode the icon
start("Icon");
encodeHref(mapLayer);
element("viewRefreshMode", "never");
element("viewBoundScale", "0.75");
end("Icon");
//encde the bounding box
ReferencedEnvelope box = new ReferencedEnvelope(mapContext.getAreaOfInterest());
boolean reprojectBBox = (box.getCoordinateReferenceSystem() != null)
&& !CRS.equalsIgnoreMetadata(box.getCoordinateReferenceSystem(), DefaultGeographicCRS.WGS84);
if (reprojectBBox) {
try {
box = box.transform(DefaultGeographicCRS.WGS84, true);
} catch(Exception e) {
throw new WmsException("Could not transform bbox to WGS84", "ReprojectionError", e);
}
}
start("LatLonBox");
element("north", Double.toString(box.getMaxY()));
element("south", Double.toString(box.getMinY()));
element("east", Double.toString(box.getMaxX()));
element("west", Double.toString(box.getMinX()));
end("LatLonBox");
end("GroundOverlay");
// if the kmplacemark format option is true, add placemarks to the output
GetMapRequest request = mapContext.getRequest();
boolean kmplacemark = request.getWMS().getKmlPlacemark();
if (request.getFormatOptions().get("kmplacemark") != null)
kmplacemark = (Boolean) request.getFormatOptions().get("kmplacemark");
if (kmplacemark) {
FeatureCollection<SimpleFeatureType, SimpleFeature> features = null;
try {
features = KMLUtils
.loadFeatureCollection(
(FeatureSource<SimpleFeatureType, SimpleFeature>) mapLayer
.getFeatureSource(), mapLayer,
mapContext);
} catch (Exception ex) {
String msg = "Error getting features.";
LOGGER.log(Level.WARNING, msg, ex);
}
if (features != null && features.size() > 0) {
Geometry geom = null;
Geometry centroidGeom = null;
// get geometry of the area of interest
Envelope aoi = mapContext.getAreaOfInterest();
GeometryFactory factory = new GeometryFactory();
Geometry displayGeom = factory.toGeometry(new Envelope(aoi
.getMinX(), aoi.getMaxX(), aoi.getMinY(), aoi
.getMaxY()));
// get the styles for this feature
SimpleFeatureType featureType = features.getSchema();
FeatureTypeStyle[] fts = KMLUtils.filterFeatureTypeStyles(
mapLayer.getStyle(), featureType);
Iterator<SimpleFeature> iter = features.iterator();
while (iter.hasNext()) {
SimpleFeature ftr = iter.next();
geom = (Geometry) ftr.getDefaultGeometry();