annotate MakeWaypoints.kt @ 6:cd32e08fa37f

Add -latfirst option.
author David Barts <n5jrn@me.com>
date Wed, 25 Aug 2021 08:26:05 -0700
parents a725bd48bc0b
children 63eab373d348
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
1 /*
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
2 * Make waypoints. In Kotlin, because the JVM has a uniquely non-sucky
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
3 * way to write XML.
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
4 */
4
45be2d1d3213 Remove trailing whitespace, add warp notes.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
5
3
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
6 import javax.xml.stream.*
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
7 import java.io.FileOutputStream
5
a725bd48bc0b Time stamp all waypoints.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
8 import java.text.SimpleDateFormat
a725bd48bc0b Time stamp all waypoints.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
9 import java.util.Date
a725bd48bc0b Time stamp all waypoints.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
10 import java.util.TimeZone
3
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
11
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
12 private const val MYNAME = "MakeWaypoints"
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
13 private const val CHARSET = "UTF-8"
5
a725bd48bc0b Time stamp all waypoints.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
14 private val WP_TIME_FORMAT = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").apply {
a725bd48bc0b Time stamp all waypoints.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
15 timeZone = TimeZone.getTimeZone("GMT")
a725bd48bc0b Time stamp all waypoints.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
16 }
3
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
17
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
18 fun main(args: Array<String>): Unit {
6
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
19 /* parse arguments */
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
20 val syntaxError = "${MYNAME}: syntax: [-latfirst] file"
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
21 if (args.size < 1 || args.size > 2) {
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
22 System.err.println(syntaxError)
3
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
23 System.exit(2)
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
24 }
6
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
25 var latFirst = false
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
26 var fileArg = 0
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
27 if (args[0].startsWith('-')) {
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
28 latFirst = args[0].equals("-latfirst", ignoreCase = true)
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
29 if (!latFirst || args.size != 2) {
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
30 System.err.println(syntaxError)
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
31 System.exit(2)
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
32 }
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
33 fileArg = 1
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
34 }
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
35 val outName = args[fileArg]
4
45be2d1d3213 Remove trailing whitespace, add warp notes.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
36
6
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
37 /* explain purpose */
3
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
38 println("Enter waypoints below, end with empty line.")
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
39 println()
4
45be2d1d3213 Remove trailing whitespace, add warp notes.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
40
6
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
41 /* process input from user */
3
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
42 val invalid = "${MYNAME}: invalid entry, please try again"
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
43 FileOutputStream(outName).use stream@{
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
44 XMLOutputFactory.newInstance().createXMLStreamWriter(it, CHARSET).run {
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
45 writeStartDocument(CHARSET, "1.0")
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
46 writeStartElement("gpx")
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
47 writeAttribute("creator", "MakeWaypoints")
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
48 writeAttribute("version", "1.1")
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
49 writeDefaultNamespace("http://www.topografix.com/GPX/1/1")
4
45be2d1d3213 Remove trailing whitespace, add warp notes.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
50
3
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
51 while (true) {
6
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
52 if (latFirst)
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
53 print("Latitude, longitude: ")
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
54 else
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
55 print("Longitude, latitude: ")
3
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
56 val longLat = readLine()
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
57 if (longLat.isNullOrEmpty()) {
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
58 writeEndDocument()
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
59 close()
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
60 return@stream
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
61 }
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
62 val parts = longLat.split(',')
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
63 if (parts.size != 2) {
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
64 System.err.println(invalid)
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
65 continue
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
66 }
6
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
67 val (lat, long) = if (latFirst)
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
68 Pair(parts[0].trim(), parts[1].trim())
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
69 else
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
70 Pair(parts[1].trim(), parts[0].trim())
3
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
71 if (lat.toDoubleOrNull() == null || long.toDoubleOrNull() == null) {
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
72 System.err.println(invalid)
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
73 continue
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
74 }
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
75 print("Name: ")
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
76 val name = readLine()
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
77 if (name.isNullOrEmpty()) {
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
78 System.err.println(invalid)
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
79 continue
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
80 }
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
81 writeStartElement("wpt")
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
82 writeAttribute("lat", lat)
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
83 writeAttribute("lon", long)
5
a725bd48bc0b Time stamp all waypoints.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
84 writeStartElement("time")
a725bd48bc0b Time stamp all waypoints.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
85 writeCharacters(WP_TIME_FORMAT.format(Date()))
a725bd48bc0b Time stamp all waypoints.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
86 writeEndElement()
3
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
87 writeStartElement("name")
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
88 writeCharacters(name)
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
89 writeEndElement()
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
90 writeEndElement()
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
91 }
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
92 }
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
93 }
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
94 }