#include <fcntl.h>
#include <math.h>
#include <glib.h>
+#include <glib/gstdio.h>
#include <sqlite3.h>
#include <expat.h>
node *osm_find_node(guint32 nid);
void osm_free_way_data(way *w);
void print_way(way *w);
+gboolean db_open(void);
+void db_prepare(void);
+gboolean db_insert_node(node *n);
/****************************************************/
/* Functions */
return TRUE;
}
+void
+db_close(void)
+{
+sqlite3_finalize(sql.insert_poi);
+sqlite3_finalize(sql.delete_osm_poi);
+
+sqlite3_finalize(sql.insert_node);
+sqlite3_finalize(sql.select_node);
+sqlite3_finalize(sql.delete_nodes);
+sqlite3_finalize(sql.update_node);
+
+sqlite3_finalize(sql.insert_place);
+sqlite3_finalize(sql.delete_place);
+
+sqlite3_close(db);
+}
+
void
db_prepare(void)
{
sqlite3_step(sql.insert_node);
sqlite3_reset(sql.insert_node);
sqlite3_clear_bindings(sql.insert_node);
+
return TRUE;
}
sqlite3_step(sql.insert_poi);
sqlite3_reset(sql.insert_poi);
sqlite3_clear_bindings(sql.insert_poi);
+
return TRUE;
}
if (!w)
return FALSE;
-if (w->type==WAY_UNWAYED)
+/* Skip things we can't use */
+if (w->type==WAY_UNWAYED || w->type>WAY_ROAD_END)
return TRUE;
wsegcnt=0;
gchar **isin;
gchar **place;
-isin=g_hash_table_lookup(osm_way_isin, w->id);
+isin=g_hash_table_lookup(osm_way_isin, GINT_TO_POINTER(w->id));
if (!isin)
return 0;
/***********************************************************************/
void
-osm_node_save_node(gint key, node *value, gpointer user_data)
+osm_node_save_node(gint key, gpointer value, gpointer user_data)
{
+node *n=(node *)value;
+
dbnode_cnt++;
-db_insert_node(value);
+db_insert_node(n);
if (dbnode_cnt % 26214==0)
g_printf("Nodes: %f%%\n",((float)dbnode_cnt/(float)node_cnt)*100);
}
break;
case IS_NODE:
{
- gint i;
-
if (!osm_node_tags)
return;
/************************************************************************/
+static void
+storage_init(void)
+{
+osm_nodes=g_hash_table_new(g_direct_hash, g_direct_equal);
+osm_segments=g_hash_table_new(g_direct_hash, g_direct_equal);
+
+osm_place_country=g_hash_table_new(g_str_hash, g_str_equal);
+osm_place_city=g_hash_table_new(g_str_hash, g_str_equal);
+osm_place_suburb=g_hash_table_new(g_str_hash, g_str_equal);
+osm_place_village=g_hash_table_new(g_str_hash, g_str_equal);
+osm_place_region=g_hash_table_new(g_str_hash, g_str_equal);
+osm_node_isin=g_hash_table_new(g_direct_hash, g_direct_equal);
+}
+
+static void
+storage_free(void)
+{
+g_hash_table_destroy(osm_nodes);
+g_hash_table_destroy(osm_segments);
+
+g_hash_table_destroy(osm_place_country);
+g_hash_table_destroy(osm_place_city);
+g_hash_table_destroy(osm_place_suburb);
+g_hash_table_destroy(osm_place_village);
+g_hash_table_destroy(osm_place_region);
+g_hash_table_destroy(osm_node_isin);
+}
+
void
-osm_planet_parse_init(void)
+osm_planet_parser_init(void)
{
xp=XML_ParserCreate(NULL);
XML_SetElementHandler(xp, _osm_tag_start, _osm_tag_end);
+storage_init();
}
void
-osm_planet_parse_deinit(void)
+osm_planet_parser_deinit(void)
{
XML_ParserFree(xp);
+storage_free();
}
gboolean
g_printf("WayD size: %d\n", sizeof(way_data));
}
-static void
-storage_init(void)
-{
-osm_nodes=g_hash_table_new(g_direct_hash, g_direct_equal);
-osm_segments=g_hash_table_new(g_direct_hash, g_direct_equal);
-
-osm_place_country=g_hash_table_new(g_str_hash, g_str_equal);
-osm_place_city=g_hash_table_new(g_str_hash, g_str_equal);
-osm_place_suburb=g_hash_table_new(g_str_hash, g_str_equal);
-osm_place_village=g_hash_table_new(g_str_hash, g_str_equal);
-osm_place_region=g_hash_table_new(g_str_hash, g_str_equal);
-osm_node_isin=g_hash_table_new(g_direct_hash, g_direct_equal);
-}
-
-static void
-storage_free(void)
-{
-g_hash_table_destroy(osm_nodes);
-g_hash_table_destroy(osm_segments);
-
-g_hash_table_destroy(osm_place_country);
-g_hash_table_destroy(osm_place_city);
-g_hash_table_destroy(osm_place_suburb);
-g_hash_table_destroy(osm_place_village);
-g_hash_table_destroy(osm_place_region);
-g_hash_table_destroy(osm_node_isin);
-}
-
/************************************************************************/
int main (int argc, char **argv)
print_memory_usage();
-storage_init();
-
-osm_planet_parse_init();
+osm_planet_parser_init();
osm_planet_parse_file(argv[1]);
-osm_planet_parse_deinit();
g_printf("Planet loaded.\nTotal nodes %d, POIs: %d, Segments %d and Ways %d.\n",
node_cnt, noded_cnt, seg_cnt, way_cnt);
g_printf("Suburbs: %d\n", g_hash_table_size(osm_place_suburb));
osm_planet_save_to_db();
+osm_planet_parser_deinit();
+db_close();
-storage_free();
-
-sqlite3_close(db);
sync();
return 0;
}