maps: indicate high-traffic segments #82
- Commit
a975ab27957a81e72c467466439d7624c80588fb- Author
- Vayun Mathur <[email protected]>
- When
- 2026-05-11 01:29 UTC (6 months ago)
- Parents
7657357
Diff
maps/src/main/cpp/native-lib.cpp
diff --git a/maps/src/main/cpp/native-lib.cpp b/maps/src/main/cpp/native-lib.cppindex ddaf06476d..19b02d0082 100644--- a/maps/src/main/cpp/native-lib.cpp+++ b/maps/src/main/cpp/native-lib.cpp@@ -171,9 +171,7 @@ inline uint32_t accurate_dist_mm(int32_t lat1_e7, int32_t lon1_e7, int32_t lat2_ inline uint32_t get_edge_time_10ms(int zone_id, uint32_t local_edge_id, uint32_t dist_mm, uint8_t type, uint8_t limit, int mode) { if (mode == DRIVING) { uint8_t traffic_speed = 0;- if (zone_id >= 0 && zone_id < NUM_ZONES) {- traffic_speed = g_traffic_zones[zone_id].get_speed(local_edge_id);- }+ if (zone_id >= 0 && zone_id < NUM_ZONES) traffic_speed = g_traffic_zones[zone_id].get_speed(local_edge_id); uint8_t effective_limit = (traffic_speed > 0) ? traffic_speed : limit; if (effective_limit > 0) { double speed_m_s = (double)effective_limit / 3.6;@@ -344,6 +342,7 @@ jobjectArray reconstruct_path(JNIEnv* env, int mode, const RoutingContext& ctx) struct StepData { uint32_t name_off; uint64_t dist_mm = 0; uint64_t time_10ms = 0; std::vector<double> coords; int maneuver = 0;+ double speed_ratio = 1.0; }; std::vector<StepData> steps; double last_bearing = 0; int total_edges = 0;@@ -369,16 +368,26 @@ jobjectArray reconstruct_path(JNIEnv* env, int mode, const RoutingContext& ctx) } total_edges++;- if (resolved_edge_idx != 0xFFFFFFFF && g_traffic_zones[z_u].get_speed(resolved_edge_idx) > 0) {- traffic_edges++;- }+ uint8_t traffic_speed = 0;+ if (resolved_edge_idx != 0xFFFFFFFF) traffic_speed = g_traffic_zones[z_u].get_speed(resolved_edge_idx);+ if (traffic_speed > 0) traffic_edges++;++ double ratio = 1.0;+ if (traffic_speed > 0 && edge_limit > 0) ratio = (double)traffic_speed / edge_limit; if (edge_dist_mm == 0) edge_dist_mm = accurate_dist_mm(node_u.lat_e7, node_u.lon_e7, node_v.lat_e7, node_v.lon_e7); uint32_t edge_time_10ms = get_edge_time_10ms(z_u, resolved_edge_idx, edge_dist_mm, edge_type, edge_limit, mode); double current_bearing = get_bearing(node_u.lat_e7, node_u.lon_e7, node_v.lat_e7, node_v.lon_e7);- if (steps.empty() || current_name_off != steps.back().name_off) {++ auto get_ratio_cat = [](double r) {+ if (r < 0.5) return 0;+ if (r < 0.9) return 1;+ return 2;+ };++ if (steps.empty() || current_name_off != steps.back().name_off || get_ratio_cat(ratio) != get_ratio_cat(steps.back().speed_ratio)) { int maneuver = steps.empty() ? 0 : get_maneuver(last_bearing, current_bearing);- steps.push_back({current_name_off, 0, 0, {}, maneuver});+ steps.push_back({current_name_off, 0, 0, {}, maneuver, ratio}); steps.back().coords.push_back(node_u.lon_e7 / 1e7); steps.back().coords.push_back(node_u.lat_e7 / 1e7); } steps.back().dist_mm += edge_dist_mm; steps.back().time_10ms += edge_time_10ms;@@ -392,13 +401,13 @@ jobjectArray reconstruct_path(JNIEnv* env, int mode, const RoutingContext& ctx) } jclass stepClass = env->FindClass("com/vayunmathur/maps/util/OfflineRouter$RawStep");- jmethodID stepCtor = env->GetMethodID(stepClass, "<init>", "(ILjava/lang/String;JJ[D)V");+ jmethodID stepCtor = env->GetMethodID(stepClass, "<init>", "(ILjava/lang/String;JJ[DD)V"); jobjectArray res = env->NewObjectArray(steps.size(), stepClass, nullptr); for (size_t i = 0; i < steps.size(); ++i) { const char* name_ptr = (steps[i].name_off < g_road_names_size) ? (g_road_names + steps[i].name_off) : "Unknown Road"; jstring jName = env->NewStringUTF(name_ptr); jdoubleArray jGeom = env->NewDoubleArray(steps[i].coords.size()); env->SetDoubleArrayRegion(jGeom, 0, steps[i].coords.size(), steps[i].coords.data());- jobject stepObj = env->NewObject(stepClass, stepCtor, (jint)steps[i].maneuver, jName, (jlong)steps[i].dist_mm, (jlong)steps[i].time_10ms, jGeom);+ jobject stepObj = env->NewObject(stepClass, stepCtor, (jint)steps[i].maneuver, jName, (jlong)steps[i].dist_mm, (jlong)steps[i].time_10ms, jGeom, (jdouble)steps[i].speed_ratio); env->SetObjectArrayElement(res, i, stepObj); env->DeleteLocalRef(jName); env->DeleteLocalRef(jGeom); env->DeleteLocalRef(stepObj); }@@ -480,6 +489,12 @@ Java_com_vayunmathur_maps_util_OfflineRouter_updateTrafficNative(JNIEnv* env, jo env->ReleaseIntArrayElements(edge_ids, ids_ptr, JNI_ABORT); env->ReleaseByteArrayElements(speeds, speeds_ptr, JNI_ABORT); } +extern "C" JNIEXPORT jdoubleArray JNICALL+Java_com_vayunmathur_maps_util_OfflineRouter_getTrafficSegmentsNative(JNIEnv* env, jobject thiz) {+ jdoubleArray jRes = env->NewDoubleArray(0);+ return jRes;+}+ extern "C" JNIEXPORT void JNICALL Java_com_vayunmathur_maps_util_OfflineRouter_ensureTrafficLoadedNative(JNIEnv* env, jobject thiz, jdouble lat, jdouble lon) { ensure_traffic_loaded(env, thiz, (int32_t)(lat * 1e7), (int32_t)(lon * 1e7));
maps/src/main/java/com/vayunmathur/maps/ui/MyMapLayers.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/ui/MyMapLayers.kt b/maps/src/main/java/com/vayunmathur/maps/ui/MyMapLayers.ktindex 9e1b5a4a90..569f94200e 100644--- a/maps/src/main/java/com/vayunmathur/maps/ui/MyMapLayers.kt+++ b/maps/src/main/java/com/vayunmathur/maps/ui/MyMapLayers.kt@@ -125,9 +125,17 @@ fun MyMapLayers(selectedFeature: SpecificFeature?, route: RouteService.RouteType val features: List<Feature1> = listOf<Feature1>( //Feature1(LineString(route.polyline), JsonObject(emptyMap())) ) + route.step.mapIndexed { idx, it ->+ val color = if (it.travelMode == RouteService.TravelMode.DRIVE) {+ when {+ it.speedRatio < 0.5 -> "#FF0000" // Red+ it.speedRatio < 0.9 -> "#FFFF00" // Yellow+ else -> "#1710F1" // Blue+ }+ } else "#1710F1"+ Feature1( LineString(it.polyline),- JsonObject(mapOf("color" to JsonPrimitive("#1710F1")))+ JsonObject(mapOf("color" to JsonPrimitive(color))) ) } routeSource.setData(GeoJsonData.Features(FeatureCollection(features)))
maps/src/main/java/com/vayunmathur/maps/util/OfflineRouter.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/util/OfflineRouter.kt b/maps/src/main/java/com/vayunmathur/maps/util/OfflineRouter.ktindex e27384db2d..3c50ceabc9 100644--- a/maps/src/main/java/com/vayunmathur/maps/util/OfflineRouter.kt+++ b/maps/src/main/java/com/vayunmathur/maps/util/OfflineRouter.kt@@ -21,6 +21,7 @@ object OfflineRouter { private external fun init(basePath: String): Boolean private external fun findRouteNative(sLat: Double, sLon: Double, eLat: Double, eLon: Double, mode: Int): Array<RawStep> private external fun updateTrafficNative(zoneId: Int, edgeIds: IntArray, speeds: ByteArray)+ private external fun ensureTrafficLoadedNative(lat: Double, lon: Double) private val trafficScope = kotlinx.coroutines.CoroutineScope(Dispatchers.IO) @@ -52,7 +53,8 @@ object OfflineRouter { val roadName: String, val distanceMm: Long, val duration10ms: Long,- val geometry: DoubleArray+ val geometry: DoubleArray,+ val speedRatio: Double ) private var isInitialized = false@@ -118,7 +120,8 @@ object OfflineRouter { staticDuration = (raw.duration10ms / 100.0).seconds, polyline = positions, navInstruction = RouteService.API.NavInstruction(maneuver, instructionText),- travelMode = mode+ travelMode = mode,+ speedRatio = raw.speedRatio ) }
maps/src/main/java/com/vayunmathur/maps/util/RouteService.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/util/RouteService.kt b/maps/src/main/java/com/vayunmathur/maps/util/RouteService.ktindex af937b06f6..872e362528 100644--- a/maps/src/main/java/com/vayunmathur/maps/util/RouteService.kt+++ b/maps/src/main/java/com/vayunmathur/maps/util/RouteService.kt@@ -157,7 +157,8 @@ object RouteService { val polyline: List<Position>, val navInstruction: API.NavInstruction, val travelMode: TravelMode,- val transitDetails: API.TransitDetails? = null+ val transitDetails: API.TransitDetails? = null,+ val speedRatio: Double = 1.0 ) interface RouteType {