8 #include <libxml/xmlstring.h>
9 #include <libxml/xmlreader.h>
10 #include <libxml/hash.h>
12 #include "nominatim.h"
16 typedef enum { FILETYPE_NONE, FILETYPE_STRUCTUREDV0P1 } filetypes_t;
17 typedef enum { FILEMODE_NONE, FILEMODE_ADD, FILEMODE_UPDATE, FILEMODE_DELETE } filemodes_t;
19 #define MAX_FEATUREADDRESS 5000
20 #define MAX_FEATURENAMES 10000
21 #define MAX_FEATUREEXTRATAGS 10000
22 #define MAX_FEATURENAMESTRING 1000000
23 #define MAX_FEATUREEXTRATAGSTRING 500000
25 struct feature_address
50 xmlChar * rankAddress;
52 xmlChar * countryCode;
53 xmlChar * parentPlaceID;
57 xmlChar * houseNumber;
61 int fileType = FILETYPE_NONE;
62 int fileMode = FILEMODE_ADD;
64 struct feature_address featureAddress[MAX_FEATUREADDRESS];
65 struct feature_tag featureName[MAX_FEATURENAMES];
66 struct feature_tag featureExtraTag[MAX_FEATUREEXTRATAGS];
67 struct feature feature;
68 int featureAddressLines = 0;
69 int featureNameLines = 0;
70 int featureExtraTagLines = 0;
72 xmlHashTablePtr partionTableTagsHash;
73 xmlHashTablePtr partionTableTagsHashDelete;
74 char featureNameString[MAX_FEATURENAMESTRING];
75 char featureExtraTagString[MAX_FEATUREEXTRATAGSTRING];
79 void StartElement(xmlTextReaderPtr reader, const xmlChar *name)
85 if (fileType == FILETYPE_NONE)
87 // Potential to handle other file types in the future / versions
88 if (xmlStrEqual(name, BAD_CAST "osmStructured"))
90 value = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "version");
91 version = strtof(value, NULL);
94 if (version == (float)0.1)
96 fileType = FILETYPE_STRUCTUREDV0P1;
97 fileMode = FILEMODE_ADD;
101 fprintf( stderr, "Unknown osmStructured version %f (%s)\n", version, value );
107 fprintf( stderr, "Unknown XML document type: %s\n", name );
113 if (xmlStrEqual(name, BAD_CAST "add"))
115 fileMode = FILEMODE_ADD;
118 if (xmlStrEqual(name, BAD_CAST "update"))
120 fileMode = FILEMODE_UPDATE;
123 if (xmlStrEqual(name, BAD_CAST "delete"))
125 fileMode = FILEMODE_DELETE;
128 if (fileMode == FILEMODE_NONE)
130 fprintf( stderr, "Unknown import mode in: %s\n", name );
134 if (xmlStrEqual(name, BAD_CAST "feature"))
136 feature.placeID = xmlTextReaderGetAttribute(reader, BAD_CAST "place_id");
137 feature.type = xmlTextReaderGetAttribute(reader, BAD_CAST "type");
138 feature.id = xmlTextReaderGetAttribute(reader, BAD_CAST "id");
139 feature.key = xmlTextReaderGetAttribute(reader, BAD_CAST "key");
140 feature.value = xmlTextReaderGetAttribute(reader, BAD_CAST "value");
141 feature.rankAddress = xmlTextReaderGetAttribute(reader, BAD_CAST "rank");
142 feature.rankSearch = xmlTextReaderGetAttribute(reader, BAD_CAST "importance");
144 feature.parentPlaceID = xmlTextReaderGetAttribute(reader, BAD_CAST "parent_place_id");
146 if (strlen(feature.parentPlaceID) == 0)
148 xmlFree(feature.parentPlaceID);
149 feature.parentPlaceID = NULL;
152 feature.parentType = xmlTextReaderGetAttribute(reader, BAD_CAST "parent_type");
153 feature.parentID = xmlTextReaderGetAttribute(reader, BAD_CAST "parent_id");
155 feature.countryCode = NULL;
156 feature.adminLevel = NULL;
157 feature.houseNumber = NULL;
158 feature.geometry = NULL;
159 featureAddressLines = 0;
160 featureNameLines = 0;
161 featureExtraTagLines = 0;
165 if (xmlStrEqual(name, BAD_CAST "names")) return;
166 if (xmlStrEqual(name, BAD_CAST "name"))
168 if (featureNameLines < MAX_FEATURENAMES)
170 featureName[featureNameLines].type = xmlTextReaderGetAttribute(reader, BAD_CAST "type");
171 featureName[featureNameLines].value = xmlTextReaderReadString(reader);
176 fprintf( stderr, "Too many name elements (%s%s)\n", feature.type, feature.id);
181 if (xmlStrEqual(name, BAD_CAST "tags")) return;
182 if (xmlStrEqual(name, BAD_CAST "tag"))
184 if (featureExtraTagLines < MAX_FEATUREEXTRATAGS)
186 featureExtraTag[featureExtraTagLines].type = xmlTextReaderGetAttribute(reader, BAD_CAST "type");
187 featureExtraTag[featureExtraTagLines].value = xmlTextReaderReadString(reader);
188 featureExtraTagLines++;
192 fprintf( stderr, "Too many extra tag elements (%s%s)\n", feature.type, feature.id);
197 if (xmlStrEqual(name, BAD_CAST "osmGeometry"))
199 feature.geometry = xmlTextReaderReadString(reader);
202 if (xmlStrEqual(name, BAD_CAST "adminLevel"))
204 feature.adminLevel = xmlTextReaderReadString(reader);
207 if (xmlStrEqual(name, BAD_CAST "countryCode"))
209 feature.countryCode = xmlTextReaderReadString(reader);
212 if (xmlStrEqual(name, BAD_CAST "houseNumber"))
214 feature.houseNumber = xmlTextReaderReadString(reader);
217 if (xmlStrEqual(name, BAD_CAST "address"))
219 featureAddressLines = 0;
223 if (xmlStrEqual(name, BAD_CAST "continent"))
227 else if (xmlStrEqual(name, BAD_CAST "sea"))
231 else if (xmlStrEqual(name, BAD_CAST "country"))
235 else if (xmlStrEqual(name, BAD_CAST "state"))
239 else if (xmlStrEqual(name, BAD_CAST "county"))
243 else if (xmlStrEqual(name, BAD_CAST "city"))
247 else if (xmlStrEqual(name, BAD_CAST "town"))
251 else if (xmlStrEqual(name, BAD_CAST "village"))
255 else if (xmlStrEqual(name, BAD_CAST "unknown"))
259 else if (xmlStrEqual(name, BAD_CAST "suburb"))
263 else if (xmlStrEqual(name, BAD_CAST "postcode"))
267 else if (xmlStrEqual(name, BAD_CAST "neighborhood"))
271 else if (xmlStrEqual(name, BAD_CAST "street"))
275 else if (xmlStrEqual(name, BAD_CAST "access"))
279 else if (xmlStrEqual(name, BAD_CAST "building"))
283 else if (xmlStrEqual(name, BAD_CAST "other"))
289 if (featureAddressLines < MAX_FEATUREADDRESS)
291 value = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "rank");
294 fprintf( stderr, "Address element missing rank\n");
297 featureAddress[featureAddressLines].rankAddress = atoi(value);
300 value = (char*)xmlTextReaderGetAttribute(reader, BAD_CAST "isaddress");
303 fprintf( stderr, "Address element missing rank\n");
306 if (*value == 't') strcpy(featureAddress[featureAddressLines].isAddress, "t");
307 else strcpy(featureAddress[featureAddressLines].isAddress, "f");
310 featureAddress[featureAddressLines].type = xmlTextReaderGetAttribute(reader, BAD_CAST "type");
311 featureAddress[featureAddressLines].id = xmlTextReaderGetAttribute(reader, BAD_CAST "id");
312 featureAddress[featureAddressLines].key = xmlTextReaderGetAttribute(reader, BAD_CAST "key");
313 featureAddress[featureAddressLines].value = xmlTextReaderGetAttribute(reader, BAD_CAST "value");
314 featureAddress[featureAddressLines].distance = xmlTextReaderGetAttribute(reader, BAD_CAST "distance");
316 featureAddressLines++;
320 fprintf( stderr, "Too many address elements (%s%s)\n", feature.type, feature.id);
326 fprintf(stderr, "%s: Unknown element name: %s\n", __FUNCTION__, name);
329 void EndElement(xmlTextReaderPtr reader, const xmlChar *name)
332 const char * paramValues[14];
334 char * partionQueryName;
335 int i, namePos, lineTypeLen, lineValueLen;
337 if (xmlStrEqual(name, BAD_CAST "feature"))
340 if (featureCount % 1000 == 0) printf("feature %i(k)\n", featureCount/1000);
342 if (fileMode == FILEMODE_ADD)
344 resPlaceID = PQexecPrepared(conn, "get_new_place_id", 0, NULL, NULL, NULL, 0);
345 if (PQresultStatus(resPlaceID) != PGRES_TUPLES_OK)
347 fprintf(stderr, "get_place_id: INSERT failed: %s", PQerrorMessage(conn));
354 paramValues[0] = (const char *)feature.type;
355 paramValues[1] = (const char *)feature.id;
356 paramValues[2] = (const char *)feature.key;
357 paramValues[3] = (const char *)feature.value;
358 resPlaceID = PQexecPrepared(conn, "get_new_place_id", 4, paramValues, NULL, NULL, 0);
359 if (PQresultStatus(resPlaceID) != PGRES_TUPLES_OK)
361 fprintf(stderr, "index_placex: INSERT failed: %s", PQerrorMessage(conn));
367 place_id = (char *)feature.placeID;
369 if (fileMode == FILEMODE_UPDATE || fileMode == FILEMODE_DELETE || fileMode == FILEMODE_ADD)
371 paramValues[0] = (const char *)place_id;
372 if (verbose) fprintf(stderr, "placex_delete: %s\n", paramValues[0]);
373 res = PQexecPrepared(conn, "placex_delete", 1, paramValues, NULL, NULL, 0);
374 if (PQresultStatus(res) != PGRES_COMMAND_OK)
376 fprintf(stderr, "placex_delete: DELETE failed: %s", PQerrorMessage(conn));
382 if (verbose) fprintf(stderr, "search_name_delete: %s\n", paramValues[0]);
383 res = PQexecPrepared(conn, "search_name_delete", 1, paramValues, NULL, NULL, 0);
384 if (PQresultStatus(res) != PGRES_COMMAND_OK)
386 fprintf(stderr, "search_name_delete: DELETE failed: %s", PQerrorMessage(conn));
392 if (verbose) fprintf(stderr, "place_addressline_delete: %s\n", paramValues[0]);
393 res = PQexecPrepared(conn, "place_addressline_delete", 1, paramValues, NULL, NULL, 0);
394 if (PQresultStatus(res) != PGRES_COMMAND_OK)
396 fprintf(stderr, "place_addressline_delete: DELETE failed: %s", PQerrorMessage(conn));
402 partionQueryName = xmlHashLookup2(partionTableTagsHashDelete, feature.key, feature.value);
403 if (partionQueryName)
405 res = PQexecPrepared(conn, partionQueryName, 1, paramValues, NULL, NULL, 0);
406 if (PQresultStatus(res) != PGRES_COMMAND_OK)
408 fprintf(stderr, "%s: DELETE failed: %s", partionQueryName, PQerrorMessage(conn));
416 if (fileMode == FILEMODE_UPDATE || fileMode == FILEMODE_ADD)
418 // Insert into placex
419 paramValues[0] = (const char *)place_id;
420 paramValues[1] = (const char *)feature.type;
421 paramValues[2] = (const char *)feature.id;
422 paramValues[3] = (const char *)feature.key;
423 paramValues[4] = (const char *)feature.value;
425 featureNameString[0] = 0;
426 if (featureNameLines)
431 for (i = 0; i < featureNameLines; i++)
433 lineTypeLen = (int)strlen((char *) featureName[i].type);
434 lineValueLen = (int)strlen((char *) featureName[i].value);
435 if (namePos+lineTypeLen+lineValueLen+7 > MAX_FEATURENAMESTRING)
437 fprintf(stderr, "feature name too long: %s", (const char *)featureName[i].value);
440 if (namePos) strcpy(featureNameString+(namePos++), ",");
441 strcpy(featureNameString+(namePos++), "\"");
442 strcpy(featureNameString+namePos, (char*) featureName[i].type);
443 namePos += lineTypeLen;
444 strcpy(featureNameString+namePos, "\"=>\"");
446 strcpy(featureNameString+namePos, (char *) featureName[i].value);
447 namePos += lineValueLen;
448 strcpy(featureNameString+(namePos++), "\"");
450 xmlFree(featureName[i].type);
451 xmlFree(featureName[i].value);
454 paramValues[5] = (const char *)featureNameString;
456 paramValues[6] = (const char *)feature.countryCode;
458 featureExtraTagString[0] = 0;
459 if (featureExtraTagLines)
464 for (i = 0; i < featureExtraTagLines; i++)
466 lineTypeLen = strlen((char *) featureExtraTag[i].type);
467 lineValueLen = strlen((char *) featureExtraTag[i].value);
468 if (namePos+lineTypeLen+lineValueLen+7 > MAX_FEATUREEXTRATAGSTRING)
470 fprintf(stderr, "feature extra tag too long: %s", (const char *)featureExtraTag[i].value);
473 if (namePos) strcpy(featureExtraTagString+(namePos++),",");
474 strcpy(featureExtraTagString+(namePos++), "\"");
475 strcpy(featureExtraTagString+namePos, (char *) featureExtraTag[i].type);
476 namePos += lineTypeLen;
477 strcpy(featureExtraTagString+namePos, "\"=>\"");
479 strcpy(featureExtraTagString+namePos, (char *) featureExtraTag[i].value);
480 namePos += lineValueLen;
481 strcpy(featureExtraTagString+(namePos++), "\"");
483 xmlFree(featureExtraTag[i].type);
484 xmlFree(featureExtraTag[i].value);
487 paramValues[7] = (const char *)featureExtraTagString;
489 if (strlen(feature.parentPlaceID) == 0)
490 paramValues[8] = "0";
492 paramValues[8] = (const char *)feature.parentPlaceID;
494 paramValues[9] = (const char *)feature.adminLevel;
495 paramValues[10] = (const char *)feature.houseNumber;
496 paramValues[11] = (const char *)feature.rankAddress;
497 paramValues[12] = (const char *)feature.rankSearch;
498 paramValues[13] = (const char *)feature.geometry;
499 if (strlen(paramValues[3]) && strlen(paramValues[13]))
501 if (verbose) fprintf(stderr, "placex_insert: %s\n", paramValues[0]);
502 res = PQexecPrepared(conn, "placex_insert", 14, paramValues, NULL, NULL, 0);
503 if (PQresultStatus(res) != PGRES_COMMAND_OK)
505 fprintf(stderr, "index_placex: INSERT failed: %s", PQerrorMessage(conn));
506 fprintf(stderr, "index_placex: INSERT failed: %s %s %s", paramValues[0], paramValues[1], paramValues[2]);
513 for (i = 0; i < featureAddressLines; i++)
515 // insert into place_address
516 paramValues[0] = (const char *)place_id;
517 paramValues[1] = (const char *)featureAddress[i].distance;
518 paramValues[2] = (const char *)featureAddress[i].type;
519 paramValues[3] = (const char *)featureAddress[i].id;
520 paramValues[4] = (const char *)featureAddress[i].key;
521 paramValues[5] = (const char *)featureAddress[i].value;
522 paramValues[6] = (const char *)featureAddress[i].isAddress;
523 res = PQexecPrepared(conn, "place_addressline_insert", 7, paramValues, NULL, NULL, 0);
524 if (PQresultStatus(res) != PGRES_COMMAND_OK)
526 fprintf(stderr, "place_addressline_insert: INSERT failed: %s", PQerrorMessage(conn));
532 xmlFree(featureAddress[i].type);
533 xmlFree(featureAddress[i].id);
534 xmlFree(featureAddress[i].key);
535 xmlFree(featureAddress[i].value);
536 xmlFree(featureAddress[i].distance);
539 if (featureNameLines)
541 if (strlen(feature.parentPlaceID) > 0 && featureAddressLines == 0)
543 paramValues[0] = (const char *)place_id;
544 paramValues[1] = feature.parentPlaceID;
545 if (verbose) fprintf(stderr, "search_name_from_parent_insert: INSERT %s %s\n", paramValues[0], paramValues[1]);
546 res = PQexecPrepared(conn, "search_name_from_parent_insert", 2, paramValues, NULL, NULL, 0);
547 if (PQresultStatus(res) != PGRES_COMMAND_OK)
549 fprintf(stderr, "search_name_from_parent_insert: INSERT failed: %s", PQerrorMessage(conn));
557 paramValues[0] = (const char *)place_id;
558 if (verbose) fprintf(stderr, "search_name_insert: INSERT %s\n", paramValues[0]);
559 res = PQexecPrepared(conn, "search_name_insert", 1, paramValues, NULL, NULL, 0);
560 if (PQresultStatus(res) != PGRES_COMMAND_OK)
562 fprintf(stderr, "search_name_insert: INSERT failed: %s", PQerrorMessage(conn));
570 partionQueryName = xmlHashLookup2(partionTableTagsHash, feature.key, feature.value);
571 if (partionQueryName)
573 // insert into partition table
574 paramValues[0] = (const char *)place_id;
575 paramValues[1] = (const char *)feature.geometry;
576 res = PQexecPrepared(conn, partionQueryName, 2, paramValues, NULL, NULL, 0);
577 if (PQresultStatus(res) != PGRES_COMMAND_OK)
579 fprintf(stderr, "%s: INSERT failed: %s", partionQueryName, PQerrorMessage(conn));
589 for (i = 0; i < featureAddressLines; i++)
591 xmlFree(featureAddress[i].type);
592 xmlFree(featureAddress[i].id);
593 xmlFree(featureAddress[i].key);
594 xmlFree(featureAddress[i].value);
595 xmlFree(featureAddress[i].distance);
599 xmlFree(feature.placeID);
600 xmlFree(feature.type);
602 xmlFree(feature.key);
603 xmlFree(feature.value);
604 xmlFree(feature.rankAddress);
605 xmlFree(feature.rankSearch);
606 if (feature.countryCode) xmlFree(feature.countryCode);
607 if (feature.parentPlaceID) xmlFree(feature.parentPlaceID);
608 if (feature.parentType) xmlFree(feature.parentType);
609 if (feature.parentID) xmlFree(feature.parentID);
610 // if (feature.name) xmlFree(feature.name);
611 if (feature.adminLevel) xmlFree(feature.adminLevel);
612 if (feature.houseNumber) xmlFree(feature.houseNumber);
613 if (feature.geometry) xmlFree(feature.geometry);
615 // PQclear(resPlaceID);
619 static void processNode(xmlTextReaderPtr reader)
622 name = xmlTextReaderName(reader);
625 name = xmlStrdup(BAD_CAST "--");
628 switch (xmlTextReaderNodeType(reader))
630 case XML_READER_TYPE_ELEMENT:
631 StartElement(reader, name);
632 if (xmlTextReaderIsEmptyElement(reader))
633 EndElement(reader, name); /* No end_element for self closing tags! */
635 case XML_READER_TYPE_END_ELEMENT:
636 EndElement(reader, name);
638 case XML_READER_TYPE_TEXT:
639 case XML_READER_TYPE_CDATA:
640 case XML_READER_TYPE_SIGNIFICANT_WHITESPACE:
644 fprintf(stderr, "Unknown node type %d\n", xmlTextReaderNodeType(reader));
651 int nominatim_import(const char *conninfo, const char *partionTagsFilename, const char *filename)
653 xmlTextReaderPtr reader;
656 FILE * partionTagsFile;
657 char * partionQueryName;
658 char partionQuerySQL[1024];
660 conn = PQconnectdb(conninfo);
661 if (PQstatus(conn) != CONNECTION_OK)
663 fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(conn));
667 partionTableTagsHash = xmlHashCreate(200);
668 partionTableTagsHashDelete = xmlHashCreate(200);
670 partionTagsFile = fopen(partionTagsFilename, "rt");
671 if (!partionTagsFile)
673 fprintf(stderr, "Unable to read partition tags file: %s\n", partionTagsFilename);
677 char buffer[1024], osmkey[256], osmvalue[256];
679 while (fgets(buffer, sizeof(buffer), partionTagsFile) != NULL)
681 fields = sscanf( buffer, "%23s %63s", osmkey, osmvalue );
683 if ( fields <= 0 ) continue;
687 fprintf( stderr, "Error partition file\n");
690 partionQueryName = malloc(strlen("partition_insert_")+strlen(osmkey)+strlen(osmvalue)+2);
691 strcpy(partionQueryName, "partition_insert_");
692 strcat(partionQueryName, osmkey);
693 strcat(partionQueryName, "_");
694 strcat(partionQueryName, osmvalue);
696 strcpy(partionQuerySQL, "insert into place_classtype_");
697 strcat(partionQuerySQL, osmkey);
698 strcat(partionQuerySQL, "_");
699 strcat(partionQuerySQL, osmvalue);
700 strcat(partionQuerySQL, " (place_id, centroid) values ($1, ST_Centroid(st_setsrid($2, 4326)))");
702 res = PQprepare(conn, partionQueryName, partionQuerySQL, 2, NULL);
703 if (PQresultStatus(res) != PGRES_COMMAND_OK)
705 fprintf(stderr, "Failed to prepare %s: %s\n", partionQueryName, PQerrorMessage(conn));
709 xmlHashAddEntry2(partionTableTagsHash, BAD_CAST osmkey, BAD_CAST osmvalue, BAD_CAST partionQueryName);
711 partionQueryName = malloc(strlen("partition_delete_")+strlen(osmkey)+strlen(osmvalue)+2);
712 strcpy(partionQueryName, "partition_delete_");
713 strcat(partionQueryName, osmkey);
714 strcat(partionQueryName, "_");
715 strcat(partionQueryName, osmvalue);
717 strcpy(partionQuerySQL, "delete from place_classtype_");
718 strcat(partionQuerySQL, osmkey);
719 strcat(partionQuerySQL, "_");
720 strcat(partionQuerySQL, osmvalue);
721 strcat(partionQuerySQL, " where place_id = $1::integer");
723 res = PQprepare(conn, partionQueryName, partionQuerySQL, 1, NULL);
724 if (PQresultStatus(res) != PGRES_COMMAND_OK)
726 fprintf(stderr, "Failed to prepare %s: %s\n", partionQueryName, PQerrorMessage(conn));
730 xmlHashAddEntry2(partionTableTagsHashDelete, BAD_CAST osmkey, BAD_CAST osmvalue, BAD_CAST partionQueryName);
733 res = PQprepare(conn, "get_new_place_id",
734 "select nextval('seq_place')",
736 if (PQresultStatus(res) != PGRES_COMMAND_OK)
738 fprintf(stderr, "Failed to prepare get_new_place_id: %s\n", PQerrorMessage(conn));
742 res = PQprepare(conn, "get_place_id",
743 "select place_id from placex where osm_type = $1 and osm_id = $2 and class = $3 and type = $4",
745 if (PQresultStatus(res) != PGRES_COMMAND_OK)
747 fprintf(stderr, "Failed to prepare get_place_id: %s\n", PQerrorMessage(conn));
751 res = PQprepare(conn, "placex_insert",
752 "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) "
753 "values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, st_setsrid($14, 4326))",
755 if (PQresultStatus(res) != PGRES_COMMAND_OK)
757 fprintf(stderr, "Failed to prepare placex_insert: %s\n", PQerrorMessage(conn));
761 res = PQprepare(conn, "search_name_insert",
762 "insert into search_name (place_id, search_rank, address_rank, country_code, name_vector, nameaddress_vector, centroid) "
763 "select place_id, rank_search, rank_address, country_code, make_keywords(name), "
764 "(select uniq(sort(array_agg(parent_search_name.name_vector))) from search_name as parent_search_name where place_id in "
765 "(select distinct address_place_id from place_addressline where place_addressline.place_id = $1 limit 1000)"
766 "), st_centroid(geometry) from placex "
767 "where place_id = $1",
770 if (PQresultStatus(res) != PGRES_COMMAND_OK)
772 fprintf(stderr, "Failed to prepare search_name_insert: %s\n", PQerrorMessage(conn));
776 res = PQprepare(conn, "search_name_from_parent_insert",
777 "insert into search_name (place_id, search_rank, address_rank, country_code, name_vector, nameaddress_vector, centroid) "
778 "select place_id, rank_search, rank_address, country_code, make_keywords(name), "
779 "(select uniq(sort(name_vector+nameaddress_vector)) from search_name as parent_search_name "
780 "where parent_search_name.place_id = $2 ), st_centroid(geometry) from placex "
781 "where place_id = $1",
783 if (PQresultStatus(res) != PGRES_COMMAND_OK)
785 fprintf(stderr, "Failed to prepare search_name_insert: %s\n", PQerrorMessage(conn));
789 res = PQprepare(conn, "place_addressline_insert",
790 "insert into place_addressline (place_id, address_place_id, fromarea, isaddress, distance, cached_rank_address) "
791 "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",
793 if (PQresultStatus(res) != PGRES_COMMAND_OK)
795 fprintf(stderr, "Failed to prepare place_addressline_insert: %s\n", PQerrorMessage(conn));
799 res = PQprepare(conn, "placex_delete",
800 "delete from placex where place_id = $1",
802 if (PQresultStatus(res) != PGRES_COMMAND_OK)
804 fprintf(stderr, "Failed to prepare placex_delete: %s\n", PQerrorMessage(conn));
808 res = PQprepare(conn, "search_name_delete",
809 "delete from search_name where place_id = $1",
811 if (PQresultStatus(res) != PGRES_COMMAND_OK)
813 fprintf(stderr, "Failed to prepare search_name_delete: %s\n", PQerrorMessage(conn));
817 res = PQprepare(conn, "place_addressline_delete",
818 "delete from place_addressline where place_id = $1",
820 if (PQresultStatus(res) != PGRES_COMMAND_OK)
822 fprintf(stderr, "Failed to prepare place_addressline_delete: %s\n", PQerrorMessage(conn));
828 reader = inputUTF8(filename);
832 fprintf(stderr, "Unable to open %s\n", filename);
836 ret = xmlTextReaderRead(reader);
840 ret = xmlTextReaderRead(reader);
844 fprintf(stderr, "%s : failed to parse\n", filename);
848 xmlFreeTextReader(reader);
849 xmlHashFree(partionTableTagsHash, NULL);
850 xmlHashFree(partionTableTagsHashDelete, NULL);