Osrm
This commit is contained in:
@@ -19,6 +19,7 @@ package com.kouros.navigation.data
|
||||
import android.location.Location
|
||||
import android.location.LocationManager
|
||||
import android.net.Uri
|
||||
import com.kouros.navigation.data.route.Lane
|
||||
import io.objectbox.annotation.Entity
|
||||
import io.objectbox.annotation.Id
|
||||
import kotlinx.serialization.Serializable
|
||||
@@ -63,7 +64,9 @@ data class StepData (
|
||||
|
||||
var arrivalTime : Long,
|
||||
|
||||
var leftDistance: Double
|
||||
var leftDistance: Double,
|
||||
|
||||
var lane: List<Lane> = listOf(Lane(valid = false, indications = emptyList())),
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.google.gson.annotations.SerializedName
|
||||
|
||||
data class Intersections(
|
||||
|
||||
@SerializedName("in") var inV: Int? = null,
|
||||
@SerializedName("out") var out: Int? = null,
|
||||
@SerializedName("entry") var entry: ArrayList<Boolean> = arrayListOf(),
|
||||
@SerializedName("bearings") var bearings: ArrayList<Int> = arrayListOf(),
|
||||
|
||||
@@ -4,7 +4,9 @@ import android.location.Location
|
||||
import com.kouros.navigation.data.NavigationRepository
|
||||
import com.kouros.navigation.data.SearchFilter
|
||||
|
||||
private const val routeUrl = "https://kouros-online.de/osrm/route/v1/driving/"
|
||||
//private const val routeUrl = "https://kouros-online.de/osrm/route/v1/driving/"
|
||||
|
||||
private const val routeUrl = "http://192.168.1.37:5000/route/v1/driving/"
|
||||
|
||||
class OsrmRepository : NavigationRepository() {
|
||||
override fun getRoute(
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.kouros.navigation.data.osrm
|
||||
|
||||
import com.kouros.navigation.data.Route
|
||||
import com.kouros.navigation.data.route.Intersection
|
||||
import com.kouros.navigation.data.route.Lane
|
||||
import com.kouros.navigation.data.route.Leg
|
||||
import com.kouros.navigation.data.route.Maneuver as RouteManeuver
|
||||
import com.kouros.navigation.data.route.Step
|
||||
@@ -13,11 +15,12 @@ class OsrmRoute {
|
||||
fun mapToOsrm(routeJson: OsrmResponse, builder: Route.Builder) {
|
||||
val waypoints = mutableListOf<List<Double>>()
|
||||
val summary = Summary()
|
||||
summary.distance = routeJson.routes.first().distance!!
|
||||
summary.duration = routeJson.routes.first().duration!!
|
||||
summary.distance = routeJson.routes.first().distance!! / 1000
|
||||
summary.duration = routeJson.routes.first().duration!! / 1000
|
||||
val steps = mutableListOf<Step>()
|
||||
var stepIndex = 0
|
||||
routeJson.routes.first().legs.first().steps.forEach {
|
||||
val intersections = mutableListOf<Intersection>()
|
||||
if (it.maneuver != null) {
|
||||
val points = decodePolyline(it.geometry!!, 5)
|
||||
waypoints.addAll(points)
|
||||
@@ -27,7 +30,17 @@ class OsrmRoute {
|
||||
type = convertType(it.maneuver!!),
|
||||
waypoints = points
|
||||
)
|
||||
val step = Step( index = stepIndex, name = it.name!!, distance = it.distance!!, duration = it.duration!!, maneuver = maneuver)
|
||||
it.intersections.forEach { it2 ->
|
||||
if (it2.location[0] != 0.0) {
|
||||
val lanes = mutableListOf<Lane>()
|
||||
it2.lanes.forEach { it3 ->
|
||||
val lane = Lane(it3.valid, it3.indications)
|
||||
lanes.add(lane)
|
||||
}
|
||||
intersections.add(Intersection(it2.location, lanes))
|
||||
}
|
||||
}
|
||||
val step = Step( index = stepIndex, name = it.name!!, distance = it.distance!! / 1000, duration = it.duration!!, maneuver = maneuver, intersection = intersections)
|
||||
steps.add(step)
|
||||
stepIndex += 1
|
||||
}
|
||||
@@ -58,6 +71,11 @@ class OsrmRoute {
|
||||
newType = androidx.car.app.navigation.model.Maneuver.TYPE_TURN_NORMAL_RIGHT
|
||||
}
|
||||
}
|
||||
ManeuverType.turn.value -> {
|
||||
if (maneuver.modifier == "left") {
|
||||
newType = androidx.car.app.navigation.model.Maneuver.TYPE_TURN_NORMAL_LEFT
|
||||
}
|
||||
}
|
||||
}
|
||||
return newType
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.kouros.navigation.data.route
|
||||
|
||||
import java.util.Collections
|
||||
|
||||
data class Intersection(
|
||||
val location: ArrayList<Double> = arrayListOf(0.0, 0.0),
|
||||
val lane : List<Lane> = Collections.emptyList<Lane>(),
|
||||
)
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.kouros.navigation.data.route
|
||||
|
||||
data class Lane (
|
||||
val valid: Boolean,
|
||||
var indications: List<String>,
|
||||
)
|
||||
@@ -1,10 +1,11 @@
|
||||
package com.kouros.navigation.data.route
|
||||
|
||||
class Step(
|
||||
data class Step(
|
||||
var index : Int = 0,
|
||||
var waypointIndex : Int = 0,
|
||||
val maneuver: Maneuver,
|
||||
val duration: Double = 0.0,
|
||||
val distance: Double = 0.0,
|
||||
val name : String = "",
|
||||
val intersection: List<Intersection> = mutableListOf(),
|
||||
)
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.kouros.navigation.model
|
||||
|
||||
import android.content.Context
|
||||
import com.kouros.data.R
|
||||
import org.maplibre.compose.style.BaseStyle
|
||||
|
||||
class BaseStyleModel {
|
||||
fun readStyle(context: Context): BaseStyle.Json {
|
||||
println("Read Style")
|
||||
val liberty = context.resources.openRawResource(R.raw.liberty)
|
||||
val libertyString = liberty.bufferedReader().use { it.readText() }
|
||||
val baseStyle = BaseStyle.Json(libertyString)
|
||||
return baseStyle
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,8 @@ import com.kouros.navigation.data.Place
|
||||
import com.kouros.navigation.data.Route
|
||||
import com.kouros.navigation.data.RouteEngine
|
||||
import com.kouros.navigation.data.StepData
|
||||
import com.kouros.navigation.data.route.Intersection
|
||||
import com.kouros.navigation.data.route.Lane
|
||||
import com.kouros.navigation.data.route.Leg
|
||||
import com.kouros.navigation.utils.NavigationUtils.getIntKeyValue
|
||||
import com.kouros.navigation.utils.location
|
||||
@@ -37,6 +39,7 @@ open class RouteModel() {
|
||||
val lastSpeedLocation: Location = location(0.0, 0.0),
|
||||
val lastSpeedIndex: Int = 0,
|
||||
val maxSpeed: Int = 0,
|
||||
val location: Location = location(0.0, 0.0),
|
||||
)
|
||||
|
||||
var routeState = RouteState()
|
||||
@@ -74,6 +77,7 @@ open class RouteModel() {
|
||||
|
||||
@OptIn(DelicateCoroutinesApi::class)
|
||||
fun updateLocation(location: Location, viewModel: ViewModel) {
|
||||
routeState = routeState.copy(location = location)
|
||||
findStep(location)
|
||||
updateSpeedLimit(location, viewModel)
|
||||
}
|
||||
@@ -103,6 +107,19 @@ open class RouteModel() {
|
||||
//println("Current Index ${route.currentStep} WayPoint: ${route.currentStep().waypointIndex}")
|
||||
}
|
||||
|
||||
private fun currentIntersection(location: Location): Intersection {
|
||||
|
||||
var inter = Intersection()
|
||||
var nearestDistance = 100000.0f
|
||||
route.currentStep().intersection.forEach {
|
||||
val distance = location.distanceTo(location(it.location[0], it.location[1]))
|
||||
if (distance < nearestDistance) {
|
||||
nearestDistance = distance
|
||||
inter = it
|
||||
}
|
||||
}
|
||||
return inter
|
||||
}
|
||||
fun updateSpeedLimit(location: Location, viewModel: ViewModel) = runBlocking {
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
// speed limit
|
||||
@@ -136,26 +153,28 @@ open class RouteModel() {
|
||||
ManeuverType.None.value
|
||||
}
|
||||
// Get the single, correct maneuver for this state
|
||||
val relevantManeuver = if (shouldAdvance) {
|
||||
val relevantStep = if (shouldAdvance) {
|
||||
route.nextStep() // This advances the route's state
|
||||
} else {
|
||||
route.currentStep()
|
||||
}
|
||||
// Safely get the street name from the maneuver
|
||||
val streetName = relevantManeuver.name
|
||||
val streetName = relevantStep.name
|
||||
if (shouldAdvance) {
|
||||
maneuverType = relevantManeuver.maneuver.type
|
||||
maneuverType = relevantStep.maneuver.type
|
||||
}
|
||||
val maneuverIconPair = maneuverIcon(maneuverType)
|
||||
routeState = routeState.copy(maneuverType = maneuverIconPair.first)
|
||||
// Construct and return the final StepData object
|
||||
val intersection = currentIntersection(routeState.location)
|
||||
return StepData(
|
||||
streetName,
|
||||
distanceToNextStep,
|
||||
maneuverIconPair.first,
|
||||
maneuverIconPair.second,
|
||||
arrivalTime(),
|
||||
travelLeftDistance()
|
||||
travelLeftDistance(),
|
||||
intersection.lane
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -67,7 +67,7 @@ class ViewModel(private val repository: NavigationRepository) : ViewModel() {
|
||||
MutableLiveData()
|
||||
}
|
||||
|
||||
fun loadRecentPlace(location: Location) {
|
||||
fun loadRecentPlace(location: Location, context: Context) {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
try {
|
||||
val placeBox = boxStore.boxFor(Place::class)
|
||||
@@ -79,12 +79,12 @@ class ViewModel(private val repository: NavigationRepository) : ViewModel() {
|
||||
query.close()
|
||||
for (place in results) {
|
||||
val plLocation = location(place.longitude, place.latitude)
|
||||
//val distance = repository.getRouteDistance(location, plLocation, SearchFilter())
|
||||
//place.distance = distance.toFloat()
|
||||
//if (place.distance == 0F) {
|
||||
recentPlace.postValue(place)
|
||||
val distance = repository.getRouteDistance(location, plLocation, SearchFilter(), context)
|
||||
place.distance = distance.toFloat()
|
||||
if (place.distance > 1F) {
|
||||
recentPlace.postValue(place)
|
||||
return@launch
|
||||
//}
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
|
||||
BIN
common/data/src/main/res/drawable/lanes.png
Normal file
BIN
common/data/src/main/res/drawable/lanes.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.0 KiB |
Reference in New Issue
Block a user