use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class KmlInputTransformer method transform.
@Override
public Metacard transform(InputStream inputStream, String id) throws IOException, CatalogTransformerException {
MetacardImpl metacard;
try (TemporaryFileBackedOutputStream fileBackedOutputStream = new TemporaryFileBackedOutputStream()) {
try {
IOUtils.copy(inputStream, fileBackedOutputStream);
} catch (IOException e) {
throw new CatalogTransformerException("Unable to transform KML to Metacard. Error reading input stream.", e);
} finally {
IOUtils.closeQuietly(inputStream);
}
try (InputStream inputStreamCopy = fileBackedOutputStream.asByteSource().openStream()) {
metacard = (MetacardImpl) unmarshal(inputStreamCopy);
}
if (metacard == null) {
throw new CatalogTransformerException("Unable to transform Kml to Metacard.");
} else if (StringUtils.isNotEmpty(id)) {
metacard.setAttribute(Metacard.ID, id);
}
try (Reader reader = fileBackedOutputStream.asByteSource().asCharSource(Charsets.UTF_8).openStream()) {
String kmlString = CharStreams.toString(reader);
metacard.setAttribute(Metacard.METADATA, kmlString);
}
} catch (IOException e) {
throw new CatalogTransformerException("Unable to transform KML to Metacard. Error using file-backed stream.", e);
}
return metacard;
}
use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class KmzInputTransformer method transform.
@Override
public Metacard transform(InputStream inputStream, String id) throws IOException, CatalogTransformerException {
ZipInputStream zipInputStream = new ZipInputStream(inputStream);
ZipEntry entry;
while ((entry = zipInputStream.getNextEntry()) != null) {
// so we stop at the first one we find.
if (entry.getName().endsWith(KML_EXTENSION)) {
return kmlInputTransformer.transform(zipInputStream, id);
}
}
throw new CatalogTransformerException("Unable to parse any KML from KMZ file");
}
use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class KmzTransformer method transform.
/**
* Transforms a single metacard to a zipped KMZ file.
*
* @param metacard the {@link Metacard} to be transformed
* @param arguments any arguments to be used in the transformation. Keys are specific to each
* {@link MetacardTransformer} implementation
* @return KMZ BinaryContent
* @throws CatalogTransformerException
*/
@Override
public BinaryContent transform(Metacard metacard, Map<String, Serializable> arguments) throws CatalogTransformerException {
BinaryContent kml = kmlTransformer.transform(metacard, arguments);
BinaryContent kmz = transformKmlToKmz(kml);
if (kmz == null) {
throw new CatalogTransformerException("Unable to transform to KMZ for metacard ID: " + metacard.getId());
}
return kmz;
}
use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class KmzTransformer method transform.
/**
* Transforms a {@link SourceResponse} to a zipped KMZ file.
*
* @param upstreamResponse {@link SourceResponse} containing the results of a query.
* @param arguments the arguments that may be used to execute the transform
* @return KMZ BinaryContent
* @throws CatalogTransformerException
*/
@Override
public BinaryContent transform(SourceResponse upstreamResponse, Map<String, Serializable> arguments) throws CatalogTransformerException {
BinaryContent kml = kmlTransformer.transform(upstreamResponse, arguments);
BinaryContent kmz = transformKmlToKmz(kml);
if (kmz == null) {
throw new CatalogTransformerException("Unable to transform KML to KMZ.");
}
return kmz;
}
use of ddf.catalog.transform.CatalogTransformerException in project ddf by codice.
the class GeoJsonMetacardTransformer method convertToJSON.
public static JSONObject convertToJSON(Metacard metacard) throws CatalogTransformerException {
if (metacard == null) {
throw new CatalogTransformerException("Cannot transform null metacard.");
}
// must be LinkedHashMap to maintain order
JSONObject rootObject = new JSONObject();
rootObject.put("type", "Feature");
JSONObject properties = new JSONObject();
for (AttributeDescriptor ad : metacard.getMetacardType().getAttributeDescriptors()) {
Attribute attribute = metacard.getAttribute(ad.getName());
if (attribute != null) {
Object value = convertAttribute(attribute, ad);
if (value != null) {
if (Metacard.GEOGRAPHY.equals(attribute.getName())) {
rootObject.put(CompositeGeometry.GEOMETRY_KEY, value);
} else {
properties.put(attribute.getName(), value);
}
}
}
}
if (rootObject.get(CompositeGeometry.GEOMETRY_KEY) == null) {
rootObject.put(CompositeGeometry.GEOMETRY_KEY, null);
}
properties.put(METACARD_TYPE_PROPERTY_KEY, metacard.getMetacardType().getName());
if (metacard.getSourceId() != null && !"".equals(metacard.getSourceId())) {
properties.put(SOURCE_ID_PROPERTY, metacard.getSourceId());
}
rootObject.put(CompositeGeometry.PROPERTIES_KEY, properties);
return rootObject;
}
Aggregations