]> git.openstreetmap.org Git - nominatim.git/blobdiff - nominatim/import.c
be more strict with the root url
[nominatim.git] / nominatim / import.c
index 89851d2f136a2cd682d2d694470b2fe465f4deeb..419ab6a5db3ab9c43d2af7f87148f73347ad9627 100644 (file)
@@ -74,6 +74,8 @@ xmlHashTablePtr               partionTableTagsHashDelete;
 char                                   featureNameString[MAX_FEATURENAMESTRING];
 char                                   featureExtraTagString[MAX_FEATUREEXTRATAGSTRING];
 
+extern int verbose;
+
 void StartElement(xmlTextReaderPtr reader, const xmlChar *name)
 {
     char * value;
@@ -140,6 +142,13 @@ void StartElement(xmlTextReaderPtr reader, const xmlChar *name)
         feature.rankSearch = xmlTextReaderGetAttribute(reader, BAD_CAST "importance");
 
         feature.parentPlaceID = xmlTextReaderGetAttribute(reader, BAD_CAST "parent_place_id");
+/*
+       if (strlen(feature.parentPlaceID) == 0)
+       {
+               xmlFree(feature.parentPlaceID);
+               feature.parentPlaceID = NULL;
+       }
+*/
         feature.parentType = xmlTextReaderGetAttribute(reader, BAD_CAST "parent_type");
         feature.parentID = xmlTextReaderGetAttribute(reader, BAD_CAST "parent_id");
 
@@ -320,7 +329,7 @@ void StartElement(xmlTextReaderPtr reader, const xmlChar *name)
 void EndElement(xmlTextReaderPtr reader, const xmlChar *name)
 {
     PGresult *                 res;
-    const char *       paramValues[11];
+    const char *       paramValues[14];
     char *                     place_id;
     char *                     partionQueryName;
     int i, namePos, lineTypeLen, lineValueLen;
@@ -360,6 +369,7 @@ void EndElement(xmlTextReaderPtr reader, const xmlChar *name)
         if (fileMode == FILEMODE_UPDATE || fileMode == FILEMODE_DELETE || fileMode == FILEMODE_ADD)
         {
             paramValues[0] = (const char *)place_id;
+            if (verbose) fprintf(stderr, "placex_delete: %s\n", paramValues[0]);
             res = PQexecPrepared(conn, "placex_delete", 1, paramValues, NULL, NULL, 0);
             if (PQresultStatus(res) != PGRES_COMMAND_OK)
             {
@@ -369,6 +379,7 @@ void EndElement(xmlTextReaderPtr reader, const xmlChar *name)
             }
             PQclear(res);
 
+            if (verbose) fprintf(stderr, "search_name_delete: %s\n", paramValues[0]);
             res = PQexecPrepared(conn, "search_name_delete", 1, paramValues, NULL, NULL, 0);
             if (PQresultStatus(res) != PGRES_COMMAND_OK)
             {
@@ -378,6 +389,7 @@ void EndElement(xmlTextReaderPtr reader, const xmlChar *name)
             }
             PQclear(res);
 
+            if (verbose) fprintf(stderr, "place_addressline_delete: %s\n", paramValues[0]);
             res = PQexecPrepared(conn, "place_addressline_delete", 1, paramValues, NULL, NULL, 0);
             if (PQresultStatus(res) != PGRES_COMMAND_OK)
             {
@@ -434,10 +446,15 @@ void EndElement(xmlTextReaderPtr reader, const xmlChar *name)
                     strcpy(featureNameString+namePos, (char *) featureName[i].value);
                     namePos += lineValueLen;
                     strcpy(featureNameString+(namePos++), "\"");
+
+                    xmlFree(featureName[i].type);
+                    xmlFree(featureName[i].value);
                 }
             }
             paramValues[5] = (const char *)featureNameString;
 
+            paramValues[6] = (const char *)feature.countryCode;
+
             featureExtraTagString[0] = 0;
             if (featureExtraTagLines)
             {
@@ -462,20 +479,27 @@ void EndElement(xmlTextReaderPtr reader, const xmlChar *name)
                     strcpy(featureExtraTagString+namePos, (char *) featureExtraTag[i].value);
                     namePos += lineValueLen;
                     strcpy(featureExtraTagString+(namePos++), "\"");
+
+                    xmlFree(featureExtraTag[i].type);
+                    xmlFree(featureExtraTag[i].value);
                 }
             }
-            paramValues[6] = (const char *)featureExtraTagString;
-
-            paramValues[7] = (const char *)feature.parentPlaceID;
+            paramValues[7] = (const char *)featureExtraTagString;
 
-            paramValues[8] = (const char *)feature.adminLevel;
-            paramValues[9] = (const char *)feature.houseNumber;
-            paramValues[10] = (const char *)feature.rankAddress;
-            paramValues[11] = (const char *)feature.rankSearch;
-            paramValues[12] = (const char *)feature.geometry;
-            if (strlen(paramValues[3]))
+            if (xmlStrlen(feature.parentPlaceID) == 0)
+                paramValues[8] = "0";
+            else
+                paramValues[8] = (const char *)feature.parentPlaceID;
+
+            paramValues[9] = (const char *)feature.adminLevel;
+            paramValues[10] = (const char *)feature.houseNumber;
+            paramValues[11] = (const char *)feature.rankAddress;
+            paramValues[12] = (const char *)feature.rankSearch;
+            paramValues[13] = (const char *)feature.geometry;
+            if (strlen(paramValues[3]) && strlen(paramValues[13]))
             {
-                res = PQexecPrepared(conn, "placex_insert", 13, paramValues, NULL, NULL, 0);
+                if (verbose) fprintf(stderr, "placex_insert: %s\n", paramValues[0]);
+                res = PQexecPrepared(conn, "placex_insert", 14, paramValues, NULL, NULL, 0);
                 if (PQresultStatus(res) != PGRES_COMMAND_OK)
                 {
                     fprintf(stderr, "index_placex: INSERT failed: %s", PQerrorMessage(conn));
@@ -491,15 +515,18 @@ void EndElement(xmlTextReaderPtr reader, const xmlChar *name)
                 // insert into place_address
                 paramValues[0] = (const char *)place_id;
                 paramValues[1] = (const char *)featureAddress[i].distance;
+                if (paramValues[1] == NULL || strlen(paramValues[1]) == 0) paramValues[1] = "0";
                 paramValues[2] = (const char *)featureAddress[i].type;
                 paramValues[3] = (const char *)featureAddress[i].id;
                 paramValues[4] = (const char *)featureAddress[i].key;
                 paramValues[5] = (const char *)featureAddress[i].value;
                 paramValues[6] = (const char *)featureAddress[i].isAddress;
+                if (verbose) fprintf(stderr, "placex_insert: %s %s\n", paramValues[2], paramValues[3]);
                 res = PQexecPrepared(conn, "place_addressline_insert", 7, paramValues, NULL, NULL, 0);
                 if (PQresultStatus(res) != PGRES_COMMAND_OK)
                 {
                     fprintf(stderr, "place_addressline_insert: INSERT failed: %s", PQerrorMessage(conn));
+                    fprintf(stderr, "(%s,%s,%s,%s,%s,%s,%s)",paramValues[0],paramValues[1],paramValues[2],paramValues[3],paramValues[4],paramValues[5],paramValues[6]);
                     PQclear(res);
                     exit(EXIT_FAILURE);
                 }
@@ -514,15 +541,33 @@ void EndElement(xmlTextReaderPtr reader, const xmlChar *name)
 
             if (featureNameLines)
             {
-                paramValues[0] = (const char *)place_id;
-                res = PQexecPrepared(conn, "search_name_insert", 1, paramValues, NULL, NULL, 0);
-                if (PQresultStatus(res) != PGRES_COMMAND_OK)
-                {
-                    fprintf(stderr, "search_name_insert: INSERT failed: %s", PQerrorMessage(conn));
+                if (xmlStrlen(feature.parentPlaceID) > 0 && featureAddressLines == 0)
+               {
+                    paramValues[0] = (const char *)place_id;
+                    paramValues[1] = (const char *)feature.parentPlaceID;
+                    if (verbose) fprintf(stderr, "search_name_from_parent_insert: INSERT %s %s\n", paramValues[0], paramValues[1]);
+                    res = PQexecPrepared(conn, "search_name_from_parent_insert", 2, paramValues, NULL, NULL, 0);
+                    if (PQresultStatus(res) != PGRES_COMMAND_OK)
+                    {
+                        fprintf(stderr, "search_name_from_parent_insert: INSERT failed: %s", PQerrorMessage(conn));
+                        PQclear(res);
+                        exit(EXIT_FAILURE);
+                    }
+                    PQclear(res);
+               }
+               else
+               {
+                    paramValues[0] = (const char *)place_id;
+                    if (verbose) fprintf(stderr, "search_name_insert: INSERT %s\n", paramValues[0]);
+                    res = PQexecPrepared(conn, "search_name_insert", 1, paramValues, NULL, NULL, 0);
+                    if (PQresultStatus(res) != PGRES_COMMAND_OK)
+                    {
+                        fprintf(stderr, "search_name_insert: INSERT failed: %s", PQerrorMessage(conn));
+                        PQclear(res);
+                        exit(EXIT_FAILURE);
+                    }
                     PQclear(res);
-                    exit(EXIT_FAILURE);
                 }
-                PQclear(res);
             }
 
             partionQueryName = xmlHashLookup2(partionTableTagsHash, feature.key, feature.value);
@@ -561,8 +606,11 @@ void EndElement(xmlTextReaderPtr reader, const xmlChar *name)
         xmlFree(feature.value);
         xmlFree(feature.rankAddress);
         xmlFree(feature.rankSearch);
-//             if (feature.name) xmlFree(feature.name);
         if (feature.countryCode) xmlFree(feature.countryCode);
+       if (feature.parentPlaceID) xmlFree(feature.parentPlaceID);
+       if (feature.parentType) xmlFree(feature.parentType);
+       if (feature.parentID) xmlFree(feature.parentID);
+//             if (feature.name) xmlFree(feature.name);
         if (feature.adminLevel) xmlFree(feature.adminLevel);
         if (feature.houseNumber) xmlFree(feature.houseNumber);
         if (feature.geometry) xmlFree(feature.geometry);
@@ -704,8 +752,8 @@ int nominatim_import(const char *conninfo, const char *partionTagsFilename, cons
     }
 
     res = PQprepare(conn, "placex_insert",
-                    "insert into placex (place_id,osm_type,osm_id,class,type,name,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, st_setsrid($13, 4326))",
+                    "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)
     {
@@ -715,11 +763,26 @@ int nominatim_import(const char *conninfo, const char *partionTagsFilename, cons
 
     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_address, rank_search, country_code, make_keywords(name), "
-                    "(select uniq(sort(array_agg(name_vector))) from place_addressline join search_name on "
-                    "(address_place_id = search_name.place_id) where place_addressline.place_id = $1 ), st_centroid(geometry) from placex "
+                    "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));