Skip to content

Commit

Permalink
First commit of GPXrouteConverter for VfrFlight
Browse files Browse the repository at this point in the history
  • Loading branch information
alus- committed Jan 4, 2020
1 parent 1b8d922 commit 4d901d0
Show file tree
Hide file tree
Showing 5 changed files with 477 additions and 19 deletions.
39 changes: 20 additions & 19 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
# Compiled class file
*.class

# Log file
*.log
#============================================================================
# GPXrouteConverter for VfrFlight
# Since : 4/1/2020
# Author : Alberto Realis-Luc <[email protected]>
# Web : https://www.alus.it/airnavigator/gpx.php
# Repository : https://github.com/alus-it/GPXrouteConverter.git
# Copyright : (C) 2019-2020 Alberto Realis-Luc
# License : GNU GPL v3
#
# This source file is part of GPXrouteConverter project
#============================================================================

# BlueJ files
*.ctxt
# Ignore bin and build folders
/bin/

# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Compiled Object files
*.class

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# Ignore Eclipse project files
.project
.cproject
.classpath
.settings/*

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
135 changes: 135 additions & 0 deletions src/it/alus/GPXrouteConverter/GPX.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
//============================================================================
// GPXrouteConverter for VfrFlight
// Since : 16/3/2019
// Author : Alberto Realis-Luc <[email protected]>
// Web : https://www.alus.it/airnavigator/gpx.php
// Repository : https://github.com/alus-it/GPXrouteConverter.git
// Copyright : (C) 2019-2020 Alberto Realis-Luc
// License : GNU GPL v3
//
// This source file is part of GPXrouteConverter project
//============================================================================

package it.alus.GPXrouteConverter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import javax.swing.JOptionPane;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class GPX {

public static Boolean Read(final File inputFile, List<Waypoint> waypoints) {
try {
final Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputFile);
doc.getDocumentElement().normalize();
if (doc.getDocumentElement().getNodeName() != "gpx") {
JOptionPane.showMessageDialog(null, "Expected \"gpx\" as root element but not found it.","ERROR",JOptionPane.ERROR_MESSAGE);
return false;
}
NodeList nList = doc.getElementsByTagName("rtept");
for (int i = 0; i < nList.getLength(); i++) {
Node nNode = nList.item(i);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
double lat = Double.parseDouble(eElement.getAttribute("lat"));
double lon = Double.parseDouble(eElement.getAttribute("lon"));
double alt = Double.parseDouble(eElement.getElementsByTagName("ele").item(0).getTextContent());
String name = eElement.getElementsByTagName("name").item(0).getTextContent();
waypoints.add(new Waypoint(lat,lon,alt,name));
}
}
return true;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Exception caught while reading GPX file.","ERROR",JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
return false;
}

public static Boolean Write(final String filename, final List<Waypoint> waypoints) {
if (waypoints.size() < 2) {
JOptionPane.showMessageDialog(null, "Found less than 2 waypoints.","ERROR",JOptionPane.ERROR_MESSAGE);
return false;
}
final File file = new File(filename);
if(file.exists() && !file.isDirectory() && JOptionPane.showConfirmDialog(null,
"The destination file: " + file.getName() + " already exists.\n" +
"Do you want to overwrite it?",
"Warning", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) return false;
OutputStreamWriter gpx = null;
Boolean retVal = false;
try {
gpx = new OutputStreamWriter(new FileOutputStream(filename), "UTF-8");
gpx.write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
"<gpx\n" +
"version=\"1.1\"\n" +
" creator=\"VfrFlight GPX Converter - http://www.alus.it\"\n" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
" xmlns=\"http://www.topografix.com/GPX/1/1\"\n" +
" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\">\n" +
"<metadata>\n");
final Date time = Calendar.getInstance().getTime();
SimpleDateFormat zuluFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
zuluFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
gpx.write("<time>" + zuluFormat.format(time) + "</time>\n");
Waypoint w = waypoints.get(0);
double minLat = w.getLat();
double maxLat = minLat;
double minLon = w.getLon();
double maxLon = minLon;
for (int i = 1; i < waypoints.size(); i++) {
w = waypoints.get(i);
if (minLat > w.getLat()) minLat = w.getLat();
if (maxLat < w.getLat()) maxLat = w.getLat();
if (minLon > w.getLon()) minLon = w.getLon();
if (maxLon < w.getLon()) maxLon = w.getLon();
}
gpx.write("<bounds minlat=\"" + minLat + "\" minlon=\"" + minLon + "\" maxlat=\"" + maxLat + "\" maxlon=\"" + maxLon + "\"/>\n" +
"</metadata>\n" +
"<rte>\n" +
" <name>" + file.getName() + "</name>\n" +
" <desc></desc>\n");
for (int i = 0; i < waypoints.size(); i++) {
w = waypoints.get(i);
gpx.write("<rtept lat=\"" + w.getLat() + "\" lon=\"" + w.getLon() + "\">\n");
gpx.write(" <ele>" + w.getRoundedAltMt() + "</ele>\n");
gpx.write(" <name>" + w.getName() + "</name>\n");
gpx.write(" <cmt></cmt>\n" +
" <desc></desc>\n" +
" <sym>" + ((i==0 || i == waypoints.size()-1) ? "Airport" : "Waypoint") + "</sym>\n" +
"</rtept>\n");
}
gpx.write("</rte>\n" +
"<extensions>\n" +
"</extensions>\n" +
"</gpx>\n");

retVal = true;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Exception caught while writing GPX file.","ERROR",JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
} finally {
try {
gpx.flush();
gpx.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Exception caught while finalizing GPX file.","ERROR",JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
}
return retVal;
}

}
60 changes: 60 additions & 0 deletions src/it/alus/GPXrouteConverter/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//============================================================================
// GPXrouteConverter for VfrFlight
// Since : 16/3/2019
// Author : Alberto Realis-Luc <[email protected]>
// Web : https://www.alus.it/airnavigator/gpx.php
// Repository : https://github.com/alus-it/GPXrouteConverter.git
// Copyright : (C) 2019-2020 Alberto Realis-Luc
// License : GNU GPL v3
//
// This source file is part of GPXrouteConverter project
//============================================================================

package it.alus.GPXrouteConverter;

import java.io.File;
import java.util.List;
import java.util.Vector;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

public class Main {

public static void main(String[] args) {
JFileChooser fc = new JFileChooser();
fc.setDialogTitle("Select VfrFlight or GPX flightplan to convert");
fc.setMultiSelectionEnabled(true);
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
fc.setAcceptAllFileFilterUsed(false);
final FileNameExtensionFilter vfr = new FileNameExtensionFilter("VfrFlight flight plans", "vfr");
final FileNameExtensionFilter gpx = new FileNameExtensionFilter("GPX flight plans", "gpx");
fc.addChoosableFileFilter(vfr);
fc.addChoosableFileFilter(gpx);
final int returnVal = fc.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
final File[] files = fc.getSelectedFiles();
for (int i=0; i<files.length; i++) {
String filename = files[i].getPath();
System.out.println("Opening: " + filename);
filename = filename.substring(0, filename.lastIndexOf('.')); // remove extension
List<Waypoint> waypoints = new Vector<Waypoint>();
if (fc.getFileFilter().equals(vfr)) {
System.out.println("Selected VfrFlight flight plan, they will be converted to GPX routes.");
if (VfrFlight.Read(files[i],waypoints)) {
System.out.println("Read: " + files[i].getName() + " Waypoints: " + waypoints.size());
filename += ".gpx";
if (GPX.Write(filename, waypoints)) System.out.println("Succesfully converted to: " + filename);
}
} else {
System.out.println("Selected a GPX file, it will be converted to VfrFlight flight plan.");
if (GPX.Read(files[i], waypoints)) {
System.out.println("Read: " + files[i].getName() + " Waypoints: " + waypoints.size());
filename += ".vfr";
if (VfrFlight.Write(filename, waypoints)) System.out.println("Succesfully converted to: " + filename);
}
}
}
}
}

}
Loading

0 comments on commit 4d901d0

Please sign in to comment.