Search in sources :

Example 6 with Kml

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, String url) 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 = null;
    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(getKmlGeoFromWkt(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);
    String styleUrl = styleMapper.getStyleForMetacard(entry);
    if (StringUtils.isNotBlank(styleUrl)) {
        kmlPlacemark.setStyleUrl(styleUrl);
    }
    return kmlPlacemark;
}
Also used : TimeSpan(de.micromata.opengis.kml.v_2_2_0.TimeSpan) Placemark(de.micromata.opengis.kml.v_2_2_0.Placemark) Handlebars(com.github.jknack.handlebars.Handlebars) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) LineString(com.vividsolutions.jts.geom.LineString) IOException(java.io.IOException) SimpleDateFormat(java.text.SimpleDateFormat) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Date(java.util.Date) Template(com.github.jknack.handlebars.Template)

Example 7 with Kml

use of de.micromata.opengis.kml.v_2_2_0.Kml in project ddf by codice.

the class TestKmlEndpoint method testGetKmlNetworkLink.

@Test
public void testGetKmlNetworkLink() {
    when(mockUriInfo.getQueryParameters(false)).thenReturn(mockMap);
    KmlEndpoint kmlEndpoint = new KmlEndpoint(mockBranding, mockFramework);
    kmlEndpoint.setDescription("This is some description.");
    kmlEndpoint.setLogo("https://tools.codice.org/wiki/download/attachments/3047457/DDF?version=1&modificationDate=1369422662164&api=v2");
    kmlEndpoint.setWebSite("https://tools.codice.org/wiki/display/DDF/DDF+Home");
    Kml response = kmlEndpoint.getKmlNetworkLink(mockUriInfo);
    assertThat(response, notNullValue());
    assertThat(response.getFeature(), instanceOf(NetworkLink.class));
    NetworkLink networkLink = (NetworkLink) response.getFeature();
    Link link = networkLink.getLink();
    assertThat(link, notNullValue());
    assertThat(link.getHref(), notNullValue());
    UriBuilder builder = UriBuilder.fromUri(link.getHref());
    URI uri = builder.build();
    assertThat(uri.getHost(), is(TEST_HOST));
    assertThat(String.valueOf(uri.getPort()), is(TEST_PORT));
    assertThat(uri.getPath(), is("/services/catalog/kml/sources"));
}
Also used : NetworkLink(de.micromata.opengis.kml.v_2_2_0.NetworkLink) Kml(de.micromata.opengis.kml.v_2_2_0.Kml) UriBuilder(javax.ws.rs.core.UriBuilder) URI(java.net.URI) Link(de.micromata.opengis.kml.v_2_2_0.Link) NetworkLink(de.micromata.opengis.kml.v_2_2_0.NetworkLink) Test(org.junit.Test)

Example 8 with Kml

use of de.micromata.opengis.kml.v_2_2_0.Kml in project ddf by codice.

the class TestKmlEndpoint 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)));
}
Also used : NetworkLink(de.micromata.opengis.kml.v_2_2_0.NetworkLink) Kml(de.micromata.opengis.kml.v_2_2_0.Kml) Folder(de.micromata.opengis.kml.v_2_2_0.Folder) Test(org.junit.Test)

Example 9 with Kml

use of de.micromata.opengis.kml.v_2_2_0.Kml in project ddf by codice.

the class KMLTransformerImpl method createKmlGeo.

private Geometry createKmlGeo(com.vividsolutions.jts.geom.Geometry geo) throws CatalogTransformerException {
    Geometry kmlGeo = null;
    if (Point.class.getSimpleName().equals(geo.getGeometryType())) {
        Point jtsPoint = (Point) geo;
        kmlGeo = KmlFactory.createPoint().addToCoordinates(jtsPoint.getX(), jtsPoint.getY());
    } else if (LineString.class.getSimpleName().equals(geo.getGeometryType())) {
        LineString jtsLS = (LineString) geo;
        de.micromata.opengis.kml.v_2_2_0.LineString kmlLS = KmlFactory.createLineString();
        List<Coordinate> kmlCoords = kmlLS.createAndSetCoordinates();
        for (com.vividsolutions.jts.geom.Coordinate coord : jtsLS.getCoordinates()) {
            kmlCoords.add(new Coordinate(coord.x, coord.y));
        }
        kmlGeo = kmlLS;
    } else if (Polygon.class.getSimpleName().equals(geo.getGeometryType())) {
        Polygon jtsPoly = (Polygon) geo;
        de.micromata.opengis.kml.v_2_2_0.Polygon kmlPoly = KmlFactory.createPolygon();
        List<Coordinate> kmlCoords = kmlPoly.createAndSetOuterBoundaryIs().createAndSetLinearRing().createAndSetCoordinates();
        for (com.vividsolutions.jts.geom.Coordinate coord : jtsPoly.getCoordinates()) {
            kmlCoords.add(new Coordinate(coord.x, coord.y));
        }
        kmlGeo = kmlPoly;
    } else if (geo instanceof GeometryCollection) {
        List<Geometry> geos = new ArrayList<Geometry>();
        for (int xx = 0; xx < geo.getNumGeometries(); xx++) {
            geos.add(createKmlGeo(geo.getGeometryN(xx)));
        }
        kmlGeo = KmlFactory.createMultiGeometry().withGeometry(geos);
    } else {
        throw new CatalogTransformerException("Unknown / Unsupported Geometry Type '" + geo.getGeometryType() + "'. Unale to preform KML Transform.");
    }
    return kmlGeo;
}
Also used : CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) Point(com.vividsolutions.jts.geom.Point) Geometry(de.micromata.opengis.kml.v_2_2_0.Geometry) GeometryCollection(com.vividsolutions.jts.geom.GeometryCollection) LineString(com.vividsolutions.jts.geom.LineString) Coordinate(de.micromata.opengis.kml.v_2_2_0.Coordinate) List(java.util.List) ArrayList(java.util.ArrayList) Polygon(com.vividsolutions.jts.geom.Polygon)

Example 10 with Kml

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 = marshalKml(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);
    }
}
Also used : Placemark(de.micromata.opengis.kml.v_2_2_0.Placemark) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) Kml(de.micromata.opengis.kml.v_2_2_0.Kml) LineString(com.vividsolutions.jts.geom.LineString) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) URISyntaxException(java.net.URISyntaxException) XMLStreamException(javax.xml.stream.XMLStreamException) ParseException(com.vividsolutions.jts.io.ParseException) JAXBException(javax.xml.bind.JAXBException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) MimeTypeParseException(javax.activation.MimeTypeParseException) IOException(java.io.IOException)

Aggregations

Kml (de.micromata.opengis.kml.v_2_2_0.Kml)9 NetworkLink (de.micromata.opengis.kml.v_2_2_0.NetworkLink)6 LineString (com.vividsolutions.jts.geom.LineString)4 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)4 Folder (de.micromata.opengis.kml.v_2_2_0.Folder)4 Test (org.junit.Test)4 Placemark (de.micromata.opengis.kml.v_2_2_0.Placemark)3 IOException (java.io.IOException)3 UriBuilder (javax.ws.rs.core.UriBuilder)3 Handlebars (com.github.jknack.handlebars.Handlebars)2 Template (com.github.jknack.handlebars.Template)2 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)2 Document (de.micromata.opengis.kml.v_2_2_0.Document)2 Geometry (de.micromata.opengis.kml.v_2_2_0.Geometry)2 Link (de.micromata.opengis.kml.v_2_2_0.Link)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 GeometryCollection (com.vividsolutions.jts.geom.GeometryCollection)1 Point (com.vividsolutions.jts.geom.Point)1 Polygon (com.vividsolutions.jts.geom.Polygon)1