Mercurial > cgi-bin > hgweb.cgi > TopoTiler
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 |
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 | 8 import java.text.SimpleDateFormat |
9 import java.util.Date | |
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 | 14 private val WP_TIME_FORMAT = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").apply { |
15 timeZone = TimeZone.getTimeZone("GMT") | |
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 | 19 /* parse arguments */ |
20 val syntaxError = "${MYNAME}: syntax: [-latfirst] file" | |
21 if (args.size < 1 || args.size > 2) { | |
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 | 25 var latFirst = false |
26 var fileArg = 0 | |
27 if (args[0].startsWith('-')) { | |
28 latFirst = args[0].equals("-latfirst", ignoreCase = true) | |
29 if (!latFirst || args.size != 2) { | |
30 System.err.println(syntaxError) | |
31 System.exit(2) | |
32 } | |
33 fileArg = 1 | |
34 } | |
35 val outName = args[fileArg] | |
4
45be2d1d3213
Remove trailing whitespace, add warp notes.
David Barts <n5jrn@me.com>
parents:
3
diff
changeset
|
36 |
6 | 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 | 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 | 52 if (latFirst) |
53 print("Latitude, longitude: ") | |
54 else | |
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 | 67 val (lat, long) = if (latFirst) |
68 Pair(parts[0].trim(), parts[1].trim()) | |
69 else | |
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 | 84 writeStartElement("time") |
85 writeCharacters(WP_TIME_FORMAT.format(Date())) | |
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 } |