# HG changeset patch # User David Barts # Date 1629876270 25200 # Node ID ab4d4434c37fc05deaf5b3d7e28fde9d6c8e2b29 # Parent 94762476b171582a8aeab6ae602188896828d3d7 This is useful when grabbing coordinates from QGIS. diff -r 94762476b171 -r ab4d4434c37f MakeWaypoints.kt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MakeWaypoints.kt Wed Aug 25 00:24:30 2021 -0700 @@ -0,0 +1,68 @@ +/* + * Make waypoints. In Kotlin, because the JVM has a uniquely non-sucky + * way to write XML. + */ + +import javax.xml.stream.* +import java.io.FileOutputStream + +private const val MYNAME = "MakeWaypoints" +private const val CHARSET = "UTF-8" + +fun main(args: Array): Unit { + if (args.size != 1) { + System.err.println("${MYNAME}: expecting a single file name") + System.exit(2) + } + + println("Enter waypoints below, end with empty line.") + println() + + val outName = args[0] + val invalid = "${MYNAME}: invalid entry, please try again" + FileOutputStream(outName).use stream@{ + XMLOutputFactory.newInstance().createXMLStreamWriter(it, CHARSET).run { + writeStartDocument(CHARSET, "1.0") + writeStartElement("gpx") + writeAttribute("creator", "MakeWaypoints") + writeAttribute("version", "1.1") + writeDefaultNamespace("http://www.topografix.com/GPX/1/1") + + while (true) { + print("Longitude, latitude: ") + val longLat = readLine() + if (longLat.isNullOrEmpty()) { + writeEndDocument() + close() + return@stream + } + val parts = longLat.split(',') + if (parts.size != 2) { + System.err.println(invalid) + continue + } + val long = parts[0].trim() + val lat = parts[1].trim() + if (lat.toDoubleOrNull() == null || long.toDoubleOrNull() == null) { + System.err.println(invalid) + continue + } + print("Name: ") + val name = readLine() + if (name.isNullOrEmpty()) { + System.err.println(invalid) + continue + } + writeStartElement("wpt") + writeAttribute("lat", lat) + writeAttribute("lon", long) + writeStartElement("name") + writeCharacters(name) + writeEndElement() + writeEndElement() + } + } + } +} + + \ No newline at end of file