Skip to content

Instantly share code, notes, and snippets.

@orionll
Last active December 25, 2015 02:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orionll/6904832 to your computer and use it in GitHub Desktop.
Save orionll/6904832 to your computer and use it in GitHub Desktop.
import java.text.DecimalFormat
import java.util.Locale
import scala.collection.mutable.ListBuffer
import scala.xml.XML
import org.joda.time.DateTime
object Main extends App {
Locale.setDefault(Locale.US)
val FloorLength = 3.0
val FloorHeight = 3.0
val StartLatitude = 54.868877
val StartLongitude = 83.089077
val EndLatitude = StartLatitude
val EndLongitude = StartLongitude + 3.0 / 6400000 * 180 / Math.PI
val StartEle = 132.0
val LongitudeFormat = new DecimalFormat("#.######");
val EleFormat = new DecimalFormat("#.#");
val BuildingHeight = 8
// Enter date, climbs count and total time
val startDate = new DateTime(2013, 10, 9, 2, 55, 0)
val count = 25
val totalTimeMinutes = 87.0
val totalTimeMillis = totalTimeMinutes * 60 * 1000
val oneClimbTimeMillis = totalTimeMillis / count
val timeMillis = oneClimbTimeMillis / 2 / BuildingHeight
val points = ListBuffer[TrackPoint]()
for (i <- 0 until count) {
for (floor <- 0 until BuildingHeight) {
val startTimestamp = startDate.plusMillis((i * oneClimbTimeMillis + floor * timeMillis).toInt)
points += TrackPoint(StartLatitude, StartLongitude, StartEle + floor * FloorHeight, startTimestamp)
val endTimeStamp = startDate.plusMillis((i * oneClimbTimeMillis + (floor + 0.5) * timeMillis).toInt)
points += TrackPoint(EndLatitude, EndLongitude, StartEle + (floor + 0.5) * FloorHeight, endTimeStamp)
}
for (j <- 0 until BuildingHeight) {
val floor = BuildingHeight - j
val startTimestamp = startDate.plusMillis(((i + 0.5) * oneClimbTimeMillis + j * timeMillis).toInt)
points += TrackPoint(StartLatitude, StartLongitude, StartEle + floor * FloorHeight, startTimestamp)
val endTimeStamp = startDate.plusMillis(((i + 0.5) * oneClimbTimeMillis + (j + 0.5) * timeMillis).toInt)
points += TrackPoint(EndLatitude, EndLongitude, StartEle + (floor - 0.5) * FloorHeight, endTimeStamp)
}
}
val filename = s"D:/temp/trace${startDate.year().get()}-${startDate.monthOfYear().get()}-${startDate.dayOfMonth().get()}.gpx"
val xml = getXml(startDate, points.toList)
XML.save(filename, xml, "UTF-8", true)
def getXml(date: DateTime, points: List[TrackPoint]) = {
<gpx version="1.1" creator="Endomondo.com" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd" xmlns="http://www.topografix.com/GPX/1/1" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<metadata>
<author>
<name>Иван Иванов</name>
<email id="ivanov" domain="gmail.com"/>
</author>
<link href="http://www.endomondo.com">
<text>Endomondo</text>
</link>
<time>{date}</time>
</metadata>
<trk>
<src>http://www.endomondo.com/</src>
<link href="http://www.endomondo.com/workouts/217959499/1697606">
<text>endomondo</text>
</link>
<type>WALKING</type>
<trkseg>
{points.map(_.toXml)}
</trkseg>
</trk>
</gpx>
}
case class TrackPoint(val latitude: Double, val longitude: Double, val ele: Double, val timestamp: DateTime) {
def toXml() = {
<trkpt lat={LongitudeFormat.format(latitude)} lon={LongitudeFormat.format(longitude)}>
<ele>{EleFormat.format(ele)}</ele>
<time>{timestamp}</time>
</trkpt>
}
}
}
@NIA
Copy link

NIA commented Oct 10, 2013

Классно. Возможность вот так вот просто писать xml делает Scala прямо идеальным тулом для этой маленькой задачки! Сохранил себе так же как минимальный reference по формату gpx :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment