annotate MakeWaypoints.kt @ 5:a725bd48bc0b

Time stamp all waypoints.
author David Barts <n5jrn@me.com>
date Wed, 25 Aug 2021 07:44:53 -0700
parents 45be2d1d3213
children cd32e08fa37f
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 {
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
19 if (args.size != 1) {
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
20 System.err.println("${MYNAME}: expecting a single file name")
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
21 System.exit(2)
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
22 }
4
45be2d1d3213 Remove trailing whitespace, add warp notes.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
23
3
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
24 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
25 println()
4
45be2d1d3213 Remove trailing whitespace, add warp notes.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
26
3
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
27 val outName = args[0]
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
28 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
29 FileOutputStream(outName).use stream@{
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
30 XMLOutputFactory.newInstance().createXMLStreamWriter(it, CHARSET).run {
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
31 writeStartDocument(CHARSET, "1.0")
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
32 writeStartElement("gpx")
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
33 writeAttribute("creator", "MakeWaypoints")
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
34 writeAttribute("version", "1.1")
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
35 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
36
3
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
37 while (true) {
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
38 print("Longitude, latitude: ")
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
39 val longLat = readLine()
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
40 if (longLat.isNullOrEmpty()) {
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
41 writeEndDocument()
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
42 close()
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
43 return@stream
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
44 }
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
45 val parts = longLat.split(',')
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
46 if (parts.size != 2) {
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
47 System.err.println(invalid)
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
48 continue
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
49 }
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
50 val long = parts[0].trim()
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
51 val lat = parts[1].trim()
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
52 if (lat.toDoubleOrNull() == null || long.toDoubleOrNull() == null) {
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
53 System.err.println(invalid)
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
54 continue
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
55 }
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
56 print("Name: ")
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
57 val name = readLine()
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
58 if (name.isNullOrEmpty()) {
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
59 System.err.println(invalid)
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
60 continue
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 writeStartElement("wpt")
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
63 writeAttribute("lat", lat)
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
64 writeAttribute("lon", long)
5
a725bd48bc0b Time stamp all waypoints.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
65 writeStartElement("time")
a725bd48bc0b Time stamp all waypoints.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
66 writeCharacters(WP_TIME_FORMAT.format(Date()))
a725bd48bc0b Time stamp all waypoints.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
67 writeEndElement()
3
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
68 writeStartElement("name")
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
69 writeCharacters(name)
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
70 writeEndElement()
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
71 writeEndElement()
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
72 }
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
73 }
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 }