]> git.openstreetmap.org Git - nominatim.git/commitdiff
take postcode into account for other searches
authorSarah Hoffmann <lonvia@denofr.de>
Wed, 5 Jul 2017 21:35:22 +0000 (23:35 +0200)
committerSarah Hoffmann <lonvia@denofr.de>
Sat, 19 Aug 2017 17:37:05 +0000 (19:37 +0200)
Existence of postcode is still optional but if a matching
result is found, then non-matching ones will be discarded.

lib/Geocode.php

index 7f1c4edc88f7b2f35b8a941490a9d016a937d34c..d77d9752bd1e276ed194d384a5d0113d66b7b95f 100644 (file)
@@ -755,7 +755,7 @@ class Geocode
                                         // If we have a structured search or this is not the first term,
                                         // add the postcode as an addendum.
                                         if ($sPhraseType == 'postalcode' || sizeof($aSearch['aName'])) {
-                                            $aSearch['sPostcode'] = $aSearchTerm['word_token'];
+                                            $aSearch['sPostcode'] = substr($aSearchTerm['word_token'], 1);
                                             if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
                                         }
                                     }
@@ -1423,6 +1423,8 @@ class Geocode
                             $aTerms[] = $aSearch['oNear']->withinSQL('centroid');
 
                             $aOrder[] = $aSearch['oNear']->distanceSQL('centroid');
+                        } elseif ($aSearch['sPostcode']) {
+                            $aOrder[] = "(SELECT min(ST_Distance(search_name.centroid, p.geometry)) FROM location_postcode p WHERE p.postcode = '".$aSearch['sPostcode']."')";
                         }
                         if (sizeof($this->aExcludePlaceIDs)) {
                             $aTerms[] = "place_id not in (".join(',', $this->aExcludePlaceIDs).")";
@@ -1711,6 +1713,21 @@ class Geocode
                         var_Dump($aPlaceIDs);
                     }
 
+                    if ($aSearch['sPostcode']) {
+                        $sSQL = 'SELECT place_id FROM placex';
+                        $sSQL .= ' WHERE place_id in ('.join(',', $aPlaceIDs).')';
+                        $sSQL .= " AND postcode = '".pg_escape_string($aSearch['sPostcode'])."'";
+                        if (CONST_Debug) var_dump($sSQL);
+                        $aFilteredPlaceIDs = chksql($this->oDB->getCol($sSQL));
+                        if ($aFilteredPlaceIDs) {
+                            $aPlaceIDs = $aFilteredPlaceIDs;
+                            if (CONST_Debug) {
+                                echo "<br><b>Place IDs after postcode filtering:</b> ";
+                                var_Dump($aPlaceIDs);
+                            }
+                        }
+                    }
+
                     foreach ($aPlaceIDs as $iPlaceID) {
                         // array for placeID => -1 | Tiger housenumber
                         $aResultPlaceIDs[$iPlaceID] = $searchedHousenumber;