Mercurial > cgi-bin > hgweb.cgi > TopoTiler
view MakeWaypoints.kt @ 5:a725bd48bc0b
Time stamp all waypoints.
author | David Barts <n5jrn@me.com> |
---|---|
date | Wed, 25 Aug 2021 07:44:53 -0700 |
parents | 45be2d1d3213 |
children | cd32e08fa37f |
line wrap: on
line source
/* * Make waypoints. In Kotlin, because the JVM has a uniquely non-sucky * way to write XML. */ import javax.xml.stream.* import java.io.FileOutputStream import java.text.SimpleDateFormat import java.util.Date import java.util.TimeZone private const val MYNAME = "MakeWaypoints" private const val CHARSET = "UTF-8" private val WP_TIME_FORMAT = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").apply { timeZone = TimeZone.getTimeZone("GMT") } fun main(args: Array<String>): Unit { if (args.size != 1) { System.err.println("${MYNAME}: expecting a single file name") System.exit(2) } println("Enter waypoints below, end with empty line.") println() val outName = args[0] val invalid = "${MYNAME}: invalid entry, please try again" FileOutputStream(outName).use stream@{ XMLOutputFactory.newInstance().createXMLStreamWriter(it, CHARSET).run { writeStartDocument(CHARSET, "1.0") writeStartElement("gpx") writeAttribute("creator", "MakeWaypoints") writeAttribute("version", "1.1") writeDefaultNamespace("http://www.topografix.com/GPX/1/1") while (true) { print("Longitude, latitude: ") val longLat = readLine() if (longLat.isNullOrEmpty()) { writeEndDocument() close() return@stream } val parts = longLat.split(',') if (parts.size != 2) { System.err.println(invalid) continue } val long = parts[0].trim() val lat = parts[1].trim() if (lat.toDoubleOrNull() == null || long.toDoubleOrNull() == null) { System.err.println(invalid) continue } print("Name: ") val name = readLine() if (name.isNullOrEmpty()) { System.err.println(invalid) continue } writeStartElement("wpt") writeAttribute("lat", lat) writeAttribute("lon", long) writeStartElement("time") writeCharacters(WP_TIME_FORMAT.format(Date())) writeEndElement() writeStartElement("name") writeCharacters(name) writeEndElement() writeEndElement() } } } }