]> git.openstreetmap.org Git - nominatim.git/blobdiff - website/search.php
more restrictive parsing of lat/lon
[nominatim.git] / website / search.php
index a29cb0cbfb9d77d89ca52c771304949c3eb15541..272324b19e6853a533928853fa6f40e76aae2652 100755 (executable)
                                array('amenity', 26, 30, false),
                                array('street', 26, 30, false),
                                array('city', 14, 24, false),
-                               array('postalcode', 5, 11, array(5, 11)),
                                array('county', 9, 13, false),
                                array('state', 8, 8, false),
                                array('country', 4, 4, false),
+                               array('postalcode', 5, 11, array(5, 11)),
                                );
        $aStructuredQuery = array();
        $sAllowedTypesSQLList = '';
                                $sQuery = trim(str_replace($aData[0], ' ', $sQuery));
                        }
                }
-               elseif (preg_match('/(\\[|^|\\b)(-?[0-9]+[0-9.]*)[, ]+(-?[0-9]+[0-9.]*)(\\]|$|\\b)/', $sQuery, $aData))
+               elseif (preg_match('/(\\[|^|\\b)(-?[0-9]+[0-9]*\\.[0-9]+)[, ]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]|$|\\b)/', $sQuery, $aData))
                {
                        $fQueryLat = $aData[2];
                        $fQueryLon = $aData[3];
                                                        $aValidTokens[$sToken] = $aGBPostcodeLocation;
                                                }
                                        }
+                                       // US ZIP+4 codes - if there is no token,
+                                       //      merge in the 5-digit ZIP code
+                                       else if (!isset($aValidTokens[$sToken]) && preg_match('/^([0-9]{5}) [0-9]{4}$/', $sToken, $aData))
+                                       {
+                                               if (isset($aValidTokens[$aData[1]]))
+                                               {
+                                                       foreach($aValidTokens[$aData[1]] as $aToken)
+                                                       {
+                                                               if (!$aToken['class'])
+                                                               {
+                                                                       if (isset($aValidTokens[$sToken]))
+                                                                       {
+                                                                               $aValidTokens[$sToken][] = $aToken;
+                                                                       }
+                                                                       else
+                                                                       {
+                                                                               $aValidTokens[$sToken] = array($aToken);
+                                                                       }
+                                                               }
+                                                       }
+                                               }
+                                       }
                                }
 
                                foreach($aTokens as $sToken)
 
                                /*
                                   Calculate all searches using aValidTokens i.e.
-
                                   'Wodsworth Road, Sheffield' =>
 
                                   Phrase Wordset
                                                                                elseif ($sPhraseType == 'postalcode')
                                                                                {
                                                                                        // We need to try the case where the postal code is the primary element (i.e. no way to tell if it is (postalcode, city) OR (city, postalcode) so try both
-                                                                                       if (sizeof($aSearch['aName']))
+                                                                                       if (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'])
                                                                                        {
-                                                                                               $aSearch['aAddress'] = array_merge($aSearch['aAddress'], $aSearch['aName']);
-                                                                                               $aSearch['aName'] = array();
-                                                                                               $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
+                                                                                               // If we already have a name try putting the postcode first
+                                                                                               if (sizeof($aSearch['aName']))
+                                                                                               {
+                                                                                                       $aNewSearch = $aSearch;
+                                                                                                       $aNewSearch['aAddress'] = array_merge($aNewSearch['aAddress'], $aNewSearch['aName']);
+                                                                                                       $aNewSearch['aName'] = array();
+                                                                                                       $aNewSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
+                                                                                                       if ($aSearch['iSearchRank'] < $iMaxRank) $aNewWordsetSearches[] = $aNewSearch;
+                                                                                               }
+
+                                                                                               if (sizeof($aSearch['aName']))
+                                                                                               {
+                                                                                                       if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strlen($sToken) < 4 || strpos($sToken, ' ') !== false))
+                                                                                                       {
+                                                                                                               $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
+                                                                                                       }
+                                                                                                       else
+                                                                                                       {
+                                                                                                               $aCurrentSearch['aFullNameAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
+                                                                                                               $aSearch['iSearchRank'] += 1000; // skip;
+                                                                                                       }
+                                                                                               }
+                                                                                               else
+                                                                                               {
+                                                                                                       $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
+                                                                                                       //$aSearch['iNamePhrase'] = $iPhrase;
+                                                                                               }
+                                                                                               if ($aSearch['iSearchRank'] < $iMaxRank) $aNewWordsetSearches[] = $aSearch;
                                                                                        }
-                                                                                       if ($aSearch['iSearchRank'] < $iMaxRank) $aNewWordsetSearches[] = $aSearch;
+
                                                                                }
                                                                                elseif (($sPhraseType == '' || $sPhraseType == 'street') && $aSearchTerm['class'] == 'place' && $aSearchTerm['type'] == 'house')
                                                                                {
                                        //if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
 
                                }
+
                        }
                        else
                        {
                                        $sSQL .= "and (placex.rank_address between $iMinAddressRank and $iMaxAddressRank ";
                                        if (14 >= $iMinAddressRank && 14 <= $iMaxAddressRank) $sSQL .= " OR (extratags->'place') = 'city'";
                                        if ($aAddressRankList) $sSQL .= " OR placex.rank_address in (".join(',',$aAddressRankList).")";
-                                       $sSQL .= ") ";
+                                       $sSQL .= ") UNION select place_id from location_property_tiger where place_id in (".join(',',$aResultPlaceIDs).") ";
+                                       $sSQL .= "and (30 between $iMinAddressRank and $iMaxAddressRank ";
+                                       if ($aAddressRankList) $sSQL .= " OR 30 in (".join(',',$aAddressRankList).")";
+                                       $sSQL .= ")";
                                        if (CONST_Debug) var_dump($sSQL);
                                        $aResultPlaceIDs = $oDB->getCol($sSQL);
                                }