Modern Apps git

← Modern-Apps.git

maps: live traffic data #82

Commit
7657357102d156b53b3c67a8e9ccfa5496d302d2
Author
Vayun Mathur <[email protected]>
When
2026-05-11 01:08 UTC (6 months ago)
Parents
c5ee4e0

Diff

library/network/src/main/java/com/vayunmathur/library/network/NetworkClient.kt
diff --git a/library/network/src/main/java/com/vayunmathur/library/network/NetworkClient.kt b/library/network/src/main/java/com/vayunmathur/library/network/NetworkClient.ktindex 2f953bbe48..fd0449709b 100644--- a/library/network/src/main/java/com/vayunmathur/library/network/NetworkClient.kt+++ b/library/network/src/main/java/com/vayunmathur/library/network/NetworkClient.kt@@ -71,6 +71,20 @@ object NetworkClient {         )     } +    suspend fun performRequestBytes(+        url: String,+        method: String = "GET",+        headers: Map<String, *> = emptyMap<String, Any>(),+        body: Any? = null+    ): Pair<Int, ByteArray> {+        val response: HttpResponse = client.request(url) {+            this.method = HttpMethod.parse(method)+            applyHeaders(headers)+            body?.let { setBody(it) }+        }+        return response.status.value to response.body<ByteArray>()+    }+     suspend fun performRequest(         url: String,         method: String = "GET",
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 97f45555a7..ddaf06476d 100644--- a/maps/src/main/cpp/native-lib.cpp+++ b/maps/src/main/cpp/native-lib.cpp@@ -6,6 +6,7 @@ #include <string> #include <cmath> #include <algorithm>+#include <mutex> #include <android/log.h>  #include "scratchpad.h"@@ -45,8 +46,7 @@ struct Edge { const int NUM_ZONES = 64; NodeMaster* g_node_zones[NUM_ZONES] = {nullptr}; Edge* g_edge_zones[NUM_ZONES] = {nullptr};-size_t g_node_zone_sizes[NUM_ZONES] = {0};-size_t g_edge_zone_sizes[NUM_ZONES] = {0};+size_t g_edge_count_in_zone[NUM_ZONES] = {0};  uint32_t g_zone_offsets[NUM_ZONES + 1] = {0}; char* g_road_names = nullptr;@@ -61,9 +61,57 @@ const double DEG_TO_RAD = M_PI / 180.0; uint32_t g_lon_to_mm_scale[4096];  static RoutingScratchpad g_scratchpad;+static TrafficPageTable g_traffic_zones[NUM_ZONES]; static RadixHeap g_heap; // Only one heap needed for unidirectional -uint32_t g_zone_max_ids[64];+static std::vector<uint32_t> g_requested_squares; // Packed (lat_idx << 16 | lon_idx)+static std::mutex g_traffic_mutex;++uint64_t latlng_to_spatial(double lat, double lon) {+    double x = (lon + 180.0) / 360.0;+    double y = (lat + 90.0) / 180.0;+    auto ix = (uint32_t)(x * 4294967295.0), iy = (uint32_t)(y * 4294967295.0);+    uint64_t res = 0;+    for (int i = 0; i < 32; i++) {+        res |= ((uint64_t)((ix >> i) & 1) << (2 * i));+        res |= ((uint64_t)((iy >> i) & 1) << (2 * i + 1));+    }+    return res;+}++bool is_square_requested(int32_t lat_idx, int32_t lon_idx) {+    uint32_t packed = ((uint32_t)(lat_idx + 360) << 16) | (uint32_t)(lon_idx + 720);+    std::lock_guard<std::mutex> lock(g_traffic_mutex);+    return std::find(g_requested_squares.begin(), g_requested_squares.end(), packed) != g_requested_squares.end();+}++void mark_square_requested(int32_t lat_idx, int32_t lon_idx) {+    uint32_t packed = ((uint32_t)(lat_idx + 360) << 16) | (uint32_t)(lon_idx + 720);+    std::lock_guard<std::mutex> lock(g_traffic_mutex);+    g_requested_squares.push_back(packed);+}++void ensure_traffic_loaded(JNIEnv* env, jobject thiz, int32_t lat_e7, int32_t lon_e7) {+    int32_t lat_idx = (int32_t)floor(lat_e7 * 1e-7);+    int32_t lon_idx = (int32_t)floor(lon_e7 * 1e-7);++    if (is_square_requested(lat_idx, lon_idx)) return;+    mark_square_requested(lat_idx, lon_idx);++    uint64_t spatial = latlng_to_spatial(lat_e7 * 1e-7, lon_e7 * 1e-7);+    int zone_id = (int)((spatial >> 58) & 0x3F);++    double min_lat = (double)lat_idx;+    double min_lon = (double)lon_idx;+    double max_lat = min_lat + 1.0;+    double max_lon = min_lon + 1.0;++    jclass clazz = env->GetObjectClass(thiz);+    jmethodID method = env->GetMethodID(clazz, "fetchTrafficData", "(DDDDI)V");+    if (method) {+        env->CallVoidMethod(thiz, method, min_lat, min_lon, max_lat, max_lon, zone_id);+    }+}  // --- ROAD PERMISSIONS --- @@ -76,7 +124,7 @@ inline bool is_mode_allowed(uint8_t road_type, int mode) {  inline int get_zone_for_id(uint32_t global_id) {     for (int i = 0; i < 64; ++i) {-        if (global_id < g_zone_max_ids[i]) return i;+        if (global_id < g_zone_offsets[i+1]) return i;     }     return -1; }@@ -99,14 +147,10 @@ inline const NodeMaster& get_node(uint32_t global_id) { __attribute__((always_inline)) inline uint32_t fast_dist_mm(int32_t lat1_e7, int32_t lon1_e7, int32_t lat2_e7, int32_t lon2_e7) {     int64_t dlat = std::abs((int64_t)lat1_e7 - lat2_e7);     int64_t dlon = std::abs((int64_t)lon1_e7 - lon2_e7);--    // Accurate mm scaling for Lat/Lon     int64_t dy_mm = (dlat * 111319) / 10;-     auto scale_idx = (uint32_t)(((lat1_e7 + lat2_e7) / 2 >> 19) + 2048);     uint32_t scale = g_lon_to_mm_scale[scale_idx & 4095];     int64_t dx_mm = (dlon * scale) >> 10;-     uint64_t max_v = (dx_mm > dy_mm) ? dx_mm : dy_mm;     uint64_t min_v = (dx_mm > dy_mm) ? dy_mm : dx_mm;     return (uint32_t)(max_v - (max_v >> 5) + (min_v >> 1) - (min_v >> 3));@@ -118,31 +162,28 @@ inline uint32_t accurate_dist_mm(int32_t lat1_e7, int32_t lon1_e7, int32_t lat2_     double phi2 = (lat2_e7 * 1e-7) * DEG_TO_RAD;     double delta_phi = (lat2_e7 - lat1_e7) * 1e-7 * DEG_TO_RAD;     double delta_lambda = (lon2_e7 - lon1_e7) * 1e-7 * DEG_TO_RAD;-     double s_dphi = sin(delta_phi / 2.0);     double s_dlamb = sin(delta_lambda / 2.0);     double a = s_dphi * s_dphi + cos(phi1) * cos(phi2) * s_dlamb * s_dlamb;     return (uint32_t)(R * 2.0 * atan2(sqrt(a), sqrt(1.0 - a))); } -// Updated to use Edge structure directly for speed limit access with road-type fallback-inline uint32_t get_edge_time_10ms(uint32_t dist_mm, uint8_t type, uint8_t limit, int mode) {-    if (mode == DRIVING && limit > 0) {-        // Use the explicit speed limit for driving mode if available-        double speed_m_s = (double)limit / 3.6;-        return (uint32_t)((double)dist_mm / (speed_m_s * 10.0));+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);+        }+        uint8_t effective_limit = (traffic_speed > 0) ? traffic_speed : limit;+        if (effective_limit > 0) {+            double speed_m_s = (double)effective_limit / 3.6;+            return (uint32_t)((double)dist_mm / (speed_m_s * 10.0));+        }     }--    // Fallback: If not driving, or if speed_limit is 0, use the road type based multipliers     uint64_t multiplier = g_edge_time_multipliers[mode & 0x3][type & 0xF];     return (uint32_t)(((uint64_t)dist_mm * multiplier) >> 32); } -// Overload for convenience with Edge struct-inline uint32_t get_edge_time_10ms_struct(uint32_t dist_mm, const Edge& edge, int mode) {-    return get_edge_time_10ms(dist_mm, edge.type, edge.speed_limit, mode);-}- inline uint32_t heuristic_time_10ms(int32_t lat1, int32_t lon1, int32_t lat2, int32_t lon2, int mode) {     uint32_t dist_mm = fast_dist_mm(lat1, lon1, lat2, lon2);     uint64_t scaled_time = (uint64_t)dist_mm * g_time_scale_fixed[mode & 0x3];@@ -172,18 +213,6 @@ int get_maneuver(double prev_bearing, double next_bearing) {     return 6; } -uint64_t latlng_to_spatial(double lat, double lon) {-    double x = (lon + 180.0) / 360.0;-    double y = (lat + 90.0) / 180.0;-    auto ix = (uint32_t)(x * 4294967295.0), iy = (uint32_t)(y * 4294967295.0);-    uint64_t res = 0;-    for (int i = 0; i < 32; i++) {-        res |= ((uint64_t)((ix >> i) & 1) << (2 * i));-        res |= ((uint64_t)((iy >> i) & 1) << (2 * i + 1));-    }-    return res;-}- // --- CORE ROUTING ---  struct SnappedEdge {@@ -209,7 +238,7 @@ Projection get_projection(int32_t px, int32_t py, int32_t x1, int32_t y1, int32_     double t = (mag_sq == 0) ? 0 : ((double)(px - x1) * dx + (double)(py - y1) * dy) / mag_sq;     t = std::max(0.0, std::min(1.0, t));     int32_t proj_lat = (int32_t)(x1 + t * dx), proj_lon = (int32_t)(y1 + t * dy);-    return {proj_lat, lon_e7 : proj_lon, dist_mm : fast_dist_mm(px, py, proj_lat, proj_lon)};+    return {proj_lat, proj_lon, fast_dist_mm(px, py, proj_lat, proj_lon)}; }  SnappedEdge find_nearest_edge(double lat, double lon, int mode) {@@ -217,44 +246,35 @@ SnappedEdge find_nearest_edge(double lat, double lon, int mode) {     int32_t pLat = lat * 1e7, pLon = lon * 1e7;     SnappedEdge best = {0xFFFFFFFF, 0xFFFFFFFF, pLat, pLon, 0, 0, 0, 0, 0xFFFFFFFF};     uint32_t minSnapDist = 0xFFFFFFFF;-     int target_zone = (int)((target_spatial >> 58) & 0x3F);      for (int z = std::max(0, target_zone - 2); z <= std::min(NUM_ZONES - 1, target_zone + 2); ++z) {         if (!is_zone_mapped(z)) continue;         uint32_t zone_node_count = g_zone_offsets[z + 1] - g_zone_offsets[z];-         uint32_t low = 0, high = zone_node_count - 1, local_center = 0;         while (low <= high) {             uint32_t mid = low + (high - low) / 2;             if (g_node_zones[z][mid].spatial_id < target_spatial) low = mid + 1;             else { local_center = mid; if (mid == 0) break; high = mid - 1; }         }-         int window = 4000;         for (int i = std::max(0, (int)local_center - window); i <= std::min((int)zone_node_count - 1, (int)local_center + window); ++i) {             uint32_t u_global = g_zone_offsets[z] + i;             const auto& node_u = g_node_zones[z][i];-            uint32_t s_ptr = node_u.edge_ptr, e_ptr = g_node_zones[z][i + 1].edge_ptr;--            for (uint32_t j = s_ptr; j < e_ptr; ++j) {+            uint32_t e_ptr = (i + 1 < zone_node_count) ? g_node_zones[z][i+1].edge_ptr : (uint32_t)g_edge_count_in_zone[z];+            for (uint32_t j = node_u.edge_ptr; j < e_ptr; ++j) {                 Edge& e = g_edge_zones[z][j];                 if (!is_mode_allowed(e.type, mode)) continue;-                 int zone_v = get_zone_for_id(e.target);                 if (!is_zone_mapped(zone_v)) continue;-                 const auto& node_v = g_node_zones[zone_v][e.target - g_zone_offsets[zone_v]];                 Projection p = get_projection(pLat, pLon, node_u.lat_e7, node_u.lon_e7, node_v.lat_e7, node_v.lon_e7);-                 if (p.dist_mm < minSnapDist) {                     minSnapDist = p.dist_mm;                     best.nodeA = u_global; best.nodeB = e.target; best.proj_lat = p.lat_e7; best.proj_lon = p.lon_e7;                     best.distA_mm = fast_dist_mm(p.lat_e7, p.lon_e7, node_u.lat_e7, node_u.lon_e7);                     best.distB_mm = fast_dist_mm(p.lat_e7, p.lon_e7, node_v.lat_e7, node_v.lon_e7);-                    best.type = e.type;-                    best.speed_limit = e.speed_limit;-                    best.name_offset = e.name_offset;+                    best.type = e.type; best.speed_limit = e.speed_limit; best.name_offset = e.name_offset;                 }             }         }@@ -262,67 +282,48 @@ SnappedEdge find_nearest_edge(double lat, double lon, int mode) {     return best; } -bool prepare_routing(double sLat, double sLon, double eLat, double eLon, int mode, RoutingContext& ctx) {-    g_scratchpad.reset();-    g_heap.clear();-    ctx.iterations = 0;-    ctx.target_node = 0xFFFFFFFF;-+bool prepare_routing(JNIEnv* env, jobject thiz, double sLat, double sLon, double eLat, double eLon, int mode, RoutingContext& ctx) {+    g_scratchpad.reset(); g_heap.clear();+    ctx.iterations = 0; ctx.target_node = 0xFFFFFFFF;     ctx.start = find_nearest_edge(sLat, sLon, mode);     ctx.end = find_nearest_edge(eLat, eLon, mode);-     if (ctx.start.nodeA == 0xFFFFFFFF || ctx.end.nodeA == 0xFFFFFFFF) return false;-+    if (mode == DRIVING) ensure_traffic_loaded(env, thiz, ctx.start.proj_lat, ctx.start.proj_lon);     auto push = [&](uint32_t node, uint32_t travel_dist_mm) {-        // Calculate g (cost from start) using snapped edge speed limit or type fallback-        uint32_t g = get_edge_time_10ms(travel_dist_mm, ctx.start.type, ctx.start.speed_limit, mode);--        auto& entry = g_scratchpad[node];-        entry.g_fwd = g;-+        uint32_t g = get_edge_time_10ms(-1, 0, travel_dist_mm, ctx.start.type, ctx.start.speed_limit, mode);+        auto& entry = g_scratchpad[node]; entry.g_fwd = g;         const auto& n_data = get_node(node);         uint32_t h = heuristic_time_10ms(n_data.lat_e7, n_data.lon_e7, ctx.end.proj_lat, ctx.end.proj_lon, mode);         g_heap.push(g + h, node);     };--    // Inject start points (A and B of the snapped edge)-    push(ctx.start.nodeA, ctx.start.distA_mm);-    push(ctx.start.nodeB, ctx.start.distB_mm);-+    push(ctx.start.nodeA, ctx.start.distA_mm); push(ctx.start.nodeB, ctx.start.distB_mm);     return true; } -void perform_search_loop(int mode, RoutingContext& ctx) {-    while (!g_heap.empty()) {+void perform_search_loop(JNIEnv* env, jobject thiz, int mode, RoutingContext& ctx) {+    while (!g_heap.empty() && ctx.iterations < 50000000) {         ctx.iterations++;         uint32_t u = g_heap.pop();--        if (u == ctx.end.nodeA || u == ctx.end.nodeB) {-            ctx.target_node = u;-            break;-        }-+        if (u == ctx.end.nodeA || u == ctx.end.nodeB) { ctx.target_node = u; break; }         auto& entry_u = g_scratchpad[u];         uint32_t u_g = entry_u.g_fwd;-         int zone_u = get_zone_for_id(u);-        uint32_t s = g_node_zones[zone_u][u - g_zone_offsets[zone_u]].edge_ptr;-        uint32_t e_ptr = g_node_zones[zone_u][u - g_zone_offsets[zone_u] + 1].edge_ptr;-+        if (zone_u < 0 || !g_node_zones[zone_u]) continue;+        const auto& n_u = g_node_zones[zone_u][u - g_zone_offsets[zone_u]];+        if (mode == DRIVING) ensure_traffic_loaded(env, thiz, n_u.lat_e7, n_u.lon_e7);+        uint32_t s = n_u.edge_ptr;+        uint32_t node_idx = u - g_zone_offsets[zone_u];+        uint32_t node_count = g_zone_offsets[zone_u + 1] - g_zone_offsets[zone_u];+        uint32_t e_ptr = (node_idx + 1 < node_count) ? g_node_zones[zone_u][node_idx + 1].edge_ptr : (uint32_t)g_edge_count_in_zone[zone_u];         for (uint32_t i = s; i < e_ptr; ++i) {             Edge& edge = g_edge_zones[zone_u][i];             if (!is_mode_allowed(edge.type, mode)) continue;--            // Updated cost calculation using the specific edge speed limit or fallback-            uint32_t travel = get_edge_time_10ms_struct(edge.dist_mm, edge, mode);+            uint32_t travel = get_edge_time_10ms(zone_u, i, edge.dist_mm, edge.type, edge.speed_limit, mode);             uint32_t v = edge.target;-             uint32_t new_g = u_g + travel;             auto& entry_v = g_scratchpad[v];-             if (new_g < entry_v.g_fwd) {-                entry_v.g_fwd = new_g;-                entry_v.p_fwd = u;+                entry_v.g_fwd = new_g; entry_v.p_fwd = u;                 const auto& n_v = get_node(v);                 uint32_t h = heuristic_time_10ms(n_v.lat_e7, n_v.lon_e7, ctx.end.proj_lat, ctx.end.proj_lon, mode);                 g_heap.push(new_g + h, v);@@ -335,75 +336,67 @@ jobjectArray reconstruct_path(JNIEnv* env, int mode, const RoutingContext& ctx)     std::vector<uint32_t> path;     uint32_t curr = ctx.target_node;     uint32_t safety = 0;-    while (curr != 0xFFFFFFFF && safety < 100000) {-        path.push_back(curr);-        curr = g_scratchpad[curr].p_fwd;-        safety++;+    while (curr != 0xFFFFFFFF && safety < 1000000) {+        path.push_back(curr); curr = g_scratchpad[curr].p_fwd; safety++;     }     std::reverse(path.begin(), path.end());-     if (path.empty()) return nullptr;-     struct StepData {-        uint32_t name_off;-        uint64_t dist_mm = 0;-        uint64_t time_10ms = 0;-        std::vector<double> coords;-        int maneuver = 0;+        uint32_t name_off; uint64_t dist_mm = 0; uint64_t time_10ms = 0;+        std::vector<double> coords; int maneuver = 0;     };--    std::vector<StepData> steps;-    double last_bearing = 0;+    std::vector<StepData> steps; double last_bearing = 0;+    int total_edges = 0;+    int traffic_edges = 0;      for (size_t i = 0; i < path.size() - 1; ++i) {         uint32_t u = path[i], v = path[i+1];-        int z_u = get_zone_for_id(u);+        int z_u = get_zone_for_id(u); if (z_u < 0) continue;         const auto& node_u = g_node_zones[z_u][u - g_zone_offsets[z_u]];         const auto& node_v = get_node(v);--        uint32_t current_name_off = 0xFFFFFFFF;-        uint8_t edge_type = 7;-        uint8_t edge_limit = 0;-        uint32_t edge_dist_mm = 0;--        uint32_t s = node_u.edge_ptr, e = g_node_zones[z_u][u - g_zone_offsets[z_u] + 1].edge_ptr;-        for (uint32_t k = s; k < e; ++k) {+        uint32_t current_name_off = 0xFFFFFFFF; uint8_t edge_type = 7, edge_limit = 0; uint32_t edge_dist_mm = 0;+        uint32_t s = node_u.edge_ptr;+        uint32_t node_idx = u - g_zone_offsets[z_u];+        uint32_t node_count = g_zone_offsets[z_u + 1] - g_zone_offsets[z_u];+        uint32_t e_ptr = (node_idx + 1 < node_count) ? g_node_zones[z_u][node_idx + 1].edge_ptr : (uint32_t)g_edge_count_in_zone[z_u];+        uint32_t resolved_edge_idx = 0xFFFFFFFF;+        for (uint32_t k = s; k < e_ptr; ++k) {             if (g_edge_zones[z_u][k].target == v) {-                edge_type = g_edge_zones[z_u][k].type;-                edge_limit = g_edge_zones[z_u][k].speed_limit;-                current_name_off = g_edge_zones[z_u][k].name_offset;-                edge_dist_mm = g_edge_zones[z_u][k].dist_mm;-                break;+                edge_type = g_edge_zones[z_u][k].type; edge_limit = g_edge_zones[z_u][k].speed_limit;+                current_name_off = g_edge_zones[z_u][k].name_offset; edge_dist_mm = g_edge_zones[z_u][k].dist_mm;+                resolved_edge_idx = k; break;             }         } -        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);+        total_edges++;+        if (resolved_edge_idx != 0xFFFFFFFF && g_traffic_zones[z_u].get_speed(resolved_edge_idx) > 0) {+            traffic_edges++;+        } -        uint32_t edge_time_10ms = get_edge_time_10ms(edge_dist_mm, edge_type, edge_limit, mode);+        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) {             int maneuver = steps.empty() ? 0 : get_maneuver(last_bearing, current_bearing);             steps.push_back({current_name_off, 0, 0, {}, maneuver});-            steps.back().coords.push_back(node_u.lon_e7 / 1e7);-            steps.back().coords.push_back(node_u.lat_e7 / 1e7);+            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;-        steps.back().coords.push_back(node_v.lon_e7 / 1e7);-        steps.back().coords.push_back(node_v.lat_e7 / 1e7);+        steps.back().dist_mm += edge_dist_mm; steps.back().time_10ms += edge_time_10ms;+        steps.back().coords.push_back(node_v.lon_e7 / 1e7); steps.back().coords.push_back(node_v.lat_e7 / 1e7);         last_bearing = current_bearing;     } +    if (total_edges > 0) {+        double percent = (double)traffic_edges / total_edges * 100.0;+        LOGD("Path traffic stats: %d/%d edges have traffic data (%.2f%%)", traffic_edges, total_edges, percent);+    }+     jclass stepClass = env->FindClass("com/vayunmathur/maps/util/OfflineRouter$RawStep");     jmethodID stepCtor = env->GetMethodID(stepClass, "<init>", "(ILjava/lang/String;JJ[D)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());+        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);         env->SetObjectArrayElement(res, i, stepObj);@@ -418,52 +411,45 @@ extern "C" JNIEXPORT jboolean JNICALL Java_com_vayunmathur_maps_util_OfflineRouter_init(JNIEnv* env, jobject thiz, jstring base_path) {     const char* path_raw = env->GetStringUTFChars(base_path, nullptr); std::string base(path_raw);     if (!base.empty() && base.back() != '/') base += "/";-     auto m_file = [&](const std::string& p, size_t& s) -> void* {-        int fd = open(p.c_str(), O_RDONLY); if (fd < 0) return nullptr;-        s = lseek(fd, 0, SEEK_END);-        void* a = mmap(nullptr, s, PROT_READ, MAP_SHARED, fd, 0); close(fd);+        s = 0; int fd = open(p.c_str(), O_RDONLY); if (fd < 0) return nullptr;+        s = lseek(fd, 0, SEEK_END); void* a = mmap(nullptr, s, PROT_READ, MAP_SHARED, fd, 0); close(fd);         return (a == MAP_FAILED) ? nullptr : a;     };-     size_t s_meta; uint32_t* meta = (uint32_t*)m_file(base + "metadata.bin", s_meta);     if (!meta) { env->ReleaseStringUTFChars(base_path, path_raw); return false; }-+    bool has_edge_metadata = (s_meta >= NUM_ZONES * 2 * sizeof(uint32_t));     g_zone_offsets[0] = 0;-    for (int i = 0; i < NUM_ZONES; ++i) g_zone_offsets[i+1] = g_zone_offsets[i] + meta[i];-+    if (has_edge_metadata) {+        for (int i = 0; i < NUM_ZONES; ++i) g_zone_offsets[i+1] = g_zone_offsets[i] + meta[i*2];+    } else {+        for (int i = 0; i < NUM_ZONES; ++i) g_zone_offsets[i+1] = g_zone_offsets[i] + meta[i];+    }     for (int i = 0; i < NUM_ZONES; ++i) {-        uint32_t count = meta[i]; if (count == 0) continue;+        uint32_t node_count = has_edge_metadata ? meta[i*2] : meta[i];+        if (node_count == 0) continue;         size_t s_n, s_e;         g_node_zones[i] = (NodeMaster*)m_file(base + "nodes_zone_" + std::to_string(i) + ".bin", s_n);         g_edge_zones[i] = (Edge*)m_file(base + "edges_zone_" + std::to_string(i) + ".bin", s_e);-        g_zone_max_ids[i] = g_zone_offsets[i+1];+        g_edge_count_in_zone[i] = s_e / sizeof(Edge);     }     munmap(meta, s_meta);-     size_t s_r; g_road_names = (char*)m_file(base + "road_names.bin", s_r); g_road_names_size = s_r;-     for (int i = 0; i < 4096; ++i) {         double lat_deg = ((double)((int64_t)(i - 2048) << 19)) / 1e7;         g_lon_to_mm_scale[i] = (uint32_t)((111139000.0 / 1e7) * cos(lat_deg * DEG_TO_RAD) * 1024.0);     }-     auto calc_scale = [](double speed_m_s) { return (uint64_t)((100.0 / (speed_m_s * 1000.0)) * 4294967296.0); };-    g_time_scale_fixed[WALK] = calc_scale(WALK_SPEED_M_S);-    g_time_scale_fixed[BICYCLE] = calc_scale(BICYCLE_SPEED_M_S);+    g_time_scale_fixed[WALK] = calc_scale(WALK_SPEED_M_S); g_time_scale_fixed[BICYCLE] = calc_scale(BICYCLE_SPEED_M_S);     g_time_scale_fixed[DRIVING] = calc_scale(105.0 / 3.6);-     for (int m = 0; m < 4; ++m) {         for (int r = 0; r < 16; ++r) {             double speed_m_s = 1.0;             if (m == DRIVING) {                 switch(r) {-                    case MOTORWAY: speed_m_s = 105.0/3.6; break;-                    case TRUNK: speed_m_s = 85.0/3.6; break;-                    case PRIMARY: speed_m_s = 65.0/3.6; break;-                    case SECONDARY: speed_m_s = 55.0/3.6; break;-                    case TERTIARY: speed_m_s = 45.0/3.6; break;-                    default: speed_m_s = 30.0/3.6;+                    case MOTORWAY: speed_m_s = 105.0/3.6; break; case TRUNK: speed_m_s = 85.0/3.6; break;+                    case PRIMARY: speed_m_s = 65.0/3.6; break; case SECONDARY: speed_m_s = 55.0/3.6; break;+                    case TERTIARY: speed_m_s = 45.0/3.6; break; default: speed_m_s = 30.0/3.6;                 }             } else { speed_m_s = (m == BICYCLE) ? BICYCLE_SPEED_M_S : WALK_SPEED_M_S; }             g_edge_time_multipliers[m][r] = (uint64_t)((100.0 / (speed_m_s * 1000.0)) * 4294967296.0);@@ -474,9 +460,27 @@ Java_com_vayunmathur_maps_util_OfflineRouter_init(JNIEnv* env, jobject thiz, jst  extern "C" JNIEXPORT jobjectArray JNICALL Java_com_vayunmathur_maps_util_OfflineRouter_findRouteNative(JNIEnv* env, jobject thiz, jdouble sLat, jdouble sLon, jdouble eLat, jdouble eLon, jint mode) {-    RoutingContext ctx; if (!prepare_routing(sLat, sLon, eLat, eLon, mode, ctx)) return nullptr;-    perform_search_loop(mode, ctx);+    RoutingContext ctx; if (!prepare_routing(env, thiz, sLat, sLon, eLat, eLon, mode, ctx)) return nullptr;+    perform_search_loop(env, thiz, mode, ctx);     if (ctx.target_node == 0xFFFFFFFF) return nullptr;-    LOGD("Route found. Iterations: %d", ctx.iterations);     return reconstruct_path(env, mode, ctx);-}\ No newline at end of file+}++extern "C" JNIEXPORT void JNICALL+Java_com_vayunmathur_maps_util_OfflineRouter_updateTrafficNative(JNIEnv* env, jobject thiz, jint zone_id, jintArray edge_ids, jbyteArray speeds) {+    jsize len = env->GetArrayLength(edge_ids); jint* ids_ptr = env->GetIntArrayElements(edge_ids, nullptr);+    jbyte* speeds_ptr = env->GetByteArrayElements(speeds, nullptr);+    if (zone_id >= 0 && zone_id < NUM_ZONES) {+        std::lock_guard<std::mutex> lock(g_traffic_mutex);+        for (jsize i = 0; i < len; i++) {+            uint32_t local_id = (uint32_t)ids_ptr[i]; uint8_t speed = (uint8_t)speeds_ptr[i];+            g_traffic_zones[zone_id].set_speed(local_id, speed);+        }+    }+    env->ReleaseIntArrayElements(edge_ids, ids_ptr, JNI_ABORT); env->ReleaseByteArrayElements(speeds, speeds_ptr, JNI_ABORT);+}++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/cpp/scratchpad.h
diff --git a/maps/src/main/cpp/scratchpad.h b/maps/src/main/cpp/scratchpad.hindex 3ef2e67b07..59f699f36f 100644--- a/maps/src/main/cpp/scratchpad.h+++ b/maps/src/main/cpp/scratchpad.h@@ -12,14 +12,11 @@  /**  * @brief Two-Level Page Table Scratchpad.- * Designed to handle 2.6 Billion+ node IDs with ZERO collisions.- * Uses a directory of pages to keep RAM usage low by only allocating- * memory for the geographic area explored during the search.  */ class RoutingScratchpad { public:     struct Entry {-        uint32_t node_id; // Explicitly kept to match your existing API+        uint32_t node_id;         uint32_t g_fwd;         uint32_t g_bwd;         uint32_t p_fwd;@@ -27,9 +24,6 @@ public:     };  private:-    // Page bits 14 = 16,384 entries per page.-    // Directory size = 2^32 / 2^14 = 262,144 pointers.-    // Directory RAM = ~2MB.     static constexpr uint32_t PAGE_BITS = 14;     static constexpr uint32_t PAGE_SIZE = (1 << PAGE_BITS);     static constexpr uint32_t PAGE_MASK = (PAGE_SIZE - 1);@@ -40,7 +34,6 @@ private:  public:     RoutingScratchpad() {-        // Allocate the directory (the top level of the page table)         m_directory = (Entry**)calloc(DIR_SIZE, sizeof(Entry*));         m_active_pages.reserve(1024);     }@@ -50,9 +43,6 @@ public:         if (m_directory) free(m_directory);     } -    /**-     * @brief Frees all allocated pages to prevent OOM between routes.-     */     void cleanup_pages() {         for (uint32_t page_idx : m_active_pages) {             if (m_directory[page_idx]) {@@ -63,50 +53,83 @@ public:         m_active_pages.clear();     } -    /**-     * @brief Clears the scratchpad for a new search.-     */     inline void reset() {         cleanup_pages();     } -    /**-     * @brief Direct-mapped access to a node's data.-     * Zero collisions, O(1) complexity.-     */     inline Entry& get_entry(uint32_t node_id) {         uint32_t dir_idx = node_id >> PAGE_BITS;         uint32_t page_offset = node_id & PAGE_MASK; -        // Level 1: Check if the page exists         if (__builtin_expect(m_directory[dir_idx] == nullptr, 0)) {-            // Level 2: Lazy allocation of the page             Entry* new_page = (Entry*)malloc(PAGE_SIZE * sizeof(Entry));--            // Initialize the new page with 0xFF (Infinity/Null)             memset(new_page, 0xFF, PAGE_SIZE * sizeof(Entry));-             m_directory[dir_idx] = new_page;             m_active_pages.push_back(dir_idx);         }          Entry& e = m_directory[dir_idx][page_offset];--        // Ensure the node_id is set for the first time it's accessed-        // to stay compatible with your existing reconstruction logic.         if (__builtin_expect(e.node_id == 0xFFFFFFFF, 0)) {             e.node_id = node_id;         }-         return e;     } -    /**-     * @brief API-compatible operator overload.-     */     inline Entry& operator[](uint32_t node_id) {         return get_entry(node_id);     } }; -#endif\ No newline at end of file+/**+ * @brief Simple Page Table for Traffic Speeds per zone.+ */+class TrafficPageTable {+private:+    static constexpr uint32_t PAGE_BITS = 14;+    static constexpr uint32_t PAGE_SIZE = (1 << PAGE_BITS);+    static constexpr uint32_t PAGE_MASK = (PAGE_SIZE - 1);+    static constexpr uint32_t DIR_SIZE = (1ULL << 32) >> PAGE_BITS; // Support full 32-bit ID space++    uint8_t** m_directory;+    std::vector<uint32_t> m_active_pages;++public:+    TrafficPageTable() {+        m_directory = (uint8_t**)calloc(DIR_SIZE, sizeof(uint8_t*));+    }++    ~TrafficPageTable() {+        clear();+        if (m_directory) free(m_directory);+    }++    inline void set_speed(uint32_t local_edge_id, uint8_t speed_kph) {+        uint32_t dir_idx = local_edge_id >> PAGE_BITS;+        uint32_t page_offset = local_edge_id & PAGE_MASK;++        if (__builtin_expect(m_directory[dir_idx] == nullptr, 0)) {+            uint8_t* new_page = (uint8_t*)malloc(PAGE_SIZE * sizeof(uint8_t));+            memset(new_page, 0, PAGE_SIZE * sizeof(uint8_t));+            m_directory[dir_idx] = new_page;+            m_active_pages.push_back(dir_idx);+        }+        m_directory[dir_idx][page_offset] = speed_kph;+    }++    inline uint8_t get_speed(uint32_t local_edge_id) const {+        uint32_t dir_idx = local_edge_id >> PAGE_BITS;+        uint32_t page_offset = local_edge_id & PAGE_MASK;+        if (m_directory[dir_idx] == nullptr) return 0;+        return m_directory[dir_idx][page_offset];+    }++    void clear() {+        for (uint32_t page_idx : m_active_pages) {+            if (m_directory[page_idx]) free(m_directory[page_idx]);+            m_directory[page_idx] = nullptr;+        }+        m_active_pages.clear();+    }+};++#endif
maps/src/main/java/com/vayunmathur/maps/data/OpeningHours.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/data/OpeningHours.kt b/maps/src/main/java/com/vayunmathur/maps/data/OpeningHours.ktindex 7d25621415..71946c07df 100644--- a/maps/src/main/java/com/vayunmathur/maps/data/OpeningHours.kt+++ b/maps/src/main/java/com/vayunmathur/maps/data/OpeningHours.kt@@ -13,7 +13,7 @@ import kotlinx.datetime.toLocalDateTime import kotlinx.serialization.Serializable  @Serializable-class OpeningHours(rawString: String) {+class OpeningHours(val rawString: String) {      private val rules: List<OpeningRule> = parse(rawString) 
maps/src/main/java/com/vayunmathur/maps/ui/MapPage.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/ui/MapPage.kt b/maps/src/main/java/com/vayunmathur/maps/ui/MapPage.ktindex 779525ead9..ede9188911 100644--- a/maps/src/main/java/com/vayunmathur/maps/ui/MapPage.kt+++ b/maps/src/main/java/com/vayunmathur/maps/ui/MapPage.kt@@ -163,6 +163,7 @@ fun MapPage(backStack: NavBackStack<Route>, viewModel: SelectedFeatureViewModel,      // --- UI & BOTTOM SHEET STATE ---     var allowProgrammaticHide by retain { mutableStateOf(false) }+     val scaffoldState = rememberBottomSheetScaffoldState(         rememberStandardBottomSheetState(SheetValue.Hidden, {             it != SheetValue.Hidden || allowProgrammaticHide@@ -252,7 +253,7 @@ fun MapPage(backStack: NavBackStack<Route>, viewModel: SelectedFeatureViewModel,                     if(camera.projection != null) {                         selectedFeature?.let { it as? SpecificFeature.RoutableFeature }?.let {                             Icon(-                                painterResource(R.drawable.location_on_24px),+                                painterResource(MapsR.drawable.location_on_24px),                                 null,                                 Modifier.size(48.dp).graphicsLayer {                                     val offset =
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 3aa4baa9fe..e27384db2d 100644--- a/maps/src/main/java/com/vayunmathur/maps/util/OfflineRouter.kt+++ b/maps/src/main/java/com/vayunmathur/maps/util/OfflineRouter.kt@@ -1,11 +1,16 @@ package com.vayunmathur.maps.util import android.content.Context+import android.util.Log import androidx.annotation.Keep+import com.vayunmathur.library.network.NetworkClient import com.vayunmathur.maps.R import com.vayunmathur.maps.data.SpecificFeature import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import org.maplibre.spatialk.geojson.Position+import java.nio.ByteBuffer+import java.nio.ByteOrder import kotlin.time.Duration.Companion.seconds  object OfflineRouter {@@ -15,21 +20,43 @@ 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 val trafficScope = kotlinx.coroutines.CoroutineScope(Dispatchers.IO)++    @Keep+    private fun fetchTrafficData(minLat: Double, minLon: Double, maxLat: Double, maxLon: Double, zoneId: Int) {+        Log.d("TRAFFIC_DATA", "fetchTrafficData for bbox ($minLat,$minLon)-($maxLat,$maxLon) zone=$zoneId")+        trafficScope.launch {+            try {+                val (status, bytes) = NetworkClient.performRequestBytes(+                    url = "https://api.vayunmathur.com/maps/traffic?min_lat=$minLat&min_lon=$minLon&max_lat=$maxLat&max_lon=$maxLon"+                )+                if (status == 200 && bytes.size >= 5) {+                    val n = bytes.size / 5+                    val edgeIds = IntArray(n)+                    val buffer = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN)+                    for (i in 0 until n) edgeIds[i] = buffer.int+                    val speeds = ByteArray(n)+                    buffer.get(speeds)+                    updateTrafficNative(zoneId, edgeIds, speeds)+                }+            } catch (e: Exception) {+                e.printStackTrace()+            }+        }+    } -    // Simple container for JNI data transfer     class RawStep @Keep constructor(         val maneuverId: Int,         val roadName: String,         val distanceMm: Long,         val duration10ms: Long,-        val geometry: DoubleArray // [lon0, lat0, lon1, lat1...]+        val geometry: DoubleArray     )      private var isInitialized = false -    /**-     * Overload to get route from a SpecificFeature.Route object.-     */     suspend fun getRoute(         context: Context,         route: SpecificFeature.Route,@@ -41,9 +68,6 @@ object OfflineRouter {         return@withContext getRoute(context, start, end, type)     } -    /**-     * Primary routing function using the native A* implementation.-     */     suspend fun getRoute(         context: Context,         start: Position,@@ -105,4 +129,4 @@ object OfflineRouter {             step = processedSteps         )     }-}\ No newline at end of file+}