changeset 3:ab4d4434c37f

This is useful when grabbing coordinates from QGIS.
author David Barts <n5jrn@me.com>
date Wed, 25 Aug 2021 00:24:30 -0700
parents 94762476b171
children 45be2d1d3213
files MakeWaypoints.kt
diffstat 1 files changed, 68 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /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<String>): 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