use of de.micromata.opengis.kml.v_2_2_0.Kml in project ddf by codice.
the class KMLTransformerImpl method transform.
@Override
public BinaryContent transform(Metacard metacard, Map<String, Serializable> arguments) throws CatalogTransformerException {
try {
Placemark placemark = transformEntry(null, metacard, arguments);
if (placemark.getStyleSelector().isEmpty() && StringUtils.isBlank(placemark.getStyleUrl())) {
placemark.getStyleSelector().addAll(defaultStyle);
}
Kml kml = KmlFactory.createKml().withFeature(placemark);
String transformedKmlString = kmlMarshaller.marshal(kml);
InputStream kmlInputStream = new ByteArrayInputStream(transformedKmlString.getBytes(StandardCharsets.UTF_8));
return new BinaryContentImpl(kmlInputStream, KML_MIMETYPE);
} catch (Exception e) {
LOGGER.debug("Error transforming metacard ({}) to KML: {}", metacard.getId(), e.getMessage());
throw new CatalogTransformerException("Error transforming metacard to KML.", e);
}
}
use of de.micromata.opengis.kml.v_2_2_0.Kml in project ddf by codice.
the class KMLTransformerImpl method performDefaultTransformation.
/**
* The default Transformation from a {@link Metacard} to a KML {@link Placemark}. Protected to
* easily allow other default transformations.
*
* @param entry - the {@link Metacard} to transform.
* @param urlToMetacard
* @return
* @throws javax.xml.transform.TransformerException
*/
protected Placemark performDefaultTransformation(Metacard entry) throws CatalogTransformerException {
// wrap metacard to work around classLoader/reflection issues
entry = new MetacardImpl(entry);
Placemark kmlPlacemark = KmlFactory.createPlacemark();
kmlPlacemark.setId("Placemark-" + entry.getId());
kmlPlacemark.setName(entry.getTitle());
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
String effectiveTime;
if (entry.getEffectiveDate() == null) {
effectiveTime = dateFormat.format(new Date());
} else {
effectiveTime = dateFormat.format(entry.getEffectiveDate());
}
TimeSpan timeSpan = KmlFactory.createTimeSpan();
timeSpan.setBegin(effectiveTime);
kmlPlacemark.setTimePrimitive(timeSpan);
kmlPlacemark.setGeometry(getKmlGeoWithPointsFromWkt(entry.getLocation()));
String description = entry.getTitle();
Handlebars handlebars = new Handlebars(templateLoader);
handlebars.registerHelpers(templateHelper);
try {
Template template = handlebars.compile(DESCRIPTION_TEMPLATE);
description = template.apply(new HandlebarsMetacard(entry));
LOGGER.debug(description);
} catch (IOException e) {
LOGGER.debug("Failed to apply description Template", e);
}
kmlPlacemark.setDescription(description);
setExtendedData(kmlPlacemark, entry);
String styleUrl = styleMapper.getStyleForMetacard(entry);
if (StringUtils.isNotBlank(styleUrl)) {
kmlPlacemark.setStyleUrl(styleUrl);
}
return kmlPlacemark;
}
use of de.micromata.opengis.kml.v_2_2_0.Kml in project ddf by codice.
the class KMLTransformerImpl method transform.
@Override
public BinaryContent transform(SourceResponse upstreamResponse, Map<String, Serializable> arguments) throws CatalogTransformerException {
LOGGER.trace("ENTERING: ResponseQueue transform");
if (arguments == null) {
LOGGER.debug("Null arguments, unable to complete transform");
throw new CatalogTransformerException("Unable to complete transform without arguments");
}
String docId = UUID.randomUUID().toString();
String restUriAbsolutePath = (String) arguments.get("url");
LOGGER.debug("rest string url arg: {}", LogSanitizer.sanitize(restUriAbsolutePath));
// Transform Metacards to KML
Document kmlDoc = KmlFactory.createDocument();
boolean needDefaultStyle = false;
for (Result result : upstreamResponse.getResults()) {
try {
Placemark placemark = transformEntry(null, result.getMetacard(), arguments);
if (placemark.getStyleSelector().isEmpty() && StringUtils.isEmpty(placemark.getStyleUrl())) {
placemark.setStyleUrl("#default");
needDefaultStyle = true;
}
kmlDoc.getFeature().add(placemark);
} catch (CatalogTransformerException e) {
LOGGER.debug("Error transforming current metacard ({}) to KML and will continue with remaining query responses.", LogSanitizer.sanitize(result.getMetacard().getId()), e);
}
}
if (needDefaultStyle) {
kmlDoc.getStyleSelector().addAll(defaultStyle);
}
Kml kmlResult = encloseKml(kmlDoc, docId, KML_RESPONSE_QUEUE_PREFIX + kmlDoc.getFeature().size() + CLOSE_PARENTHESIS);
String transformedKml = kmlMarshaller.marshal(kmlResult);
InputStream kmlInputStream = new ByteArrayInputStream(transformedKml.getBytes(StandardCharsets.UTF_8));
LOGGER.trace("EXITING: ResponseQueue transform");
return new BinaryContentImpl(kmlInputStream, KML_MIMETYPE);
}
use of de.micromata.opengis.kml.v_2_2_0.Kml in project ddf by codice.
the class KmlEndpointTest method testGetAvailableSourcesWithCount.
@Test
public void testGetAvailableSourcesWithCount() throws UnknownHostException, MalformedURLException, IllegalArgumentException, UriBuilderException, SourceUnavailableException {
when(mockUriInfo.getQueryParameters(false)).thenReturn(mockMap);
KmlEndpoint kmlEndpoint = new KmlEndpoint(mockBranding, mockFramework);
kmlEndpoint.setMaxResults(250);
Kml response = kmlEndpoint.getAvailableSources(mockUriInfo);
assertThat(response, notNullValue());
assertThat(response.getFeature(), instanceOf(Folder.class));
Folder folder = (Folder) response.getFeature();
assertThat(folder.getFeature(), notNullValue());
assertThat(folder.getFeature().size(), is(2));
assertThat(folder.getFeature().get(0), instanceOf(NetworkLink.class));
assertThat(folder.getFeature().get(1), instanceOf(NetworkLink.class));
NetworkLink nl1 = (NetworkLink) folder.getFeature().get(0);
assertThat(nl1.getName(), anyOf(is(REMOTE_SITE_NAME), is(LOCAL_SITE_NAME)));
assertThat(nl1.getLink().getHttpQuery(), is("count=250"));
NetworkLink nl2 = (NetworkLink) folder.getFeature().get(1);
assertThat(nl2.getName(), anyOf(is(REMOTE_SITE_NAME), is(LOCAL_SITE_NAME)));
assertThat(nl2.getLink().getHttpQuery(), is("count=250"));
}
use of de.micromata.opengis.kml.v_2_2_0.Kml in project ddf by codice.
the class KmlEndpointTest method testGetAvailableSources.
@Test
public void testGetAvailableSources() throws UnknownHostException, MalformedURLException, IllegalArgumentException, UriBuilderException, SourceUnavailableException {
when(mockUriInfo.getQueryParameters(false)).thenReturn(mockMap);
KmlEndpoint kmlEndpoint = new KmlEndpoint(mockBranding, mockFramework);
Kml response = kmlEndpoint.getAvailableSources(mockUriInfo);
assertThat(response, notNullValue());
assertThat(response.getFeature(), instanceOf(Folder.class));
Folder folder = (Folder) response.getFeature();
assertThat(folder.getFeature(), notNullValue());
assertThat(folder.getFeature().size(), is(2));
assertThat(folder.getFeature().get(0), instanceOf(NetworkLink.class));
assertThat(folder.getFeature().get(1), instanceOf(NetworkLink.class));
NetworkLink nl1 = (NetworkLink) folder.getFeature().get(0);
assertThat(nl1.getName(), anyOf(is(REMOTE_SITE_NAME), is(LOCAL_SITE_NAME)));
NetworkLink nl2 = (NetworkLink) folder.getFeature().get(1);
assertThat(nl2.getName(), anyOf(is(REMOTE_SITE_NAME), is(LOCAL_SITE_NAME)));
}
Aggregations