Search in sources :

Example 1 with KmlLocalActivities

use of uk.m0nom.kml.activity.KmlLocalActivities in project adif-processor by urbancamo.

the class KmlWriter method write.

public void write(String pathname, String name, ActivityDatabases activities, Qsos qsos, TransformResults results) {
    KmlLocalActivities kmlLocalActivities = new KmlLocalActivities();
    KmlCommsUtils kmlCommsUtils = new KmlCommsUtils(control, activities);
    KmlStationUtils kmlStationUtils = new KmlStationUtils(control);
    KmlSatelliteTrack kmlSatelliteTrack = new KmlSatelliteTrack();
    final Kml kml = new Kml();
    Document doc = kml.createAndSetDocument().withName(name).withOpen(true);
    // create a Folder
    Folder contactsFolder = doc.createAndAddFolder();
    contactsFolder.withName("Contacts").withOpen(true);
    if (results.getSatelliteActivity().hasActivity()) {
        results.getSatelliteActivity().spaceOutContactsInPasses();
    }
    Station myStation = null;
    Iterator<Qso> qsoIterator = qsos.getQsos().iterator();
    Folder folder = null;
    while (qsoIterator.hasNext()) {
        Qso qso = qsoIterator.next();
        if (!qso.getFrom().equals(myStation)) {
            folder = contactsFolder.createAndAddFolder().withName(qso.getFrom().getCallsign()).withOpen(true);
            String error = kmlStationUtils.addMyStationToMap(doc, folder, qso);
            if (error != null) {
                results.setError(error);
            }
            if (qso.getFrom().hasActivity() && control.isKmlShowLocalActivationSites()) {
                kmlLocalActivities.addLocalActivities(doc, folder, qso.getFrom(), control.getKmlLocalActivationSitesRadius(), activities);
            }
            myStation = qso.getFrom();
        }
        Folder contactFolder = folder.createAndAddFolder().withName(qso.getTo().getCallsign()).withOpen(false);
        GlobalCoordinates coords = qso.getRecord().getCoordinates();
        if (LatLongUtils.isCoordinateValid(coords)) {
            String error = kmlStationUtils.createStationMarker(control, doc, contactFolder, qso);
            if (error != null) {
                results.setError(error);
            }
            if (qso.getTo().hasActivity() && control.isKmlShowLocalActivationSites()) {
                Folder localActivityFolder = contactFolder.createAndAddFolder().withName("Local Activity").withOpen(false);
                kmlLocalActivities.addLocalActivities(doc, localActivityFolder, qso.getTo(), control.getKmlLocalActivationSitesRadius(), activities);
            }
            error = kmlCommsUtils.createCommsLink(doc, contactFolder, qso, control, kmlStationUtils);
            if (error != null) {
                results.setError(error);
            }
            if (MaidenheadLocatorConversion.isADubiousGridSquare(qso.getRecord().getGridsquare())) {
                results.addContactWithDubiousLocation(qso.getTo().getCallsign());
            }
        } else {
            results.addContactWithoutLocation(qso.getTo().getCallsign());
            logger.warning(String.format("Cannot determine communication link, no location data for: %s", qso.getTo().getCallsign()));
        }
    }
    if (!results.hasErrors()) {
        if (results.getSatelliteActivity().hasActivity()) {
            GlobalCoordinates coords = qsos.getQsos().get(0).getRecord().getMyCoordinates();
            GlobalCoords3D coordinatesWithSourceAccuracy = new GlobalCoords3D(coords, 0.0);
            kmlSatelliteTrack.addSatelliteTracks(control, doc, results.getSatelliteActivity(), coordinatesWithSourceAccuracy);
        }
        try {
            logger.info(String.format("Writing KML to: %s", pathname));
            File file = new File(pathname);
            kml.marshal(file);
            String kmlContent = FileUtils.readFileToString(file, "UTF-8");
            kmlContent = kmlContent.replaceAll("ns2:", "").replace("<kml xmlns:ns2=\"http://www.opengis.net/kml/2.2\" xmlns:ns3=\"http://www.w3.org/2005/Atom\" xmlns:ns4=\"urn:oasis:names:tc:ciq:xsdschema:xAL:2.0\" xmlns:ns5=\"http://www.google.com/kml/ext/2.2\">", "<kml>");
            FileUtils.write(file, kmlContent, "UTF-8");
        } catch (IOException e) {
            results.setError(e.getMessage());
        }
    }
}
Also used : KmlLocalActivities(uk.m0nom.kml.activity.KmlLocalActivities) GlobalCoords3D(uk.m0nom.coords.GlobalCoords3D) Kml(de.micromata.opengis.kml.v_2_2_0.Kml) IOException(java.io.IOException) Document(de.micromata.opengis.kml.v_2_2_0.Document) Folder(de.micromata.opengis.kml.v_2_2_0.Folder) Qso(uk.m0nom.adif3.contacts.Qso) Station(uk.m0nom.adif3.contacts.Station) KmlStationUtils(uk.m0nom.kml.station.KmlStationUtils) KmlSatelliteTrack(uk.m0nom.kml.comms.KmlSatelliteTrack) KmlCommsUtils(uk.m0nom.kml.comms.KmlCommsUtils) GlobalCoordinates(org.gavaghan.geodesy.GlobalCoordinates) File(java.io.File)

Aggregations

Document (de.micromata.opengis.kml.v_2_2_0.Document)1 Folder (de.micromata.opengis.kml.v_2_2_0.Folder)1 Kml (de.micromata.opengis.kml.v_2_2_0.Kml)1 File (java.io.File)1 IOException (java.io.IOException)1 GlobalCoordinates (org.gavaghan.geodesy.GlobalCoordinates)1 Qso (uk.m0nom.adif3.contacts.Qso)1 Station (uk.m0nom.adif3.contacts.Station)1 GlobalCoords3D (uk.m0nom.coords.GlobalCoords3D)1 KmlLocalActivities (uk.m0nom.kml.activity.KmlLocalActivities)1 KmlCommsUtils (uk.m0nom.kml.comms.KmlCommsUtils)1 KmlSatelliteTrack (uk.m0nom.kml.comms.KmlSatelliteTrack)1 KmlStationUtils (uk.m0nom.kml.station.KmlStationUtils)1