Mercurial > cgi-bin > hgweb.cgi > TopoTiler
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 |
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 | |
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 | 15 private val WP_TIME_FORMAT = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").apply { |
16 timeZone = TimeZone.getTimeZone("GMT") | |
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 | 20 /* parse arguments */ |
21 val syntaxError = "${MYNAME}: syntax: [-latfirst] file" | |
22 if (args.size < 1 || args.size > 2) { | |
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 | 26 var latFirst = false |
27 var fileArg = 0 | |
28 if (args[0].startsWith('-')) { | |
29 latFirst = args[0].equals("-latfirst", ignoreCase = true) | |
30 if (!latFirst || args.size != 2) { | |
31 System.err.println(syntaxError) | |
32 System.exit(2) | |
33 } | |
34 fileArg = 1 | |
35 } | |
8 | 36 if (args.size != fileArg + 1) { |
7 | 37 System.err.println(syntaxError) |
38 System.exit(2) | |
39 } | |
6 | 40 val outName = args[fileArg] |
4
45be2d1d3213
Remove trailing whitespace, add warp notes.
David Barts <n5jrn@me.com>
parents:
3
diff
changeset
|
41 |
6 | 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 | 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 | 57 if (latFirst) |
58 print("Latitude, longitude: ") | |
59 else | |
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 | 72 val (lat, long) = if (latFirst) |
73 Pair(parts[0].trim(), parts[1].trim()) | |
74 else | |
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 | 91 writeStartElement("time") |
92 writeCharacters(WP_TIME_FORMAT.format(Date())) | |
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 } |