-
-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3573 from goldbattle/record_gps_tracks
Recording and Uploading of GPX Traces
- Loading branch information
Showing
26 changed files
with
364 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
app/src/main/java/de/westnordost/streetcomplete/data/osmtracks/Track.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package de.westnordost.streetcomplete.data.osmtracks | ||
|
||
import de.westnordost.streetcomplete.data.osm.mapdata.LatLon | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class Track( | ||
val id: Long, | ||
val userName: String, | ||
) | ||
|
||
@Serializable | ||
data class Trackpoint( | ||
val position: LatLon, | ||
val time: Long, | ||
val horizontalDilutionOfPrecision: Float, | ||
val elevation: Float, | ||
) |
24 changes: 24 additions & 0 deletions
24
app/src/main/java/de/westnordost/streetcomplete/data/osmtracks/TracksApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package de.westnordost.streetcomplete.data.osmtracks | ||
|
||
import de.westnordost.streetcomplete.data.download.ConnectionException | ||
import de.westnordost.streetcomplete.data.user.AuthorizationException | ||
|
||
/** | ||
* Creates GPS / GPX trackpoint histories | ||
*/ | ||
interface TracksApi { | ||
|
||
/** | ||
* Create a new GPX track history | ||
* | ||
* @param trackpoints history of recorded trackpoints | ||
* @param noteText optional text appended to the track | ||
* | ||
* @throws AuthorizationException if this application is not authorized to write notes | ||
* (Permission.READ_GPS_TRACES, Permission.WRITE_GPS_TRACES) | ||
* @throws ConnectionException if a temporary network connection problem occurs | ||
* | ||
* @return the new track | ||
*/ | ||
fun create(trackpoints: List<Trackpoint>, noteText: String?): Track | ||
} |
64 changes: 64 additions & 0 deletions
64
app/src/main/java/de/westnordost/streetcomplete/data/osmtracks/TracksApiImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package de.westnordost.streetcomplete.data.osmtracks | ||
|
||
import de.westnordost.osmapi.OsmConnection | ||
import de.westnordost.osmapi.common.errors.OsmApiException | ||
import de.westnordost.osmapi.common.errors.OsmApiReadResponseException | ||
import de.westnordost.osmapi.common.errors.OsmAuthorizationException | ||
import de.westnordost.osmapi.common.errors.OsmConnectionException | ||
import de.westnordost.osmapi.map.data.OsmLatLon | ||
import de.westnordost.osmapi.traces.GpsTraceDetails | ||
import de.westnordost.osmapi.traces.GpsTracesApi | ||
import de.westnordost.osmapi.traces.GpsTrackpoint | ||
import de.westnordost.streetcomplete.ApplicationConstants | ||
import de.westnordost.streetcomplete.data.download.ConnectionException | ||
import de.westnordost.streetcomplete.data.user.AuthorizationException | ||
import java.time.Instant | ||
import java.time.ZoneOffset | ||
import java.time.format.DateTimeFormatter | ||
|
||
class TracksApiImpl(osm: OsmConnection) : TracksApi { | ||
private val api: GpsTracesApi = GpsTracesApi(osm) | ||
|
||
override fun create(trackpoints: List<Trackpoint>, noteText: String?): Track = wrapExceptions { | ||
// Filename is just the start of the track | ||
// https://stackoverflow.com/a/49862573/7718197 | ||
val name = DateTimeFormatter | ||
.ofPattern("yyyy_MM_dd'T'HH_mm_ss.SSSSSS'Z'") | ||
.withZone(ZoneOffset.UTC) | ||
.format(Instant.ofEpochSecond(trackpoints[0].time)) + ".gpx" | ||
val visibility = GpsTraceDetails.Visibility.IDENTIFIABLE | ||
val description = noteText ?: "Uploaded via ${ApplicationConstants.USER_AGENT}" | ||
val tags = listOf(ApplicationConstants.NAME.lowercase()) | ||
|
||
// Generate history of trackpoints | ||
val history = trackpoints.mapIndexed { idx, it -> | ||
GpsTrackpoint( | ||
OsmLatLon(it.position.latitude, it.position.longitude), | ||
Instant.ofEpochMilli(it.time), | ||
idx == 0, | ||
it.horizontalDilutionOfPrecision, | ||
it.elevation | ||
) | ||
} | ||
|
||
// Finally query the API and return! | ||
val traceId = api.create(name, visibility, description, tags, history) | ||
val details = api.get(traceId) | ||
Track(details.id, details.userName) | ||
} | ||
} | ||
|
||
private inline fun <T> wrapExceptions(block: () -> T): T = | ||
try { | ||
block() | ||
} catch (e: OsmAuthorizationException) { | ||
throw AuthorizationException(e.message, e) | ||
} catch (e: OsmConnectionException) { | ||
throw ConnectionException(e.message, e) | ||
} catch (e: OsmApiReadResponseException) { | ||
// probably a temporary connection error | ||
throw ConnectionException(e.message, e) | ||
} catch (e: OsmApiException) { | ||
// request timeout is a temporary connection error | ||
throw if (e.errorCode == 408) ConnectionException(e.message, e) else e | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.