annotate MakeWaypoints.kt @ 12:698b3335a63d default tip

Fix leading comment.
author David Barts <n5jrn@me.com>
date Thu, 26 Aug 2021 23:06:33 -0700
parents a56d3d3b5efd
children
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
9
a56d3d3b5efd Reject out of range latitide and longitide values.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
11 import kotlin.math.abs
3
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
12
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
13 private const val MYNAME = "MakeWaypoints"
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
14 private const val CHARSET = "UTF-8"
5
a725bd48bc0b Time stamp all waypoints.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
15 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
16 timeZone = TimeZone.getTimeZone("GMT")
a725bd48bc0b Time stamp all waypoints.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
17 }
3
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
18
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
19 fun main(args: Array<String>): Unit {
6
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
20 /* parse arguments */
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
21 val syntaxError = "${MYNAME}: syntax: [-latfirst] file"
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
22 if (args.size < 1 || args.size > 2) {
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
23 System.err.println(syntaxError)
3
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
24 System.exit(2)
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
25 }
6
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
26 var latFirst = false
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
27 var fileArg = 0
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
28 if (args[0].startsWith('-')) {
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
29 latFirst = args[0].equals("-latfirst", ignoreCase = true)
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
30 if (!latFirst || args.size != 2) {
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
31 System.err.println(syntaxError)
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
32 System.exit(2)
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
33 }
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
34 fileArg = 1
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
35 }
8
290b0b2a584b Fix bug.
David Barts <n5jrn@me.com>
parents: 7
diff changeset
36 if (args.size != fileArg + 1) {
7
63eab373d348 Catch more syntax errors.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
37 System.err.println(syntaxError)
63eab373d348 Catch more syntax errors.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
38 System.exit(2)
63eab373d348 Catch more syntax errors.
David Barts <n5jrn@me.com>
parents: 6
diff changeset
39 }
6
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
40 val outName = args[fileArg]
4
45be2d1d3213 Remove trailing whitespace, add warp notes.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
41
6
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
42 /* explain purpose */
3
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
43 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
44 println()
4
45be2d1d3213 Remove trailing whitespace, add warp notes.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
45
6
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
46 /* process input from user */
3
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
47 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
48 FileOutputStream(outName).use stream@{
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
49 XMLOutputFactory.newInstance().createXMLStreamWriter(it, CHARSET).run {
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
50 writeStartDocument(CHARSET, "1.0")
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
51 writeStartElement("gpx")
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
52 writeAttribute("creator", "MakeWaypoints")
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
53 writeAttribute("version", "1.1")
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
54 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
55
3
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
56 while (true) {
6
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
57 if (latFirst)
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
58 print("Latitude, longitude: ")
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
59 else
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
60 print("Longitude, latitude: ")
3
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
61 val longLat = readLine()
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
62 if (longLat.isNullOrEmpty()) {
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
63 writeEndDocument()
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
64 close()
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
65 return@stream
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
66 }
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
67 val parts = longLat.split(',')
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
68 if (parts.size != 2) {
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
69 System.err.println(invalid)
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
70 continue
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
71 }
6
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
72 val (lat, long) = if (latFirst)
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
73 Pair(parts[0].trim(), parts[1].trim())
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
74 else
cd32e08fa37f Add -latfirst option.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
75 Pair(parts[1].trim(), parts[0].trim())
9
a56d3d3b5efd Reject out of range latitide and longitide values.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
76 val dLat = lat.toDoubleOrNull()
a56d3d3b5efd Reject out of range latitide and longitide values.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
77 val dLong = long.toDoubleOrNull()
a56d3d3b5efd Reject out of range latitide and longitide values.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
78 if (dLat == null || abs(dLat) > 90.0 || dLong == null || abs(dLong) > 180.0) {
3
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
79 System.err.println(invalid)
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
80 continue
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
81 }
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
82 print("Name: ")
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
83 val name = readLine()
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
84 if (name.isNullOrEmpty()) {
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
85 System.err.println(invalid)
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
86 continue
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
87 }
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
88 writeStartElement("wpt")
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
89 writeAttribute("lat", lat)
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
90 writeAttribute("lon", long)
5
a725bd48bc0b Time stamp all waypoints.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
91 writeStartElement("time")
a725bd48bc0b Time stamp all waypoints.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
92 writeCharacters(WP_TIME_FORMAT.format(Date()))
a725bd48bc0b Time stamp all waypoints.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
93 writeEndElement()
3
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
94 writeStartElement("name")
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
95 writeCharacters(name)
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
96 writeEndElement()
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
97 writeEndElement()
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
98 }
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
99 }
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
100 }
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
101 }