comparison 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
comparison
equal deleted inserted replaced
5:a725bd48bc0b 6:cd32e08fa37f
14 private val WP_TIME_FORMAT = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").apply { 14 private val WP_TIME_FORMAT = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").apply {
15 timeZone = TimeZone.getTimeZone("GMT") 15 timeZone = TimeZone.getTimeZone("GMT")
16 } 16 }
17 17
18 fun main(args: Array<String>): Unit { 18 fun main(args: Array<String>): Unit {
19 if (args.size != 1) { 19 /* parse arguments */
20 System.err.println("${MYNAME}: expecting a single file name") 20 val syntaxError = "${MYNAME}: syntax: [-latfirst] file"
21 if (args.size < 1 || args.size > 2) {
22 System.err.println(syntaxError)
21 System.exit(2) 23 System.exit(2)
22 } 24 }
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]
23 36
37 /* explain purpose */
24 println("Enter waypoints below, end with empty line.") 38 println("Enter waypoints below, end with empty line.")
25 println() 39 println()
26 40
27 val outName = args[0] 41 /* process input from user */
28 val invalid = "${MYNAME}: invalid entry, please try again" 42 val invalid = "${MYNAME}: invalid entry, please try again"
29 FileOutputStream(outName).use stream@{ 43 FileOutputStream(outName).use stream@{
30 XMLOutputFactory.newInstance().createXMLStreamWriter(it, CHARSET).run { 44 XMLOutputFactory.newInstance().createXMLStreamWriter(it, CHARSET).run {
31 writeStartDocument(CHARSET, "1.0") 45 writeStartDocument(CHARSET, "1.0")
32 writeStartElement("gpx") 46 writeStartElement("gpx")
33 writeAttribute("creator", "MakeWaypoints") 47 writeAttribute("creator", "MakeWaypoints")
34 writeAttribute("version", "1.1") 48 writeAttribute("version", "1.1")
35 writeDefaultNamespace("http://www.topografix.com/GPX/1/1") 49 writeDefaultNamespace("http://www.topografix.com/GPX/1/1")
36 50
37 while (true) { 51 while (true) {
38 print("Longitude, latitude: ") 52 if (latFirst)
53 print("Latitude, longitude: ")
54 else
55 print("Longitude, latitude: ")
39 val longLat = readLine() 56 val longLat = readLine()
40 if (longLat.isNullOrEmpty()) { 57 if (longLat.isNullOrEmpty()) {
41 writeEndDocument() 58 writeEndDocument()
42 close() 59 close()
43 return@stream 60 return@stream
45 val parts = longLat.split(',') 62 val parts = longLat.split(',')
46 if (parts.size != 2) { 63 if (parts.size != 2) {
47 System.err.println(invalid) 64 System.err.println(invalid)
48 continue 65 continue
49 } 66 }
50 val long = parts[0].trim() 67 val (lat, long) = if (latFirst)
51 val lat = parts[1].trim() 68 Pair(parts[0].trim(), parts[1].trim())
69 else
70 Pair(parts[1].trim(), parts[0].trim())
52 if (lat.toDoubleOrNull() == null || long.toDoubleOrNull() == null) { 71 if (lat.toDoubleOrNull() == null || long.toDoubleOrNull() == null) {
53 System.err.println(invalid) 72 System.err.println(invalid)
54 continue 73 continue
55 } 74 }
56 print("Name: ") 75 print("Name: ")