int streamFileXML2(char *filename, int sanitize, struct osmdata_t *osmdata) { xmlTextReaderPtr reader; int ret = 0; if (sanitize) reader = sanitizerOpen(filename); else reader = inputUTF8(filename); if (reader != NULL) { ret = xmlTextReaderRead(reader); while (ret == 1) { processNode(reader, osmdata); ret = xmlTextReaderRead(reader); } if (ret != 0) { fprintf(stderr, "%s : failed to parse\n", filename); return ret; } xmlFreeTextReader(reader); } else { fprintf(stderr, "Unable to open %s\n", filename); return 1; } return 0; }
int nominatim_import(const char *conninfo, const char *partionTagsFilename, const char *filename) { xmlTextReaderPtr reader; int ret = 0; PGresult * res; FILE * partionTagsFile; char * partionQueryName; char partionQuerySQL[1024]; conn = PQconnectdb(conninfo); if (PQstatus(conn) != CONNECTION_OK) { fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(conn)); exit(EXIT_FAILURE); } partionTableTagsHash = xmlHashCreate(200); partionTableTagsHashDelete = xmlHashCreate(200); partionTagsFile = fopen(partionTagsFilename, "rt"); if (!partionTagsFile) { fprintf(stderr, "Unable to read partition tags file: %s\n", partionTagsFilename); exit(EXIT_FAILURE); } char buffer[1024], osmkey[256], osmvalue[256]; int fields; while (fgets(buffer, sizeof(buffer), partionTagsFile) != NULL) { fields = sscanf( buffer, "%23s %63s", osmkey, osmvalue ); if ( fields <= 0 ) continue; if ( fields != 2 ) { fprintf( stderr, "Error partition file\n"); exit_nicely(); } partionQueryName = malloc(strlen("partition_insert_")+strlen(osmkey)+strlen(osmvalue)+2); strcpy(partionQueryName, "partition_insert_"); strcat(partionQueryName, osmkey); strcat(partionQueryName, "_"); strcat(partionQueryName, osmvalue); strcpy(partionQuerySQL, "insert into place_classtype_"); strcat(partionQuerySQL, osmkey); strcat(partionQuerySQL, "_"); strcat(partionQuerySQL, osmvalue); strcat(partionQuerySQL, " (place_id, centroid) values ($1, ST_Centroid(st_setsrid($2, 4326)))"); res = PQprepare(conn, partionQueryName, partionQuerySQL, 2, NULL); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Failed to prepare %s: %s\n", partionQueryName, PQerrorMessage(conn)); exit(EXIT_FAILURE); } xmlHashAddEntry2(partionTableTagsHash, BAD_CAST osmkey, BAD_CAST osmvalue, BAD_CAST partionQueryName); partionQueryName = malloc(strlen("partition_delete_")+strlen(osmkey)+strlen(osmvalue)+2); strcpy(partionQueryName, "partition_delete_"); strcat(partionQueryName, osmkey); strcat(partionQueryName, "_"); strcat(partionQueryName, osmvalue); strcpy(partionQuerySQL, "delete from place_classtype_"); strcat(partionQuerySQL, osmkey); strcat(partionQuerySQL, "_"); strcat(partionQuerySQL, osmvalue); strcat(partionQuerySQL, " where place_id = $1::integer"); res = PQprepare(conn, partionQueryName, partionQuerySQL, 1, NULL); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Failed to prepare %s: %s\n", partionQueryName, PQerrorMessage(conn)); exit(EXIT_FAILURE); } xmlHashAddEntry2(partionTableTagsHashDelete, BAD_CAST osmkey, BAD_CAST osmvalue, BAD_CAST partionQueryName); } res = PQprepare(conn, "get_new_place_id", "select nextval('seq_place')", 0, NULL); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Failed to prepare get_new_place_id: %s\n", PQerrorMessage(conn)); exit(EXIT_FAILURE); } res = PQprepare(conn, "get_place_id", "select place_id from placex where osm_type = $1 and osm_id = $2 and class = $3 and type = $4", 4, NULL); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Failed to prepare get_place_id: %s\n", PQerrorMessage(conn)); exit(EXIT_FAILURE); } res = PQprepare(conn, "placex_insert", "insert into placex (place_id,osm_type,osm_id,class,type,name,country_code,extratags,parent_place_id,admin_level,housenumber,rank_address,rank_search,geometry) " "values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, st_setsrid($14, 4326))", 12, NULL); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Failed to prepare placex_insert: %s\n", PQerrorMessage(conn)); exit(EXIT_FAILURE); } res = PQprepare(conn, "search_name_insert", "insert into search_name (place_id, search_rank, address_rank, country_code, name_vector, nameaddress_vector, centroid) " "select place_id, rank_search, rank_address, country_code, make_keywords(name), " "(select uniq(sort(array_agg(parent_search_name.name_vector))) from search_name as parent_search_name where place_id in " "(select distinct address_place_id from place_addressline where place_addressline.place_id = $1 limit 1000)" "), st_centroid(geometry) from placex " "where place_id = $1", 1, NULL); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Failed to prepare search_name_insert: %s\n", PQerrorMessage(conn)); exit(EXIT_FAILURE); } res = PQprepare(conn, "search_name_from_parent_insert", "insert into search_name (place_id, search_rank, address_rank, country_code, name_vector, nameaddress_vector, centroid) " "select place_id, rank_search, rank_address, country_code, make_keywords(name), " "(select uniq(sort(name_vector+nameaddress_vector)) from search_name as parent_search_name " "where parent_search_name.place_id = $2 ), st_centroid(geometry) from placex " "where place_id = $1", 2, NULL); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Failed to prepare search_name_insert: %s\n", PQerrorMessage(conn)); exit(EXIT_FAILURE); } res = PQprepare(conn, "place_addressline_insert", "insert into place_addressline (place_id, address_place_id, fromarea, isaddress, distance, cached_rank_address) " "select $1, place_id, false, $7, $2, rank_address from placex where osm_type = $3 and osm_id = $4 and class = $5 and type = $6", 7, NULL); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Failed to prepare place_addressline_insert: %s\n", PQerrorMessage(conn)); exit(EXIT_FAILURE); } res = PQprepare(conn, "placex_delete", "delete from placex where place_id = $1", 1, NULL); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Failed to prepare placex_delete: %s\n", PQerrorMessage(conn)); exit(EXIT_FAILURE); } res = PQprepare(conn, "search_name_delete", "delete from search_name where place_id = $1", 1, NULL); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Failed to prepare search_name_delete: %s\n", PQerrorMessage(conn)); exit(EXIT_FAILURE); } res = PQprepare(conn, "place_addressline_delete", "delete from place_addressline where place_id = $1", 1, NULL); if (PQresultStatus(res) != PGRES_COMMAND_OK) { fprintf(stderr, "Failed to prepare place_addressline_delete: %s\n", PQerrorMessage(conn)); exit(EXIT_FAILURE); } featureCount = 0; reader = inputUTF8(filename); if (reader == NULL) { fprintf(stderr, "Unable to open %s\n", filename); return 1; } ret = xmlTextReaderRead(reader); while (ret == 1) { processNode(reader); ret = xmlTextReaderRead(reader); } if (ret != 0) { fprintf(stderr, "%s : failed to parse\n", filename); return ret; } xmlFreeTextReader(reader); xmlHashFree(partionTableTagsHash, NULL); xmlHashFree(partionTableTagsHashDelete, NULL); return 0; }