/* Use g_convert to transliterate names.. my iconv seems to be fucked so this didn't work... */
/* #define TRANSLIT_NAMES */
-#define FILE_BUFFER 65535
+#define FILE_BUFFER (128*1024)
static guint node_cnt=0;
static guint node_skip_cnt=0;
static guint dbnoded_cnt=0;
static guint dbway_cnt=0;
+/* For threaded importing */
+static GThread* import_thread=NULL;
+static GSourceFunc osm_import_progress_cb=NULL;
+static osm_import_data_req osm_import_req;
+static guint import_sid=0;
+
static gboolean is_update=FALSE;
static XML_Parser xp;
{ "amenity", "biergarten", NODE_AMENITY_PUB },
{ "amenity", "cafe", NODE_AMENITY_CAFE },
{ "amenity", "fast_food", NODE_AMENITY_FOOD },
- { "amenity", "restaurant", NODE_AMENITY_FOOD },
+ { "amenity", "restaurant", NODE_AMENITY_RESTAURANT },
{ "amenity", "telephone", NODE_AMENITY_TELEPHONE },
{ "amenity", "toilets", NODE_AMENITY_WC },
sqlite3_finalize(sql.insert_way_names_nls);
}
-void
+static void
db_prepare(sqlite3 *db)
{
/* Way nodes */
{
if (XML_Parse(xp, buffer, r, r>0 ? 0:1) == XML_STATUS_ERROR) {
g_printerr("Parse error at line %d:\n%s\n",
- XML_GetCurrentLineNumber(xp),
- XML_ErrorString(XML_GetErrorCode(xp)));
+ (gint)XML_GetCurrentLineNumber(xp),
+ XML_ErrorString(XML_GetErrorCode(xp)));
return FALSE;
}
return TRUE;
bbox.lat_min, bbox.lon_min, bbox.lat_max, bbox.lon_max);
}
+static void
+osm_print_import_stats(void)
+{
+g_printf("Total nodes %d, POIs: %d and Ways %d.\n", node_cnt, noded_cnt, way_cnt);
+g_printf("Cities/Towns: %d\n", g_hash_table_size(osm_place_city));
+g_printf("Villages/Hamlets: %d\n", g_hash_table_size(osm_place_village));
+g_printf("Suburbs: %d\n", g_hash_table_size(osm_place_suburb));
+g_printf("Nodes: %d\n", g_hash_table_size(osm_nodes));
+}
+
/**
* Simple helper to do all preparations and importing from planet -> database
*
db_create_tables(db);
db_create_indexes(db);
db_prepare(db);
-
osm_planet_parser_init();
if (osm_planet_parse_file(planet)==FALSE) {
return FALSE;
}
-g_printf("Total nodes %d, POIs: %d and Ways %d.\n", node_cnt, noded_cnt, way_cnt);
-g_printf("Cities/Towns: %d\n", g_hash_table_size(osm_place_city));
-g_printf("Villages/Hamlets: %d\n", g_hash_table_size(osm_place_village));
-g_printf("Suburbs: %d\n", g_hash_table_size(osm_place_suburb));
-g_printf("Nodes: %d\n", g_hash_table_size(osm_nodes));
+osm_print_import_stats();
osm_planet_save_all_nodes();
osm_planet_save_all_ways();
osm_planet_parser_deinit();
+db_finalize();
db_close(&db);
+g_printf("All done.\n");
return TRUE;
}
+static gpointer
+osm_import_thread(gpointer user_data)
+{
+gboolean r;
+osm_import_data_req *req=(osm_import_data_req *)user_data;
+
+g_assert(req);
+g_assert(req->planet);
+g_assert(req->db);
+
+osm_import_progress_cb=req->progress_cb!=NULL ? req->progress_cb : NULL;
+
+r=osm_import(req->planet, req->db);
+g_debug("OSM import result: %d", r);
+
+g_free(req->planet);
+g_free(req->db);
+
+if (req->done_cb!=NULL)
+ g_idle_add(req->done_cb, GINT_TO_POINTER(r==TRUE ? 0 : 1));
+
+return r==TRUE ? 0 : 1;
+}
+
+/**
+ * Helper to start an import in the background using a thread.
+ *
+ * Two callback can be given, one for progress feedback and one when the operation is done.
+ * Done callback must call the join function.
+ * Only one import thread can run at a time.
+ *
+ */
+gboolean
+osm_import_bg(const gchar *planet, const gchar *database, GSourceFunc progress_cb, GSourceFunc done_cb)
+{
+GError *error=NULL;
+
+g_return_val_if_fail(import_thread==NULL, FALSE);
+
+osm_import_req.planet=g_strdup(planet);
+osm_import_req.db=g_strdup(database);
+osm_import_req.progress_cb=progress_cb;
+osm_import_req.done_cb=done_cb;
+
+import_thread=g_thread_create(osm_import_thread, &osm_import_req, TRUE, &error);
+if (import_thread==NULL) {
+ g_free(osm_import_req.planet);
+ g_free(osm_import_req.db);
+ g_printerr("Import thread creation failed.\n");
+ return FALSE;
+}
+if (osm_import_progress_cb!=NULL)
+ import_sid=g_timeout_add(1000, osm_import_progress_cb, NULL);
+return TRUE;
+}
+
+gint
+osm_import_join_bg(void)
+{
+gint ret;
+g_assert(import_thread!=NULL);
+
+if (import_sid!=0)
+ g_source_remove(import_sid);
+ret=g_thread_join(import_thread);
+import_thread=NULL;
+return ret;
+}