int editor_db_open (int map_id) { char name[100]; const char *map_path; char file_name[512]; int do_read = 0; editor_log_push ("editor_db_open"); #ifndef IPHONE map_path = roadmap_db_map_path(); #else map_path = roadmap_path_preferred("maps"); #endif //IPHONE if (!map_path) { editor_log (ROADMAP_ERROR, "Can't find editor path"); editor_log_pop (); return -1; } snprintf (name, sizeof(name), "edt%05d.dat", map_id); roadmap_path_format (file_name, sizeof (file_name), map_path, name); if (roadmap_file_exists (map_path, name)) { EditorDataFile = roadmap_file_open(file_name, "rw"); do_read = 1; } else { roadmap_path_create (map_path); EditorDataFile = roadmap_file_open(file_name, "w"); roadmap_file_write (EditorDataFile, &DB_SIGNATURE, sizeof (int)); } do { if (!ROADMAP_FILE_IS_VALID(EditorDataFile)) { editor_log (ROADMAP_ERROR, "Can't open/create new database: %s/%s", map_path, name); editor_log_pop (); return -1; } if (do_read) { do_read = 0; if (editor_db_read () == -1) { editor_db_free (); //roadmap_messagebox("Error", "Offline data file is currupt: Re-Initializing data"); roadmap_log (ROADMAP_ERROR, "Offline data file is currupt: Re-Initializing data"); roadmap_file_close (EditorDataFile); roadmap_file_remove (NULL, file_name); EditorDataFile = roadmap_file_open(file_name, "w"); roadmap_file_write (EditorDataFile, &DB_SIGNATURE, sizeof (int)); } } } while (do_read); EditorActiveMap = map_id; editor_log_pop (); return 0; }
int editor_db_open (int map_id) { char name[100]; const char *map_path; char *file_name; int do_read = 0; editor_log_push ("editor_db_open"); map_path = roadmap_db_map_path(); if (!map_path) { editor_log (ROADMAP_ERROR, "Can't find editor path"); editor_log_pop (); return -1; } snprintf (name, sizeof(name), "edt%05d.dat", map_id); file_name = roadmap_path_join(map_path, name); if (roadmap_file_exists (map_path, name)) { EditorDataFile = roadmap_file_open(file_name, "rw"); do_read = 1; } else { roadmap_path_create (map_path); EditorDataFile = roadmap_file_open(file_name, "w"); } #ifdef WIN32 roadmap_path_free(map_path); #endif do { if (!ROADMAP_FILE_IS_VALID(EditorDataFile)) { editor_log (ROADMAP_ERROR, "Can't open/create new database: %s/%s", map_path, name); roadmap_path_free(file_name); editor_log_pop (); return -1; } if (do_read) { do_read = 0; if (editor_db_read () == -1) { editor_db_free (); roadmap_messagebox("Error", "Offline data file is currupt: Re-Initializing data"); roadmap_file_close (EditorDataFile); roadmap_file_remove (NULL, file_name); EditorDataFile = roadmap_file_open(file_name, "w"); } } } while (do_read); roadmap_path_free(file_name); EditorActiveMap = map_id; editor_log_pop (); return 0; }
static int create_new_line (int gps_first_point, int gps_last_point, int from_point, int to_point, int cfcc) { int p_from; int p_to; int line_id; if (editor_track_util_create_db (track_point_pos (gps_last_point)) == -1) { editor_log (ROADMAP_ERROR, "create_new_line: can't create db."); return -1; } if (from_point != -1) { p_from = from_point; } else { p_from = create_node (gps_first_point, &cur_node); if (p_from == -1) { return -1; } } if (to_point != -1) { p_to = to_point; } else { RoadMapPosition start_pos; editor_point_position (p_from, &start_pos); /* check if first and last point are the same */ if (!roadmap_math_compare_points (&start_pos, track_point_pos (gps_last_point))) { p_to = p_from; } else { p_to = editor_point_add (track_point_pos (gps_last_point), -1); if (p_to == -1) { return -1; } } } cur_node.id = p_to; cur_node.plugin_id = EditorPluginID; line_id = editor_track_util_create_line (gps_first_point, gps_last_point, p_from, p_to, cfcc, is_new_track); is_new_track = 0; return line_id; }
int editor_line_add (int p_from, int p_to, int trkseg, int cfcc, int direction, int street, int flags) { editor_db_line line; int id; time_t update_time = roadmap_navigate_get_time (); line.update_timestamp = (int)update_time; line.point_from = p_from; line.point_to = p_to; line.trkseg = trkseg; line.cfcc = cfcc; line.flags = flags; line.street = street; line.direction = direction; id = editor_db_add_item (ActiveLinesDB, &line, 1); if (id == -1) return -1; assert (editor_line_length (id) > 0); if (editor_line_length (id) == 0) return -1; editor_bar_set_temp_length(0); editor_bar_set_length(editor_line_length (id)); editor_log (ROADMAP_INFO, "Adding new line - from:%d, to:%d, trkseg:%d, cfcc:%d, flags:%d", p_from, p_to, trkseg, cfcc, flags); if (id == -1) { editor_log (ROADMAP_ERROR, "Can't add new line."); } return id; }
void editor_db_confirm_commit (editor_db_section *section, int id) { if (id > section->committed_generation) { section->committed_generation = id; if (editor_db_write_committed (section, id) != 0) { editor_log (ROADMAP_ERROR, "editor_db_confirm_commit - editor_db_write_committed failed."); } } }
static int editor_db_allocate_new_block (editor_db_section *section, int block_id) { if (section->max_blocks == block_id) { int new_size = section->max_blocks * 2; char **more_blocks = (char **)realloc (section->blocks, sizeof (char *) * new_size); if (!more_blocks) { editor_log (ROADMAP_ERROR, "editor_db_allocate_new_block - reached max memory."); return -1; } section->blocks = more_blocks; while (section->max_blocks < new_size) { section->blocks[section->max_blocks++] = NULL; } } section->blocks[block_id] = malloc(DB_DEFAULT_BLOCK_SIZE); if (!section->blocks[block_id]) return -1; return 0; }
static int editor_db_read_items (char **buffer, size_t size, editor_db_section **item_section, int *item_id, int *error) { unsigned int type_id; int count = 1; editor_db_section *section; char *read_buffer; int committed = -1; *error = 0; if (size < sizeof (unsigned int)) return -1; type_id = *(unsigned int *)(*buffer); read_buffer = (*buffer) + sizeof (unsigned int); size -= sizeof (unsigned int); if (type_id & TYPE_MULTIPLE_FLAG) { if (size < sizeof(int)) return -1; count = *(int *)read_buffer; read_buffer += sizeof(int); size -= sizeof(int); type_id &= ~TYPE_MULTIPLE_FLAG; } if (type_id & TYPE_UPDATE_FLAG) { if (size < sizeof(int)) return -1; *item_id = *(int *)read_buffer; read_buffer += sizeof(int); size -= sizeof(int); type_id &= ~TYPE_UPDATE_FLAG; } else { *item_id = -1; } if (type_id & TYPE_COMMITTED_FLAG) { if (size < sizeof(int)) return -1; committed = *(int *)read_buffer; read_buffer += sizeof (int); type_id &= ~TYPE_COMMITTED_FLAG; count = 0; } //assert(type_id < MAX_TYPES); if (type_id >= MAX_TYPES) { editor_log (ROADMAP_ERROR, "editor_db_read_items() - bad type_id."); *error = -1; return -1; } section = EditorActiveSections[type_id]; //assert(section); if (!section) { editor_log (ROADMAP_ERROR, "editor_db_read_items() - invalid section pointer."); *error = -1; return -1; } if (committed >= 0) { section->committed_generation = committed; } if (size < section->record_size * count) return -1; *item_section = section; *buffer = read_buffer; return count; }
int editor_track_known_end_segment (PluginLine *previous_line, int last_point_id, PluginLine *line, RoadMapTracking *street, int is_new_track) { //TODO: add stuff //Notice that previous_line may not be valid (only at first) // //check for low confidence & in static update, do not use a trkseg with low confidence! RoadMapPosition from; RoadMapPosition to; RoadMapPosition *current; int trkseg; int trkseg_line_id; int trkseg_plugin_id; int trkseg_square; int line_length; int segment_length; int percentage; int flags = 0; editor_log_push ("editor_track_end_known_segment"); assert (last_point_id != 0); if (!last_point_id) return 0; if (editor_db_activate (line->fips) == -1) { editor_db_create (line->fips); if (editor_db_activate (line->fips) == -1) { editor_log_pop (); return 0; } } roadmap_street_extend_line_ends (line, &from, &to, FLAG_EXTEND_BOTH, NULL, NULL); trkseg_plugin_id = roadmap_plugin_get_id (line); trkseg_line_id = roadmap_plugin_get_line_id (line); trkseg_square = roadmap_plugin_get_square (line); line_length = editor_track_util_get_line_length (line); segment_length = editor_track_util_length (0, last_point_id); editor_log (ROADMAP_INFO, "Ending line %d (plugin_id:%d). Line length:%d, Segment length:%d", trkseg_line_id, trkseg_plugin_id, line_length, segment_length); /* End current segment if we really passed through it * and not only touched a part of it. */ /*SRUL*: avoid this problem, see above comment assert (line_length > 0); if (line_length == 0) { editor_log (ROADMAP_ERROR, "line %d (plugin_id:%d) has length of zero.", trkseg_line_id, trkseg_plugin_id); editor_log_pop (); return 0; } */ if (line_length <= 0) line_length = 1; current = track_point_pos (last_point_id); if (roadmap_math_distance (current, &to) > roadmap_math_distance (current, &from)) { flags = ED_TRKSEG_OPPOSITE_DIR; #if DEBUG_TRACKS printf ("Closing trkseg from %d.%06d,%d.%06d to %d.%06d,%d.%06d", to.longitude / 1000000, to.longitude % 1000000, to.latitude / 1000000, to.latitude % 1000000, from.longitude / 1000000, from.longitude % 1000000, from.latitude / 1000000, from.latitude % 1000000); } else { printf ("Closing trkseg from %d.%06d,%d.%06d to %d.%06d,%d.%06d", from.longitude / 1000000, from.longitude % 1000000, from.latitude / 1000000, from.latitude % 1000000, to.longitude / 1000000, to.longitude % 1000000, to.latitude / 1000000, to.latitude % 1000000); #endif } #if DEBUG_TRACKS printf (" %d/%d", segment_length, line_length); #endif if (is_new_track) { flags |= ED_TRKSEG_NEW_TRACK; #if DEBUG_TRACKS printf (" NEW"); #endif } if (!editor_ignore_new_roads ()) { flags |= ED_TRKSEG_RECORDING_ON; } percentage = 100 * segment_length / line_length; if (percentage < 70) { editor_log (ROADMAP_INFO, "segment is too small to consider: %d%%", percentage); if (segment_length > (editor_track_point_distance ()*1.5)) { trkseg = editor_track_util_create_trkseg (trkseg_square, trkseg_line_id, trkseg_plugin_id, 0, last_point_id, flags|ED_TRKSEG_LOW_CONFID); editor_track_add_trkseg (line, trkseg, ROUTE_DIRECTION_NONE, ROUTE_CAR_ALLOWED); editor_log_pop (); #if DEBUG_TRACKS printf (" LOW (percentage)\n"); #endif return 1; } else { trkseg = editor_track_util_create_trkseg (trkseg_square, trkseg_line_id, trkseg_plugin_id, 0, last_point_id, flags|ED_TRKSEG_IGNORE); editor_track_add_trkseg (line, trkseg, ROUTE_DIRECTION_NONE, ROUTE_CAR_ALLOWED); editor_log_pop (); #if DEBUG_TRACKS printf (" IGNORE\n"); #endif return 0; } } #if 0 if (!roadmap_fuzzy_is_good (street->entry_fuzzyfied) || !roadmap_fuzzy_is_good (street->cur_fuzzyfied)) { flags |= ED_TRKSEG_LOW_CONFID; #if DEBUG_TRACKS printf (" LOW"); if (!roadmap_fuzzy_is_good (street->entry_fuzzyfied)) printf (" (entry)"); if (!roadmap_fuzzy_is_good (street->cur_fuzzyfied)) printf (" (cur)"); #endif } #endif if (trkseg_plugin_id != ROADMAP_PLUGIN_ID) flags |= ED_TRKSEG_LOW_CONFID; trkseg = editor_track_util_create_trkseg (trkseg_square, trkseg_line_id, trkseg_plugin_id, 0, last_point_id, flags); if (flags & ED_TRKSEG_OPPOSITE_DIR) { editor_log (ROADMAP_INFO, "Updating route direction: to -> from"); editor_track_add_trkseg (line, trkseg, ROUTE_DIRECTION_AGAINST_LINE, ROUTE_CAR_ALLOWED); } else { editor_log (ROADMAP_INFO, "Updating route direction: from -> to"); editor_track_add_trkseg (line, trkseg, ROUTE_DIRECTION_WITH_LINE, ROUTE_CAR_ALLOWED); } editor_log_pop (); #if DEBUG_TRACKS printf ("\n"); #endif return 1; }
static void editor_track_report_get_points (int offline, LPNodeInTime nodes, int *num_nodes, LPGPSPointInTime points, int *num_points, time_t *toggle_times, int *num_toggles, int *first_toggle) { /* should be called immediately after editor_export_prepare_track() */ int i; int j; int node = 0; int point = 0; int max_nodes = *num_nodes; int max_points = *num_points; int max_toggles = *num_toggles; int num_extra; int count; RoadMapPosition last_gps_pos; time_t last_gps_time; int ordinal; int toggle = 0; int recording_mode = 0; PendingLastNode = LastReportedNode; PendingOrdinal = LastOrdinal; /* loop over all trksegs */ if (editor_trkseg_items_pending ()) { int first_shape; int last_shape; int flags; int from; RoadMapPosition from_pos; int from_id; int to_id; time_t start_time; time_t shape_time; time_t end_time; count = editor_trkseg_get_count (); for (i = 0; i < count; i++) { if (editor_trkseg_item_committed (i)) continue; /* extract details of trkseg */ editor_trkseg_get (i, &from, &first_shape, &last_shape, &flags); if (flags & ED_TRKSEG_FAKE) continue; editor_point_position (from, &from_pos); editor_trkseg_get_time (i, &start_time, &end_time); /* add all points */ if (first_shape < 0) last_shape = first_shape - 1; shape_time = start_time; for (j = first_shape; j <= last_shape; j++) { ordinal = editor_shape_ordinal (j); editor_shape_position (j, &from_pos); editor_shape_time (j, &shape_time); if (ordinal > PendingOrdinal) { /* on new track, add a null point */ if (j == first_shape && (flags & ED_TRKSEG_NEW_TRACK)) { if (point >= max_points) { editor_log (ROADMAP_FATAL, "not enough space (%d) for GPSPath", max_points); } points[point].Position.longitude = INVALID_COORDINATE; points[point].Position.latitude = INVALID_COORDINATE; points[point].GPS_time = 0; point++; } if (offline && j == first_shape && recording_mode != (flags & ED_TRKSEG_RECORDING_ON)) { recording_mode = flags & ED_TRKSEG_RECORDING_ON; if (toggle >= max_toggles) { editor_log (ROADMAP_FATAL, "not enough space (%d) for recording togles", max_toggles); } if (toggle == 0) { *first_toggle = recording_mode ? 1 : 0; } toggle_times[toggle++] = start_time; } if (point >= max_points) { editor_log (ROADMAP_FATAL, "not enough space (%d) for GPSPath", max_points); } points[point].Position = from_pos; points[point].GPS_time = shape_time; point++; PendingOrdinal = ordinal; } } if (flags & (ED_TRKSEG_IGNORE | ED_TRKSEG_LOW_CONFID)) continue; editor_trkseg_get_points (i, &from_id, &to_id); if ((flags & ED_TRKSEG_NEW_TRACK) == 0 && from_id != PendingLastNode) { /* add from node */ if (node >= max_nodes) { editor_log (ROADMAP_FATAL, "not enough space (%d) for NodePath", max_nodes); } nodes[node].node = from_id; nodes[node].GPS_time = start_time; node++; } /* add to node */ if (node >= max_nodes) { editor_log (ROADMAP_FATAL, "not enough space (%d) for NodePath", max_nodes); } nodes[node].node = to_id; nodes[node].GPS_time = end_time; node++; PendingLastNode = to_id; } } /* add points that are not yet in trkseg */ num_extra = export_track_num_points (); if (export_track_is_new () && num_extra > 0) { if (point >= max_points) { editor_log (ROADMAP_FATAL, "not enough space (%d) for GPSPath", max_points); } points[point].Position.longitude = INVALID_COORDINATE; points[point].Position.latitude = INVALID_COORDINATE; points[point].GPS_time = 0; point++; } for (i = 0; i < num_extra; i++) { ordinal = export_track_point_ordinal (i); if (ordinal > PendingOrdinal) { if (*export_track_point_status (i) == POINT_STATUS_SAVE) { if (point >= max_points) { editor_log (ROADMAP_FATAL, "not enough space (%d) for GPSPath", max_points); } points[point].Position = *export_track_point_pos (i); points[point].GPS_time = export_track_point_time (i); point++; } PendingOrdinal = ordinal; } } export_track_reset_points (); /* add optional last gps point */ if (editor_track_filter_get_current (editor_track_get_gps_filter (), &last_gps_pos, &last_gps_time)) { if (point == 0 || last_gps_pos.longitude != points[point - 1].Position.longitude || last_gps_pos.latitude != points[point - 1].Position.latitude || last_gps_time != points[point - 1].GPS_time) { if (export_track_is_new () && num_extra == 0) { if (point >= max_points) { editor_log (ROADMAP_FATAL, "not enough space (%d) for GPSPath", max_points); } points[point].Position.longitude = INVALID_COORDINATE; points[point].Position.latitude = INVALID_COORDINATE; points[point].GPS_time = 0; point++; } if (point >= max_points) { editor_log (ROADMAP_FATAL, "not enough space (%d) for GPSPath", max_points); } points[point].Position = last_gps_pos; points[point].GPS_time = last_gps_time; point++; } } /* add real-time recording mode toggles */ if (!offline) { toggle = editor_track_get_num_update_toggles (); if (toggle > max_toggles) { editor_log (ROADMAP_FATAL, "not enough space (%d) for recording togles", max_toggles); } memcpy (toggle_times, editor_track_get_update_toggle_times (), toggle * sizeof (time_t)); *first_toggle = editor_track_get_update_toggle_state (0); } *num_nodes = node; *num_points = point; *num_toggles = toggle; }