annotate MakeWaypoints.kt @ 4:45be2d1d3213

Remove trailing whitespace, add warp notes.
author David Barts <n5jrn@me.com>
date Wed, 25 Aug 2021 06:33:31 -0700
parents ab4d4434c37f
children a725bd48bc0b
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
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
8
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
9 private const val MYNAME = "MakeWaypoints"
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
10 private const val CHARSET = "UTF-8"
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 fun main(args: Array<String>): Unit {
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
13 if (args.size != 1) {
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
14 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
15 System.exit(2)
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
16 }
4
45be2d1d3213 Remove trailing whitespace, add warp notes.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
17
3
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
18 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
19 println()
4
45be2d1d3213 Remove trailing whitespace, add warp notes.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
20
3
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
21 val outName = args[0]
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
22 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
23 FileOutputStream(outName).use stream@{
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
24 XMLOutputFactory.newInstance().createXMLStreamWriter(it, CHARSET).run {
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
25 writeStartDocument(CHARSET, "1.0")
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
26 writeStartElement("gpx")
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
27 writeAttribute("creator", "MakeWaypoints")
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
28 writeAttribute("version", "1.1")
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
29 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
30
3
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
31 while (true) {
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
32 print("Longitude, latitude: ")
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
33 val longLat = readLine()
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
34 if (longLat.isNullOrEmpty()) {
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
35 writeEndDocument()
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
36 close()
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
37 return@stream
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
38 }
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
39 val parts = longLat.split(',')
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
40 if (parts.size != 2) {
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
41 System.err.println(invalid)
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
42 continue
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
43 }
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
44 val long = parts[0].trim()
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
45 val lat = parts[1].trim()
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
46 if (lat.toDoubleOrNull() == null || long.toDoubleOrNull() == null) {
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 print("Name: ")
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
51 val name = readLine()
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
52 if (name.isNullOrEmpty()) {
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 writeStartElement("wpt")
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
57 writeAttribute("lat", lat)
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
58 writeAttribute("lon", long)
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
59 writeStartElement("name")
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
60 writeCharacters(name)
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
61 writeEndElement()
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
62 writeEndElement()
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
63 }
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
64 }
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
65 }
ab4d4434c37f This is useful when grabbing coordinates from QGIS.
David Barts <n5jrn@me.com>
parents:
diff changeset
66 }